Skip to content

Commit

Permalink
wip: improve prearm handling
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlong13 committed Dec 29, 2024
1 parent 722b1d5 commit 2cc3e70
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,14 @@ function ESC:update()
end
end

-- Return error code
-- Return error messages
function ESC:check_for_errors()
local error_count = 0
for _, status in ipairs(self.srv_telem_in_err_status) do
if status then
error_count = error_count + 1
return {"ESC Telemetry Lost"}
end
end
for _, status in ipairs(self.srv_rpm_in_err_status) do
if status then
error_count = error_count + 1
end
end

-- No errors found
return error_count
return {}
end

return ESC
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local cx_msg = require("scripts.utils.msg")
local cx_esc = require("scripts.bit_subsystems.esc")
local cx_gps = require("scripts.bit_subsystems.gps")

-- Add subsystems that require Built-in-test (implemented in subsystems)
-- Each subsystem should have the following functions:
Expand All @@ -9,9 +10,32 @@ local cx_esc = require("scripts.bit_subsystems.esc")
-- - built in test errors are managed by each subsystem
local subsystems = {
cx_esc,
cx_gps,
}

-- auth id for prearm check
local prearm_msg = nil
local auth_id = arming:get_aux_auth_id()
assert(auth_id, SCRIPT_NAME .. ": could not get prearm check auth id")

-- ******************* Functions *******************
-- get time in seconds since boot
local function get_time()
return millis():tofloat() * 0.001
end

local function set_prearm_error(txt)
if (not prearm_msg) or (prearm_msg ~= txt) then
prearm_msg = txt
arming:set_aux_auth_failed(auth_id, txt)
end
end

local function clear_prearm_error()
prearm_msg = nil
arming:set_aux_auth_passed(auth_id)
end

-- initialize function
local function init()
-- initialize all subsystems that are part of constructor
Expand All @@ -24,32 +48,58 @@ local function init()
end

-- Pre-arm status check before arming
local last_prearm_msg_s = 0 -- timestamp of last message sent
local prearm_messages = {} -- Set of all unique messages seen since we last sent
local function check_prearm_status()
-- Track errors in subsystems
local subsystems_with_errors = {}
local total_errors = 0

local msg = ""
for _, subsystem in pairs(subsystems) do
local error_count = subsystem:check_for_errors()
if error_count > 0 then
table.insert(subsystems_with_errors, { name = subsystem.name, errors = error_count })
total_errors = total_errors + error_count
local errors = subsystem:check_for_errors()
if #errors > 0 then
table.insert(subsystems_with_errors, subsystem.name)
for _, error in pairs(errors) do
prearm_messages[error] = true
end
if #errors == 1 then
msg = errors[1]
else
msg = errors .. " " .. subsystem.name " errors. Check messages."
end
end
end

-- Handle error cases
-- Handle prearm
if #subsystems_with_errors == 0 then
-- No subsystems have errors, clear error flag
cx_msg:clear_prearm_error()
clear_prearm_error()
elseif #subsystems_with_errors == 1 then
-- Only one subsystem has errors
local subsystem = subsystems_with_errors[1]
msg = subsystem.name .. " (" .. subsystem.errors .. " checks fail)"
cx_msg:set_prearm_error(msg)
set_prearm_error(msg)
else
-- More than one subsystems has errors
msg = #subsystems_with_errors .. " subsystems (" .. total_errors .. " checks) failing. Check messages"
cx_msg:set_prearm_error(msg)
msg = ""
for _, subsystem in pairs(subsystems_with_errors) do
msg = msg .. subsystem .. ", "
end
msg = msg:sub(1, -3) .. " failing. Check messages."
set_prearm_error(msg)
end

-- Every 2 seconds, send a message with all the unique errors seen. This
-- helps the operator see the specific errors if there are more than one
-- (since the prearm library only allows one error message for all scripts
-- to share)
if get_time() - last_prearm_msg_s > 2 or get_time() < last_prearm_msg_s then
-- Count the number of unique errors (# operator doesn't work on sets)
local num_prearm_errors = 0
for _ in pairs(prearm_messages) do
num_prearm_errors = num_prearm_errors + 1
end
if num_prearm_errors > 1 then
for err, _ in pairs(prearm_messages) do
gcs:send_text(cx_msg.MAV_SEVERITY.CRITICAL, "Prearm: " .. err)
end
end
last_prearm_msg_s = get_time()
prearm_messages = {}
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,10 @@
SCRIPT_NAME = 'CX_BIT'

local msg = {

-- MAVLink severity level definitions
MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7},

-- auth id for prearm check
prearm_msg = "nil",
auth_id = arming:get_aux_auth_id(),
}

assert(msg.auth_id, SCRIPT_NAME .. ": could not get prearm check auth id")

function msg:set_prearm_error(txt)
if self.prearm_msg == nil or self.prearm_msg ~= txt then
self.prearm_msg = txt
arming:set_aux_auth_failed(self.auth_id, txt)
self:send(self.MAV_SEVERITY.WARNING, txt)
end
end

function msg:clear_prearm_error()
if self.prearm_msg ~= nil then
self.prearm_msg = nil
arming:set_aux_auth_passed(self.auth_id)
end
end

-- wrapper for gcs:send_text(). Helps identify bit messages
function msg:send(severity, txt)
if type(severity) == 'string' then
Expand Down

0 comments on commit 2cc3e70

Please sign in to comment.