--This example configured for 06+ S2000 CAN --Virtual channels --chan_id = addChannel(name, ?, ?, min, max, unit) vss_chan = addChannel("VSS", 10, 0, 0, 255, "MPH") rpm_chan = addChannel("RPM", 10, 0, 0, 10000) --tps_chan = addChannel("TPS", 10, 0, 0, 100, '%') --ect_chan = addChannel("ECT", 10, 0, 0, 255, 'F') --iat_chan = addChannel("IAT", 10, 0, 0, 255, 'F') --Convert raw to RPM --only start logging / telemetry if engine is running function toRPM(value) if value > 500 then startLogging() else stopLogging() end return value end --Convert KPH to MPH function toMPH(value) return value * 0.62 end --Convert raw to TPS function toTPS(value) return value end --Convert C to F function toF(value) return value * 1.8 + 32 end --Callback function map by something --[CAN RXID] = function(data) map_chan(, data, , , , , [filter]) RAWCAN_map = { --0x1C0 is a 50Hz RAW CAN channel sent by VSA [0x1C0] = function(data) map_chan(vss_chan, data, 4, 2, 1, 0, toMPH) end, --0x12C is a 30Hz RAW CAN Channel sent by something [0x12C] = function(data) map_chan(rpm_chan, data, 4, 2, 1, 0, toRPM) end --[0x18DAF110] = function(data) map_chan(tps_chan, data, 0xB, 1, 1, 0) end } --ISOCAN_map = { -- [0x09] = function(data) map_chan(rpm_chan, data, 2, 1, 0) end, -- [0x0B] = function(data) map_chan(tps_chan, data, 1, 1, 0, toTPS) end, -- [0x0E] = function(data) map_chan(ect_chan, data, 1, 1, 0, toF) end --} ISOCAN_map = { [0x00] = function(data) map_chan(rpm_chan, data, 0x09, 2, 1, 0) end, [0x01] = function(data) map_chan(tps_chan, data, 0x0B, 1, 1, 0, toTPS) end, [0x02] = function(data) map_chan(ect_chan, data, 0x0E, 1, 1, 0, toF) end } --Callback for processing RAW CAN channels function map_chan(vid, data, pos, size, scalar, offset, filter) local value = 0 --Shift offset because? pos = pos + 1 --Pack bytes into word while size > 0 do --Use 256 because? value = (value * 256) + data[pos] pos = pos + 1 size = size - 1 end --Convert raw to user using scalar/offset local cv = value * scalar + offset if filter ~= nil then cv = filter(cv) end setChannel(vid, cv) end --Listen on the CAN1 CAN_chan = 0 --Look for CAN messages on each tick tickRate = 30 function onTick() processRAW(CAN_chan) --processISO(CAN_chan) end function processRAW(chan) repeat local id, e, data = rxCAN(chan) if id ~= nil then local map = RAWCAN_map[id] if map ~= nil then map(data) end end until id == nil end ISO_TXID = 0x18DA10F1 ISO_RXID = 0x18DAF110 ISO_EXT = 1 function processISO(chan) local data = { 0x22, 0x26, 0x10 } local result = txCAN(chan, ISO_TXID, ISO_EXT, data) if result == 1 then repeat local id, ext, data = rxCAN(chan) if id == ISO_RXID then for i, func in ipairs(ISOCAN_map) do func(data) end end until id == nil end end CAN_baud = 500000 initCAN(CAN_chan, CAN_baud) setTickRate(tickRate)