Skip to content

Commit

Permalink
AP_Scripting: update switch_rates.lua based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
andyp1per committed Dec 4, 2024
1 parent 19edb0f commit 70cd7b9
Showing 1 changed file with 5 additions and 31 deletions.
36 changes: 5 additions & 31 deletions libraries/AP_Scripting/applets/switch_rates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())

Expand All @@ -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()

0 comments on commit 70cd7b9

Please sign in to comment.