Skip to content

Instantly share code, notes, and snippets.

@rusefillc
Created July 10, 2023 00:38
Show Gist options
  • Save rusefillc/26d36ced900dc54b5fdef01e43d842d3 to your computer and use it in GitHub Desktop.
Save rusefillc/26d36ced900dc54b5fdef01e43d842d3 to your computer and use it in GitHub Desktop.
2013 current state of rusEFI CAN bus
function getTwoBytesLSB(data, offset, factor)
return (data[offset + 2] * 256 + data[offset + 1]) * factor
end
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(math.floor(arr[index])) index = index + 1
end
return str
end
function setTwoBytes(data, offset, value)
value = math.floor(value)
data[offset + 2] = value >> 8
data[offset + 1] = value & 0xff
end
function setTwoBytesLsb(data, offset, value)
value = math.floor(value)
data[offset + 2] = value >> 8
data[offset + 1] = value & 0xff
end
function hyuindaiSumNibbles(data, seed)
local sum = seed
for i = 1, 7, 1
do
b = data[i]
sum = sum + (b % 16) + math.floor(b / 16)
end
return (16 - sum) % 16
end
GDI4_BASE_ADDRESS = 0xBB20
GDI_CHANGE_ADDRESS = GDI4_BASE_ADDRESS + 0x10
GDI4_CAN_SET_TAG = 0x78
local data_set_settings = { GDI4_CAN_SET_TAG, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
FIXED_POINT = 128
setTickRate(2)
function onCanConfiguration3(bus, id, dlc, data)
-- print("Received configuration3 "..arrayToString(data))
pumpPeak = getTwoBytesLSB(data, 6, 1 / 128)
-- print("GDI4 says PumpPeakCurrent ".. pumpPeak)
setLuaGauge(1, pumpPeak)
end
canRxAdd(GDI4_BASE_ADDRESS + 3, onCanConfiguration3)
EMS_DCT11_128 = 0x80
EMS_DCT12_129 = 0x81
EMS11_790 = 0x316
EMS14_1349 = 0x545
counter = 0
payLoad128 = { 0x00, 0x17, 0x70, 0x0F, 0x1B, 0x2C, 0x1B, 0x75 }
payLoad129 = { 0x40, 0x84, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x75 }
payLoad1349 = { 0xCA, 0x16, 0x00, 0x8A, 0x75, 0xFF, 0x75, 0xFF }
speedSensor = Sensor.new("VehicleSpeed")
speedSensor : setTimeout(3000)
function getBitRange(data, bitIndex, bitWidth)
byteIndex = bitIndex >> 3
shift = bitIndex - byteIndex * 8
value = data[1 + byteIndex]
if (shift + bitWidth > 8) then
value = value + data[2 + byteIndex] * 256
end
mask = (1 << bitWidth) - 1
return (value >> shift) & mask
end
function onCluPacket(bus, id, dlc, data)
speedKph = getBitRange(data, 8, 9) * 0.5
print('onCAR_SPEED ' ..speedKph)
speedSensor : set(speedKph)
end
canRxAdd(1, 1264, onCluPacket)
function onTick()
local RPMread = math.floor(getSensor("RPM") * 4)
local RPMhi = RPMread >> 8
local RPMlo = RPMread & 0xff
payLoad128[3] = RPMlo
payLoad128[4] = RPMhi
counter = (counter + 1) % 16
check128 = hyuindaiSumNibbles(payLoad128, counter)
payLoad128[8] = check128 * 16 + counter
txCan(1, EMS_DCT11_128, 0, payLoad128)
check129 = hyuindaiSumNibbles(payLoad129, counter)
payLoad129[8] = check129 * 16 + counter
txCan(1, EMS_DCT12_129, 0, payLoad129)
canRPMpayload = { 0x05, 0x1B, RPMlo, RPMhi, 0x1B, 0x2C, 0x00, 0x7F }
txCan(1, EMS11_790, 0, canRPMpayload)
txCan(1, EMS14_1349, 0, payLoad1349)
pumpPeakCurrent = getCalibration("mc33_hpfp_i_peak")
pumpHoldCurrent = getCalibration("mc33_hpfp_i_hold")
TholdOff = getCalibration("mc33_t_hold_off")
THoldDuration = getCalibration("mc33_t_hold_tot")
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)
outputCanID = 0
outputCanID = GDI4_BASE_ADDRESS
setTwoBytesLsb(data_set_settings, 3, outputCanID)
-- print('Will be sending ' ..arrayToString(data_set_settings))
txCan(1, GDI_CHANGE_ADDRESS + 4, 1, data_set_settings)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment