ARGoS 3
A parallel, multi-engine simulator for swarm robotics
ci_range_and_bearing_sensor.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 void CCI_RangeAndBearingSensor::CreateLuaState(lua_State* pt_lua_state) {
27 CLuaUtility::OpenRobotStateTable(pt_lua_state, "range_and_bearing");
28 for(size_t i = 0; i < m_tReadings.size(); ++i) {
29 CLuaUtility::StartTable(pt_lua_state, i+1);
30 CLuaUtility::AddToTable(pt_lua_state, "range", m_tReadings[i].Range);
31 CLuaUtility::AddToTable(pt_lua_state, "horizontal_bearing", m_tReadings[i].HorizontalBearing);
32 CLuaUtility::AddToTable(pt_lua_state, "vertical_bearing", m_tReadings[i].VerticalBearing);
33 CLuaUtility::StartTable(pt_lua_state, "data");
34 const CByteArray& cData = m_tReadings[i].Data;
35 for(size_t j = 0; j < cData.Size(); ++j) {
36 CLuaUtility::AddToTable(pt_lua_state, j+1, cData[j]);
37 }
38 CLuaUtility::EndTable(pt_lua_state);
39 CLuaUtility::EndTable(pt_lua_state);
40 }
42 }
43#endif
44
45 /****************************************/
46 /****************************************/
47
48#ifdef ARGOS_WITH_LUA
49 void CCI_RangeAndBearingSensor::ReadingsToLuaState(lua_State* pt_lua_state) {
50 lua_getfield(pt_lua_state, -1, "range_and_bearing");
51 /* Save the number of elements in the RAB table */
52 size_t unLastMsgNum = lua_rawlen(pt_lua_state, -1);
53 /* Overwrite the table with the new messages */
54 for(size_t i = 0; i < m_tReadings.size(); ++i) {
55 CLuaUtility::StartTable(pt_lua_state, i+1);
56 CLuaUtility::AddToTable(pt_lua_state, "range", m_tReadings[i].Range);
57 CLuaUtility::AddToTable(pt_lua_state, "horizontal_bearing", m_tReadings[i].HorizontalBearing);
58 CLuaUtility::AddToTable(pt_lua_state, "vertical_bearing", m_tReadings[i].VerticalBearing);
59 CLuaUtility::StartTable(pt_lua_state, "data");
60 const CByteArray& cData = m_tReadings[i].Data;
61 for(size_t j = 0; j < cData.Size(); ++j) {
62 CLuaUtility::AddToTable(pt_lua_state, j+1, cData[j]);
63 }
64 CLuaUtility::EndTable(pt_lua_state);
65 CLuaUtility::EndTable(pt_lua_state);
66 }
67 /* Are the new messages less than the old ones? */
68 if(m_tReadings.size() < unLastMsgNum) {
69 /* Yes, set to nil all the extra entries */
70 for(size_t i = m_tReadings.size()+1; i <= unLastMsgNum; ++i) {
71 lua_pushnumber(pt_lua_state, i);
72 lua_pushnil (pt_lua_state );
73 lua_settable (pt_lua_state, -3);
74 }
75 }
76 lua_pop(pt_lua_state, 1);
77 }
78#endif
79
80 /****************************************/
81 /****************************************/
82
85
86 /****************************************/
87 /****************************************/
88
89}
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
Byte array utility class.
Definition byte_array.h:28
size_t Size() const
Returns the current size of the byte array.
Definition byte_array.h:66
static void EndTable(lua_State *pt_state)
Adds a table to the Lua stack.
static void AddToTable(lua_State *pt_state, const std::string &str_key, void *pt_data)
Adds a pointer to a chunk of data with the given string key to the table located at the top of the st...
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.
static void OpenRobotStateTable(lua_State *pt_state, const std::string &str_key)
Opens a table in the robot state, creating it if it does not exist.
static void CloseRobotStateTable(lua_State *pt_state)
Closes a table in the robot state.