Skip to content

Instantly share code, notes, and snippets.

@brentpicasso
Last active July 14, 2016 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentpicasso/d9fbf7dd44e010651bccbad910d290a4 to your computer and use it in GitHub Desktop.
Save brentpicasso/d9fbf7dd44e010651bccbad910d290a4 to your computer and use it in GitHub Desktop.
Trailing average for an sensor channel
fuel2Id = addChannel("Fuel2", 25, 2, 0,10,"%")
--change this to make a bigger averaging window
--300 = 10 seconds averaging at 30Hz tick rate
maxAvg = 300
fuelAvg={}
fuel2Index = 1
function updateFuel2(value)
local i
if #fuelAvg == 0 then
--initialize averaging table
for i = 1, maxAvg do fuelAvg[i]=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[i]
end
setChannel(fuel2Id, sum / maxAvg)
end
setTickRate(30)
function onTick()
--replace getImu(2) with getAnalog(0)
updateFuel2(getImu(2))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment