rusefi/firmware/controllers/lua/examples/nissan-350z-bcm.txt

66 lines
1.5 KiB
Plaintext
Raw Normal View History

2023-05-31 18:04:46 -07:00
-- this controls onCanRx rate as well!
setTickRate(300)
timeout = 3000
rpmSensor = Sensor.new("rpm")
rpmSensor : setTimeout(timeout)
--ppsSensor = Sensor.new("AcceleratorPedal")
--ppsSensor : setTimeout(timeout)
speedSensor = Sensor.new("VehicleSpeed")
speedSensor : setTimeout(timeout)
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
2023-06-04 15:26:42 -07:00
-- pretty LOL if you ask me: RPM and vehicle speed uses different byte order
function getTwoBytesMSB(data, offset, factor)
return (data[offset + 1] * 256 + data[offset + 2]) * factor
end
2023-05-31 20:15:32 -07:00
function getTwoBytesLSB(data, offset, factor)
return (data[offset + 2] * 256 + data[offset + 1]) * factor
end
2023-05-31 18:04:46 -07:00
function onRpmData(bus, id, dlc, data)
-- print('onRpmData ' ..arrayToString(data))
2023-05-31 20:15:32 -07:00
rpm = getTwoBytesLSB(data, 3, 3.15)
print ('RPM ' .. rpm)
2023-06-04 15:26:42 -07:00
rpmSensor : set(rpm)
2023-05-31 18:04:46 -07:00
end
function onSpeedData(bus, id, dlc, data)
-- print('onSpeedData ' ..arrayToString(data))
2023-06-04 15:26:42 -07:00
speed = getTwoBytesMSB(data, 4, 0.01)
print ('speed ' .. speed)
speedSensor : set(speed)
2023-05-31 18:04:46 -07:00
end
canRxAdd(1, 0x23D, onRpmData)
2023-06-04 15:26:42 -07:00
canRxAdd(1, 0x280, onSpeedData)