8 #include <argos3/core/utility/configuration/argos_exception.h>
9 #include <argos3/core/utility/math/angles.h>
10 #include <argos3/core/utility/math/vector2.h>
11 #include <argos3/core/utility/math/vector3.h>
12 #include <argos3/core/utility/math/quaternion.h>
13 #include <argos3/core/utility/datatypes/color.h>
20 static const CRange<Real> UNIT(0.0f, 1.0f);
24 if(lua_gettop(pt_state) > 1) {
25 return luaL_error(pt_state,
"robot.random.bernoulli() expects 0 or 1 arguments");
28 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
30 if(lua_gettop(pt_state) == 0) {
32 lua_pushnumber(pt_state, pcRNG->
Bernoulli());
37 luaL_checktype(pt_state, 1, LUA_TNUMBER);
39 lua_pushnumber(pt_state,
40 pcRNG->
Bernoulli(lua_tonumber(pt_state, 1)));
49 if(lua_gettop(pt_state) > 2) {
50 return luaL_error(pt_state,
"robot.random.uniform() expects 0, 1, or 2 arguments");
53 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
55 if(lua_gettop(pt_state) == 0) {
57 lua_pushnumber(pt_state, pcRNG->
Uniform(UNIT));
60 else if(lua_gettop(pt_state) == 1) {
62 luaL_checktype(pt_state, 1, LUA_TNUMBER);
64 lua_pushnumber(pt_state,
66 lua_tonumber(pt_state, 1))));
71 luaL_checktype(pt_state, 1, LUA_TNUMBER);
72 luaL_checktype(pt_state, 2, LUA_TNUMBER);
74 lua_pushnumber(pt_state,
76 lua_tonumber(pt_state, 2))));
85 if(lua_gettop(pt_state) > 2) {
86 return luaL_error(pt_state,
"robot.random.uniform_int() expects 1 or 2 arguments");
89 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
91 if(lua_gettop(pt_state) == 1) {
93 luaL_checktype(pt_state, 1, LUA_TNUMBER);
95 lua_pushnumber(pt_state,
97 Floor(lua_tonumber(pt_state, 1)))));
102 luaL_checktype(pt_state, 1, LUA_TNUMBER);
103 luaL_checktype(pt_state, 2, LUA_TNUMBER);
105 lua_pushnumber(pt_state,
107 Floor(lua_tonumber(pt_state, 2)))));
116 if(lua_gettop(pt_state) != 1) {
117 return luaL_error(pt_state,
"robot.random.exponential() expects 1 argument");
120 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
122 luaL_checktype(pt_state, 1, LUA_TNUMBER);
124 lua_pushnumber(pt_state,
131 if(lua_gettop(pt_state) != 1 && lua_gettop(pt_state) != 2) {
132 return luaL_error(pt_state,
"robot.random.gaussian() expects 1 or 2 arguments");
135 CRandom::CRNG* pcRNG = CLuaUtility::GetDeviceInstance<CRandom::CRNG>(pt_state,
"random");
137 if(lua_gettop(pt_state) == 1) {
139 luaL_checktype(pt_state, 1, LUA_TNUMBER);
141 lua_pushnumber(pt_state, pcRNG->
Gaussian(lua_tonumber(pt_state, 1)));
146 luaL_checktype(pt_state, 1, LUA_TNUMBER);
147 luaL_checktype(pt_state, 2, LUA_TNUMBER);
149 lua_pushnumber(pt_state,
150 pcRNG->
Gaussian(lua_tonumber(pt_state, 1),
151 lua_tonumber(pt_state, 2)));
162 const std::string& str_filename) {
163 if(luaL_loadfile(pt_state, str_filename.c_str())) {
166 if(lua_pcall(pt_state, 0, 0, 0)) {
176 const std::string& str_function) {
177 lua_getglobal(pt_state, str_function.c_str());
178 if(lua_pcall(pt_state, 0, 0, 0)) {
188 switch(lua_type(pt_state, n_index)) {
189 case LUA_TBOOLEAN: c_log << lua_toboolean(pt_state, n_index);
break;
190 case LUA_TNUMBER: c_log << lua_tonumber(pt_state, n_index);
break;
191 case LUA_TSTRING: c_log << lua_tostring(pt_state, n_index);
break;
192 default: c_log << lua_topointer(pt_state, n_index);
break;
199 for(
size_t i = 0; i < un_depth; ++i) {
203 c_log <<
" [" << lua_typename(pt_state, lua_type(pt_state, -1)) <<
"] ";
204 if(lua_istable(pt_state, -1)) {
206 lua_pushnil(pt_state);
207 while(lua_next(pt_state, -2)) {
208 if(lua_type(pt_state, -1) != LUA_TFUNCTION &&
209 (lua_type(pt_state, -2) != LUA_TSTRING ||
210 (std::string(lua_tostring(pt_state, -2)) !=
"_G" &&
211 std::string(lua_tostring(pt_state, -2)) !=
"_VERSION" &&
212 std::string(lua_tostring(pt_state, -2)) !=
"package" &&
213 std::string(lua_tostring(pt_state, -2)) !=
"string" &&
214 std::string(lua_tostring(pt_state, -2)) !=
"os" &&
215 std::string(lua_tostring(pt_state, -2)) !=
"io" &&
216 std::string(lua_tostring(pt_state, -2)) !=
"math" &&
217 std::string(lua_tostring(pt_state, -2)) !=
"debug" &&
218 std::string(lua_tostring(pt_state, -2)) !=
"coroutine" &&
219 std::string(lua_tostring(pt_state, -2)) !=
"table"))) {
222 lua_pop(pt_state, 1);
232 lua_State* pt_state) {
233 c_log <<
"*** LUA GLOBALS START ***" << std::endl;
234 lua_getglobal(pt_state,
"_G");
236 lua_pop(pt_state, 1);
237 c_log <<
"*** LUA GLOBALS END ***" << std::endl;
238 #ifdef ARGOS_THREADSAFE_LOG
247 lua_State* pt_state) {
248 c_log <<
"*** LUA STACK START ***" << std::endl;
249 size_t unTop = lua_gettop(pt_state);
250 c_log <<
"Elements in stack: " << unTop << std::endl;
251 for(
size_t i = unTop; i > 0; --i) {
252 c_log <<
"#" << i <<
" [" << lua_typename(pt_state, lua_type(pt_state, i)) <<
"] ";
256 c_log <<
"*** LUA STACK END ***" << std::endl;
257 #ifdef ARGOS_THREADSAFE_LOG
266 lua_register(pt_state,
"log", LOGWrapper);
267 lua_register(pt_state,
"logerr", LOGERRWrapper);
290 const std::string& str_key) {
291 lua_pushstring(pt_state, str_key.c_str());
292 lua_rawget(pt_state, -2);
293 if(lua_isnil(pt_state, -1)) {
294 lua_pop(pt_state, 1);
297 lua_pushstring(pt_state, str_key.c_str());
298 lua_rawget(pt_state, -2);
306 lua_pop(pt_state, 1);
313 const std::string& str_key) {
314 lua_pushstring(pt_state, str_key.c_str());
315 lua_newtable (pt_state);
323 lua_pushnumber(pt_state, n_key);
324 lua_newtable (pt_state);
331 lua_settable(pt_state, -3);
338 const std::string& str_key,
340 lua_pushstring (pt_state, str_key.c_str());
341 lua_pushlightuserdata(pt_state, pt_data );
342 lua_settable (pt_state, -3 );
349 const std::string& str_key,
350 lua_CFunction t_data) {
351 lua_pushstring (pt_state, str_key.c_str());
352 lua_pushcfunction(pt_state, t_data );
353 lua_settable (pt_state, -3 );
360 const std::string& str_key,
362 lua_pushstring(pt_state, str_key.c_str());
363 lua_pushnumber(pt_state, f_data );
364 lua_settable (pt_state, -3 );
371 const std::string& str_key,
372 const std::string& str_data){
373 lua_pushstring(pt_state, str_key.c_str() );
374 lua_pushstring(pt_state, str_data.c_str());
375 lua_settable (pt_state, -3 );
384 lua_pushnumber(pt_state, n_key );
385 lua_pushnumber(pt_state, f_data);
386 lua_settable (pt_state, -3 );
393 const std::string& str_key,
395 lua_pushstring(pt_state, str_key.c_str() );
396 lua_pushnumber(pt_state, c_data.
GetValue());
397 lua_settable (pt_state, -3 );
406 lua_pushnumber(pt_state, n_key );
407 lua_pushnumber(pt_state, c_data.
GetValue());
408 lua_settable (pt_state, -3 );
415 const std::string& str_key,
441 const std::string& str_key,
469 const std::string& str_key,
501 const std::string& str_key,
528 int CLuaUtility::LOGWrapper(lua_State* pt_state) {
529 return LoggerWrapper(
LOG, pt_state);
535 int CLuaUtility::LOGERRWrapper(lua_State* pt_state) {
536 return LoggerWrapper(
LOGERR, pt_state);
542 int CLuaUtility::LoggerWrapper(CARGoSLog& c_log,
543 lua_State* pt_state) {
545 UInt32 unArgc = lua_gettop(pt_state);
548 for(
UInt32 i = 1; i <= unArgc; ++i) {
549 unType = lua_type(pt_state, i);
551 case LUA_TBOOLEAN: c_log << lua_toboolean(pt_state, i);
break;
552 case LUA_TNUMBER: c_log << lua_tonumber (pt_state, i);
break;
553 case LUA_TSTRING: c_log << lua_tostring (pt_state, i);
break;
554 default: c_log << lua_typename (pt_state, unType);
break;