mirror of
https://github.com/ArduPilot/ardupilot.git
synced 2026-11-09 16:41:21 +08:00
AP_Scripting: Create a thread and spin
This commit is contained in:
1 parent
3aed07a83a
commit
8049c64a5b
2 files changed
+74
-15
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user