ARGoS 3
A parallel, multi-engine simulator for swarm robotics
ci_radios_actuator.cpp
Go to the documentation of this file.
1
8
9#ifdef ARGOS_WITH_LUA
10#include <argos3/core/wrappers/lua/lua_utility.h>
11#endif
12
13namespace argos {
14
15 /****************************************/
16 /****************************************/
17
21
22 /****************************************/
23 /****************************************/
24
25#ifdef ARGOS_WITH_LUA
26 int LuaSendData(lua_State* pt_lua_state) {
27 /* get a reference to the actuator instance */
28 CCI_RadiosActuator::SInterface* ps_interface =
29 reinterpret_cast<CCI_RadiosActuator::SInterface*>(lua_touserdata(pt_lua_state, lua_upvalueindex(1)));
30 /* Check parameters */
31 if(lua_gettop(pt_lua_state) != 1) {
32 std::string strErrMsg = "robot.radios." + ps_interface->Id + ".tx_data() requires 1 argument";
33 return luaL_error(pt_lua_state, strErrMsg.c_str());
34 }
35 luaL_checktype(pt_lua_state, 1, LUA_TTABLE);
36 /* copy the specified data into a CByteArray */
37 CByteArray cBuffer(lua_rawlen(pt_lua_state, -1));
38 for(size_t i = 0; i < cBuffer.Size(); ++i) {
39 lua_pushnumber(pt_lua_state, i + 1);
40 lua_gettable(pt_lua_state, -2);
41 if(lua_type(pt_lua_state, -1) == LUA_TNUMBER) {
42 cBuffer[i] = static_cast<UInt8>(lua_tonumber(pt_lua_state, -1));
43 lua_pop(pt_lua_state, 1);
44 }
45 else {
46 return luaL_error(pt_lua_state, "element #%d of the table is not a number", i + 1);
47 }
48 }
49 ps_interface->Data.emplace_back(cBuffer);
50 return 0;
51 }
52#endif
53
54 /****************************************/
55 /****************************************/
56
57#ifdef ARGOS_WITH_LUA
58 void CCI_RadiosActuator::CreateLuaState(lua_State* pt_lua_state) {
59 CLuaUtility::StartTable(pt_lua_state, "radios");
60 for(SInterface& s_interface : m_vecInterfaces) {
61 CLuaUtility::StartTable(pt_lua_state, s_interface.Id);
62 /* push a pointer to each actuator instance onto the lua stack */
63 lua_pushstring(pt_lua_state, "tx_data");
64 lua_pushlightuserdata(pt_lua_state, &s_interface);
65 lua_pushcclosure(pt_lua_state, &LuaSendData, 1);
66 lua_settable(pt_lua_state, -3);
67 CLuaUtility::EndTable(pt_lua_state);
68 }
69 CLuaUtility::EndTable(pt_lua_state);
70 }
71#endif
72
73
74}
unsigned char UInt8
8-bit unsigned integer.
Definition datatypes.h:60
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
virtual void CreateLuaState(lua_State *pt_lua_state)=0
Creates the Lua state for this actuator.
static void EndTable(lua_State *pt_state)
Adds a table to the Lua stack.
static void StartTable(lua_State *pt_state, const std::string &str_key)
Adds a table with the given string key to the table located at the top of the stack.
SInterface::TVector m_vecInterfaces
SInterface::TVector & GetInterfaces()
Returns a reference to the radio interfaces.