|
|
Line 7: |
Line 7: |
|
| |
|
| ==Status== | | ==Status== |
| Verified
| | Use the CAN bus preset for the Link G4 |
| | |
| <pre>
| |
| --This example configured for Link G4+ ECU
| |
| | |
| --how frequently we poll for CAN messages
| |
| tickRate = 30
| |
| --the CAN baud rate
| |
| CAN_baud = 1000000
| |
| --CAN channel to listen on. 0=first CAN channel, 1=second
| |
| CAN_chan = 0
| |
| --1 for Big Endian (MSB) mode; 0 for Little Endian mode (LSB)
| |
| be_mode = 1
| |
| | |
| --add your virtual channels here
| |
| --params: <channel name>,<sample rate>, <logging precision>, <min value>, <max value>, <units label>
| |
| rpmId = addChannel("RPM", 10, 0, 0, 10000)
| |
| tempId = addChannel("EngineTemp", 1, 0, -50, 205, "C")
| |
| iatId = addChannel("IAT", 10, 0, -20, 205, "C")
| |
| oilTempId = addChannel("OilTemp", 1, 0, -20, 205, "F")
| |
| tpsId = addChannel("TPS", 10, 0, 0, 100, "%")
| |
| ignAdvId = addChannel("IgnTiming", 10, 0, -100, 100, "Deg")
| |
| wheelSpdId = addChannel("WheelSpeed", 10, 0, 0, 300, "kph")
| |
| oilPressId = addChannel("OilPress", 10, 0, 0, 1000, "kPa")
| |
| fuelPressId = addChannel("FuelPress", 10, 0, 0, 1000, "kPa")
| |
| lambda1 = addChannel("Lambda1", 10, 3, 0, 3, "")
| |
| lambda2 = addChannel("Lambda2", 10, 3, 0, 3, "")
| |
| steerId = addChannel("Steering", 10, 0, -360, 360)
| |
| baroId = addChannel("Baro", 1, 0, 0, 200, "kPa")
| |
| gearId = addChannel("Gear", 5, 0, 0, 6, "")
| |
| knockId = addChannel("Knock", 10, 0, 0, 1000, "")
| |
| | |
| ----------------------------------------
| |
| --customize here for CAN channel mapping
| |
| --format is:
| |
| --[CAN Id] = function(data) map_chan(<chan_id>, data, <CAN offset>, <CAN length>, <multiplier>,
| |
| -- <adder>, [filter])
| |
| ----------------------------------------
| |
| CAN_map = {
| |
| [1200] = function(data) map_chan(rpmId, data, 0, 2, 1, 0)
| |
| map_chan(tempId, data, 4, 1, 1, -50)
| |
| map_chan(iatId, data, 5, 1, 1, -50)
| |
| map_chan(oilTempId, data, 7, 1, 1, -50) end,
| |
| [1201] = function(data) map_chan(tpsId, data, 0, 2, 0.1, 0)
| |
| map_chan(ignAdvId, data, 2, 2, 0.1, -100)
| |
| map_chan(wheelSpdId, data, 4, 1, 0.1, 0)
| |
| map_chan(oilPressId, data, 5, 1, 0.1, 0)
| |
| map_chan(fuelPressId, data, 6, 1, 1, 0) end,
| |
| [1202] = function(data) map_chan(lambda1, data, 0, 2, 0.001, 0)
| |
| map_chan(lambda2, data, 2, 2, 0.001, 0)
| |
| map_chan(steerId, data, 4, 2, 0.1, -3000)
| |
| map_chan(baroId, data, 6, 2, 0.1, 0) end,
| |
| [1203] = function(data) map_chan(gearId, data, 0, 1, 1, 0)
| |
| map_chan(knockId, data, 6, 2, 5, 0) end
| |
| }
| |
| | |
| function onTick()
| |
| processCAN(CAN_chan)
| |
| end
| |
| | |
| --===========do not edit below===========
| |
| function processCAN(chan)
| |
| repeat
| |
| local id, e, data = rxCAN(chan)
| |
| if id ~= nil then
| |
| local map = CAN_map[id]
| |
| if map ~= nil then
| |
| map(data)
| |
| end
| |
| end
| |
| until id == nil
| |
| end
| |
| | |
| --Map CAN channel, little endian format
| |
| function map_chan_le(cid, data, offset, len, mult, add, filter)
| |
| offset = offset + 1
| |
| local value = 0
| |
| local shift = 1
| |
| while len > 0 do
| |
| value = value + (data[offset] * shift)
| |
| shift = shift * 256
| |
| offset = offset + 1
| |
| len = len - 1
| |
| end
| |
| local cv = value * mult + add
| |
| if filter ~= nil then cv = filter(cv) end
| |
| setChannel(cid, cv)
| |
| end
| |
| | |
| --Map CAN channel, big endian format
| |
| function map_chan_be(cid, data, offset, len, mult, add, filter)
| |
| offset = offset + 1
| |
| local value = 0
| |
| while len > 0 do
| |
| value = (value * 256) + data[offset]
| |
| offset = offset + 1
| |
| len = len - 1
| |
| end
| |
| local cv = value * mult + add
| |
| if filter ~= nil then cv = filter(cv) end
| |
| setChannel(cid, cv)
| |
| end
| |
| | |
| map_chan = (be_mode == 1) and map_chan_be or map_chan_le
| |
| initCAN(CAN_chan, CAN_baud)
| |
| setTickRate(tickRate)
| |
| </pre>
| |