Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_Scripting: update HFE EFI driver script #28772

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 88 additions & 9 deletions libraries/AP_Scripting/drivers/EFI_HFE.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--[[
EFI Scripting backend driver for HFE based on HFEDCN0191 Rev E
EFI Scripting backend driver for HFE based on HFEDCN0191 Rev L
--]]
-- luacheck: only 0
---@diagnostic disable: param-type-mismatch
Expand Down Expand Up @@ -64,16 +64,78 @@ end
local efi_backend = nil

-- Setup EFI Parameters
assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 6), 'could not add EFI_HFE param table')
assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 10), 'could not add EFI_HFE param table')

--[[
// @Param: EFI_HFE_ENABLE
// @DisplayName: Enable HFE EFI driver
// @Description: Enable HFE EFI driver
// @Values: 0:Disabled,1:Enabled
// @User: Standard
--]]
local EFI_HFE_ENABLE = bind_add_param('ENABLE', 1, 0)
local EFI_HFE_RATE_HZ = bind_add_param('RATE_HZ', 2, 200) -- Script update frequency in Hz
local EFI_HFE_ECU_IDX = bind_add_param('ECU_IDX', 3, 0) -- ECU index on CAN bus, 0 for automatic
local EFI_HFE_FUEL_DTY = bind_add_param('FUEL_DTY', 4, 740) -- fuel density, g/litre
local EFI_HFE_REL_IDX = bind_add_param('REL_IDX', 5, 0) -- relay number for engine enable
local EFI_HFE_CANDRV = bind_add_param('CANDRV', 6, 0) -- CAN driver number

local ICE_PWM_IGN_ON = bind_param("ICE_PWM_IGN_ON")
--[[
// @Param: EFI_HFE_RATE_HZ
// @DisplayName: HFI EFI Update rate
// @Description: HFI EFI Update rate
// @Range: 0 400
// @User: Standard
--]]
local EFI_HFE_RATE_HZ = bind_add_param('RATE_HZ', 2, 200)

--[[
// @Param: EFI_HFE_ECU_IDX
// @DisplayName: HFI EFI ECU index
// @Description: HFI EFI ECU index, 0 for automatic
// @Range: 0 10
// @User: Standard
--]]
local EFI_HFE_ECU_IDX = bind_add_param('ECU_IDX', 3, 0)

--[[
// @Param: EFI_HFE_FUEL_DTY
// @DisplayName: HFI EFI fuel density
// @Description: HFI EFI fuel density in gram per litre
// @Range: 0 2000
// @User: Standard
--]]
local EFI_HFE_FUEL_DTY = bind_add_param('FUEL_DTY', 4, 740)

--[[
// @Param: EFI_HFE_REL_IDX
// @DisplayName: HFI EFI relay index
// @Description: HFI EFI relay index
// @Range: 0 10
// @User: Standard
--]]
local EFI_HFE_REL_IDX = bind_add_param('REL_IDX', 5, 0)

--[[
// @Param: EFI_HFE_CANDRV
// @DisplayName: HFI EFI CAN driver
// @Description: HFI EFI CAN driver
// @Values: 0:None,1:1stCANDriver,2:2ndCanDriver
// @User: Standard
--]]
local EFI_HFE_CANDRV = bind_add_param('CANDRV', 6, 0)

--[[
// @Param: EFI_HFE_OPTIONS
// @DisplayName: HFI EFI options
// @Description: HFI EFI options
// @Bitmask: 1:EnableCANLogging
// @User: Standard
--]]
local EFI_HFE_OPTIONS = bind_add_param('OPTIONS', 7, 0)

local OPTION_LOGALLFRAMES = 0x01

-- on 4.6.x this will be nil and direct relay support in AP_ICEngine can be used
local ICE_PWM_IGN_ON = nil
if param:get("ICE_PWM_IGN_ON") then
ICE_PWM_IGN_ON = Parameter("ICE_PWM_IGN_ON")
end

if EFI_HFE_ENABLE:get() == 0 then
return
Expand All @@ -92,6 +154,20 @@ if not driver1 then
return
end

local frame_count = 0

--[[
frame logging - can be replayed with Tools/scripts/CAN/CAN_playback.py
--]]
local function log_can_frame(frame)
logger:write("CANF",'Id,DLC,FC,B0,B1,B2,B3,B4,B5,B6,B7','IBIBBBBBBBB',
frame:id(),
frame:dlc(),
frame_count,
frame:data(0), frame:data(1), frame:data(2), frame:data(3),
frame:data(4), frame:data(5), frame:data(6), frame:data(7))
frame_count = frame_count + 1
end

local now_s = get_time_sec()

Expand Down Expand Up @@ -146,6 +222,9 @@ local function engine_control(_driver)
if not frame then
break
end
if EFI_HFE_OPTIONS:get() & OPTION_LOGALLFRAMES ~= 0 then
log_can_frame(frame)
end

-- All Frame IDs for this EFI Engine are in the 29-bit extended address space
if frame:isExtended() then
Expand Down Expand Up @@ -263,7 +342,7 @@ local function engine_control(_driver)

-- map K_IGNITION to relay for enable of engine
local relay_idx = EFI_HFE_REL_IDX:get()
if relay_idx > 0 then
if relay_idx > 0 and ICE_PWM_IGN_ON then
local ignition_pwm = SRV_Channels:get_output_pwm(K_IGNITION)
if ignition_pwm == ICE_PWM_IGN_ON:get() then
relay:on(relay_idx-1)
Expand Down
Loading