Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting/AP_Arming: add pre-arm check that scripting has initialised successfull #12894

Merged
merged 8 commits into from Dec 3, 2019
@@ -199,9 +199,7 @@ void Rover::startup_ground(void)
#endif

#ifdef ENABLE_SCRIPTING
if (!g2.scripting.init()) {
gcs().send_text(MAV_SEVERITY_ERROR, "Scripting failed to start");
}
g2.scripting.init();
#endif // ENABLE_SCRIPTING

// we don't want writes to the serial port to cause us to pause
@@ -60,9 +60,7 @@ void Tracker::init_tracker()
#endif

#ifdef ENABLE_SCRIPTING
if (!scripting.init()) {
gcs().send_text(MAV_SEVERITY_ERROR, "Scripting failed to start");
}
scripting.init();
#endif // ENABLE_SCRIPTING

// initialise compass
@@ -232,9 +232,7 @@ void Copter::init_ardupilot()
startup_INS_ground();

#ifdef ENABLE_SCRIPTING
if (!g2.scripting.init()) {
gcs().send_text(MAV_SEVERITY_ERROR, "Scripting failed to start");
}
g2.scripting.init();
#endif // ENABLE_SCRIPTING

// set landed flags
@@ -228,9 +228,7 @@ void Plane::startup_ground(void)
#endif

#ifdef ENABLE_SCRIPTING
if (!g2.scripting.init()) {
gcs().send_text(MAV_SEVERITY_ERROR, "Scripting failed to start");
}
g2.scripting.init();
#endif // ENABLE_SCRIPTING

// reset last heartbeat time, so we don't trigger failsafe on slow
@@ -190,9 +190,7 @@ void Sub::init_ardupilot()
startup_INS_ground();

#ifdef ENABLE_SCRIPTING
if (!g2.scripting.init()) {
gcs().send_text(MAV_SEVERITY_ERROR, "Scripting failed to start");
}
g2.scripting.init();
#endif // ENABLE_SCRIPTING

// we don't want writes to the serial port to cause us to pause
@@ -32,6 +32,7 @@
#include <AP_Baro/AP_Baro.h>
#include <AP_RangeFinder/AP_RangeFinder.h>
#include <AP_Terrain/AP_Terrain.h>
#include <AP_Scripting/AP_Scripting.h>

#if HAL_WITH_UAVCAN
#include <AP_BoardConfig/AP_BoardConfig_CAN.h>
@@ -705,6 +706,13 @@ bool AP_Arming::system_checks(bool report)
check_failed(ARMING_CHECK_SYSTEM, report, "Terrain out of memory");
return false;
}
#endif
#ifdef ENABLE_SCRIPTING
const AP_Scripting *scripting = AP_Scripting::get_singleton();
if ((scripting != nullptr) && scripting->enabled() && scripting->init_failed()) {
check_failed(ARMING_CHECK_SYSTEM, report, "Scripting out of memory");
return false;
}
#endif
}
if (AP::internalerror().errors() != 0) {
@@ -79,25 +79,24 @@ AP_Scripting::AP_Scripting() {
_singleton = this;
}

bool AP_Scripting::init(void) {
void AP_Scripting::init(void) {
if (!_enable) {
return true;
return;
}

if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Scripting::thread, void),
"Scripting", SCRIPTING_STACK_SIZE, AP_HAL::Scheduler::PRIORITY_SCRIPTING, 0)) {
gcs().send_text(MAV_SEVERITY_CRITICAL, "Could not create scripting stack (%d)", SCRIPTING_STACK_SIZE);
_enable = 0;
return false;
gcs().send_text(MAV_SEVERITY_ERROR, "Scripting failed to start");
_init_failed = true;
}

return true;
}

void AP_Scripting::thread(void) {
lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_level);
if (lua == nullptr) {
if (lua == nullptr || !lua->heap_allocated()) {
gcs().send_text(MAV_SEVERITY_CRITICAL, "Unable to allocate scripting memory");
_init_failed = true;
return;
}
lua->run();
@@ -28,7 +28,8 @@ class AP_Scripting
AP_Scripting(const AP_Scripting &other) = delete;
AP_Scripting &operator=(const AP_Scripting&) = delete;

bool init(void);
void init(void);
bool init_failed(void) const { return _init_failed; }

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

@@ -46,6 +47,8 @@ class AP_Scripting
AP_Int32 _script_heap_size;
AP_Int8 _debug_level;

bool _init_failed; // true if memory allocation failed

static AP_Scripting *_singleton;

};
@@ -30,6 +30,9 @@ class lua_scripts
lua_scripts(const lua_scripts &other) = delete;
lua_scripts &operator=(const lua_scripts&) = delete;

// return true if initialisation failed
bool heap_allocated() const { return _heap != nullptr; }

// run scripts, does not return unless an error occured
void run(void);

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.