Monthly Archives: October 2013

Class ready!

After some complementary activities (build a new table for next experiments, for example) I finalized the test on the IMU and created this sensor object  that can run in a parallel thread , so I can get last updated sensor information anytime (every 5 -8 ms) while my main quadcopter loop is running.
You can find and download the sensor_test  example.When you run it you can see on the terminal the current roll,pitch and yaw of your IMU.

The available data are: angular position [deg],angular rate[deg/sec], linear acceleration[m/sec]

The main differences respect the IMU_test are:

  • I saw that the  mpu6050 initialization of same parameter can fail.The order on how you set the IMU parameter can affect the setting itself. So I added a function to verify the parameters are in fact what I decide.
  • The accelerometer vector has been normalized, so the lenght ,when not moved is equal to the gravity. this can help if I want to integrate it to extimate the linear speed and the linear movement
  • the class  can run in a parallel thread, so anytime I can have the current sensor values.
  • minor adjustments done on the naming of the variables.

Next steps are:

  • Buy some connectors and do some soldering to have a final version of the wiring
  • mount the sensor on the frame and test it with running motors and see if i need more severe filtering.

How to combine data from Gyro and Accel

Ok, so after some experiments on different approach in filtering the data,I’m now interested in how to mix data from GYRO and ACC in order to get optimized results.

As always, also this time I found out a very interesting article that describe exactly this type of approach: kalman filter vs complementary filter.

Here it is possible to find how to implement 2 type of complementary filters, and the kalman filter to solve our problem.If you are interested in the kalman filter I strongly recommand to have a look on it since it is really simple to be implemented.I have to say that is is written for Arduino, not in python, but I don’t think this can allarm us.

Personally I just implemented the first order complementary filter and it seems to solve both the noise and drift problem.


In the picture you can see the comparison between the returned angle from gyro, from acc,  from the complementary filter and also from the complementary filter  using the acc data filtered with kalman filter.

As you can see the gyro drift that is really evident in the right (the blue line) is completelty deleted .


Here it is the pic zoom of the left side, where the registered acc noise is clear (red line).Look as the complementary filter reduces it strongly.

So finally after this test I decide to implement the complementary filter on my sw.

You can  find it here as IMU_Test3.

Note that the complementary filter is included directly in the code in the getAngleCompl() .

Now I can say that the development related to the IMU can be frozen at this stage. Now I want to move this into a python object that runs in a parallel thread so  I can monitor the IMU data anytime. After that It’s time to investigate in the PID.

How to filter data from IMU

In the previous post you can find how to read data from the sensor.

One note: the data used in this phase are taken from a IMU not yet mounted on the Quadcopter, to avoid mixing up too many factors. So I’ll test the data from IMU in case  4 running  motors in a second phase.

As described the accelerometer results are affected by noise.
In order to reduce this noise it is necessary to manage to have a filter that returns a smoother values.
In the IMU I’m using it is embedded a low pass filter that can work in a range from 260Hz to 5Hz.
To have a more generic software I developed a low pass filter anyway.

I got some preliminary introduction to this on wikipedia.Here the link
The parameter RC has to be varied empirically  in order to get a smoother results.


Another interesting approach that it is  used often is the Kalman filter, especially when there are different parameters to be filtered.

For this particular case, I  found a very simple and clear blog that describe how to develop Kalman Filter :  Kalman filter for undergrads

It describes the case for Single-Variable example.Our case in facts it is a single variable example, since the value ACC x,ACC y and ACC z have no relation.

In my code I just modified the single case, for “3 single-variables”.

In the following picture you can see the kalman filter results. The interesting comment is that by playing with the parameters in Q , the results obtained by Kalman Filter is Equal to the result from low pass filter.


I’ll keep this development any way in the code, since i don’t know if it is necessary a more severe filter when the IMU will be mounted on the flying quadcopter.

Here you can find the code called IMU_test2  including the low pass filter and the kalman filter.

Note it could be necessary to install the python library numpy on raspberry pi.

apt-get install python-numpy

apt-get install python-numpy-doc

So using the low pass filter (hw from IMU and/or sw from this code) I can reduce the noise of the ACC. In the next post I’ll try to explain how to reduce the GYRO drift problem by  a “fusion” of the results  from ACC and GYRO.

In parallel I’m thinking about improvements in kalman filter to include on it also the math model that links ACC and GYRO data. I’m looking into this Kalman filter for dummies