AP_Scripting: Create a thread and spin

This commit is contained in:
Michael du Breuil 2018-09-27 16:04:37 -07:00 committed by Andrew Tridgell
parent 3aed07a83a
commit 8049c64a5b
2 changed files with 74 additions and 15 deletions

View File

@ -13,12 +13,43 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if ENABLE_SCRIPTING
#include <AP_Scripting/AP_Scripting.h>
#include <AP_HAL/AP_HAL.h>
#include <GCS_MAVLink/GCS.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
#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

View File

@ -14,9 +14,10 @@
*/
#pragma once
#include <AP_Common/AP_Common.h>
#ifdef ENABLE_SCRIPTING
#if ENABLE_SCRIPTING
#include <AP_Common/AP_Common.h>
#include <AP_Param/AP_Param.h>
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