Skip to content

Commit

Permalink
AP_Scripting: fixed crc and added logging to INF_Inject driver
Browse files Browse the repository at this point in the history
  • Loading branch information
tridge committed May 1, 2024
1 parent 54d117b commit 9ebcc21
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
52 changes: 50 additions & 2 deletions libraries/AP_Scripting/drivers/INF_Inject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,30 @@ assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 8), 'could not add p
--]]
EFI_INF_ENABLE = bind_add_param("ENABLE", 1, 1)

--[[
// @Param: EFI_INF_OPTIONS
// @DisplayName: EFI INF-Inject options
// @Description: EFI INF driver options
// @Bitmask: 0:EnableLogging
// @User: Standard
--]]
EFI_INF_OPTIONS = bind_add_param("OPTIONS", 2, 0)

local OPTION_LOGGING = (1<<0)

--[[
return true if an option is enabled
--]]
local function option_enabled(option)
return (EFI_INF_OPTIONS:get() & option) ~= 0
end

if EFI_INF_ENABLE:get() ~= 1 then
return
end

local EFI_FUEL_DENS = bind_param("EFI_FUEL_DENS")
local SCR_VM_I_COUNT = bind_param("SCR_VM_I_COUNT")

local uart = serial:find_serial(0) -- first scripting serial
if not uart then
Expand All @@ -53,12 +72,38 @@ if not efi_backend then
return
end

--[[
we need a bit more time in this driver
--]]
if SCR_VM_I_COUNT:get() < 50000 then
gcs:send_text(MAV_SEVERITY.INFO, "EFI_INF: raising SCR_VM_I_COUNT to 50000")
SCR_VM_I_COUNT:set_and_save(50000)
end

local state = {}
state.last_read_us = uint32_t(0)
state.chk0 = 0
state.chk1 = 0
state.total_fuel_g = 0.0

local file_handle = nil

--[[
log a set of bytes
--]]
local function log_bytes(s)
if not file_handle then
file_handle = io.open("INF_Inject.log", "w")
end
if file_handle then
local magic = 0x7fe53b04
local now_ms = millis():toint()
local hdr = string.pack("<III", magic, now_ms, string.len(s))
file_handle:write(hdr)
file_handle:write(s)
end
end

local function read_bytes(n)
local ret = ""
for _ = 1, n do
Expand All @@ -67,6 +112,9 @@ local function read_bytes(n)
state.chk1 = state.chk1 ~ state.chk0
ret = ret .. string.char(b)
end
if option_enabled(OPTION_LOGGING) then
log_bytes(ret)
end
return ret
end

Expand Down Expand Up @@ -94,12 +142,12 @@ local function check_input()
end

local tus = micros()
state.chk0 = 0
state.chk1 = 0

-- sync on header start
local header_ok = false
while n_bytes >= packet_size and not header_ok do
state.chk0 = 0
state.chk1 = 0
local header0 = string.unpack("<B", read_bytes(1))
n_bytes = n_bytes - 1
if header0 == 0xB5 then
Expand Down
7 changes: 7 additions & 0 deletions libraries/AP_Scripting/drivers/INF_Inject.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ The script used the following parameters:

this must be set to 1 to enable the driver

## EFI_INF_OPTIONS

This sets options for the driver. Currently the only option is to set
EFI_INF_OPTIONS to 1 to enable logging of the raw serial bytes to a
file called INF_Inject.log

# Operation

This driver should be loaded by placing the lua script in the
APM/SCRIPTS directory on the microSD card, which can be done either
directly or via MAVFTP. The following key parameters should be set:

- SCR_ENABLE should be set to 1
- SCR_VM_I_COUNT should be set to at least 50000
- EFI_TYPE should be set to 7
- EFI_INF_ENABLE should be set to 1
- SERIALn_PROTOCOL should be set to 28 for the connected EFI serial
Expand Down

0 comments on commit 9ebcc21

Please sign in to comment.