8 #include <argos3/core/utility/logging/argos_log.h>
10 #include <argos3/core/wrappers/lua/lua_quaternion.h>
11 #include <argos3/core/wrappers/lua/lua_utility.h>
12 #include <argos3/core/wrappers/lua/lua_vector2.h>
13 #include <argos3/core/wrappers/lua/lua_vector3.h>
22 m_bScriptActive(false),
41 std::string strScriptFileName;
43 if(strScriptFileName !=
"") {
51 m_ptLuaState = luaL_newstate();
53 luaL_openlibs(m_ptLuaState);
68 if(m_bScriptActive && m_bIsOK) {
96 if(m_bScriptActive && m_bIsOK) {
101 lua_close(m_ptLuaState);
110 if(m_bScriptActive) {
111 lua_close(m_ptLuaState);
112 m_bScriptActive =
false;
113 m_strScriptFileName =
"";
116 m_ptLuaState = luaL_newstate();
118 luaL_openlibs(m_ptLuaState);
124 const std::string& strScriptPath =
125 str_script.substr(0, str_script.find_last_of(
'/'));
126 std::string strPackagePath;
127 strPackagePath += (strScriptPath +
"/?.lua;");
128 strPackagePath += (strScriptPath +
"/?/init.lua;");
129 lua_getglobal(m_ptLuaState,
"package");
130 lua_getfield(m_ptLuaState, -1,
"path");
131 strPackagePath += lua_tostring(m_ptLuaState, -1);
132 lua_pop(m_ptLuaState, 1);
133 lua_pushstring(m_ptLuaState, strPackagePath.c_str());
134 lua_setfield(m_ptLuaState, -2,
"path");
135 lua_pop(m_ptLuaState, 1);
141 m_strScriptFileName = str_script;
148 m_bScriptActive =
true;
170 lua_newtable(m_ptLuaState);
172 lua_pushstring(m_ptLuaState,
"id");
173 lua_pushstring(m_ptLuaState,
GetId().c_str());
174 lua_settable(m_ptLuaState, -3);
181 it->second->CreateLuaState(m_ptLuaState);
183 for(CCI_Sensor::TMap::iterator it =
m_mapSensors.begin();
186 it->second->CreateLuaState(m_ptLuaState);
189 lua_setglobal(m_ptLuaState,
"robot");
197 lua_getglobal(m_ptLuaState,
"robot");
199 for(CCI_Sensor::TMap::iterator it =
m_mapSensors.begin();
202 it->second->ReadingsToLuaState(m_ptLuaState);
205 lua_pop(m_ptLuaState, 1);
213 lua_getglobal(m_ptLuaState,
"robot");
218 std::string strValue;
219 for (it = it.begin(&t_tree); it != it.end(); ++it) {
220 it.Get()->GetName(&strKey);
221 it.Get()->GetValue(&strValue);
226 lua_pop(m_ptLuaState, 1);
238 while(i <= lua_gettop(m_ptLuaState) && lua_type(m_ptLuaState, i) != LUA_TSTRING) {
242 if(i > lua_gettop(m_ptLuaState))
return "Unknown compilation error";
243 else return lua_tostring(m_ptLuaState, i);
#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.
The namespace containing all the ARGoS related code.
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
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::Element TConfigurationNode
The ARGoS configuration XML node.
ticpp::Iterator< ticpp::Attribute > TConfigurationAttributeIterator
The iterator for the attributes of an XML node.
REGISTER_CONTROLLER(CLuaController, "lua_controller")
CCI_Sensor::TMap m_mapSensors
A map containing all the sensors associated to this controller.
const std::string & GetId() const
Returns the id of the robot associated to this controller.
CCI_Actuator::TMap m_mapActuators
A map containing all the actuators associated to this controller.
The exception that wraps all errors in ARGoS.
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
virtual void SetLuaScript(const std::string &str_script, TConfigurationNode &t_tree)
virtual void ParametersToLuaState(TConfigurationNode &t_tree)
virtual void Destroy()
The default implementation of this method does nothing.
virtual void Init(TConfigurationNode &t_tree)
Initializes the controller.
std::string GetErrorMessage()
virtual void Reset()
Resets the state of the controller to what it was right after Init() was executed.
virtual void ControlStep()
Executes a control step.
virtual ~CLuaController()
virtual void SensorReadingsToLuaState()
virtual void CreateLuaState()
static void RegisterType(lua_State *pt_state)
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 bool CallLuaFunction(lua_State *pt_state, const std::string &str_function)
Calls a parameter-less function in the Lua script.
static bool LoadScript(lua_State *pt_state, const std::string &str_filename)
Loads the given Lua script.
static void RegisterLoggerWrapper(lua_State *pt_state)
Registers LOG and LOGERR in the Lua state.
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 void PrintStack(CARGoSLog &c_log, lua_State *pt_state)
Prints the Lua stack on the specified log.
static void RegisterRNG(lua_State *pt_state, CRandom::CRNG *pc_rng)
Registers the given random number generator in the Lua state.
static void RegisterType(lua_State *pt_state)
static void RegisterType(lua_State *pt_state)