From fecc108e721bc424a27b951a9a8ba0a22c6bd91d Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 25 Sep 2023 14:34:27 +1000 Subject: [PATCH] AP_Scripting: added INF_Inject EFI driver --- libraries/AP_Scripting/drivers/INF_Inject.lua | 247 ++++++++++++++++++ libraries/AP_Scripting/drivers/INF_Inject.md | 38 +++ 2 files changed, 285 insertions(+) create mode 100644 libraries/AP_Scripting/drivers/INF_Inject.lua create mode 100644 libraries/AP_Scripting/drivers/INF_Inject.md diff --git a/libraries/AP_Scripting/drivers/INF_Inject.lua b/libraries/AP_Scripting/drivers/INF_Inject.lua new file mode 100644 index 00000000000000..068dcf258a3edd --- /dev/null +++ b/libraries/AP_Scripting/drivers/INF_Inject.lua @@ -0,0 +1,247 @@ +--[[ + Driver for INF_Inject EFI system + + https://innoflighttechnology.com/efi/ +--]] + +local PARAM_TABLE_KEY = 43 +local PARAM_TABLE_PREFIX = "EFI_INF_" + +local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7} + +-- bind a parameter to a variable given +local function bind_param(name) + local p = Parameter() + assert(p:init(name), string.format('could not find %s parameter', name)) + return p +end + +-- add a parameter and bind it to a variable +local 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)) + return bind_param(PARAM_TABLE_PREFIX .. name) +end + +-- setup script specific parameters +assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 8), 'could not add param table') + +--[[ + // @Param: EFI_INF_ENABLE + // @DisplayName: EFI INF-Inject enable + // @Description: Enable EFI INF-Inject driver + // @Values: 0:Disabled,1:Enabled + // @User: Standard +--]] +EFI_INF_ENABLE = bind_add_param("ENABLE", 1, 0) + +if EFI_INF_ENABLE:get() ~= 1 then + return +end + +local EFI_FUEL_DENS = bind_param("EFI_FUEL_DENS") + +local uart = serial:find_serial(0) -- first scripting serial +if not uart then + gcs:send_text(MAV_SEVERITY.ERROR, "EFI_INF: unable to find scripting serial") + return +end +uart:begin(9600) + +local efi_backend = efi:get_backend(0) +if not efi_backend then + gcs:send_text(MAV_SEVERITY.ERROR, "EFI_INF: unable to find EFI backend") + return +end + +local state = {} +state.last_read_us = uint32_t(0) +state.chk0 = 0 +state.chk1 = 0 +state.total_fuel_g = 0.0 + +local function read_bytes(n) + local ret = "" + for _ = 1, n do + local b = uart:read() + state.chk0 = state.chk0 ~ b + state.chk1 = state.chk1 ~ state.chk0 + ret = ret .. string.char(b) + end + return ret +end + +--[[ + discard pending bytes +--]] +local function discard_pending() + local n = uart:available():toint() + for _ = 1, n do + uart:read() + end +end + +--[[ + convert grams of fuel to cm3 +--]] +local function gram_to_cm3(g) + local kg = g * 0.001 + local kg_per_m3 = EFI_FUEL_DENS:get() + if kg_per_m3 <= 0 then + kg_per_m3 = 742.9 + end + local m3 = kg / kg_per_m3 + return m3 * 1.0e6 +end + +--[[ + check for input and parse data +--]] +local function check_input() + local n_bytes = uart:available():toint() + if n_bytes < 83 then + return + end + if n_bytes > 83 then + discard_pending() + return + end + + local tus = micros() + state.chk0 = 0 + state.chk1 = 0 + + -- look for basic data table 2 + header0, header1, source, target, dtype, num, id, ack = string.unpack("