Megasquirt CAN: Difference between revisions

(→‎Megasquirt Wiring: corrected wiring)
(update to filter TPS)
Line 39: Line 39:
--add your virtual channels here
--add your virtual channels here
--addChannel(<name>, <sample rate>, [logging precision], [min], [max], [units label])
--addChannel(<name>, <sample rate>, [logging precision], [min], [max], [units label])
rpmId = addChannel("RPM", 10, 0, 0, 10000)
rpmId = addChannel("RPM", 10, 0, 0, 8000)
tpsId = addChannel("TPS", 10, 0, 0, 100, "%")
tpsId = addChannel("TPS", 10, 0, 0, 100, "%")
mapId = addChannel("MAP", 10, 0, 0, 250, "kPa")
mapId = addChannel("MAP", 10, 0, 0, 105, "kPa")
cltId = addChannel("Coolant", 1, 0, 0, 200, "F")
cltId = addChannel("Coolant", 1, 0, 0, 250, "F")
afrId = addChannel("AFR", 10, 1, 0, 20)
afrId = addChannel("AFR", 10, 1, 0, 20)


Line 48: Line 48:
--format is: [CAN Id] = function(data) map_chan(<channel id>, data, <CAN offset>, <CAN length>, <multiplier>, <adder>)
--format is: [CAN Id] = function(data) map_chan(<channel id>, data, <CAN offset>, <CAN length>, <multiplier>, <adder>)
CAN_map = {
CAN_map = {
[1512] = function(data) map_chan(mapId, data, 0, 2, 0.1, 0) map_chan(rpmId, data, 2, 2, 1, 0) map_chan(cltId, data, 4, 2, 0.1, 0) map_chan(tpsId, data, 6, 2, 0.1, 0) end,
[1512] = function(data) map_chan(mapId, data, 0, 2, 0.1, 0) map_chan(rpmId, data, 2, 2, 1, 0) map_chan(cltId, data, 4, 2, 0.1, 0) map_chan(tpsId, data, 6, 2, 0.1, 0, filter_tps) end,
[1514] = function(data) map_chan(afrId, data, 1, 1, 0.1, 0) end
[1514] = function(data) map_chan(afrId, data, 1, 1, 0.1, 0) end
}
}
Line 54: Line 54:
function onTick()
function onTick()
     processCAN(CAN_chan)
     processCAN(CAN_chan)
end
function filter_tps(value)
  --filter out overflow
  if value > 100 then value = 0 end
  return value
end
end


Line 67: Line 74:
         end
         end
     until id == nil
     until id == nil
end
--Map CAN channel, little endian format
function map_chan_le(cid, data, offset, len, mult, add)
    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
    setChannel(cid, (value * mult) + add)
end
end


--Map CAN channel, big endian format
--Map CAN channel, big endian format
function map_chan_be(cid, data, offset, len, mult, add)
function map_chan(cid, data, offset, len, mult, add, filter)
     offset = offset + 1
     offset = offset + 1
     local value = 0
     local value = 0
Line 92: Line 85:
         len = len - 1
         len = len - 1
     end
     end
     setChannel(cid, (value * mult) + add)
     local cv = value * mult + add
    if filter ~= nil then cv = filter(cv) end
    setChannel(cid, cv)
end
end


map_chan = (be_mode == 1) and map_chan_be or map_chan_le
initCAN(CAN_chan, CAN_baud)
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)
setTickRate(tickRate)

Revision as of 01:01, 17 July 2016

Megasquirt Dashboard CAN broadcast

This CAN script enables integration with the simplified dashboard mode CAN stream. Refer to the Megasquirt documentation for enabling Dashboard mode CAN broadcast.

Channels enabled:

  • Engine RPM (RPM)
  • Throttle Position (TPS)
  • Coolant Temperature (Coolant)
  • Manifold Absolute Pressure (MAP)
  • Air/Fuel Ratio (AFR)

Additional channels can be enabled by expanding the script.

CAN Database

CAN Bus database for Megasquirt

Megasquirt Wiring

MS3-Pro MS3-Pro wiring

CAN bus connections available on MS3-Pro white connector:

  • CAN High: Pin 34
  • CAN Low : Pin 33

Photos

Megasquirt CAN RCP.jpg

--This example configured for Megasquirt Dashboard Mode

--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
--1 for Big Endian (MSB) mode; 0 for Little Endian mode (LSB)
be_mode = 1

--add your virtual channels here
--addChannel(<name>, <sample rate>, [logging precision], [min], [max], [units label])
rpmId = addChannel("RPM", 10, 0, 0, 8000)
tpsId = addChannel("TPS", 10, 0, 0, 100, "%")
mapId = addChannel("MAP", 10, 0, 0, 105, "kPa")
cltId = addChannel("Coolant", 1, 0, 0, 250, "F")
afrId = addChannel("AFR", 10, 1, 0, 20)

--customize here for CAN channel mapping
--format is: [CAN Id] = function(data) map_chan(<channel id>, data, <CAN offset>, <CAN length>, <multiplier>, <adder>)
CAN_map = {
[1512] = function(data) map_chan(mapId, data, 0, 2, 0.1, 0) map_chan(rpmId, data, 2, 2, 1, 0) map_chan(cltId, data, 4, 2, 0.1, 0) map_chan(tpsId, data, 6, 2, 0.1, 0, filter_tps) end,
[1514] = function(data) map_chan(afrId, data, 1, 1, 0.1, 0) end
}

function onTick()
    processCAN(CAN_chan)
end

function filter_tps(value)
  --filter out overflow
  if value > 100 then value = 0 end
  return value

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, big endian format
function map_chan(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

initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)

References