AP_Scripting: added SCR_THD_PRIORITY parameter

this makes it possible to run lua scripts at higher priorities, which
makes real time lua scripts (such as IMU drivers) possible
This commit is contained in:
Andrew Tridgell 2023-12-03 11:36:18 +11:00
parent 56cc78b853
commit 61b3ad326d
2 changed files with 43 additions and 1 deletions

View File

@ -153,6 +153,14 @@ const AP_Param::GroupInfo AP_Scripting::var_info[] = {
// @User: Advanced // @User: Advanced
AP_GROUPINFO("RUN_CHECKSUM", 13, AP_Scripting, _required_running_checksum, -1), AP_GROUPINFO("RUN_CHECKSUM", 13, AP_Scripting, _required_running_checksum, -1),
// @Param: THD_PRIORITY
// @DisplayName: Scripting thread priority
// @Description: This sets the priority of the scripting thread. This is normally set to a low priority to prevent scripts from interfering with other parts of the system. Advanced users can change this priority if scripting needs to be prioritised for realtime applications. WARNING: changing this parameter can impact the stability of your flight controller. The scipting thread priority in this parameter is chosen based on a set of system level priorities for other subsystems. It is strongly recommended that you use the lowest priority that is sufficient for your application. Note that all scripts run at the same priority, so if you raise this priority you must carefully audit all lua scripts for behaviour that does not interfere with the operation of the system.
// @Values: 0:Normal, 1:IO Priority, 2:Storage Priority, 3:UART Priority, 4:I2C Priority, 5:SPI Priority, 6:Timer Priority, 7:Main Priority, 8:Boost Priority
// @RebootRequired: True
// @User: Advanced
AP_GROUPINFO("THD_PRIORITY", 14, AP_Scripting, _thd_priority, uint8_t(ThreadPriority::NORMAL)),
AP_GROUPEND AP_GROUPEND
}; };
@ -179,8 +187,29 @@ void AP_Scripting::init(void) {
} }
} }
AP_HAL::Scheduler::priority_base priority = AP_HAL::Scheduler::PRIORITY_SCRIPTING;
static const struct {
ThreadPriority scr_priority;
AP_HAL::Scheduler::priority_base hal_priority;
} priority_map[] = {
{ ThreadPriority::NORMAL, AP_HAL::Scheduler::PRIORITY_SCRIPTING },
{ ThreadPriority::IO, AP_HAL::Scheduler::PRIORITY_IO },
{ ThreadPriority::STORAGE, AP_HAL::Scheduler::PRIORITY_STORAGE },
{ ThreadPriority::UART, AP_HAL::Scheduler::PRIORITY_UART },
{ ThreadPriority::I2C, AP_HAL::Scheduler::PRIORITY_I2C },
{ ThreadPriority::SPI, AP_HAL::Scheduler::PRIORITY_SPI },
{ ThreadPriority::TIMER, AP_HAL::Scheduler::PRIORITY_TIMER },
{ ThreadPriority::MAIN, AP_HAL::Scheduler::PRIORITY_MAIN },
{ ThreadPriority::BOOST, AP_HAL::Scheduler::PRIORITY_BOOST },
};
for (const auto &p : priority_map) {
if (p.scr_priority == _thd_priority) {
priority = p.hal_priority;
}
}
if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Scripting::thread, void), if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Scripting::thread, void),
"Scripting", SCRIPTING_STACK_SIZE, AP_HAL::Scheduler::PRIORITY_SCRIPTING, 0)) { "Scripting", SCRIPTING_STACK_SIZE, priority, 0)) {
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Scripting: %s", "failed to start"); GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Scripting: %s", "failed to start");
_thread_failed = true; _thread_failed = true;
} }

View File

@ -159,6 +159,18 @@ private:
// The full range of uint32 integers cannot be represented by a float. // The full range of uint32 integers cannot be represented by a float.
const uint32_t checksum_param_mask = 0x007FFFFF; const uint32_t checksum_param_mask = 0x007FFFFF;
enum class ThreadPriority : uint8_t {
NORMAL = 0,
IO = 1,
STORAGE = 2,
UART = 3,
I2C = 4,
SPI = 5,
TIMER = 6,
MAIN = 7,
BOOST = 8
};
AP_Int8 _enable; AP_Int8 _enable;
AP_Int32 _script_vm_exec_count; AP_Int32 _script_vm_exec_count;
AP_Int32 _script_heap_size; AP_Int32 _script_heap_size;
@ -167,6 +179,7 @@ private:
AP_Int32 _required_loaded_checksum; AP_Int32 _required_loaded_checksum;
AP_Int32 _required_running_checksum; AP_Int32 _required_running_checksum;
AP_Enum<ThreadPriority> _thd_priority;
bool _thread_failed; // thread allocation failed bool _thread_failed; // thread allocation failed
bool _init_failed; // true if memory allocation failed bool _init_failed; // true if memory allocation failed