9 #include <argos3/core/utility/configuration/argos_exception.h>
10 #include <argos3/core/utility/math/angles.h>
11 #include <argos3/core/utility/math/vector2.h>
12 #include <argos3/core/utility/math/vector3.h>
13 #include <argos3/core/utility/math/quaternion.h>
14 #include <argos3/core/utility/datatypes/color.h>
16 #include <argos3/core/wrappers/lua/lua_vector2.h>
17 #include <argos3/core/wrappers/lua/lua_vector3.h>
18 #include <argos3/core/wrappers/lua/lua_quaternion.h>
25 static const CRange<Real> UNIT(0.0f, 1.0f);
29 if(lua_gettop(pt_state) > 1) {
30 return luaL_error(pt_state,
"robot.random.bernoulli() expects 0 or 1 arguments");
33 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
35 if(lua_gettop(pt_state) == 0) {
37 lua_pushboolean(pt_state, pcRNG->
Bernoulli());
42 luaL_checktype(pt_state, 1, LUA_TNUMBER);
44 lua_pushboolean(pt_state,
45 pcRNG->
Bernoulli(lua_tonumber(pt_state, 1)));
54 if(lua_gettop(pt_state) > 2) {
55 return luaL_error(pt_state,
"robot.random.uniform() expects 0, 1, or 2 arguments");
58 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
60 if(lua_gettop(pt_state) == 0) {
62 lua_pushnumber(pt_state, pcRNG->
Uniform(UNIT));
65 else if(lua_gettop(pt_state) == 1) {
67 luaL_checktype(pt_state, 1, LUA_TNUMBER);
69 lua_pushnumber(pt_state,
71 lua_tonumber(pt_state, 1))));
76 luaL_checktype(pt_state, 1, LUA_TNUMBER);
77 luaL_checktype(pt_state, 2, LUA_TNUMBER);
79 lua_pushnumber(pt_state,
81 lua_tonumber(pt_state, 2))));
90 if(lua_gettop(pt_state) > 2) {
91 return luaL_error(pt_state,
"robot.random.uniform_int() expects 1 or 2 arguments");
94 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
96 if(lua_gettop(pt_state) == 1) {
98 luaL_checktype(pt_state, 1, LUA_TNUMBER);
100 lua_pushnumber(pt_state,
102 Floor(lua_tonumber(pt_state, 1)))));
107 luaL_checktype(pt_state, 1, LUA_TNUMBER);
108 luaL_checktype(pt_state, 2, LUA_TNUMBER);
110 lua_pushnumber(pt_state,
112 Floor(lua_tonumber(pt_state, 2)))));
121 if(lua_gettop(pt_state) != 1) {
122 return luaL_error(pt_state,
"robot.random.exponential() expects 1 argument");
125 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
127 luaL_checktype(pt_state, 1, LUA_TNUMBER);
129 lua_pushnumber(pt_state,
136 if(lua_gettop(pt_state) != 1) {
137 return luaL_error(pt_state,
"robot.random.poisson() expects 1 argument");
140 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
142 luaL_checktype(pt_state, 1, LUA_TNUMBER);
144 lua_pushinteger(pt_state,
145 pcRNG->
Poisson(lua_tonumber(pt_state, 1)));
151 if(lua_gettop(pt_state) != 1 && lua_gettop(pt_state) != 2) {
152 return luaL_error(pt_state,
"robot.random.gaussian() expects 1 or 2 arguments");
155 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
157 if(lua_gettop(pt_state) == 1) {
159 luaL_checktype(pt_state, 1, LUA_TNUMBER);
161 lua_pushnumber(pt_state, pcRNG->
Gaussian(lua_tonumber(pt_state, 1)));
166 luaL_checktype(pt_state, 1, LUA_TNUMBER);
167 luaL_checktype(pt_state, 2, LUA_TNUMBER);
169 lua_pushnumber(pt_state,
170 pcRNG->
Gaussian(lua_tonumber(pt_state, 1),
171 lua_tonumber(pt_state, 2)));
182 const std::string& str_filename) {
183 if(luaL_loadfile(pt_state, str_filename.c_str())) {
184 LOGERR <<
"[FATAL] Error loading \"" << str_filename
185 <<
"\"" << std::endl;
188 if(lua_pcall(pt_state, 0, 0, 0)) {
189 LOGERR <<
"[FATAL] Error executing \"" << str_filename
190 <<
"\"" << std::endl;
191 LOGERR <<
"[FATAL] " << lua_tostring(pt_state, -1)
202 const std::string& str_function) {
203 lua_getglobal(pt_state, str_function.c_str());
204 if(lua_pcall(pt_state, 0, 0, 0)) {
205 LOGERR <<
"[FATAL] Error calling \"" << str_function
206 <<
"\"" << std::endl;
207 LOGERR <<
"[FATAL] " << lua_tostring(pt_state, -1)
218 switch(lua_type(pt_state, n_index)) {
219 case LUA_TBOOLEAN: c_log << lua_toboolean(pt_state, n_index);
break;
220 case LUA_TNUMBER: c_log << lua_tonumber(pt_state, n_index);
break;
221 case LUA_TSTRING: c_log << lua_tostring(pt_state, n_index);
break;
222 default: c_log << lua_topointer(pt_state, n_index);
break;
232 for(
size_t i = 0; i < un_depth; ++i) {
236 c_log <<
" [" << lua_typename(pt_state, lua_type(pt_state, -1)) <<
"] ";
237 if(lua_istable(pt_state, -1)) {
239 lua_pushnil(pt_state);
240 while(lua_next(pt_state, -2)) {
241 if(lua_type(pt_state, -1) != LUA_TFUNCTION &&
242 (lua_type(pt_state, -2) != LUA_TSTRING ||
243 (std::string(lua_tostring(pt_state, -2)) !=
"_G" &&
244 std::string(lua_tostring(pt_state, -2)) !=
"_VERSION" &&
245 std::string(lua_tostring(pt_state, -2)) !=
"package" &&
246 std::string(lua_tostring(pt_state, -2)) !=
"string" &&
247 std::string(lua_tostring(pt_state, -2)) !=
"os" &&
248 std::string(lua_tostring(pt_state, -2)) !=
"io" &&
249 std::string(lua_tostring(pt_state, -2)) !=
"math" &&
250 std::string(lua_tostring(pt_state, -2)) !=
"debug" &&
251 std::string(lua_tostring(pt_state, -2)) !=
"coroutine" &&
252 std::string(lua_tostring(pt_state, -2)) !=
"table"))) {
255 lua_pop(pt_state, 1);
268 lua_State* pt_state) {
269 c_log <<
"*** LUA GLOBALS START ***" << std::endl;
270 lua_getglobal(pt_state,
"_G");
272 lua_pop(pt_state, 1);
273 c_log <<
"*** LUA GLOBALS END ***" << std::endl;
274 #ifdef ARGOS_THREADSAFE_LOG
283 lua_State* pt_state) {
284 c_log <<
"*** LUA STACK START ***" << std::endl;
285 size_t unTop = lua_gettop(pt_state);
286 c_log <<
"Elements in stack: " << unTop << std::endl;
287 for(
size_t i = unTop; i > 0; --i) {
288 c_log <<
"#" << i <<
" [" << lua_typename(pt_state, lua_type(pt_state, i)) <<
"] ";
292 c_log <<
"*** LUA STACK END ***" << std::endl;
293 #ifdef ARGOS_THREADSAFE_LOG
302 lua_register(pt_state,
"log", LOGWrapper);
303 lua_register(pt_state,
"logerr", LOGERRWrapper);
327 const std::string& str_key) {
328 lua_pushstring(pt_state, str_key.c_str());
329 lua_rawget(pt_state, -2);
330 if(lua_isnil(pt_state, -1)) {
331 lua_pop(pt_state, 1);
334 lua_pushstring(pt_state, str_key.c_str());
335 lua_rawget(pt_state, -2);
343 lua_pop(pt_state, 1);
350 const std::string& str_key) {
351 lua_pushstring(pt_state, str_key.c_str());
352 lua_newtable (pt_state);
360 lua_pushnumber(pt_state, n_key);
361 lua_newtable (pt_state);
368 lua_settable(pt_state, -3);
375 const std::string& str_key) {
376 luaL_getmetatable(pt_state, str_key.c_str());
377 lua_setmetatable(pt_state, -2);
384 const std::string& str_key,
386 lua_pushstring (pt_state, str_key.c_str());
387 lua_pushlightuserdata(pt_state, pt_data );
388 lua_settable (pt_state, -3 );
395 const std::string& str_key,
396 lua_CFunction t_data) {
397 lua_pushstring (pt_state, str_key.c_str());
398 lua_pushcfunction(pt_state, t_data );
399 lua_settable (pt_state, -3 );
406 const std::string& str_key,
407 const std::string& str_data){
408 lua_pushstring(pt_state, str_key.c_str() );
409 lua_pushstring(pt_state, str_data.c_str());
410 lua_settable (pt_state, -3 );
418 const std::string& str_data){
419 lua_pushnumber(pt_state, n_key );
420 lua_pushstring(pt_state, str_data.c_str());
421 lua_settable (pt_state, -3 );
428 const std::string& str_key,
430 lua_pushstring(pt_state, str_key.c_str());
431 lua_pushnumber(pt_state, f_data );
432 lua_settable (pt_state, -3 );
441 lua_pushnumber(pt_state, n_key );
442 lua_pushnumber(pt_state, f_data);
443 lua_settable (pt_state, -3 );
450 const std::string& str_key,
452 lua_pushstring(pt_state, str_key.c_str() );
453 lua_pushnumber(pt_state, c_data.
GetValue());
454 lua_settable (pt_state, -3 );
463 lua_pushnumber(pt_state, n_key );
464 lua_pushnumber(pt_state, c_data.
GetValue());
465 lua_settable (pt_state, -3 );
472 const std::string& str_key,
474 lua_pushstring (pt_state, str_key.c_str());
476 lua_settable (pt_state, -3);
485 lua_pushnumber (pt_state, n_key);
487 lua_settable (pt_state, -3);
494 const std::string& str_key,
496 lua_pushstring (pt_state, str_key.c_str());
498 lua_settable (pt_state, -3);
507 lua_pushnumber (pt_state, n_key);
509 lua_settable (pt_state, -3);
517 const std::string& str_key,
519 lua_pushstring (pt_state, str_key.c_str());
521 lua_settable (pt_state, -3);
530 lua_pushnumber (pt_state, n_key);
532 lua_settable (pt_state, -3);
539 const std::string& str_key,
564 int CLuaUtility::LOGWrapper(lua_State* pt_state) {
565 return LoggerWrapper(
LOG, pt_state);
571 int CLuaUtility::LOGERRWrapper(lua_State* pt_state) {
572 return LoggerWrapper(
LOGERR, pt_state);
578 int CLuaUtility::LoggerWrapper(CARGoSLog& c_log,
579 lua_State* pt_state) {
581 UInt32 unArgc = lua_gettop(pt_state);
584 for(
UInt32 i = 1; i <= unArgc; ++i) {
585 unType = lua_type(pt_state, i);
587 case LUA_TBOOLEAN: c_log << lua_toboolean(pt_state, i);
break;
588 case LUA_TNUMBER: c_log << lua_tonumber (pt_state, i);
break;
589 case LUA_TSTRING: c_log << lua_tostring (pt_state, i);
break;
590 default: c_log << lua_typename (pt_state, unType);
break;
signed int SInt32
32-bit signed integer.
unsigned int UInt32
32-bit unsigned integer.
float Real
Collects all ARGoS code.
The namespace containing all the ARGoS related code.
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
void RecursivePrintGlobals(CARGoSLog &c_log, lua_State *pt_state, size_t un_depth)
int LuaRNGBernoulli(lua_State *pt_state)
int LuaRNGPoisson(lua_State *pt_state)
int LuaRNGUniformInt(lua_State *pt_state)
int LuaRNGExponential(lua_State *pt_state)
CARGoSLog LOG(std::cout, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_GREEN))
int LuaRNGUniform(lua_State *pt_state)
SInt32 Floor(Real f_value)
Rounds the passed floating-point value to the closest lower integer.
void PrintStackEntry(CARGoSLog &c_log, lua_State *pt_state, SInt32 n_index)
int LuaRNGGaussian(lua_State *pt_state)
UInt8 GetBlue() const
Returns the blue channel of the color.
UInt8 GetGreen() const
Returns the green channel of the color.
UInt8 GetRed() const
Returns the red channel of the color.
It defines the basic type CRadians, used to store an angle value in radians.
Real GetValue() const
Returns the value in radians.
UInt32 Poisson(Real f_mean)
Returns a random value from a Poisson distribution.
bool Bernoulli(Real f_true=0.5)
Returns a random value from a Bernoulli distribution.
void Reset()
Reset the RNG.
Real Exponential(Real f_mean)
Returns a random value from an exponential distribution.
Real Gaussian(Real f_std_dev, Real f_mean=0.0f)
Returns a random value from a Gaussian distribution.
CRadians Uniform(const CRange< CRadians > &c_range)
Returns a random value from a uniform distribution.
static void PushQuaternion(lua_State *pt_state, TArguments &&... t_arguments)
static void EndTable(lua_State *pt_state)
Adds a table to the Lua stack.
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 void SetMetatable(lua_State *pt_state, const std::string &str_key)
Sets the metatable with the given string key to the table located at the top of the stack.
static bool LoadScript(lua_State *pt_state, const std::string &str_filename)
Loads the given Lua script.
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 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 PrintGlobals(CARGoSLog &c_log, lua_State *pt_state)
Prints the global Lua symbols on the specified log.
static void PushVector2(lua_State *pt_state, TArguments &&... t_arguments)
static void PushVector3(lua_State *pt_state, TArguments &&... t_arguments)