LUA / Can Bus help

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
finse
Posts: 19
Joined: Thu Jan 30, 2014 3:06 am
Location: Denver
Contact:

LUA / Can Bus help

Post by finse »

Hi everyone!

Over the last couple of months, I've been working on an interface to obtain data from my ECU to send via CAN to the Race Capture Pro.

Here are the parameters I am currently obtaining:

* RPM
* Vehicle Speed
* Coolant Temp
* Throttle Position
* Clutch Switch
* Brake Switch
* Calculated Gear

My data is structured like this

{ 'RPM 1' , 'RPM 2' , 'Speed' , 'Coolant temp','Throttle Position', 'Clutch Switch', 'Brake Switch', 'Calculated Gear'}
| high byte low byte | byte | byte | byte | byte | byte | byte |
| (RPM1 | RPM2 << 8 )/4.0 | 0 - 255 | 0 - 255 | 0 - 100 | 0/1 | 0/1 | 0-6 |

All of the parameters will be packed into a 8 byte CAN message sent on to the race capture pro:

{'canID'',EXT','RPM1','RPM2','Speed','Coolant temp','Throttle Position','Clutch Switch','Brake Switch','Calculated Gear'}

This leads me to my question. I'm absolutely terrible with Lua, and I have no idea how to interpret my RPM data in the RCP.

What I do know:
* I'm fairly confident the ECU data is little endian
* The RPM value can be calculated in C/Arduino like this: (RPM1 | RPM2 << 8 )/4.0

Below is my best guess on how to map the CAN data out in the RCP. Any help you may have is appreciated.

--format is: [CAN Id] = function(data) map_chan(<channel id>, data, <CAN offset>, <CAN length>, <multiplier>, <adder>)
CAN_map = {``
[1365] = function(data) map_chan_le(rpmID, data, 0, 2 , .25, 0)
map_chan_le(mphID, data, 2, 1 , 0, 0),
map_chan_le(tmpID, data, 3, 1 , 0, 0),
map_chan_le(tpsID, data, 4, 1 , 0, 0),
map_chan_le(cltID, data, 5, 1 , 0, 0),
map_chan_le(brkID, data, 6, 1 , 0, 0),
map_chan_le(gerID, data, 7, 1 , 0, 0) end,
}

Thanks!
Ryan

finse
Posts: 19
Joined: Thu Jan 30, 2014 3:06 am
Location: Denver
Contact:

Re: LUA / Can Bus help

Post by finse »

Replying to myself, in case it helps anyone.

Basing my script on the E46 supported LUA script, I have been able to successfully send data into the RCP and read it on the gauges screen :)

I found that packing all of the data into one can message is problematic, however, things do seem to work fairly well when each parameter is split into it's own specific can packet.

Additionally, I found the map_chan_be does a pretty good job of interpreting my RPM data (turns out my data was big endian, rather the little endian as I had erroneously thought), however, it's not perfect. I'll have to spend a little time on the formula. Once I'm done, I'll post more detail.

Cheers,
Ryan

finse
Posts: 19
Joined: Thu Jan 30, 2014 3:06 am
Location: Denver
Contact:

Re: LUA / Can Bus help

Post by finse »

finse wrote: * The RPM value can be calculated in C/Arduino like this: (RPM1 | RPM2 << 8 )/4.0
Ryan
this is how I pulled it off in LUA

Code: Select all

local byts=&#123;RPM&#91;1&#93;,RPM&#91;2&#93;&#125; 
    for i=1,2 do
      cv=cv+byts&#91;i&#93;*256^&#40;i-1&#41;
    end
    cv=&#40;&#40;cv / 4.0&#41; - 5&#41;

finse wrote: Below is my best guess on how to map the CAN data out in the RCP. Any help you may have is appreciated.
For what it's worth, this is the complete lua script for my CAN implementation:

Code: Select all

tickRate = 30
CAN_baud = 500000
CAN_chan = 0

rpmId = addChannel&#40;"RPM", 10, 0, 0, 8300&#41;

kmhId = addChannel&#40;"Speed&#40;MPH&#41;",10, 0, 0, 170&#41;
tmpId = addChannel&#40;"Coolant",10,0,0,250&#41;
brkId = addChannel&#40;"Brake",10&#41;
cltId = addChannel&#40;"Clutch",10&#41;
gerId = addChannel&#40;"Gear",10&#41;
tpsId = addChannel&#40;"Throttle", 100&#41;

CAN_map = &#123;
&#91;601&#93; = function&#40;data&#41; map_rpm&#40;rpmId, data, 2, 2, .25, 0&#41; end,
&#91;602&#93; = function&#40;data&#41; map_int&#40;kmhId, data, 2, 1, 1, 0&#41; end,
&#91;603&#93; = function&#40;data&#41; map_int&#40;tmpId, data, 2, 1, 1, 0&#41; end,
&#91;604&#93; = function&#40;data&#41; map_int&#40;brkId, data, 2, 1, 1, 0&#41; map_int&#40;cltId, data, 3, 1, 1, 0&#41; end,
&#91;605&#93; = function&#40;data&#41; map_int&#40;gerId, data, 2, 1, 1, 0&#41; end,
&#91;606&#93; = function&#40;data&#41; map_int&#40;tpsId, data, 2, 1, 1, 0&#41; end
&#125;

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

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

function map_rpm&#40;cid, data, offset, len, mult, add, filter&#41;
    local byts=&#123;data&#91;2&#93;, data&#91;3&#93;&#125;
    for i=1,2 do
      cv=cv+byts&#91;i&#93;*256^&#40;i-1&#41;
    end
    cv=&#40;&#40;cv / 4.0&#41; - 5&#41;
    setChannel&#40;cid, cv&#41;
end

function map_int&#40;cid, data, offset, len, mult, add, filter&#41;
 cv = data&#91;offset&#93;
 setChannel&#40;cid, cv&#41;
end

initCAN&#40;CAN_chan, CAN_baud&#41;
setTickRate&#40;tickRate&#41;       
[/code]

brentp
Site Admin
Posts: 6274
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

Thank you for sharing your script! Which ECU is this for? Does the script currently work?
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

finse
Posts: 19
Joined: Thu Jan 30, 2014 3:06 am
Location: Denver
Contact:

Post by finse »

brentp wrote:Thank you for sharing your script! Which ECU is this for? Does the script currently work?
This is actually for my Subaru Select Monitor integration module. The script does work, but the mappings are all made up.

Post Reply