Saturday, June 4, 2011

Averaging

I haven't posted in a little while, but I have done some robot work. The main thing I did was to create some code to do averaging of sensor readings. That forced me to learn how to use arrays (groupings of variables), and it also made me realize that I need to think a bit more about how local or global my variables are.

On the Arduino (the brand of micro-controller I am using) website, there is actually an example program that does what they call "smoothing". It basically calculates the running average every time a new reading is put in the array. It does so by keeping a running sum of all the sensor values in the array and divides by the number of values in the array. I don't think that is ideal for our application. I'd much prefer to just periodically poll the sensors and store the data in arrays and then calculate the average only when I'm interested in the value. It seems like that would save calculation time.

I set it up to average the compass heading over a certain number of readings. I arranged it so that the number of readings could be changed just by changing one number at the top of the program. I played around with it a little bit, and I decided that rather than the readings being random, they seem to kind of drift around. The averaging happens so fast that unless I set it to average several thousand readings over a few seconds, it still moves around a bit (+/- 3 degrees or so). I think its just taking readings way faster than the readings are drifting, so its not averaging terribly well. As the programming gets more complex, there will be quite a bit more time between readings, so I think that will improve the averaging somewhat. There will have to be some testing/playing around to determine the optimal delay between readings and the total number of readings to average.

No comments:

Post a Comment