Skip to content

Commit

Permalink
hwdef: CarbonixCubeOrange - added cx_built_in_test.lua
Browse files Browse the repository at this point in the history
This commit introduces a Lua script, cx_built_in_test.lua, which serves as a Continuous Built-In Test (BIT) for Carbonix Aircrafts. The script checks multiple functionalities and provides the following features:

Firmware version check: The script ensures that the firmware version is at least 5.0.
ESC Status Check and Fault Detection: The script continuously checks the status of the ESC and detects any faults.
Aircraft type detection: The script automatically detects the type of aircraft (either Ottano or Volanti) based on the EFI type.
Pre-arm checks: The script performs several pre-arm checks, including checking the firmware version, initializing parameters, and checking the aircraft type.
During-arm checks: The script performs checks while the aircraft is armed, including checking ESC telemetry and VTOL failure checks.

This script is designed to enhance the safety and reliability of Carbonix Aircrafts by continuously monitoring their status and detecting any potential issues.

SW-61
  • Loading branch information
loki077 committed Apr 7, 2024
1 parent 47894c7 commit e6bd8b3
Showing 1 changed file with 248 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
--[[
File Name: cx_built_in_test.lua
Description: This script is Carbonix Continuous Built in Test used to do multiple lua functionality of Carbonix Aircrafts, Ottano and ESC.
Functionality:
- Firmware Version base checked
- ESC Status Check and Fault Detection
- EFI Status Check and Fault Detection
Owner: [Carbonix - Software Team]
]]


-- ******************* Macros *******************

local SCRIPT_NAME = 'CX_BIT'
local CX_SANITY_SCRIPT_VERSION = 1.0 -- Script Version
local CX_PILOT_MIN_FW_VERSION = 5.0 -- Minimum Firmware Version Supported e.g CxPilot 5.0.0 should be mentioned as 5.0


-------- MAVLINK/AUTOPILOT 'CONSTANTS' --------
-- local MAV_SEVERITY_EMERGENCY=0 --/* System is unusable. This is a "panic" condition. | */
-- local MAV_SEVERITY_ALERT=1 --/* Action should be taken immediately. Indicates error in non-critical systems. | */
local MAV_SEVERITY_CRITICAL=2 --/* Action must be taken immediately. Indicates failure in a primary system. | */
local MAV_SEVERITY_ERROR=3 --/* Indicates an error in secondary/redundant systems. | */
local MAV_SEVERITY_WARNING=4 --/* Indicates about a possible future error if this is not resolved within a given timeframe. Example would be a low battery warning. | */
-- local MAV_SEVERITY_NOTICE=5 --/* An unusual event has occurred though not an error condition. This should be investigated for the root cause. | */
local MAV_SEVERITY_INFO=6 --/* Normal operational messages. Useful for logging. No action is required for these messages. | */
-- local MAV_SEVERITY_DEBUG=7 --/* Useful non-operational messages that can assist in debugging. These should not occur during normal operation. | */
-- local MAV_SEVERITY_ENUM_END=8 --/* | */

local MSG_NORMAL = 1 -- 1 for normal status messages
local MSG_DEBUG = 2 -- 2 for additional debug messages
local VERBOSE_MODE = 2 -- 0 to suppress all GCS messages, 1 for normal status messages, 2 for additional debug messages

local M1 = 0
local M2 = 1
local M3 = 2
local M4 = 3
local M5 = 4
local WARNING_MSG_TIMEOUT = 15

local UNKNOWN = 0
local VOLANTI = 1
local OTTANO = 2

local HIRTH_EFI_TYPE = 8
-- ******************* Variables *******************

local aircraft_type = UNKNOWN

local pre_arm_init = false

local servo_previous_telem_data_ms = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
local servo_telem_in_error_status = {false, false, false, false, false, false, false}
local motor_in_error_status = {false, false, false, false, false, false, false}
local last_motor_lost = -1

local number_of_esc = 5
-- ******************* Objects *******************

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

local params = {
-- CAN_D1_UC_ESC_BM = Parameter(),
-- CAN_D2_UC_ESC_BM = Parameter(),
EFI_TYPE = Parameter()
}

-- ******************* Functions *******************

local function get_time_sec()
return millis():tofloat() * 0.001
end

-- wrapper for gcs:send_text()
local function gcs_msg(msg_type, severity, txt)
if type(msg_type) == 'string' then
-- allow just a string to be passed for simple/routine messages
txt = msg_type
msg_type = MSG_NORMAL
severity = MAV_SEVERITY_INFO
end
if msg_type <= VERBOSE_MODE then
gcs:send_text(severity, string.format('%s: %s', SCRIPT_NAME, txt))
end
end

local function check_aircraft()
if params.EFI_TYPE:get() == HIRTH_EFI_TYPE then
aircraft_type = OTTANO
number_of_esc = 4
else
aircraft_type = VOLANTI
number_of_esc = 5
end
return 0
end

local function init_param()
for paramName, param in pairs(params) do
if not param:init(paramName) then
return -1
end
end
return 0
end

local function pre_arm_check_init()
-- Check Script uses a miniumum firmware version
local cx_version = FWVersion:string()
local version = cx_version:match("(%d+%.%d+)")

-- check if firmware has CxPilot or Volanti or Ottano
if cx_version:find("CxPilot") or cx_version:find("Volanti") or cx_version:find("Ottano") then
version = tonumber(version)
if CX_PILOT_MIN_FW_VERSION > version then
arming:set_aux_auth_failed(auth_id, string.format('%s Requires: %.1f. Found Version: %s', SCRIPT_NAME, CX_PILOT_MIN_FW_VERSION, version))
gcs_msg(MSG_NORMAL, MAV_SEVERITY_WARNING, string.format('%s Requires: %.1f. Found Version: %s', SCRIPT_NAME, CX_PILOT_MIN_FW_VERSION, version))
return -1
end
else
arming:set_aux_auth_failed(auth_id, string.format('Requires: CxPilot Code Base. Found Version: %s', FWVersion:string()))
gcs_msg(MSG_NORMAL, MAV_SEVERITY_WARNING, string.format('Requires: CxPilot Code Base. Found Version: %s', FWVersion:string()))
return -1
end
-- check param fetch
if init_param() ~= 0 then
arming:set_aux_auth_failed(auth_id, "Parameter Init Failed")
gcs_msg(MSG_NORMAL, MAV_SEVERITY_WARNING, "Parameter Init Failed")
return -1
end
-- check aircraft Type
if check_aircraft() ~= 0 then
arming:set_aux_auth_failed(auth_id, "Aircraft Type Check Failed")
gcs_msg(MSG_NORMAL, MAV_SEVERITY_WARNING, "Aircraft Type Check Failed")
return -1
end
gcs_msg(MSG_NORMAL, MAV_SEVERITY_INFO, "Script Version " .. CX_SANITY_SCRIPT_VERSION .. " Initialized on " .. cx_version)
arming:set_aux_auth_passed(auth_id)
return 0
end

local function pre_arm_check_loop()
-- check ESC
for i = 1, number_of_esc do
local esc_last_telem_data_ms = esc_telem:get_last_telem_data_ms(i-1):toint()
if esc_last_telem_data_ms == nil or esc_last_telem_data_ms == 0 or esc_last_telem_data_ms == servo_previous_telem_data_ms[i] then
if servo_telem_in_error_status[i] == false then
gcs_msg(MSG_NORMAL, MAV_SEVERITY_CRITICAL, "ESC " .. i .. " Telemetry Lost")
arming:set_aux_auth_failed(auth_id, "ESC " .. i .. " Telemetry Lost")
servo_telem_in_error_status[i] = true
end
if esc_last_telem_data_ms ~= nil and esc_last_telem_data_ms ~= 0 then
servo_previous_telem_data_ms[i] = esc_last_telem_data_ms
end
return -1
else
if servo_telem_in_error_status[i] == true then
gcs_msg(MSG_NORMAL, MAV_SEVERITY_INFO, "ESC " .. i .. " Telemetry Recovered")
servo_telem_in_error_status[i] = false
end
servo_previous_telem_data_ms[i] = esc_last_telem_data_ms
end
end

-- check EFI
-- if aircraft_type == OTTANO then
-- local efi_last_update_ms = tonumber(efi.get_last_update_ms(4))
-- if efi_last_update_ms == 0 or efi_last_update_ms - servo_previous_telem_data_ms[5] > 1000 then
-- arming:set_aux_auth_failed(auth_id, "EFI Telemetry Lost")
-- gcs_msg(MSG_NORMAL, MAV_SEVERITY_CRITICAL, "EFI Telemetry Lost")
-- return -1
-- end
-- servo_previous_telem_data_ms[5] = efi_last_update_ms
-- end

-- check servo

arming:set_aux_auth_passed(auth_id)
return 0
end

local function vtol_failure_check()
local lost_index = MotorsMatrix:get_thrust_boost() and MotorsMatrix:get_lost_motor() or -1
if lost_index ~= last_motor_lost then
local message = lost_index == -1 and "Motors Thrust: recovered" or "Motor ".. lost_index + 1 .." Thrust: lost"
local severity = lost_index == -1 and MAV_SEVERITY_INFO or MAV_SEVERITY_CRITICAL
gcs_msg(MSG_NORMAL, severity, message)
last_motor_lost = lost_index
end
end

local last_fail_msg_time = 0
local telem_failure = false

local function during_arm_check_loop()
--check ESC Telemetry is done in pre_arm_check_loop
vtol_failure_check()
end

local function update()
pre_arm_check_loop()

if arming:is_armed() then
during_arm_check_loop()
end
end

-- wrapper around update(). This calls update() and if update faults
-- then an error is displayed, but the script is not stopped
local function protected_wrapper()
local success, err = pcall(update)
if not success then
gcs_msg(MSG_NORMAL, MAV_SEVERITY_ERROR, "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
end
return protected_wrapper, 100
end

local function script_exit()
-- pre arm failure SCRIPT_NAME not Running
arming:set_aux_auth_failed(auth_id, SCRIPT_NAME .. " Not Running")
gcs_msg(MSG_NORMAL, MAV_SEVERITY_CRITICAL, "LUA SCRIPT EXIT ... Need Reboot to Reinitialize")
end

local function test_print_esc_telem()
local val = tostring(esc_telem:get_last_telem_data_ms(0):toint())
gcs:send_text(0, "ESC 0 Telemetry: " .. val)
val = tostring(esc_telem:get_last_telem_data_ms(1):toint())
gcs:send_text(0, "ESC 1 Telemetry: " .. val)
val = tostring(esc_telem:get_last_telem_data_ms(2):toint())
gcs:send_text(0, "ESC 2 Telemetry: " .. val)
val = tostring(esc_telem:get_last_telem_data_ms(3):toint())
gcs:send_text(0, "ESC 3 Telemetry: " .. val)
return test, 500
end

-- ******************* Main *******************
gcs_msg(MSG_NORMAL, MAV_SEVERITY_INFO, "Script Version " .. CX_SANITY_SCRIPT_VERSION .. " Running")
if pre_arm_check_init() == 0 then
return protected_wrapper()
end

-- return test_print_esc_telem()

script_exit()

0 comments on commit e6bd8b3

Please sign in to comment.