Need Help - Using Channels from AIM SmartyCam CAN
Posted: Tue Jun 11, 2019 4:29 am
Hello everyone,
New guy to RaceCapture and writing in Lua Script. My ChampCar team and I recently bought the Podium Connect with one of our goals to be able to calculate fuel consumption for our next race in July. After many hours I'm slowly figuring out the nuances of how to code in Lua, but I still got a lot to learn.
We have an AIM MXL2 Dash connected to the SmartyCam CAN using the AIM-RCP cable. I have been fairly successful in mapping all the channels using the presets in the CAN Mapping section with all channels I have set up working except acceleration. I'll make a separate thread about that.
The trouble I've been running into of late is actually using the channels from the SmartyCam CAN in any kind of Lua Script. I've tried directly pulling the CAN channels into my script by using the getChannel function but I couldn't get that to work. Eventually, I was able to pull the CAN channels into Lua Script by copying the code from the online example for the "old approach" https://wiki.autosportlabs.com/AIM_SmartyCam_CAN. This appeared to have worked, as an example I would see two channels with the same values for fuel pressure in the dashboard. My problem is I can't get any of my codes to work within the script using these new channels.
This is the strangest thing to me. When I wrote the code to do the fuel calculations, for testing purposes, I would set the channels I wanted to use to a fix value using the setChannel function. From that I was able to get a fully functional code and I could watch it add up the fuel on the dashboard at a rate I would expect for the values I substituted in. Great! All I need to do is use the new channels, turn on the car, and compare expected values from actual values. Except that's not what happened. As soon as I turned the car on it wouldn't work, I got nothing it didn't add anything, I could see the fuel pressure channel that I created was working but none of my other code worked. I had created a virtual channel for fuel remaining in tank which should automatically start at it's starting value and not even that would populate.
A few more hours of troubleshooting and I've stripped the code down to the most simplistic expression I could think of, take my channel and add it to itself. I couldn't get that to work either. What was happening was as soon as I flipped the power switch to the car it would stop adding but when I turned it off it would start adding again, turn the power on and it would stop adding. That blows my mind and I can't wrap my head around what's happening.
This is the stripped down code I've been trouble shooting with. I'm not sure why I can't have my own code in this script? Or if this is not the way to get the channels from the SmartyCam CAN I am all ears because I'm pulling my hair out over this. Hopefully someone can shed some light on this.
tickRate = 10
--the CAN baud rate
CAN_baud = 1000000
--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 virtual channels here
fpressID = addChannel ("Fuel_Press", 10, 2, 0, 100, "PSI")
sumpressID = addChannel ("Sum_Press", 10, 0, 0, 1000000, "PSI")
CAN_map = {
[1058] = function(data) map_chan(fpressID, data, 0, 2, 0.14503789, 0) end,
}
function doCalc()
if x == nil then x = 1 end
x = x + getChannel(fpressID)
setChannel(sumpressID, x)
println("Sum_Press " ..x)
end
function onTick()
processCAN(CAN_chan) -- get channels from AIM dash
doCalc()
end
--===========do not edit below===========
function processCAN(chan)
repeat
local id, e, data = rxCAN(chan)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
until id == nil
end
--Map CAN channel, little endian format
function map_chan_le(cid, data, offset, len, mult, add)
offset = offset + 1
local value = 0
local shift = 1
while len > 0 do
value = value + (data[offset] * shift)
shift = shift * 256
offset = offset + 1
len = len - 1
end
setChannel(cid, (value * mult) + add)
end
map_chan = (be_mode == 1) and map_chan_be or map_chan_le
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)
New guy to RaceCapture and writing in Lua Script. My ChampCar team and I recently bought the Podium Connect with one of our goals to be able to calculate fuel consumption for our next race in July. After many hours I'm slowly figuring out the nuances of how to code in Lua, but I still got a lot to learn.
We have an AIM MXL2 Dash connected to the SmartyCam CAN using the AIM-RCP cable. I have been fairly successful in mapping all the channels using the presets in the CAN Mapping section with all channels I have set up working except acceleration. I'll make a separate thread about that.
The trouble I've been running into of late is actually using the channels from the SmartyCam CAN in any kind of Lua Script. I've tried directly pulling the CAN channels into my script by using the getChannel function but I couldn't get that to work. Eventually, I was able to pull the CAN channels into Lua Script by copying the code from the online example for the "old approach" https://wiki.autosportlabs.com/AIM_SmartyCam_CAN. This appeared to have worked, as an example I would see two channels with the same values for fuel pressure in the dashboard. My problem is I can't get any of my codes to work within the script using these new channels.
This is the strangest thing to me. When I wrote the code to do the fuel calculations, for testing purposes, I would set the channels I wanted to use to a fix value using the setChannel function. From that I was able to get a fully functional code and I could watch it add up the fuel on the dashboard at a rate I would expect for the values I substituted in. Great! All I need to do is use the new channels, turn on the car, and compare expected values from actual values. Except that's not what happened. As soon as I turned the car on it wouldn't work, I got nothing it didn't add anything, I could see the fuel pressure channel that I created was working but none of my other code worked. I had created a virtual channel for fuel remaining in tank which should automatically start at it's starting value and not even that would populate.
A few more hours of troubleshooting and I've stripped the code down to the most simplistic expression I could think of, take my channel and add it to itself. I couldn't get that to work either. What was happening was as soon as I flipped the power switch to the car it would stop adding but when I turned it off it would start adding again, turn the power on and it would stop adding. That blows my mind and I can't wrap my head around what's happening.
This is the stripped down code I've been trouble shooting with. I'm not sure why I can't have my own code in this script? Or if this is not the way to get the channels from the SmartyCam CAN I am all ears because I'm pulling my hair out over this. Hopefully someone can shed some light on this.
tickRate = 10
--the CAN baud rate
CAN_baud = 1000000
--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 virtual channels here
fpressID = addChannel ("Fuel_Press", 10, 2, 0, 100, "PSI")
sumpressID = addChannel ("Sum_Press", 10, 0, 0, 1000000, "PSI")
CAN_map = {
[1058] = function(data) map_chan(fpressID, data, 0, 2, 0.14503789, 0) end,
}
function doCalc()
if x == nil then x = 1 end
x = x + getChannel(fpressID)
setChannel(sumpressID, x)
println("Sum_Press " ..x)
end
function onTick()
processCAN(CAN_chan) -- get channels from AIM dash
doCalc()
end
--===========do not edit below===========
function processCAN(chan)
repeat
local id, e, data = rxCAN(chan)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
until id == nil
end
--Map CAN channel, little endian format
function map_chan_le(cid, data, offset, len, mult, add)
offset = offset + 1
local value = 0
local shift = 1
while len > 0 do
value = value + (data[offset] * shift)
shift = shift * 256
offset = offset + 1
len = len - 1
end
setChannel(cid, (value * mult) + add)
end
map_chan = (be_mode == 1) and map_chan_be or map_chan_le
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)