Skip to content

Commit

Permalink
AP_Scripting: added SCR_THD_PRIORITY parameter
Browse files Browse the repository at this point in the history
this makes it possible to run lua scripts at higher priorities, which
makes real time lua scripts (such as IMU drivers) possible
  • Loading branch information
tridge committed Dec 14, 2023
1 parent 56cc78b commit 61b3ad3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
31 changes: 30 additions & 1 deletion libraries/AP_Scripting/AP_Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ const AP_Param::GroupInfo AP_Scripting::var_info[] = {
// @User: Advanced
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
};

Expand All @@ -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),
"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");
_thread_failed = true;
}
Expand Down
13 changes: 13 additions & 0 deletions libraries/AP_Scripting/AP_Scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ class AP_Scripting
// The full range of uint32 integers cannot be represented by a float.
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_Int32 _script_vm_exec_count;
AP_Int32 _script_heap_size;
Expand All @@ -167,6 +179,7 @@ class AP_Scripting
AP_Int32 _required_loaded_checksum;
AP_Int32 _required_running_checksum;

AP_Enum<ThreadPriority> _thd_priority;

bool _thread_failed; // thread allocation failed
bool _init_failed; // true if memory allocation failed
Expand Down

0 comments on commit 61b3ad3

Please sign in to comment.