From 70cd7b90703ec65419de578d9cdab0ff3825a933 Mon Sep 17 00:00:00 2001 From: Andy Piper Date: Wed, 4 Dec 2024 20:31:12 +0000 Subject: [PATCH] AP_Scripting: update switch_rates.lua based on review comments --- .../AP_Scripting/applets/switch_rates.lua | 36 +++---------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/libraries/AP_Scripting/applets/switch_rates.lua b/libraries/AP_Scripting/applets/switch_rates.lua index 28a2fd350bd6b..6b6b484cd4555 100644 --- a/libraries/AP_Scripting/applets/switch_rates.lua +++ b/libraries/AP_Scripting/applets/switch_rates.lua @@ -2,9 +2,6 @@ rate switch utility. This helps to compare different tunes in-flight to compare performance --]] ----@diagnostic disable: param-type-mismatch - - local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7} local PARAM_TABLE_KEY = 33 @@ -14,19 +11,10 @@ local PARAM_BACKUP_TABLE_PREFIX = "X_" local UPDATE_RATE_HZ = 4 --- bind a parameter to a variable, old syntax to support older firmware -function bind_param(name) - local p = Parameter() - if not p:init(name) then - return nil - end - return p -end - -- add a parameter and bind it to a variable function bind_add_param(name, idx, default_value) assert(param:add_param(PARAM_TABLE_KEY, idx, name, default_value), string.format('could not add param %s', name)) - local p = bind_param(PARAM_TABLE_PREFIX .. name) + local p = Parameter(PARAM_TABLE_PREFIX .. name) assert(p, string.format("could not find parameter %s", name)) return p end @@ -36,7 +24,7 @@ function bind_add_backup_param(name, idx, default_value) -- shorten pname to fit with X_ prefix local short_name = string.sub(name, math.max(1, 1 + string.len(name) - (16 - string.len(PARAM_BACKUP_TABLE_PREFIX)))) assert(param:add_param(PARAM_BACKUP_TABLE_KEY, idx, short_name, default_value), string.format('could not add param %s', short_name)) - local p = bind_param(PARAM_BACKUP_TABLE_PREFIX .. short_name) + local p = Parameter(PARAM_BACKUP_TABLE_PREFIX .. short_name) assert(p, string.format("could not find parameter %s", PARAM_BACKUP_TABLE_PREFIX .. short_name)) return p end @@ -146,7 +134,7 @@ local AuxSwitchPosNames = {"LOW", "MIDDLE", "HIGH"} -- start in LOW start local prev_pos = AuxSwitchPos.LOW --- main update function +-- main update function, called at 10Hz function update() local sw_pos = rc:get_aux_cached(PREV_RC_FUNC:get()) @@ -155,22 +143,8 @@ function update() gcs:send_text(MAV_SEVERITY.INFO, string.format("Rate switch: %u parameters changed to %s", count, AuxSwitchPosNames[sw_pos + 1])) prev_pos = sw_pos end -end - --- wrapper around update(). This calls update() at 10Hz, --- and if update faults then an error is displayed, but the script is not --- stopped -function protected_wrapper() - local success, err = pcall(update) - if not success then - gcs:send_text(MAV_SEVERITY.EMERGENCY, "Rate switch: internal Error: " .. err) - -- when we fault we run the update function again after 1s, slowing it - -- down a bit so we don't flood the console with errors - --return protected_wrapper, 1000 - return - end - return protected_wrapper, 1000/UPDATE_RATE_HZ + return update, 1000/UPDATE_RATE_HZ end -- start running update loop -return protected_wrapper() +return update()