ARGoS  3
A parallel, multi-engine simulator for swarm robotics
plugins/robots/generic/control_interface/ci_range_and_bearing_sensor.cpp
Go to the documentation of this file.
00001 
00007 #include "ci_range_and_bearing_sensor.h"
00008 
00009 #ifdef ARGOS_WITH_LUA
00010 #include <argos3/core/wrappers/lua/lua_utility.h>
00011 #endif
00012 
00013 namespace argos {
00014 
00015    /****************************************/
00016    /****************************************/
00017 
00018 #ifdef ARGOS_WITH_LUA
00019    void CCI_RangeAndBearingSensor::CreateLuaState(lua_State* pt_lua_state) {
00020       CLuaUtility::OpenRobotStateTable(pt_lua_state, "range_and_bearing");
00021       for(size_t i = 0; i < m_tReadings.size(); ++i) {
00022          CLuaUtility::StartTable(pt_lua_state, i+1);
00023          CLuaUtility::AddToTable(pt_lua_state, "range", m_tReadings[i].Range);
00024          CLuaUtility::AddToTable(pt_lua_state, "horizontal_bearing", m_tReadings[i].HorizontalBearing);
00025          CLuaUtility::AddToTable(pt_lua_state, "vertical_bearing", m_tReadings[i].VerticalBearing);
00026          CLuaUtility::StartTable(pt_lua_state, "data");
00027          const CByteArray& cData = m_tReadings[i].Data;
00028          for(size_t j = 0; j < cData.Size(); ++j) {
00029             CLuaUtility::AddToTable(pt_lua_state, j+1, cData[j]);
00030          }
00031          CLuaUtility::EndTable(pt_lua_state);
00032          CLuaUtility::EndTable(pt_lua_state);
00033       }
00034       CLuaUtility::CloseRobotStateTable(pt_lua_state);
00035    }
00036 #endif
00037 
00038    /****************************************/
00039    /****************************************/
00040 
00041 #ifdef ARGOS_WITH_LUA
00042    void CCI_RangeAndBearingSensor::ReadingsToLuaState(lua_State* pt_lua_state) {
00043       lua_getfield(pt_lua_state, -1, "range_and_bearing");
00044       /* Save the number of elements in the RAB table */
00045       size_t unLastMsgNum = lua_objlen(pt_lua_state, -1);
00046       /* Overwrite the table with the new messages */
00047       for(size_t i = 0; i < m_tReadings.size(); ++i) {
00048          CLuaUtility::StartTable(pt_lua_state, i+1);
00049          CLuaUtility::AddToTable(pt_lua_state, "range", m_tReadings[i].Range);
00050          CLuaUtility::AddToTable(pt_lua_state, "horizontal_bearing", m_tReadings[i].HorizontalBearing);
00051          CLuaUtility::AddToTable(pt_lua_state, "vertical_bearing", m_tReadings[i].VerticalBearing);
00052          CLuaUtility::StartTable(pt_lua_state, "data");
00053          const CByteArray& cData = m_tReadings[i].Data;
00054          for(size_t j = 0; j < cData.Size(); ++j) {
00055             CLuaUtility::AddToTable(pt_lua_state, j+1, cData[j]);
00056          }
00057          CLuaUtility::EndTable(pt_lua_state);
00058          CLuaUtility::EndTable(pt_lua_state);
00059       }
00060       /* Are the new messages less than the old ones? */
00061       if(m_tReadings.size() < unLastMsgNum) {
00062          /* Yes, set to nil all the extra entries */
00063          for(size_t i = m_tReadings.size()+1; i <= unLastMsgNum; ++i) {
00064             lua_pushnumber(pt_lua_state,  i);
00065             lua_pushnil   (pt_lua_state    );
00066             lua_settable  (pt_lua_state, -3);
00067          }
00068       }
00069       lua_pop(pt_lua_state, 1);
00070    }
00071 #endif
00072 
00073    /****************************************/
00074    /****************************************/
00075 
00076    CCI_RangeAndBearingSensor::SPacket::SPacket() :
00077       Range(0.0f) {}
00078 
00079    /****************************************/
00080    /****************************************/
00081 
00082 }