ARGoS
3
A parallel, multi-engine simulator for swarm robotics
|
Go to the documentation of this file.
8 #include <argos3/core/utility/logging/argos_log.h>
10 #include <argos3/core/wrappers/lua/lua_utility.h>
19 m_bScriptActive(false),
38 std::string strScriptFileName;
40 if(strScriptFileName !=
"") {
43 THROW_ARGOSEXCEPTION(
"Error loading Lua script \"" << strScriptFileName <<
"\": " << lua_tostring(m_ptLuaState, -1));
48 m_ptLuaState = luaL_newstate();
50 luaL_openlibs(m_ptLuaState);
65 if(m_bScriptActive && m_bIsOK) {
93 if(m_bScriptActive && m_bIsOK) {
98 lua_close(m_ptLuaState);
107 if(m_bScriptActive) {
108 lua_close(m_ptLuaState);
109 m_bScriptActive =
false;
110 m_strScriptFileName =
"";
113 m_ptLuaState = luaL_newstate();
115 luaL_openlibs(m_ptLuaState);
121 m_strScriptFileName = str_script;
132 m_bScriptActive =
true;
150 lua_newtable(m_ptLuaState);
152 lua_pushstring(m_ptLuaState,
"id");
153 lua_pushstring(m_ptLuaState,
GetId().c_str());
154 lua_settable(m_ptLuaState, -3);
161 it->second->CreateLuaState(m_ptLuaState);
163 for(CCI_Sensor::TMap::iterator it =
m_mapSensors.begin();
166 it->second->CreateLuaState(m_ptLuaState);
169 lua_setglobal(m_ptLuaState,
"robot");
177 lua_getglobal(m_ptLuaState,
"robot");
179 for(CCI_Sensor::TMap::iterator it =
m_mapSensors.begin();
182 it->second->ReadingsToLuaState(m_ptLuaState);
185 lua_pop(m_ptLuaState, 1);
193 lua_getglobal(m_ptLuaState,
"robot");
198 std::string strValue;
199 for (it = it.begin(&t_tree); it != it.end(); ++it) {
200 it.Get()->GetName(&strKey);
201 it.Get()->GetValue(&strValue);
206 lua_pop(m_ptLuaState, 1);
218 while(i <= lua_gettop(m_ptLuaState) && lua_type(m_ptLuaState, i) != LUA_TSTRING) {
222 if(i > lua_gettop(m_ptLuaState))
return "Unknown compilation error";
223 else return lua_tostring(m_ptLuaState, i);
static bool LoadScript(lua_State *pt_state, const std::string &str_filename)
Loads the given Lua script.
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.
CCI_Sensor::TMap m_mapSensors
A map containing all the sensors associated to this controller.
The namespace containing all the ARGoS related code.
The exception that wraps all errors in ARGoS.
static void RegisterRNG(lua_State *pt_state, CRandom::CRNG *pc_rng)
Registers the given random number generator in the Lua state.
std::string GetErrorMessage()
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...
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
CCI_Actuator::TMap m_mapActuators
A map containing all the actuators associated to this controller.
virtual void Destroy()
The default implementation of this method does nothing.
static void RegisterLoggerWrapper(lua_State *pt_state)
Registers LOG and LOGERR in the Lua state.
virtual void Reset()
Resets the state of the controller to what it was right after Init() was executed.
static void CloseRobotStateTable(lua_State *pt_state)
Closes a table in the robot state.
virtual void CreateLuaState()
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
signed int SInt32
32-bit signed integer.
virtual void SetLuaScript(const std::string &str_script, TConfigurationNode &t_tree)
void GetNodeAttributeOrDefault(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer, const T &t_default)
Returns the value of a node's attribute, or the passed default value.
ticpp::Iterator< ticpp::Attribute > TConfigurationAttributeIterator
The iterator for the attributes of an XML node.
const std::string & GetId() const
Returns the id of the robot associated to this controller.
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
virtual ~CLuaController()
REGISTER_CONTROLLER(CLuaController, "lua_controller")
virtual void ParametersToLuaState(TConfigurationNode &t_tree)
static void PrintStack(CARGoSLog &c_log, lua_State *pt_state)
Prints the Lua stack on the specified log.
virtual void ControlStep()
Executes a control step.
virtual void Init(TConfigurationNode &t_tree)
Initializes the controller.
virtual void SensorReadingsToLuaState()
static bool CallLuaFunction(lua_State *pt_state, const std::string &str_function)
Calls a parameter-less function in the Lua script.