Skip to content

Commit 318e6b9

Browse files
committed
AP_Scripting: update HFE EFI driver script
added option to log all CAN frames for debugging new protocols
1 parent f37bd41 commit 318e6b9

File tree

1 file changed

+100
-12
lines changed

1 file changed

+100
-12
lines changed

libraries/AP_Scripting/drivers/EFI_HFE.lua

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--[[
2-
EFI Scripting backend driver for HFE based on HFEDCN0191 Rev E
2+
EFI Scripting backend driver for HFE based on HFEDCN0191 Rev L
33
--]]
44
---@diagnostic disable: param-type-mismatch
55
---@diagnostic disable: undefined-field
@@ -62,16 +62,78 @@ end
6262
local efi_backend = nil
6363

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

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

74-
local ICE_PWM_IGN_ON = bind_param("ICE_PWM_IGN_ON")
76+
--[[
77+
// @Param: EFI_HFE_RATE_HZ
78+
// @DisplayName: HFI EFI Update rate
79+
// @Description: HFI EFI Update rate
80+
// @Range: 0 400
81+
// @User: Standard
82+
--]]
83+
local EFI_HFE_RATE_HZ = bind_add_param('RATE_HZ', 2, 200)
84+
85+
--[[
86+
// @Param: EFI_HFE_ECU_IDX
87+
// @DisplayName: HFI EFI ECU index
88+
// @Description: HFI EFI ECU index, 0 for automatic
89+
// @Range: 0 10
90+
// @User: Standard
91+
--]]
92+
local EFI_HFE_ECU_IDX = bind_add_param('ECU_IDX', 3, 0)
93+
94+
--[[
95+
// @Param: EFI_HFE_FUEL_DTY
96+
// @DisplayName: HFI EFI fuel density
97+
// @Description: HFI EFI fuel density in gram per litre
98+
// @Range: 0 2000
99+
// @User: Standard
100+
--]]
101+
local EFI_HFE_FUEL_DTY = bind_add_param('FUEL_DTY', 4, 740)
102+
103+
--[[
104+
// @Param: EFI_HFE_REL_IDX
105+
// @DisplayName: HFI EFI relay index
106+
// @Description: HFI EFI relay index
107+
// @Range: 0 10
108+
// @User: Standard
109+
--]]
110+
local EFI_HFE_REL_IDX = bind_add_param('REL_IDX', 5, 0)
111+
112+
--[[
113+
// @Param: EFI_HFE_CANDRV
114+
// @DisplayName: HFI EFI CAN driver
115+
// @Description: HFI EFI CAN driver
116+
// @Values: 0:None,1:1stCANDriver,2:2ndCanDriver
117+
// @User: Standard
118+
--]]
119+
local EFI_HFE_CANDRV = bind_add_param('CANDRV', 6, 0)
120+
121+
--[[
122+
// @Param: EFI_HFE_OPTIONS
123+
// @DisplayName: HFI EFI options
124+
// @Description: HFI EFI options
125+
// @Bitmask: 1:EnableCANLogging
126+
// @User: Standard
127+
--]]
128+
local EFI_HFE_OPTIONS = bind_add_param('OPTIONS', 7, 0)
129+
130+
local OPTION_LOGALLFRAMES = 0x01
131+
132+
-- on 4.6.x this will be nil and direct relay support in AP_ICEngine can be used
133+
local ICE_PWM_IGN_ON = nil
134+
if param:get("ICE_PWM_IGN_ON") then
135+
ICE_PWM_IGN_ON = Parameter("ICE_PWM_IGN_ON")
136+
end
75137

76138
if EFI_HFE_ENABLE:get() == 0 then
77139
return
@@ -90,6 +152,29 @@ if not driver1 then
90152
return
91153
end
92154

155+
local frame_count = 0
156+
157+
--[[
158+
frame logging - can be replayed with Tools/scripts/CAN/CAN_playback.py
159+
--]]
160+
local function log_can_frame(frame)
161+
logger:write("CANF",'Id,DLC,FC,B0,B1,B2,B3,B4,B5,B6,B7','IBIBBBBBBBB',
162+
frame:id(),
163+
frame:dlc(),
164+
frame_count,
165+
frame:data(0), frame:data(1), frame:data(2), frame:data(3),
166+
frame:data(4), frame:data(5), frame:data(6), frame:data(7))
167+
frame_count = frame_count + 1
168+
end
169+
170+
--[[
171+
in 4.5.x temperatures in EFI state structure were incorrectly
172+
used as C instead of Kelvin
173+
--]]
174+
local temp_offset = 0.0
175+
if FWVersion:major() == 4 and FWVersion:minor() <= 5 then
176+
temp_offset = -273.15
177+
end
93178

94179
local now_s = get_time_sec()
95180

@@ -137,6 +222,9 @@ local function engine_control(driver)
137222
if not frame then
138223
break
139224
end
225+
if EFI_HFE_OPTIONS:get() & OPTION_LOGALLFRAMES ~= 0 then
226+
log_can_frame(frame)
227+
end
140228

141229
-- All Frame IDs for this EFI Engine are in the 29-bit extended address space
142230
if frame:isExtended() then
@@ -200,8 +288,8 @@ local function engine_control(driver)
200288
-- Build and set the EFI_State that is passed into the EFI Scripting backend
201289
function self.set_EFI_State()
202290
-- Cylinder_Status
203-
cylinder_state:cylinder_head_temperature(temps.cht + C_TO_KELVIN)
204-
cylinder_state:exhaust_gas_temperature(temps.mat)
291+
cylinder_state:cylinder_head_temperature(temps.cht + C_TO_KELVIN + temp_offset)
292+
cylinder_state:exhaust_gas_temperature(temps.mat + temp_offset)
205293
cylinder_state:ignition_timing_deg(ignition_angle)
206294
if rpm > 0 then
207295
cylinder_state:injection_time_ms((60.0/rpm)*1000*injector_duty)
@@ -213,7 +301,7 @@ local function engine_control(driver)
213301

214302
efi_state:atmospheric_pressure_kpa(air_pressure*0.001)
215303
efi_state:intake_manifold_pressure_kpa(air_pressure*0.001*map_ratio)
216-
efi_state:intake_manifold_temperature(temps.mat + C_TO_KELVIN)
304+
efi_state:intake_manifold_temperature(temps.mat + C_TO_KELVIN + temp_offset)
217305
efi_state:throttle_position_percent(math.floor((throttle_pos*100/255)+0.5))
218306
efi_state:ignition_voltage(ecu_voltage)
219307
efi_state:fuel_pressure(fuel_press*0.001)
@@ -254,7 +342,7 @@ local function engine_control(driver)
254342

255343
-- map K_IGNITION to relay for enable of engine
256344
local relay_idx = EFI_HFE_REL_IDX:get()
257-
if relay_idx > 0 then
345+
if relay_idx > 0 and ICE_PWM_IGN_ON then
258346
local ignition_pwm = SRV_Channels:get_output_pwm(K_IGNITION)
259347
if ignition_pwm == ICE_PWM_IGN_ON:get() then
260348
relay:on(relay_idx-1)

0 commit comments

Comments
 (0)