ARGoS 3
A parallel, multi-engine simulator for swarm robotics
ci_tags_actuator.cpp
Go to the documentation of this file.
1
7#include "ci_tags_actuator.h"
8
9#ifdef ARGOS_WITH_LUA
10#include <argos3/core/wrappers/lua/lua_utility.h>
11#endif
12
13namespace argos {
14
15 /****************************************/
16 /****************************************/
17
18#ifdef ARGOS_WITH_LUA
19 /*
20 * This function expects the stack to have two arguments. The first argument
21 * must always be the index of the tag to set. The second argument is a
22 * string that represents the payload of the tag.
23 */
24 int LuaTagSetSinglePayload(lua_State* pt_lua_state) {
25 /* Check parameters */
26 if(lua_gettop(pt_lua_state) != 2) {
27 return luaL_error(pt_lua_state, "robot.tags.set_single_payload() expects 2 arguments");
28 }
29 luaL_checktype(pt_lua_state, 1, LUA_TNUMBER);
30 size_t unIdx = lua_tonumber(pt_lua_state, 1);
31 /* Get reference to actuator */
32 CCI_TagsActuator* pcTagActuator =
34 if(unIdx < 1 || unIdx > pcTagActuator->GetNumTags()) {
35 return luaL_error(pt_lua_state,
36 "passed index %d out of bounds [1,%d]",
37 unIdx, pcTagActuator->GetNumTags());
38 }
39 luaL_checktype(pt_lua_state, 2, LUA_TSTRING);
40 /* Perform action */
41 pcTagActuator->SetSinglePayload(unIdx - 1, lua_tostring(pt_lua_state, 2));
42 return 0;
43 }
44
45 /*
46 * This function expects the stack to have one arguments, a string
47 * that represents the payload of the tag
48 */
49 int LuaTagSetAllPayloads(lua_State* pt_lua_state) {
50 /* Check parameters */
51 if(lua_gettop(pt_lua_state) != 1) {
52 return luaL_error(pt_lua_state, "robot.tags.set_all_payloads() expects 1 argument");
53 }
54 luaL_checktype(pt_lua_state, 1, LUA_TSTRING);
55 /* Get reference to actuator */
56 CCI_TagsActuator* pcTagActuator =
58 /* Perform action */
59 pcTagActuator->SetAllPayloads(lua_tostring(pt_lua_state, 1));
60 return 0;
61 }
62#endif
63
64 /****************************************/
65 /****************************************/
66
68 return m_tSettings.size();
69 }
70
71 /****************************************/
72 /****************************************/
73
75 const std::string& str_payload) {
76 m_tSettings[un_tag_number] = str_payload;
77 }
78
79 /****************************************/
80 /****************************************/
81
82 void CCI_TagsActuator::SetAllPayloads(const std::string& str_payload) {
83 for(size_t i = 0; i < m_tSettings.size(); ++i) {
84 m_tSettings[i] = str_payload;
85 }
86 }
87
88 /****************************************/
89 /****************************************/
90
91#ifdef ARGOS_WITH_LUA
92 void CCI_TagsActuator::CreateLuaState(lua_State* pt_lua_state) {
93 CLuaUtility::OpenRobotStateTable(pt_lua_state, "tags");
94 CLuaUtility::AddToTable(pt_lua_state, "_instance", this);
95 CLuaUtility::AddToTable(pt_lua_state, "set_single_payload", &LuaTagSetSinglePayload);
96 CLuaUtility::AddToTable(pt_lua_state, "set_all_payloads", &LuaTagSetAllPayloads);
98 }
99#endif
100
101 /****************************************/
102 /****************************************/
103
104}
unsigned int UInt32
32-bit unsigned integer.
Definition datatypes.h:97
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 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 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.
static T * GetDeviceInstance(lua_State *pt_state, const std::string &str_key)
Returns a pointer to the instance to the wanted device.
size_t GetNumTags() const
Returns the number of tags.
virtual void SetSinglePayload(UInt32 un_tag_number, const std::string &str_payload)
Sets the payload of a single tag.
virtual void SetAllPayloads(const std::string &str_payload)
Sets all the tags with the same payload.