RegisterSearchFAQMemberlistUsergroupsLog in
Reply to topic Page 1 of 1
Re-scale/ID CAN data to drive gauge cluster
Author Message
Reply with quote
Post Re-scale/ID CAN data to drive gauge cluster 
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.

View user's profile Send private message
Reply with quote
Post  
For reference, this is the script I wrote to manually control the RX-8 gauge cluster:

Code:

setTickRate(25)

function onTick()

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

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

 data201 = {RPMH,RPML,0,0,SPEEDH,SPEEDL,0,0}
 txCAN(0, 0x201, 0, data201)

 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 = {TEMPscale,0,0,0,OPbit,0,0,0} -- byte 4 is oil pressure, 5 is CEL (64=on), 6 is battery charge (64=on)
 txCAN(0, 0x420, 0, data420)

 data200 = {0,0,255,255,0,50,6,129} -- needed for EPS to work
 txCAN(0, 0x200, 0, data200)

 data202 = {137,137,137,25,52,31,200,255} -- needed for EPS to work
 txCAN(0, 0x202, 0, data202)

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.

View user's profile Send private message
Reply with quote
Post  
I think you want to use getChannel() as defined here in the API:

https://wiki.autosportlabs.com/RaceCapturePro_Lua_Scripting#getChannel.28_Channel_ID_or_name_.29


_________________
David Ferguson
Veracity Racing Data
Autosport Labs Preferred Dealer
https://veracitydata.com
View user's profile Send private message Visit poster's website
Reply with quote
Post  
ferg wrote:
I think you want to use getChannel() as defined here in the API:

https://wiki.autosportlabs.com/RaceCapturePro_Lua_Scripting#getChannel.28_Channel_ID_or_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:

setTickRate(25)

function onTick()

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

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

 data201 = {RPMH,RPML,0,0,SPEEDH,SPEEDL,0,0}
 txCAN(0, 0x201, 0, data201)

 TEMPreal = getChannel("CLTemp") -- coolant temperature input
 if TEMPreal ~= nil then
 TEMPscale = (TEMPreal*1.8+32)*0.7 --scale coolant temp
 else TEMPscale = 0
 end

 OILPRESSreal = getChannel("OilPress") -- 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("Batt") -- battery voltage input
 if BATTreal ~= nil and BATTreal > 11 then --if logic to control battery light
 BATTbyte = 0
 else
 BATTbyte = 64
 end

 data420 = {TEMPscale,0,0,0,OPbit,0,BATTbyte,0} -- byte 4 is oil pressure, 5 is CEL (64=on), 6 is battery charge (64=on)
 txCAN(0, 0x420, 0, data420)

 data200 = {0,0,255,255,0,50,6,129} -- needed for EPS to work
 txCAN(0, 0x200, 0, data200)

 data202 = {137,137,137,25,52,31,200,255} -- needed for EPS to work
 txCAN(0, 0x202, 0, data202)

end


View user's profile Send private message
Reply with quote
Post  
Excellent news!


_________________
David Ferguson
Veracity Racing Data
Autosport Labs Preferred Dealer
https://veracitydata.com
View user's profile Send private message Visit poster's website
Reply with quote
Post  
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!

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum