Calculating a moving average
bruceh
Posts: 5
I am using the BS2 to drive a robotic servo using a potentiometer as an input (pretty much using techniques documented by Parallax). Everything works fine except that kids are the source of rotation for the potentiometer. Their imput is jerky, causing the servo to be subjected to rapid acceleration and quick reversals. This is terrorizing the gear train connected to the servo and is causing the spline on the·servo shaft to loosen.
My fix for this would be to calculate a moving average of the last x pot readings and use·this as the input to the servo pulse generation routine. This would create a damping or·shock absorber effect.·(The application doesn't require absolute accurate tracking.)
It seems to me that an array of the previous x readings would be a starting point for the summation, however, I don't see·a way to shift the readings placing the newest reading in the array and sending the oldest reading to the bit bucket each time through the loop.
Anyone solved this problem?
My fix for this would be to calculate a moving average of the last x pot readings and use·this as the input to the servo pulse generation routine. This would create a damping or·shock absorber effect.·(The application doesn't require absolute accurate tracking.)
It seems to me that an array of the previous x readings would be a starting point for the summation, however, I don't see·a way to shift the readings placing the newest reading in the array and sending the oldest reading to the bit bucket each time through the loop.
Anyone solved this problem?
Comments
This should give you adequate smoothing. If not, multiply by 15 and shift by 4 instead. In that case NewReading would have to be less than 4096.
-Phil
--Running the accumulator at 8 times the data input allows it to track small changes smoothly.
--Most filters or averaging schemes do require initialization in order to fill up the pipeline and produce "good" values right away.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support