Monday, April 21, 2014

Accelerometer Data Filtering Using Low Pass filter


      While you are working with accelerometers you may have noticed that the data are too noisy. To use those values to your application, definitely you will have to filter out them. So what is a filter, you may heard of a water filter,which is used to remove germs and unwanted materials in water. Using a filter, we will be able to get more precise data from accelerometer. There are many filters which can do data filtering. In this post I'm going to show you how to filter out accelerometer data using a simple low pass filter.

Following figure (fig.1) shows you the data from  MPU6050 accelerometer without(blue line) and with(white line) filtering. The accelerometer was mounted on the middle of a quadcopter. So when the motors are rotating accelerometer data got more and more noisy(fig.2). I felt how could I deal with these kind of very very noisy data at the first time I saw accelerometer readings come out from the accelerometer.


                                                                             fig.1
                                                                   

                                                                            fig.2


As you can see from above images, blue line is very noisy while white line is low nisy because it has been filter out by a low pass filter.

I used STM32F103RBT6 microcontroller to configure and read data through I2C interface. you can connect two MPU6050 modules to same I2C line. I hope to add a new post on how to configure and read data from MPU6050 in near future.

Some accelerometr modules have its inbuilt low pass filters, They can be configured to out put only the filter out data. MPU6050 is an example for that. If you are combining accelerometer and gyro to get precise angle, you have to use kalman filter or Complimentary filter. But in MPU6050 there is an inbuilt Digital Motion Processor capable of processing 9 axis motion fusion algorithms.

But in this post I use a low pass filter to filter out accelerometer data from accelerometer.

Below is the code for a digital low pass filter.

void accelerometer_Smooth (void)
{
    
    static int element=0;
    static int n,k = 0;
    static double x_Low,y_Low,z_Low,x0,y0,z0,z02 = 0;
    
    /* Low pass filter */
    
  const float dt = (1.0 / 50.0);
  const float RC = 0.35;
  const float alpha = dt / (RC + dt);

    
   
  x_Low = ((alpha * axData[0]) + (1.0 - alpha) * x0);
  y_Low = ((alpha * axData[1]) + (1.0 - alpha) * y0);
  z_Low = ((alpha * axData[2]) + (1.0 - alpha) * z0);  
  x0 = x_Low;
  y0 = y_Low;
  z0 = z_Low;

}

You can change the values of dt and RC according to the rise time you want.

axData[0],axData[1],axData[2] are global variables which contains the new accelerometer data without filtering.

axData[0] = x axis acceleration come out from accelerometer without filtering.
axData[1] = y axis acceleration come out from accelerometer without filtering.
axData[2] = z axis acceleration come out from accelerometer without filtering.

By using a median filter and a kalman predictor we can obtain the same results as low pass filter. I will post about them in near future.

Serial chart was really helpful to examine data coming out from accelerometer. You can download the software from here.



1 comment:

  1. Thanks for this post. I believe x0, y0, z0 are datapoints immediately before axData arrives. I see that you are using RC as 0.35, is there a particular reason you chose this value? If you could elaborate on how one would choose this. I am working on something similar, and thought you could help me understand this RC part.

    ReplyDelete