Page 1 of 1

Receiving from Mixed and multiple Canbus systems

Posted: Tue Dec 15, 2015 8:45 pm
by LPilch
Good Evening
I've done a lot of digging thru the forum and reading the information available on the scripting section, so I'm sorry if this has been asked before...
I have three CAN devices that I'm trying to receive information from but can't work out how the scripting is defined for using both canbus's and with different message formats. On one canbus I have the ECU information using 1Mbit/s extended MSB message format, on the second I have one device that measures thermocouples and sends the temperature over the canbus using 500kbit/s LSB message format and the final device measures lambda and sends that information over the canbus using 500kbit/s extended MSB message format.

Any help will be greatly appreciated

Kind regards
Louis

Posted: Tue Dec 22, 2015 5:20 pm
by LPilch
Ive been working on getting the first CAN system integrated but keep getting a nil value error when trying to read the data, the first system is just thermocouples 8 of them, this uses standard CAN messages which are LSB. I based my scripting on the Link G4 CAN Script

the error is...

Code: Select all

lua: Script error: [string "--Configured for IMC Thermocouple Unit..."]:63.0: attempt to perform arithmetic on field '?' (a nil value)
It look like its line 63 but i cant see anything wrong with it?

Code: Select all

value = value + (data[offset] * shift)
My full code is...

Code: Select all

--Configured for IMC Thermocouple Unit
--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 = 0

--add your virtual channels here
--params&#58; <channel name>,<sample rate>, <logging precision>, <min value>, <max value>, <units label>
t1Id = addChannel&#40;"t1Exh1", 10, 1, -271, 1371, "degC"&#41;
t2Id = addChannel&#40;"t2Exh2", 10, 1, -271, 1371, "degC"&#41;
t3Id = addChannel&#40;"t3Exh3", 10, 1, -271, 1371, "degC"&#41;
t4Id = addChannel&#40;"t4Exh4", 10, 1, -271, 1371, "degC"&#41;
t5Id = addChannel&#40;"t5Exh5", 10, 1, -271, 1371, "degC"&#41;
t6Id = addChannel&#40;"t6Exh6", 10, 1, -271, 1371, "degC"&#41;
t7Id = addChannel&#40;"t7Fuel", 10, 1, -271, 1371, "degC"&#41;
t8Id = addChannel&#40;"t8Air", 10, 1, -271, 1371, "degC"&#41;

----------------------------------------
--customize here for CAN channel mapping
--format is&#58; 
--&#91;CAN Id&#93; = function&#40;data&#41; map_chan&#40;<chan_id>, data, <CAN offset>, <CAN length>, <multiplier>,
--                                   <adder>, &#91;filter&#93;&#41;
----------------------------------------

CAN_map = &#123;
&#91;102&#93; = function&#40;data&#41; map_chan&#40;t1Id, data, 0, 16, 0.0251, 550&#41; 
                       map_chan&#40;t2Id, data, 16, 16, 0.0251, 550&#41; 
                       map_chan&#40;t3Id, data, 32, 16, 0.0251, 550&#41; 
                       map_chan&#40;t4Id, data, 48, 16, 0.0251, 550&#41; end,
&#91;103&#93; = function&#40;data&#41; map_chan&#40;t5Id, data, 0, 16, 0.0251, 550&#41; 
                       map_chan&#40;t6Id, data, 16, 16, 0.0251, 550&#41; 
                       map_chan&#40;t7Id, data, 32, 16, 0.0251, 550&#41; 
                       map_chan&#40;t8Id, data, 48, 16, 0.0251, 550&#41; end
&#125;

function onTick&#40;&#41;
    processCAN&#40;CAN_chan&#41;
end

--===========do not edit below===========
function processCAN&#40;chan&#41;
    repeat
        local id, e, data = rxCAN&#40;chan&#41;
        if id ~= nil then
            local map = CAN_map&#91;id&#93;
            if map ~= nil then
                map&#40;data&#41;         
            end
        end
    until id == nil
end

--Map CAN channel, little endian format
function map_chan_le&#40;cid, data, offset, len, mult, add, filter&#41;
    offset = offset + 1
    local value = 0
    local shift = 1
    while len > 0 do
        value = value + &#40;data&#91;offset&#93; * shift&#41;
        shift = shift * 256
        offset = offset + 1
        len = len - 1
    end
 local cv = value * mult + add
 if filter ~= nil then cv = filter&#40;cv&#41; end
    setChannel&#40;cid, cv&#41;
end

--Map CAN channel, big endian format
function map_chan_be&#40;cid, data, offset, len, mult, add, filter&#41;
    offset = offset + 1
    local value = 0
    while len > 0 do
        value = &#40;value * 256&#41; + data&#91;offset&#93;
        offset = offset + 1
        len = len - 1
    end
 local cv = value * mult + add
 if filter ~= nil then cv = filter&#40;cv&#41; end
    setChannel&#40;cid, cv&#41;
end

map_chan = &#40;be_mode == 1&#41; and map_chan_be or map_chan_le
initCAN&#40;CAN_chan, CAN_baud&#41;
setTickRate&#40;tickRate&#41;

Posted: Wed Jan 06, 2016 9:21 pm
by LPilch
Any Ideas what i could be doing wrong? Have checked that the messages are being received by my RCP using the simple CANbus logger and this reads every message.

Kind regards
Louis

Posted: Sun Jan 17, 2016 12:39 pm
by imstimpy
When I got the arithmetic error just recently, it was due to an undefined variable. I had altered the names of the input parameters into the function and neglected to rename one of them. The line number output was incorrect for me- it was off by one or two lines.