Re-scale/ID CAN data to drive gauge cluster

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
SportRotary
Posts: 4
Joined: Mon May 06, 2019 3:48 pm

Re-scale/ID CAN data to drive gauge cluster

Post by SportRotary »

I have an RX-8 with a 13b-REW engine swap (FD engine) and Megasquirt ECU. I'm trying to use my RaceCapture/Track MK2 to drive the OEM RX-8 gauge cluster which uses CAN data for RPM, speed, coolant temp, and oil pressure.

I need the Lua script to:
1. Receive CAN data from the Megasquirt and store as local variables (RPM, Speed, coolant temp, oil pressure)
2. Re-scale the data to what the OEM cluster is expecting and perform bit-shift operations
3. Transmit the data via CAN to the proper CAN IDs that the cluster expects

I have #2 and #3 working perfectly. I can fake an engine RPM (define "RPM=3000" in the script) and my script is able to re-scale the data properly and transmit it to the cluster, and the tach works. Same with the other variables/gauges. However, I'm still struggling with the best way to execute #1. I originally assumed that I could use "getChannel" to import a CAN variable that has already been defined in the "CAN mapping" tab - but that is not working at all (now I'm thinking it only works for channels defined in the Lua script using "addChannel"). My next attempt will be to use the "CAN bus integration" example script for the E46 to import these CAN variables. My concern is that I will be defining some CAN channels twice (once in "CAN mapping", and once in Lua) and that may cause some issues.

To summarize my questions, I'm stuck on how to receive CAN data and store as a local variable in Lua:
- Is there an easy way to grab channels that are already mapped in the CAN mapping tab for use in Lua?
- Is the CAN bus integration script the right way for me to receive CAN data and store it as a local variable in Lua?

I appreciate any help. I'll post an update if I make progress.

SportRotary
Posts: 4
Joined: Mon May 06, 2019 3:48 pm

Post by SportRotary »

For reference, this is the script I wrote to manually control the RX-8 gauge cluster:

Code: Select all

setTickRate&#40;25&#41;

function onTick&#40;&#41;

 RPMreal = 1000 -- rpm input
 RPMscale = &#40;RPMreal&#41;*3.85 -- scale to what RX8 canbus is expecting
 RPML = bit.band&#40;RPMscale, 0xFF&#41; -- mask out high byte
 RPMH = bit.rshift&#40;RPMscale, 8&#41; -- shift high byte to the right

 SPEEDreal = 20 -- speed input
 SPEEDscale = 160.06*&#40;SPEEDreal&#41;+10010 -- scale to what RX8 canbus is expecting
 SPEEDL = bit.band&#40;SPEEDscale, 0xFF&#41; -- mask out high byte
 SPEEDH = bit.rshift&#40;SPEEDscale, 8&#41; -- shift high byte to the right

 data201 = &#123;RPMH,RPML,0,0,SPEEDH,SPEEDL,0,0&#125;
 txCAN&#40;0, 0x201, 0, data201&#41;

 TEMPreal = 190 -- coolant temperature input
 TEMPscale = TEMPreal*0.7 --scale coolant temp

 OILPRESSreal = 25 -- oil pressure input
 if OILPRESSreal>15 then -- if logic to control binary oil temp gauge
 OPbit = 1
 else
 OPbit = 0
 end

 data420 = &#123;TEMPscale,0,0,0,OPbit,0,0,0&#125; -- byte 4 is oil pressure, 5 is CEL &#40;64=on&#41;, 6 is battery charge &#40;64=on&#41;
 txCAN&#40;0, 0x420, 0, data420&#41;

 data200 = &#123;0,0,255,255,0,50,6,129&#125; -- needed for EPS to work
 txCAN&#40;0, 0x200, 0, data200&#41;

 data202 = &#123;137,137,137,25,52,31,200,255&#125; -- needed for EPS to work
 txCAN&#40;0, 0x202, 0, data202&#41;

end
This works perfectly to control the tach, speedo, coolant temp gauge, and oil pressure "gauge." Now I need to make this script read CAN data and pass that data to the RPMreal, SPEEDreal, TEMPreal, and OILPRESSreal variables.

ferg
Posts: 36
Joined: Mon Dec 03, 2018 7:56 pm
Location: Paso Robles, California
Contact:

Post by ferg »

I think you want to use getChannel() as defined here in the API:

https://wiki.autosportlabs.com/RaceCapt ... r_name_.29
David Ferguson
Veracity Racing Data
Autosport Labs Preferred Dealer
https://veracitydata.com

SportRotary
Posts: 4
Joined: Mon May 06, 2019 3:48 pm

Post by SportRotary »

ferg wrote:I think you want to use getChannel() as defined here in the API:

https://wiki.autosportlabs.com/RaceCapt ... r_name_.29
Ah ha! I tried that before but wasn't having any luck. The reason was that my mapped channels were @ 10hz or 1hz, but the lua script was running @ 25hz - so most of the data was nil. I bumped all of the mapped channels that I'm using to 50hz and kept the lua script @ 25hz. Now it seems to work. I'll drive the car sometime this week to test everything, but I'm pretty confident that it's working now. I even added functionality for the battery light in the cluster.

Here's the final(ish) version of the script:

Code: Select all

setTickRate&#40;25&#41;

function onTick&#40;&#41;

 RPMreal = getChannel&#40;"RPM"&#41; -- rpm input
 if RPMreal ~= nil then
 RPMscale = &#40;RPMreal&#41;*3.85 -- scale to what RX8 canbus is expecting
 else RPMscale = 0
 end
 RPML = bit.band&#40;RPMscale, 0xFF&#41; -- mask out high byte
 RPMH = bit.rshift&#40;RPMscale, 8&#41; -- shift high byte to the right

 SPEEDreal = getChannel&#40;"LF_Wheelspd"&#41; -- speed input
 if SPEEDreal ~= nil then
 SPEEDscale = 160.06*&#40;SPEEDreal&#41;+10010 -- scale to what RX8 canbus is expecting
 else SPEEDscale = 0
 end
 SPEEDL = bit.band&#40;SPEEDscale, 0xFF&#41; -- mask out high byte
 SPEEDH = bit.rshift&#40;SPEEDscale, 8&#41; -- shift high byte to the right

 data201 = &#123;RPMH,RPML,0,0,SPEEDH,SPEEDL,0,0&#125;
 txCAN&#40;0, 0x201, 0, data201&#41;

 TEMPreal = getChannel&#40;"CLTemp"&#41; -- coolant temperature input
 if TEMPreal ~= nil then
 TEMPscale = &#40;TEMPreal*1.8+32&#41;*0.7 --scale coolant temp
 else TEMPscale = 0
 end

 OILPRESSreal = getChannel&#40;"OilPress"&#41; -- oil pressure input
 if OILPRESSreal ~= nil and OILPRESSreal>15 then -- if logic to control binary oil temp gauge
 OPbit = 1
 else
 OPbit = 0
 end

 BATTreal = getChannel&#40;"Batt"&#41; -- battery voltage input
 if BATTreal ~= nil and BATTreal > 11 then --if logic to control battery light
 BATTbyte = 0
 else
 BATTbyte = 64
 end

 data420 = &#123;TEMPscale,0,0,0,OPbit,0,BATTbyte,0&#125; -- byte 4 is oil pressure, 5 is CEL &#40;64=on&#41;, 6 is battery charge &#40;64=on&#41;
 txCAN&#40;0, 0x420, 0, data420&#41;

 data200 = &#123;0,0,255,255,0,50,6,129&#125; -- needed for EPS to work
 txCAN&#40;0, 0x200, 0, data200&#41;

 data202 = &#123;137,137,137,25,52,31,200,255&#125; -- needed for EPS to work
 txCAN&#40;0, 0x202, 0, data202&#41;

end

ferg
Posts: 36
Joined: Mon Dec 03, 2018 7:56 pm
Location: Paso Robles, California
Contact:

Post by ferg »

Excellent news!
David Ferguson
Veracity Racing Data
Autosport Labs Preferred Dealer
https://veracitydata.com

SportRotary
Posts: 4
Joined: Mon May 06, 2019 3:48 pm

Post by SportRotary »

Started it up today and confirmed that everything is working properly. This script not only runs the gauge cluster as mentioned; but it also sends the proper CAN data to the electric power steering controller for that to work. Confirmed today that that is working also!

Post Reply