rusefi/firmware/controllers/lua/examples/gdi4-communication.txt

152 lines
5.3 KiB
Plaintext
Raw Normal View History

2022-12-20 14:40:15 -08:00
-- communication with https://github.com/rusefi/rusefi-hardware/tree/main/GDI-4ch/firmware
GDI4_BASE_ADDRESS = 0xF0
2023-05-15 18:12:34 -07:00
GDI_CHANGE_ADDRESS = GDI4_BASE_ADDRESS + 0x10
2022-12-20 14:40:15 -08:00
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
function toHexString(num)
if num == 0 then
return '0'
end
local result = ""
while num > 0 do
local n = num % 16
result = hexstr[n + 1] ..result
num = math.floor(num / 16)
end
return result
end
function arrayToString(arr)
local str = ""
local index = 1
while arr[index] ~= nil do
str = str.." "..toHexString(arr[index])
index = index + 1
end
return str
end
function printPacket(bus, id, dlc, data)
2023-05-15 18:12:34 -07:00
print('Received status packet ' ..arrayToString(data))
2022-12-20 14:40:15 -08:00
end
function getTwoBytesLSB(data, offset, factor)
return (data[offset + 2] * 256 + data[offset + 1]) * factor
end
2022-12-20 16:31:26 -08:00
function setTwoBytesLsb(data, offset, value)
value = math.floor(value)
data[offset + 2] = value >> 8
data[offset + 1] = value & 0xff
end
2022-12-20 14:40:15 -08:00
2023-05-15 18:12:34 -07:00
function onCanConfiguration1(bus, id, dlc, data)
print("Received configuration1 "..arrayToString(data))
print("GDI4 says BoostVoltage "..getTwoBytesLSB(data, 0, 1) )
print("GDI4 says BoostCurrent "..getTwoBytesLSB(data, 2, 1 / 128) )
print("GDI4 says TBoostMin "..getTwoBytesLSB(data, 4, 1) )
print("GDI4 says TBoostMax "..getTwoBytesLSB(data, 6, 1) )
end
function onCanConfiguration2(bus, id, dlc, data)
print("Received configuration2 "..arrayToString(data))
print("GDI4 says PeakCurrent "..getTwoBytesLSB(data, 0, 1 / 128) )
print("GDI4 says TpeakDuration "..getTwoBytesLSB(data, 2, 1) )
print("GDI4 says TpeakOff "..getTwoBytesLSB(data, 4, 1) )
print("GDI4 says Tbypass "..getTwoBytesLSB(data, 6, 1) )
end
function onCanConfiguration3(bus, id, dlc, data)
print("Received configuration3 "..arrayToString(data))
print("GDI4 says HoldCurrent "..getTwoBytesLSB(data, 0, 1 / 128) )
print("GDI4 says TholdOff "..getTwoBytesLSB(data, 2, 1) )
print("GDI4 says THoldDuration "..getTwoBytesLSB(data, 4, 1) )
print("GDI4 says PumpPeakCurrent"..getTwoBytesLSB(data, 6, 1 / 128) )
end
function onCanConfiguration4(bus, id, dlc, data)
print("Received configuration4 "..arrayToString(data))
print("GDI4 says PumpHoldCurrent "..getTwoBytesLSB(data, 0, 1 / 128) )
2022-12-20 14:40:15 -08:00
end
2023-05-15 11:18:40 -07:00
function onCanVersion(bus, id, dlc, data)
year = data[1] * 100 + data[2]
month = data[3]
day = data[4]
2023-05-15 18:12:34 -07:00
print ("GDI4 firmware " .. year .. '/' .. month .. '/' .. day)
2023-05-15 11:18:40 -07:00
end
2022-12-20 14:40:15 -08:00
canRxAdd(GDI4_BASE_ADDRESS, printPacket)
2023-05-15 18:12:34 -07:00
canRxAdd(GDI4_BASE_ADDRESS + 1, onCanConfiguration1)
canRxAdd(GDI4_BASE_ADDRESS + 2, onCanConfiguration2)
canRxAdd(GDI4_BASE_ADDRESS + 3, onCanConfiguration3)
canRxAdd(GDI4_BASE_ADDRESS + 4, onCanConfiguration4)
canRxAdd(GDI4_BASE_ADDRESS + 5, onCanVersion)
2022-12-20 16:31:26 -08:00
GDI4_CAN_SET_TAG = 0x78
local data_set_settings = { GDI4_CAN_SET_TAG, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
2022-12-20 14:40:15 -08:00
2023-05-15 18:12:34 -07:00
FIXED_POINT = 128
2022-12-20 16:31:26 -08:00
2023-05-15 18:12:34 -07:00
setTickRate(2)
2022-12-20 16:31:26 -08:00
2023-05-15 18:12:34 -07:00
function onTick()
2022-12-20 14:40:15 -08:00
2023-05-15 18:12:34 -07:00
-- set mc33_hvolt 60
boostVoltage = getCalibration("mc33_hvolt")
boostCurrent = getCalibration("mc33_i_boost") / 1000.0
TBoostMin = getCalibration("mc33_t_min_boost")
print("To send 0: boostVoltage " .. boostVoltage .." boostCurrent " ..boostCurrent .." TBoostMin " ..TBoostMin)
-- set mc33_t_max_boost 380
TBoostMax = getCalibration("mc33_t_max_boost")
-- set mc33_i_peak 14500
peakCurrent = getCalibration("mc33_i_peak") / 1000.0
TpeakDuration = getCalibration("mc33_t_peak_tot")
print("To send 1: TBoostMax " .. TBoostMax .." peakCurrent " ..peakCurrent .." TpeakDuration " ..TpeakDuration)
TpeakOff = getCalibration("mc33_t_peak_off")
Tbypass = getCalibration("mc33_t_bypass")
holdCurrent = getCalibration("mc33_i_hold") / 1000.0
print("To send 2: TpeakOff " .. TpeakOff .. " Tbypass " .. Tbypass .." holdCurrent " ..holdCurrent)
TholdOff = getCalibration("mc33_t_hold_off")
THoldDuration = getCalibration("mc33_t_hold_tot")
print("To send 3: TholdOff " .. TholdOff .. " THoldDuration " .. THoldDuration)
pumpPeakCurrent = getCalibration("mc33_hpfp_i_peak")
pumpHoldCurrent = getCalibration("mc33_i_hold")
2023-05-15 18:12:34 -07:00
setTwoBytesLsb(data_set_settings, 1, boostVoltage)
setTwoBytesLsb(data_set_settings, 3, boostCurrent * FIXED_POINT)
setTwoBytesLsb(data_set_settings, 5, TBoostMin)
print('Will be sending ' ..arrayToString(data_set_settings))
txCan(1, GDI_CHANGE_ADDRESS, 1, data_set_settings)
setTwoBytesLsb(data_set_settings, 1, TBoostMax)
setTwoBytesLsb(data_set_settings, 3, peakCurrent * FIXED_POINT)
setTwoBytesLsb(data_set_settings, 5, TpeakDuration)
print('Will be sending ' ..arrayToString(data_set_settings))
txCan(1, GDI_CHANGE_ADDRESS + 1, 1, data_set_settings)
2023-05-15 18:12:34 -07:00
setTwoBytesLsb(data_set_settings, 1, TpeakOff)
setTwoBytesLsb(data_set_settings, 3, Tbypass)
setTwoBytesLsb(data_set_settings, 5, holdCurrent * FIXED_POINT)
print('Will be sending ' ..arrayToString(data_set_settings))
txCan(1, GDI_CHANGE_ADDRESS + 2, 1, data_set_settings)
2023-05-15 18:12:34 -07:00
setTwoBytesLsb(data_set_settings, 1, TholdOff)
setTwoBytesLsb(data_set_settings, 3, THoldDuration)
setTwoBytesLsb(data_set_settings, 5, pumpPeakCurrent * FIXED_POINT)
print('Will be sending ' ..arrayToString(data_set_settings))
txCan(1, GDI_CHANGE_ADDRESS + 3, 1, data_set_settings)
setTwoBytesLsb(data_set_settings, 1, pumpHoldCurrent * FIXED_POINT)
2023-05-15 18:12:34 -07:00
print('Will be sending ' ..arrayToString(data_set_settings))
txCan(1, GDI_CHANGE_ADDRESS + 4, 1, data_set_settings)
2022-12-20 14:40:15 -08:00
end