Last active
July 14, 2016 20:24
-
-
Save brentpicasso/d9fbf7dd44e010651bccbad910d290a4 to your computer and use it in GitHub Desktop.
Trailing average for an sensor channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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