Goliath Fall 2016
Creating a Running Averaging Filter
By: Sou Thao (Electronics and Control)
Approved by Kristen Oduca (Project Manager)
Table of Contents
Introduction
Requirement: Goliath shall follow the Biped at a fixed distance of 20(up for debate with testing) inches with a margin of error of 15%.
From the rapid prototype, it was noted that the data from the ultrasonic sensors jumped from small values such as 50cm to larger values around 100cm. By having a jump in these values, the motors’ speed changed and it caused the Goliath to have a jerking motion. In order to have a smoother signal for more accurate measurements and movements, a running averaging filter was created.
Theory
The idea of a running averaging filter consists of having an array of values, dependent on how big the user wants the filter to be. This array of sensor values will add the next data to its group and take out the latest data. Then it will average out the values in the array and produce an average number that is confined within the typical range of the datas. Thus, there will be less jump in the sensor values as the signal is smoothed. It should be noted that there is a pro and con to creating an averaging filter. First of all, by creating a larger array of data, the signal will be smooth to the point where a user is able to obtain linearity between data values. However, by creating this large array, the sensor is less prone to tracking an object that is moving at a fast rate accurately. Therefore, we created a 6 point running averaging filter.
Steps to Writing the Algorithm
- Create an array to store the sensor’s data values and initialize all values to be 0.
- Create an index to increment the pointer of the next data to be added and taken out.
- Create a variable to hold the total value of the numbers in the array.
- Create a variable to hold the average value from the array.
- Take out the first data in the array from the total or where the index is pointing in the array.
- Add the new value from the sensor data in the place of the previous value taken out and add it to the total.
- Average out the values from the array by dividing the total by 6 or whatever the user selects for their averaging filter size.
- Increment the index to select the next data in the array to be taken out.
- If the index is larger than the averaging filter size, restart it at 0.
- Repeat steps 5 through 9.
The Arduino C Code
const int leftNumReadings = 6; //initializes the size of the moving averager
int leftReadings[leftNumReadings] = {0}; //initializes array of 6 data points to all be 0
//initializes all data to create the moving averager to be 0
int readLeftIndex = 0;
int leftTotal = 0;
int leftSensorAverage = 0;
void loop() {
//Take out the oldest data
leftTotal = leftTotal-leftReadings[readLeftIndex];
//store the sensor’s data into the array
leftReadings[readLeftIndex] = Wire.read();
//Add the new distance data to the sum of the 6 point averager
leftTotal = leftTotal-leftReadings[readLeftIndex];
//Find the new average for the running 10 point averager
leftSensorAverage = leftTotal/leftNumReadings;
//advance to the next position in the array
readLeftIndex = readLeftIndex+1;
//if we’re at the end of the array start over at the beginning again
if (readLeftIndex >= leftNumReadings) {
readLeftIndex = 0;
}
Conclusion
By creating this running averaging filter, we were able to obtain a smoother signal from the data and translate those data into speed values for our motors. From the experiment conducted using the running averaging filter, we noticed there was less of a jerky motion from the movements of Goliath. This proved that the filter was very useful in our application for tracking an object.