Smoothing AnalogX signals?

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

Smoothing AnalogX signals?

Post by PS14 »

I have a pair of 0-5v brake press sensors and a 0-5v fuel level sensor connected to an AnalogX. plumbed into the RJ45 of my MK2. works fine but the dash gauges are "jumpy". for example the fuel level gauge will jump back and forth between 5-8 gallons (with car siting still in shop, no slosh involved). brake gauges do the same, kind of flutter around zero/neg zero. is this due to the 0-5v input fluctuations? is there a way to smooth it. thanks, Dave

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

Post by brentp »

Yes. There's no smoothing option for direct CAN inputs, but you could do it in Lua with a virtual channel, by first reading the channel data, applying a smoothing / averaging algortihm and then setting that to a virtual channel.

2.13.x firmware will be needed for reading the channel by name. Documentation here:
https://wiki.autosportlabs.com/RaceCapt ... r_name_.29
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 »

ok, so to account for "voltage slosh" I could use the lua example, tweeked below? i'm just not sure how to add the nil check.

--"Fuel" is the CAN channel connected to level sensor
--"vFuel" is the smoothed virtual channel viewed in dashboard

--Add Virtual Channels
v1 = addChannel("vFuel", 5, 0, 0, 22, "gall")

--change this to make a bigger averaging window
maxAvg = 600
--600 = 20 seconds averaging at 30Hz tick rate

--do not change
fuelAvg={}
fuel2Index = 1

function updateFuelAvg(value)
local i
if #fuelAvg == 0 then
--initialize averaging table
for i = 1, maxAvg do fuelAvg=0 end
end
fuelAvg[fuel2Index] = value
fuel2Index = fuel2Index + 1
if fuel2Index > maxAvg then fuel2Index = 1 end
local sum = 0
for i = 1, #fuelAvg do
sum = sum + fuelAvg
end
setChannel(v1, sum / maxAvg)
end


setTickRate(30)
function onTick()
updateFuelAvg(getChannel(Fuel))
end

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

Post by brentp »

Not able to tests on a RaceCapture right now, but I fixed a few things:

* Quotes around getting the Fuel channel, since it's specified a a string
* Guarding against nil values for getChannel


When you run it, enable the poll log feature so you can check for any script errors.

Code: Select all


-"Fuel" is the CAN channel connected to level sensor
--"vFuel" is the smoothed virtual channel viewed in dashboard

--Add Virtual Channels
v1 = addChannel&#40;"vFuel", 5, 0, 0, 22, "Gal"&#41;

--change this to make a bigger averaging window
maxAvg = 600
--600 = 20 seconds averaging at 30Hz tick rate

--do not change
fuelAvg=&#123;&#125;
fuel2Index = 1

function updateFuelAvg&#40;value&#41;
if value == nil then return end

local i
if #fuelAvg == 0 then
--initialize averaging table
for i = 1, maxAvg do fuelAvg&#91;i&#93;=0 end
end
fuelAvg&#91;fuel2Index&#93; = value
fuel2Index = fuel2Index + 1
if fuel2Index > maxAvg then fuel2Index = 1 end
local sum = 0
for i = 1, #fuelAvg do
sum = sum + fuelAvg&#91;i&#93;
end
setChannel&#40;v1, sum / maxAvg&#41;
end


setTickRate&#40;30&#41;
function onTick&#40;&#41;
updateFuelAvg&#40;getChannel&#40;"Fuel"&#41;&#41;
end
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 »

that worked well. next question. i want to add the two brake press signals that also come via AnalogX. can these be combined into the one "smoothing" script?

v1 = addChannel("vFuel", 5, 0, 0, 22, "gall")
v2 = addChannel("vFbrake", 5, 0, 0, 1600, "psi")
v3 = addChannel("vRbrake", 5, 0, 0, 1600, "psi")

--can we set multiple channels?
end
setChannel(v1, sum / maxAvg)
setChannel(v2, sum / maxAvg)
setChannel(v3, sum / maxAvg)
end

--then update each?
setTickRate(30)
function onTick()
updateFuelAvg(getChannel("Fuel"))
updateFuelAvg(getChannel("F_brake"))
updateFuelAvg(getChannel("R_brake"))
end

Post Reply