Guide to gyro and accelerometer with Arduino including Kalman filtering

Hi Lauszus,

Like the many others, I got my IMU working by studying at lot of your hard work, just wanted you to know that I really appreciate it!
I have a question, with the project I'm working on - I need to filter out the accel data that is affecting the
Kalman output such as when the IMU is under accel in any of the 3 axis. For example while placing the IMU
on a rotating platform, the addition of of the X or Y accel vectors start to affect the roll
and pitch angles. What do you recommend to eliminate these bias errors for when the IMU is not stationary and under accel/decel?

-futureM

@futureM
That's the whole purpose of the Kalman filter. If you are not satisfied with the performance as it is right now, then you have to tune the values.
See my blog for more information: TKJ Electronics » A practical approach to Kalman filter and how to implement it

Hi Lauszus,

I'm working with the Quaternion Values getting from the DMP of the MPU6050. i've some problems to get a full 360° Range.
Maybe you have a hint to convert the DMP Quaternion Values to Degrees. Normally the calculation is based on the Sensitivity Scale Factor for both Gyro and Accel. I'm correct?
But i'm getting one fusion Value of the Gyro and Accel. So i don't now how to convert this to a 360° Range ....

Why do you not use the DMP Values like Quaternion?
Is it better to work with raw Vaules and Kalman Filter?

With atan2 i should get the 360°:
mpu.dmpGetQuaternion(&q, fifoBuffer);
Serial.print((atan2(q.y,q.z)+PI)*RAD_TO_DEG);

but if i move the MPU6050 90° i get on the Serial only 70°

many Thanks!

@triplenox
I have never used the DMP on the MPU-6050 before as I thought it were a bit overkill as I usually only need the angle in one of the axis.

You can't use atan2 or anything like that on Quaternions!

To use Quaternions see the following recourses:

hello , i'm working on the same subject , and it's my first time i use the ARDUINO uno AND the IMU 6050s ,
i'm a beginner, now i try to finish the mechanical par ,
it'is possible to give my the full code, because it's a subject that i must give to the school , and the mechanical part it took a lot of time to built,
please i wayit for somewone to help my.
and thank you four your solidarity

@aziz-qessate_arduino
Hi,

I can't write all the code for you, but for now take a look at this example code I wrote: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/MPU6050/MPU6050.ino

So here's the issue I'm running into. I'm using Lauszus' code to try and set up a self stabilizing camera system. Although what I am finding is that the kalAngleX value is greatly effected by any acceleration in the z axis. If I shake the MPU6050 up and down the servo just goes crazy. When I hook it up to the bike the bumpiness of the road basically ruins it. I though that was the whole point of kalman filtering was to minimize this?

You have run into the fundamental problem, that no scheme based on accelerometers can
distinguish between movement acceleration and gravity acceleration.

@ezikiel12
You have to tune the Kalman filter. See my blog post on how to do so: TKJ Electronics » A practical approach to Kalman filter and how to implement it
The problem is that you are trusting the measurements from the accelerometer too much.

Or you could try to tune a complimentary filter to start with.

@michinyon
That's the whole point of the filter :wink:

How if i connect IMU to my Arduino Board using I2C ? How to read and filter it?

See this code I wrote a while back: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/MPU6050/MPU6050.ino for the MPU6050

And here is some code for the ITG3205/ITG3200 gyroscope and ADXL345 accelerometer: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/ITG3205_ADXL345/ITG3205_ADXL345.ino

That's the whole point of the filter

Well no. The whole point of the filter is dealing with noise. The problem of distinguishing true acceleration from the effect of gravity on the sensor remains. I've yet to see a convincing implementation of the Kalman filter approach which considers more than one dimension.

michinyon:

That's the whole point of the filter

Well no. The whole point of the filter is dealing with noise. The problem of distinguishing true acceleration from the effect of gravity on the sensor remains. I've yet to see a convincing implementation of the Kalman filter approach which considers more than one dimension.

You should look at the FreeIMU's implentation of MARG orientation fusion algorithm. I am using a modified version of this and it seems to work well.

@Lauszus
Thanks for your guide.
I'm working with an MPU6050 for my balancing robot project. But the model required yaw measurement. I wonder if the MPU6050 is capable of measure the yaw accurate enough or it needs additional magnetometer. Please help :smiley:

longlongvn:
@Lauszus
Thanks for your guide.
I'm working with an MPU6050 for my balancing robot project. But the model required yaw measurement. I wonder if the MPU6050 is capable of measure the yaw accurate enough or it needs additional magnetometer. Please help :smiley:

It can do short term yaw measurements just fine, but it drifts over time. So if you just need to know incremental yaw measurements during yaw events it will work fine. However if you are using to it maintain a specific orientation for extended periods of time, it will not work well unless you integrate a magnetometer.

thank you very much. I'm gonna try integrating an magnetometer to my IMU.

The MPU-6050 supports auxillary SCL and SDA. My GY-521 breakout board broke the pins out and called them XCL and XDA. If the magnetometer you choose uses I2C, you can plug it directly into the MPU-6050.

I used a modified FreeIMU library to get mine all working.

@Lauszus Sir I have a L3g4200d. It just a 3 axis Gyro sensor. My problem is to how to use kalman filtering to Arduino Uno. And I want to get the reading values to a degree(x,y,z). To able to control my Servo. Thanks in Advance.

thank you for your help ,that's kind of you
the code that you give me, I'll bring in the card and it will arduino controller actuators? is that the code is complete?

@michinyon
If true acceleration is not noise in the system, what is it then?

@longlongvn
As jjspierx pointed out you will need a magnetometer as well if you need a precise yaw measurement.

@wturn
To translate the gyro rate in to an angle simply multiply the reading by the delta time as I described in the blog post, but it will properly drift a lot if you use a gyro only.

@aziz-qessate_arduino
What do you mean? Please describe it in more detail.