Skip to content

Commit

Permalink
AP_Scripting: stop and restart scripting via command int
Browse files Browse the repository at this point in the history
  • Loading branch information
IamPete1 authored and tridge committed Nov 2, 2021
1 parent 6a46ccf commit b047ea5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
52 changes: 43 additions & 9 deletions libraries/AP_Scripting/AP_Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ MAV_RESULT AP_Scripting::handle_command_int_packet(const mavlink_command_int_t &
case SCRIPTING_CMD_REPL_STOP:
repl_stop();
return MAV_RESULT_ACCEPTED;
case SCRIPTING_CMD_STOP:
_restart = false;
_stop = true;
return MAV_RESULT_ACCEPTED;
case SCRIPTING_CMD_STOP_AND_RESTART:
_restart = true;
_stop = true;
return MAV_RESULT_ACCEPTED;
case SCRIPTING_CMD_ENUM_END: // cope with MAVLink generator appending to our enum
break;
}
Expand Down Expand Up @@ -197,17 +205,43 @@ void AP_Scripting::repl_stop(void) {
}

void AP_Scripting::thread(void) {
lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_level, terminal);
if (lua == nullptr || !lua->heap_allocated()) {
gcs().send_text(MAV_SEVERITY_CRITICAL, "Unable to allocate scripting memory");
while (true) {
// reset flags
_stop = false;
_restart = false;

lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_level, terminal);
if (lua == nullptr || !lua->heap_allocated()) {
gcs().send_text(MAV_SEVERITY_CRITICAL, "Unable to allocate scripting memory");
_init_failed = true;
} else {
// run won't return while scripting is still active
lua->run();

// only reachable if the lua backend has died for any reason
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting has stopped");
}
delete lua;
_init_failed = true;
return;
}
lua->run();

// only reachable if the lua backend has died for any reason
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting has stopped");
bool cleared = false;
while(true) {
// 1hz check if we should restart
hal.scheduler->delay(1000);
if (!enabled()) {
// enable must be put to 0 and back to 1 to restart from params
cleared = true;
continue;
}
// must be enabled to get this far
if (cleared || _restart) {
gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting restated");
break;
}
if (_debug_level > 0) {
gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: scripting stopped");
}
}
}
}

void AP_Scripting::handle_mission_command(const AP_Mission::Mission_Command& cmd_in)
Expand Down
3 changes: 3 additions & 0 deletions libraries/AP_Scripting/AP_Scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AP_Scripting
bool init_failed(void) const { return _init_failed; }

bool enabled(void) const { return _enable != 0; };
bool should_run(void) const { return enabled() && !_stop; }

static AP_Scripting * get_singleton(void) { return _singleton; }

Expand Down Expand Up @@ -100,6 +101,8 @@ class AP_Scripting
AP_Int16 _dir_disable;

bool _init_failed; // true if memory allocation failed
bool _restart; // true if scripts should be restarted
bool _stop; // true if scripts should be stopped

static AP_Scripting *_singleton;

Expand Down
2 changes: 1 addition & 1 deletion libraries/AP_Scripting/lua_scripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ void lua_scripts::run(void) {
succeeded_initial_load = true;
#endif // __clang_analyzer__

while (AP_Scripting::get_singleton()->enabled()) {
while (AP_Scripting::get_singleton()->should_run()) {
// handle terminal data if we have any
if (terminal.session) {
doREPL(L);
Expand Down

0 comments on commit b047ea5

Please sign in to comment.