Mapping Virtual AnalogX channels

Q&A for AnalogX Analog to CAN interface. <a href="https://wiki.autosportlabs.com/AnalogX">Installation and Operation Guide</a>
Post Reply
PS14
Posts: 97
Joined: Fri Feb 12, 2016 11:27 pm
Location: NY

Mapping Virtual AnalogX channels

Post by PS14 »

Trying to create a script that will take the front and rear brake pressures from AnalogX and create a virtual channel that shows the percentage of rear pressure vs total (f&r) pressure. what I want to see is percent of rear brake bias. I have the frt brake assigned to analog channel 3 input (named F_Brake), and the rear to analog channel 4 input (named R_Brake). does this script look like it will work?

--Create channel: "BrakeBias"
avgId = addChannel("BRAKEBIAS", 10, 0, 0, 100, "%")

function setChannel()
local a1 = F_Brake(4)
local a2 = R_Brake(6)
setChannel(avgId, (a2 / (a1 + a2))
end

function onTick()
setChannel()
end

jpf11
Posts: 57
Joined: Wed Oct 23, 2013 9:42 pm
Location: United States

Post by jpf11 »

I'm not really familiar yet with how to get values from CAN channels. What are these "F_Brake(4)" and "R_Brake(6)" calls you're making? I would have thought you'd need some sort of getChannel function to pull those values (or did you do that outside the code you've posted here?).

PS14
Posts: 97
Joined: Fri Feb 12, 2016 11:27 pm
Location: NY

Post by PS14 »

The F_Brake and R_Brake are the channel names is assigned in the CAN mapping section, so I thought maybe I could call them. but didn't work. I tried F_Brake(), F_Brake, getAnalogF_Brake(). then I tried calling the CAN inputs directly, but I don't think I have the setChannles formulas correct in the code (see below). I'm sure its doable, I'm just not a script master.

--Create Virtual Channels:
vfbrakeId = addChannel("vfbrake", 10, 0, 0, 1600, "psi")
vrbrakeId = addChannel("vrbrake", 10, 0, 0, 1600, "psi")
bbsId = addChannel("BrakeBias", 10, 0, 0, 100, "%")

function processCan()
id, ext, data = rxCAN(0)
if id == 935444 then
setChannel(vfbrakeId, data[7], 16, 40, -200)
setChannel(vrbrakeId, data[9], 16, 40, -200)
end

function setChannel()
local a1 = vfbrake()
local a2 = vrbrake()
setChannel(bbsId, (a2 / (a1 + a2)))
end

function onTick()
processCan()
setChannel()
end
end

jpf11
Posts: 57
Joined: Wed Oct 23, 2013 9:42 pm
Location: United States

Post by jpf11 »

I think firmware 2.13.0 supports getChannel by name, so if you mapped your channels using the CAN Mapping feature then you should be able to get their current values with a getChannel("NAME"), like getChannel("F_Brake"). You definitely cannot call a channel like a function by its name. So your lines in your first post's code would be:

local a1 = getChannel("F_Brake")
local a2 = getChannel("R_Brake")

PS14
Posts: 97
Joined: Fri Feb 12, 2016 11:27 pm
Location: NY

Post by PS14 »

Thanks, i'll give that a try.

PS14
Posts: 97
Joined: Fri Feb 12, 2016 11:27 pm
Location: NY

Post by PS14 »

Unfortunately, that didn't work. but i appreciate the suggestion.

--Create channel: "BrakeBias"
avgId = addChannel("BRAKEBIAS", 10, 0, 0, 100, "%")

function setChannel()
local a1 = getChannel("F_Brake")
local a2 = getChannel("R_Brake")
setChannel(avgId, (a2 / (a1 + a2))
end

function onTick()
setChannel()
end

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

Post by brentp »

You will need to ensure you're on 2.13.0 so you can use the getChannel() by name function, and you will also need to ensure you do a nil check, as the value may not always be available currently when requested (it's a function of the sample rate).

I would build up your script slowly to verify each step - if you put it all together and it doesn't work, then it's hard to identify where the problem is.

e.g.

function onTick()
local foo = getChannel("FOO")
if x ~= nil then println("got channel FOO " ..foo) end
end

Hope this helps!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

PS14
Posts: 97
Joined: Fri Feb 12, 2016 11:27 pm
Location: NY

Post by PS14 »

I'm on MK2 w/2.13.0. I have the F_Brake CAN channel on the left gauge and R_Brake CAN channel on the right gauge with the BrakeBias virtual channel in the center (on the three gauge dash). both CAN channels have data. I am sampling the AnalogX at 5hz so I should prob change the addChannel to match. so if I'm following, you suggest this?

--Create channel: "BrakeBias"
avgId = addChannel("BRAKEBIAS", 5, 0, 0, 100, "%")

function setChannel()
local a1 = getChannel("F_Brake")
local a2 = getChannel("R_Brake")
if x ~= nil then println("got channel BRAKEBIAS" ..bbs)
setChannel(avgId, (a2 / (a1 + a2))
end

function onTick()
setChannel()
end

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

Post by brentp »

Not quite. x is not defined in your script, nor is bbs.

You want something like this. Note setting the tick rate to 50 at the top, and checking if things are nil.


setTickRate(50)

--Create channel: "BrakeBias"
avgId = addChannel("BRAKEBIAS", 5, 0, 0, 100, "%")

function setChannel()
local a1 = getChannel("F_Brake")
local a2 = getChannel("R_Brake")
if a1 ~= nil then println("got channel a1" ..a1) end
if a2 ~= nil then println("got channel a2" ..a2) end

if a1 ~= nil and a2 ~= nil then setChannel(avgId, (a2 / (a1 + a2)) end
end


function onTick()
setChannel()
end

PS14
Posts: 97
Joined: Fri Feb 12, 2016 11:27 pm
Location: NY

Post by PS14 »

for some reason it fails with the setTickRate(50) at the beginning. if i remove it or place it lower script runs, here's what i get: (the memory error is intermittent). and i just realized the error is "BrakeBias" but the channel name is "BRAKEBIAS", maybe i need to change that comment line.

got channel a111.600006
got channel a27.600006
got channel a111.600006
got channel a27.600006
got channel a111.600006
got channel a27.600006
[lua] Script error: not enough memory
[lua] Script error: [string "--Create channel: "BrakeBias"..."]:5.0: Channel not found!
[lua] Script error: [string "--Create channel: "BrakeBias"..."]:5.0: Channel not found!
[lua] Failure: Runtime Error

MK2 2.13.0/1.12.1

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

Post by brentp »

Correct, the name is case sensitive. Looks like you're getting closer!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Post Reply