AP_Scripting: Initial test script

This commit is contained in:
Michael du Breuil 2018-10-03 13:37:43 -07:00 committed by Andrew Tridgell
parent 8382d377bd
commit 68576eff32
3 changed files with 56 additions and 1 deletions

View File

@ -17,13 +17,15 @@
#include <AP_HAL/AP_HAL.h>
#include <GCS_MAVLink/GCS.h>
#include "lua_bindings.h"
// ensure that we have a set of stack sizes, and enforce constraints around it
// except for the minimum size, these are allowed to be defined by the build system
#undef SCRIPTING_STACK_MIN_SIZE
#define SCRIPTING_STACK_MIN_SIZE 2048
#if !defined(SCRIPTING_STACK_SIZE)
#define SCRIPTING_STACK_SIZE 2048
#define SCRIPTING_STACK_SIZE 8192
#endif // !defined(SCRIPTING_STACK_SIZE)
#if !defined(SCRIPTING_STACK_MAX_SIZE)
@ -74,9 +76,28 @@ bool AP_Scripting::init(void) {
void AP_Scripting::thread(void) {
unsigned int loop = 0;
lua_State *state = luaL_newstate();
luaL_openlibs(state);
load_lua_bindings(state);
luaL_loadstring(state, "gcs.send_text(string.format(\"1 + 2 = %d\", 1+2))");
while (true) {
hal.scheduler->delay(1000);
gcs().send_text(MAV_SEVERITY_INFO, "Scripting Loop: %u", loop++);
const uint32_t startMem = hal.util->available_memory();
const uint32_t loadEnd = AP_HAL::micros();
lua_pushvalue(state, -1);
if(lua_pcall(state, 0, LUA_MULTRET, 0)) {
gcs().send_text(MAV_SEVERITY_INFO, "Lua: %s", lua_tostring(state, -1));
hal.console->printf("Lua: %s", lua_tostring(state, -1));
lua_pop(state, 1);
continue;
}
const uint32_t runEnd = AP_HAL::micros();
const uint32_t endMem = hal.util->available_memory();
gcs().send_text(MAV_SEVERITY_INFO, "Execution: %d Memory: %d", runEnd - loadEnd, startMem - endMem);
}
}

View File

@ -0,0 +1,26 @@
#include <AP_Common/AP_Common.h>
#include <GCS_MAVLink/GCS.h>
#include "lua_bindings.h"
int lua_gcs_send_text(lua_State *state) {
const char* str = lua_tostring(state, 1);
gcs().send_text(MAV_SEVERITY_INFO, str);
return 0;
}
static const luaL_Reg gcs_functions[] =
{
{"send_text", lua_gcs_send_text},
{NULL, NULL}
};
void load_lua_bindings(lua_State *state) {
luaL_newlib(state, gcs_functions);
lua_setglobal(state, "gcs");
}
void hook(lua_State *L, lua_Debug *ar) {
gcs().send_text(MAV_SEVERITY_INFO, "got a debug hook");
lua_error(L);
}

View File

@ -0,0 +1,8 @@
#pragma once
#include "lua/src/lua.hpp"
// load all known lua bindings into the state
void load_lua_bindings(lua_State *state);
void hook(lua_State *L, lua_Debug *ar);