; Tilt Compensating a Compass with an Accelerometer
Documents
User Generated
Resources
Learning Center
Your Federal Quarterly Tax Payments are due April 15th

# Tilt Compensating a Compass with an Accelerometer

VIEWS: 25 PAGES: 4

• pg 1
```									Tilt Compensating a Compass with an Accelerometer
Posted October 18, 2011 by Love Electronics.

Introduction
A compass is a navigation instrument that provides us with a heading parallel to the surface of the earth. The compass always points North so that you can

Compasses work by detecting the magnetic fields produced by the hot rotating iron core at the centre of the earth. The strength of the earth's magnetic field
is about 0.5 to 0.6 gauss and has a component parallel to the earth's surface that always points toward the magnetic north pole. Traditional compasses work
by using these fields to rotate a ferrous metal rod in a small container. The magnetic fields align the metal rod along this component to point to magnetic
north. Using this information, along with a scale on the outside of the compass we can work out our heading (the direction we are facing).

The device we are going to use today, called a magnetometer, does what it says, it measures magnetic
fields. These devices are similar to traditional compasses in that the magnetic fields from the earths core act on small magnetoresistors on three axis (for a 3
axis magnetometer) which adjust current flow through the sensor. By applying a scale to this current, we can calculate the magnetic force (measured in
Gauss) on the sensor.
For a (very) detailed explanation to magneto-resistive sensors read this application note: Magneto-Resistive Sensors.

An important thing to understand about magnetometers, called compasses for sake of simplicity is that they do not provide us with a direction. They provide
us with a way to measure magnetism which we can use to calculate a direction. The compass is only created once we use the magnetometer in some
calculations, as a whole system.

The magnetometer we will be using today is the HMC5883L Triple Axis Magnetometer. This chip is a bargain for including triple axis sensors at up to 8
gauss on a tiny footprint.

A simple calclation we can use to create a compass is below. When the device is level, (pitch (Xb) and roll (Yb) are at 0 degrees). The compass heading can
be determined like so:
The local earth magnetic field has a fixed
component Hh on the horizontal plane pointing to the earths magnetic north. This is measured by the magnetic sensor axis XM and YM (here named
as Xh and Yh). Using this we can calculate the heading angle using this simple equation:

This simple method is discussed in this earlier compass tutorial.

The Tilt Problem
A problem that traditional compasses have is that they need to be held flat to function. If you hold a compass at right angles it will not work at all, and if you
tilt it to 45 degrees the reading will be more inaccurate the further the compass is tilted. This problem occurs because the compass is only using the X and Y
axis of the earths magnetic field (the compass needle is fixed onto a bearing that will only allow the needle to swivel on one axis). When the compass is not
parallel to these axis the amount of magnetism felt by the needle will change based on how out of alignment the compass is to these axis.

Remembering that our magnetometer only forms a compass when integrated into a system with some calculations, we would also suffer this problem if we
only used the same two axis (X and Y) that the traditional compass uses. Of course, if in our application the compass is always going to be held flat this isn't
a problem. You can in this case just create a very simple compass using just a triple axis magnetometer using this earlier tutorial. If however we want to be
able to compensate our compass for tilt up to 40 degrees, we will need a way to include in our calculations the third axis, Z, which (when tilted) now collects
the magnetic field lost by X and Y when they are tilted out of alignment.

Of course, one cannot simply add this value to the result to get an accurate heading. We first need to know how the device is tilted, so we know how to
integrate the Z axis measurement properly, thus correct for our tilt. In other words, we need to know our orientation first. We will do this by incorporating a
triple axis accelerometer into our compass system.

Accelerometers
The accelerometer is a device which measures acceleration. Simple enough, but there are two different types of acceleration: static acceleration (tilt) and
dynamic acceleration (movement).
The accelerometer outputs a measurement telling the user in what direction on its axis force is being applied. The good news is, the accelerometer picks up
all kinds of movement, meaning you can get just about anything from it. The bad news is, the accelerometer picks up all kinds of movement, meaning you
have no idea what your looking at!
Usually when dealing with an accelerometer, we want to get the orientation of the sensor, which basically means which way is the sensor tilted. With one
axis accelerometers we can act a bit like a spirit level and measure how one axis is tilted relative to gravity, but by adding more axes into our accelerometer
we can begin to figure out our orientation in 3 dimensions.
So, how do they work? Well the accelerometer is basically a mass, suspended by a spring. The measurement you are getting from the device is the amount
these springs are flexing.

This is a illustration of an accelerometer; they are not actually constructed this way, this is just to aid
your understanding! Here you can see the mass (in blue) is suspended by four springs attached to the
frame. At the moment all these springs are zero, which means no force is being applied to the mass
relative to the frame, but this is not actually what you see when your accelerometer is sitting on the
desk next to you.
You actually see something more like this:
This is because gravity is acting on the mass and is pulling it down. The accelerometer is measuring 1
g because that is the amount of gravity you experience on the surface of the earth. So when you have
an accelerometer and you think you are measuring nothing, you are actually measuring the force of
gravity!

The accelerometer also measures movement, so if you move the accelerometer from side to side, the
result looks like this.

However usually you will never see these results, it's more likely you see the all mashed together. This
looks like a big mess, like this:

The accelerometer we will be using is the ADXL345 Triple Axis Accelerometer. This little chip packs triple axis sensing, at up to +/- 16g measurements!

Tilt Compensation Equation
So far we have discussed what a compass is, how tilt affects the results, and that we are going to use an accelerometer to detect our orientation to correct
for this. This how we are going to use both the HMC5883L magnetometer and ADXL345 accelerometer to create a tilt compensated compass.

When the device is tilted, pitch and roll angles are not 0°. The diagram below the pitch and roll angles are measured by a 3 axis accelerometer. XM, YM and
ZM(the measurement axis on the magnetometer) will be compensated to obtain Xh and Yh. Once have corrected the Xh and Yh measurements, we can use
the first equation to calculate our heading.
Xh = XM * cos(Pitch) + ZM * sin(Pitch)
Yh = XM * sin(Roll) * sin(Pitch) + YM * cos(Roll) - ZM * sin(Roll) * cos(Pitch)

Once we have tilt compensated Xh and Yh, we can now use the same equation as before to find our heading!