Page 1 of 1

Mapping Virtual AnalogX channels

Posted: Fri Mar 23, 2018 12:38 pm
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

Posted: Mon Apr 02, 2018 10:36 pm
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?).

Posted: Tue Apr 03, 2018 11:48 am
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

Posted: Tue Apr 03, 2018 6:50 pm
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")

Posted: Tue Apr 03, 2018 7:16 pm
by PS14
Thanks, i'll give that a try.

Posted: Tue Apr 03, 2018 10:30 pm
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

Posted: Wed Apr 04, 2018 4:02 pm
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!

Posted: Wed Apr 04, 2018 4:17 pm
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

Posted: Wed Apr 04, 2018 6:11 pm
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

Posted: Wed Apr 04, 2018 11:15 pm
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

Posted: Wed Apr 04, 2018 11:16 pm
by brentp
Correct, the name is case sensitive. Looks like you're getting closer!