10 #include <argos3/core/wrappers/lua/lua_utility.h>
26 int LuaRABSetData(lua_State* pt_lua_state) {
27 if(lua_gettop(pt_lua_state) == 1) {
29 luaL_checktype(pt_lua_state, 1, LUA_TTABLE);
31 CCI_RangeAndBearingActuator* pcAct = CLuaUtility::GetDeviceInstance<CCI_RangeAndBearingActuator>(pt_lua_state,
"range_and_bearing");
33 if(pcAct->GetSize() != lua_rawlen(pt_lua_state, -1)) {
34 return luaL_error(pt_lua_state,
"robot.range_and_bearing.set_data(array) expects an array of %d numbers", pcAct->GetSize());
37 CByteArray cBuf(pcAct->GetSize());
38 for(
size_t i = 0; i < pcAct->GetSize(); ++i) {
39 lua_pushnumber(pt_lua_state, i+1);
40 lua_gettable(pt_lua_state, -2);
41 if(lua_type(pt_lua_state, -1) == LUA_TNUMBER) {
42 cBuf[i] =
static_cast<UInt8>(lua_tonumber(pt_lua_state, -1));
43 lua_pop(pt_lua_state, 1);
46 return luaL_error(pt_lua_state,
"element #%d of the array is not a number", i+1);
53 else if(lua_gettop(pt_lua_state) == 2) {
55 luaL_checktype(pt_lua_state, 1, LUA_TNUMBER);
56 luaL_checktype(pt_lua_state, 2, LUA_TNUMBER);
58 CCI_RangeAndBearingActuator* pcAct = CLuaUtility::GetDeviceInstance<CCI_RangeAndBearingActuator>(pt_lua_state,
"range_and_bearing");
60 size_t unIdx = lua_tonumber(pt_lua_state, 1);
61 UInt8 unData = lua_tonumber(pt_lua_state, 2);
62 if(unIdx < 1 || unIdx > pcAct->GetSize()) {
63 return luaL_error(pt_lua_state,
"passed index %d out of bounds [1,%d]", unIdx, pcAct->GetSize());
66 pcAct->SetData(unIdx-1, unData);
70 return luaL_error(pt_lua_state,
"robot.range_and_bearing.set_data() expects either one or two arguments");
79 int LuaRABClearData(lua_State* pt_lua_state) {
81 if(lua_gettop(pt_lua_state) != 0) {
82 return luaL_error(pt_lua_state,
"robot.range_and_bearing.clear_data() expects no arguments");
85 CLuaUtility::GetDeviceInstance<CCI_RangeAndBearingActuator>(pt_lua_state,
"range_and_bearing")->ClearData();
115 #ifdef ARGOS_WITH_LUA
116 void CCI_RangeAndBearingActuator::CreateLuaState(lua_State* pt_lua_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...
size_t Size() const
Returns the current size of the byte array.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
unsigned char UInt8
8-bit unsigned integer.
void Zero()
Sets the contents of the byte array to all zeros.
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.
Byte array utility class.
The namespace containing all the ARGoS related code.
void SetData(const CByteArray &c_data)
static void EndTable(lua_State *pt_state)
Adds a table to the Lua stack.