# IMU - How to calculate the orientation in a shaken environment

I'm trying to calculate the orientation on an IMU Sensor. Until now I'm doing it by calculating the Pitch angle and it's working fine:

float pitch = atan2(-obj.x, sqrt(obj.y*obj.y + obj.z*obj.z)) * 180/M_PI;.

My problem is when the sensor shakes heavily (even though it states on the same orientation), i.e. the Pitch changes drastically and it loses the correct orientation.

Is there a way to avoid this behaviour. What I thought to do, is to store the old pitch and compare it to the current pitch and if they differ +-20%* to each other then I ignore the orientation value. This obviously won't work if the sensor is shaken continuously.

*20 is just a random number

• low-pass-filter ... Nov 15, 2016 at 22:06

A MEMS accelerometer is measuring accelerations of all kinds across its three axes, and through this noise the gravitational vector must be deduced. When the system is at total rest, then acceleration noise is at a minimum and simple math works fine. But when in motion, or more specifically in an environment of vibration energy going in all different directions, it gets to be very difficult.

My suggestion is to filter your accelerometer x, y and z values before your pitch calculation, then filter the output of your pitch calculation.

If you have a way of dumping data out of your system in real time, you could graph what the output of your accelerometer is doing. Then you can simulate and tune filters to best suit your application.

Example filters include IIR, FIR, median, averaging, Kalman, and so on. Each carries their strengths and weaknesses. You can also compound them a bit, depending on what your application needs.

What you'll probably find is that it is actually very difficult to have a stable reading on the gravitational vector in a vibration-heavy environment. As a result, the IMU chip includes the gyro. The gyro will provide you with angular rate data that is very helpful, but is subject to very significant amounts of drift (like several degrees per second). These two very imperfect sensors have to be meshed together to figure out your orientation compared to gravity. Common approaches are:

• High pass filter the gyro and low pass filter the accelerometer.
• Rely on gyro for short-term orientation and attempt to error-correct with long-term accelerometer data.
• thank you very much for your answer! I'll definitely test your suggestions and let you know, how it did go Nov 16, 2016 at 19:46