ci_radios_sensor.cpp
Go to the documentation of this file.
1 
7 #include "ci_radios_sensor.h"
8 
9 #ifdef ARGOS_WITH_LUA
10 #include <argos3/core/wrappers/lua/lua_utility.h>
11 #endif
12 
13 namespace argos {
14 
15  /****************************************/
16  /****************************************/
17 
19  return m_vecInterfaces;
20  }
21 
22  /****************************************/
23  /****************************************/
24 
25 #ifdef ARGOS_WITH_LUA
26  void CCI_RadiosSensor::CreateLuaState(lua_State* pt_lua_state) {
27  CLuaUtility::OpenRobotStateTable(pt_lua_state, "radios"); // radios
28  for(size_t i = 0; i < m_vecInterfaces.size(); ++i) {
29  CLuaUtility::OpenRobotStateTable(pt_lua_state, m_vecInterfaces[i].Id); // interfaces
30  CLuaUtility::StartTable(pt_lua_state, "rx_data");
31  CLuaUtility::EndTable(pt_lua_state);
32  CLuaUtility::CloseRobotStateTable(pt_lua_state); // interfaces
33  }
34  CLuaUtility::CloseRobotStateTable(pt_lua_state); // radios
35  }
36 #endif
37 
38  /****************************************/
39  /****************************************/
40 
41 #ifdef ARGOS_WITH_LUA
42  void CCI_RadiosSensor::ReadingsToLuaState(lua_State* pt_lua_state) {
43  lua_getfield(pt_lua_state, -1, "radios"); // radios
44  for(size_t i = 0; i < m_vecInterfaces.size(); i++) {
45  lua_getfield(pt_lua_state, -1, (m_vecInterfaces[i].Id).c_str()); // interface
46  lua_getfield(pt_lua_state, -1, "rx_data"); // data
47  size_t unLastMessageCount = lua_rawlen(pt_lua_state, -1);
48  for(size_t j = 0; j < m_vecInterfaces[i].Data.size(); ++j) {
49  CLuaUtility::StartTable(pt_lua_state, j + 1); // messages
50  for(size_t k = 0; k < m_vecInterfaces[i].Data[j].Size(); ++k) {
51  CLuaUtility::AddToTable(pt_lua_state, k + 1, m_vecInterfaces[i].Data[j][k]); // bytes
52  }
53  CLuaUtility::EndTable(pt_lua_state); // messages
54  }
55  if(m_vecInterfaces[i].Data.size() < unLastMessageCount) {
56  /* Remove the extra entries from the table */
57  for(size_t j = m_vecInterfaces[i].Data.size() + 1; j <= unLastMessageCount; ++j) {
58  lua_pushnumber(pt_lua_state, j);
59  lua_pushnil(pt_lua_state);
60  lua_settable(pt_lua_state, -3);
61  }
62  }
63  lua_pop(pt_lua_state, 1); // data
64  lua_pop(pt_lua_state, 1); // interface
65  }
66  lua_pop(pt_lua_state, 1); // radios
67  }
68 #endif
69 
70  /****************************************/
71  /****************************************/
72 
73 }
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...
std::vector< SInterface > TVector
const SInterface::TVector & GetInterfaces() const
Returns a const reference to the radio interfaces.
SInterface::TVector m_vecInterfaces
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.
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
static void CloseRobotStateTable(lua_State *pt_state)
Closes a table in the robot state.
static void EndTable(lua_State *pt_state)
Adds a table to the Lua stack.