From 8049c64a5b00c2f35118fa975015756ddf1c4df5 Mon Sep 17 00:00:00 2001 From: Michael du Breuil Date: Thu, 27 Sep 2018 16:04:37 -0700 Subject: [PATCH] AP_Scripting: Create a thread and spin --- libraries/AP_Scripting/AP_Scripting.cpp | 65 +++++++++++++++++++++---- libraries/AP_Scripting/AP_Scripting.h | 24 ++++++--- 2 files changed, 74 insertions(+), 15 deletions(-) diff --git a/libraries/AP_Scripting/AP_Scripting.cpp b/libraries/AP_Scripting/AP_Scripting.cpp index b19c098d9a..9d878c244c 100644 --- a/libraries/AP_Scripting/AP_Scripting.cpp +++ b/libraries/AP_Scripting/AP_Scripting.cpp @@ -13,12 +13,43 @@ along with this program. If not, see . */ -#if ENABLE_SCRIPTING - #include #include +#include + +// 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 +#endif // !defined(SCRIPTING_STACK_SIZE) + +#if !defined(SCRIPTING_STACK_MAX_SIZE) + #define SCRIPTING_STACK_MAX_SIZE 16384 +#endif // !defined(SCRIPTING_STACK_MAX_SIZE) + +static_assert(SCRIPTING_STACK_SIZE >= SCRIPTING_STACK_MIN_SIZE, "Scripting requires a larger minimum stack size"); +static_assert(SCRIPTING_STACK_SIZE <= SCRIPTING_STACK_MAX_SIZE, "Scripting requires a smaller stack size"); + +extern const AP_HAL::HAL& hal; + +const AP_Param::GroupInfo AP_Scripting::var_info[] = { + // @Param: ENABLE + // @DisplayName: Enable Scripting + // @Description: Controls if scripting is enabled + // @Values: 0:None,1:Lua Scripts + // @RebootRequired: True + // @User: Advanced + AP_GROUPINFO_FLAGS("ENABLE", 1, AP_Scripting, _enable, 1, AP_PARAM_FLAG_ENABLE), + + AP_GROUPEND +}; AP_Scripting::AP_Scripting() { + AP_Param::setup_object_defaults(this, var_info); + #if CONFIG_HAL_BOARD == HAL_BOARD_SITL if (_singleton != nullptr) { AP_HAL::panic("Scripting must be a singleton"); @@ -27,16 +58,32 @@ AP_Scripting::AP_Scripting() { _singleton = this; } +bool AP_Scripting::init(void) { + if (!_enable) { + return true; + } + if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Scripting::thread, void), + "Scripting", SCRIPTING_STACK_SIZE, AP_HAL::Scheduler::PRIORITY_SCRIPTING, 0)) { + return false; + } + + _running = true; + return true; +} + +void AP_Scripting::thread(void) { + unsigned int loop = 0; + while (true) { + hal.scheduler->delay(1000); + gcs().send_text(MAV_SEVERITY_INFO, "Scripting Loop: %u", loop++); + } +} AP_Scripting *AP_Scripting::_singleton = nullptr; namespace AP { - -AP_Scripting *scripting() { - return AP_Scripting::get_singleton(); + AP_Scripting *scripting() { + return AP_Scripting::get_singleton(); + } } - -}; - -#endif // ENABLE_SCRIPTING diff --git a/libraries/AP_Scripting/AP_Scripting.h b/libraries/AP_Scripting/AP_Scripting.h index 95ea22c0be..aeb61be6cc 100644 --- a/libraries/AP_Scripting/AP_Scripting.h +++ b/libraries/AP_Scripting/AP_Scripting.h @@ -14,9 +14,10 @@ */ #pragma once -#include +#ifdef ENABLE_SCRIPTING -#if ENABLE_SCRIPTING +#include +#include class AP_Scripting { @@ -27,16 +28,27 @@ public: AP_Scripting(const AP_Scripting &other) = delete; AP_Scripting &operator=(const AP_Scripting&) = delete; - static AP_Scripting * get_singleton() { - return _singleton; - } + bool init(void); + + bool is_running(void) const { return _running; } + + static AP_Scripting * get_singleton(void) { return _singleton; } + + static const struct AP_Param::GroupInfo var_info[]; private: + void thread(void); // main script execution thread + + bool _running; + + AP_Int8 _enable; + static AP_Scripting *_singleton; + }; namespace AP { - AP_Scripting * scripting(); + AP_Scripting * scripting(void); }; #endif // ENABLE_SCRIPTING