Skip to content

Instantly share code, notes, and snippets.

@brentpicasso
Last active January 26, 2016 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentpicasso/4843eae77b182e17ee53 to your computer and use it in GitHub Desktop.
Save brentpicasso/4843eae77b182e17ee53 to your computer and use it in GitHub Desktop.
Adding raw low / high byte debugging for wheel speeds
--This example configured for E46 CAN
-- Automatically starts logging with engine 'on' (RPM triggered)
--how frequently we poll for CAN messages
tickRate = 30
--the CAN baud rate
CAN_baud = 500000
--CAN channel to listen on. 0=first CAN channel, 1=second
CAN_chan = 0
--add your virtual channels here
--format addChannel(<name>, <sample rate>, <precision>, <min>, <max>, [units])
id339 = addChannel('brakesw_339', 10, 0, 0, 255)
id809 = addChannel('clutch_809', 10, 0, 0, 255)
function debug_339(data)
setChannel(id339, data[1])
end
function debug_809(data)
setChannel(id809, data[4])
end
----------------------------------------
--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 = {
[339] = function(data) debug_339(data) end,
[809] = function(data) debug_809(data) end,
}
function onTick()
processCAN(CAN_chan)
end
--===========do not edit below===========
function processCAN(chan)
local msg = 0
repeat
local id, e, data = rxCAN(chan)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
msg = msg + 1
until id == nil or msg > 100
end
--Map CAN channel, little endian format
function map_chan(cid, data, offset, len, mult, add, filter)
if offset + len > #data then return end
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
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment