IMU Maths – How To Calculate Orientation

By | February 20, 2016

IMUs typically contain 9DOF sensors and are used to calculate orientation. Let’s talk about the maths required to get orientation (pitch, roll, yaw) from these sensors.

Getting The IMU Maths Library

There once was an IMU maths library that I made and it’s been incorporated into quite a few cool projects ( such as this and this ), but I actually merged it into ETK a while ago, aaand . . . it has some other ETK dependancies. Your best option is to just download and use ETK – it comes with heaps of stuff that you’ll probably find useful such as PID controllers and a navigation library. If you don’t need or want to use these ‘etk extras’ they won’t increase the size of your binary or anything like that, so feel free to just ignore them.

Download ETK

or if you’re using Git, add it as a submodule

git submodule add https://github.com/supercamel/EmbeddedToolKit

Here’s a bonus script that updates submodules to the latest version

Mandatory Reading

If you’re totally unfamiliar with IMU maths, vector maths, matrices or quaternions I very strongly recommend you at least peruse some of these links before diving head first into this topic.

https://www.mathsisfun.com/algebra/vectors.html

http://www.camelsoftware.com/2016/02/20/quaternions-and-how-to-really-use-them/

Orientation Sensing Basics

Virtually all IMUs will use what’s known as a 3 axis MARG array. This acronym stands for Magnetometer, Accelerometer and Rate Gyroscope. These sensors have historically been big and expensive, but MEMs technology has made it possible to miniaturise all of these sensors down so that they fit inside a single chip! Infact, some sensors such as the BNO055 include a processor that calculates orientation for you (this is great for basic applications but has some serious limitations that will be discussed later).

Magnetometers and accelerometers both produce 3 dimensional vectors. A single vector is great for indicating a direction or for defining a plane (search for normal vectors). Think about acceleration due to gravity. It always points up. The acceleration vector defines the ground surface, because gravity is essentially the normal vector of the ground plane. Orientation is still unknown though, because although the ground plane is defined there is nothing to indicate which way we’re facing (where’s north??). Obviously that’s where the magnetometers come in. By defining a second vector, we’re able to determine orientation. One vector is good for direction, but two are required for orientation.

We can combine acceleration and magnetic vectors to produce a rotation matrix. A rotation matrix is simply a 3×3 matrix that contains 3 perpendicular 3D vectors. These are north, east and down vectors. Notice that the matrix contains 3 vectors, when it’s just been mentioned that only two are required for orientation? Matrices contain redundant information and occasionally the components of the matrix lose their orthogonal properties, which is one of the down sides to using a matrix. Anyway, here’s how they’re made

Let’s go through this. Creating the down vector is easy; it’s just acceleration. The east vector is created by calculating the cross product of down and the magnetometer reading. Remember the cross product of two vectors is always perpendicular (at right angles) to both. East happens to be perpendicular to down and north, so we’re using the cross product to generate the east vector for the matrix. Next the north vector is created from the cross product of east and down. This cancels out ‘magnetic dip’ and is done to ensure that north is also perpendicular.

Each vector is normalized then packed into the rows of a matrix. The matrix can then be converted to a quaternion using the quaternion fromMatrix function.

The matrix method is interesting in that it ‘automatically’ corrects for magnetic dip. An alternative method, however, is to construct q_accel from euler angles.

Notice the z axis of the magnetometers isn’t used? This particular method doesn’t account for magnetic dip.

In practice, these two sensors (accelerometers in particular) are unusable for steady orientation data because they contain so much signal noise. Every single vibration, motion or acceleration will cause the accelerometers to point in some direction that isn’t perfectly downwards and this is where gyroscopes come in handy.

Continue on to part 2 . . .

 

Leave a Reply

Your email address will not be published. Required fields are marked *