Receiving from Mixed and multiple Canbus systems

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

Post Reply
LPilch
Posts: 3
Joined: Tue Dec 15, 2015 8:28 pm
Location: UK

Receiving from Mixed and multiple Canbus systems

Post 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

LPilch
Posts: 3
Joined: Tue Dec 15, 2015 8:28 pm
Location: UK

Post 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&#58; Script error&#58; &#91;string "--Configured for IMC Thermocouple Unit..."&#93;&#58;63.0&#58; attempt to perform arithmetic on field '?' &#40;a nil value&#41;
It look like its line 63 but i cant see anything wrong with it?

Code: Select all

value = value + &#40;data&#91;offset&#93; * shift&#41;
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 &#40;MSB&#41; mode; 0 for Little Endian mode &#40;LSB&#41;
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;

LPilch
Posts: 3
Joined: Tue Dec 15, 2015 8:28 pm
Location: UK

Post 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

imstimpy
Posts: 36
Joined: Tue Nov 24, 2015 11:37 am

Post 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.

Post Reply