Magnetometer Guide: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
(37 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<metadesc>A magnetometer works like a digital compass, reporting its orientation relative to the earth's magnetic field. Learn more at Phidgets.com.</metadesc>
[[Category:Primer]]
[[Category:Primer]]
'''Under Construction'''
__TOC__
__TOC__


==Introduction==
==Introduction==
[[File:1042_0.jpg|right|400px|link=]]
A compass is a navigational instrument that is used to determine direction relative to the surface of the earth.  Compasses measure direction with respect to the 4 cardinal directions (North, East, South, West) with 0 degrees indicating straight North and 180 degrees indicating straight South.
A compass is a navigational instrument that is used to determine direction relative to the surface of the earth.  Compasses measure direction with respect to the 4 cardinal directions (North, East, South, West) with 0 degrees indicating straight North and 180 degrees indicating straight South.


Line 14: Line 14:


===Basic Use===
===Basic Use===
Compasses are typically mounted on an object that the user wants to monitor the direction of.  In order to get the most accurate readings it is recommended to mount the compass to the object in such a way that there is minimal room for shifting.  You will need to calibrate the compass as well.
Compasses are typically used to measure the direction of the object they're mounted on.  In order to get the most accurate readings it is recommended to mount the compass to the object in such a way that there is minimal room for shifting around.  You will need to calibrate the compass as well.


====Calibration====
====Calibration====
Calibration involves setting the compass up to handle the magnetic field characteristics in your particular region of the world as well as disturbances caused by a more localized magnetic fields such as speaker magnets.  To calibrate effectively you will need to determine the Earth's magnetic field strength at your location.  This data is available on line in the form of [http://www.ngdc.noaa.gov/geomag-web/#igrfwmm magnetic field calculators] all you have to do is enter in your zip or postal code.
Calibration involves setting the compass up to handle the magnetic field characteristics in your particular region of the world as well as interference caused by a more localized magnetic fields such as speaker magnets.  To calibrate effectively you will need to determine the Earth's magnetic field strength at your location.  This data is available on line in the form of [http://www.ngdc.noaa.gov/geomag-web/#igrfwmm magnetic field calculators]; all you have to do is enter in your zip or postal code.
 
Calibration is ''required'' for any applications needing more accuracy than being able to discern a simple North, East, South, or West direction. If you have already gathered compass data, you can retroactively calibrate that data by applying the same calibration formula to each data point.  


Calibration is ''required'' for any applications needing more accuracy than North, East, South, West.


For more information on calibrating the compass on the [[1056 User Guide|1056]] refer to our [[1056 User Guide#Magnetic Error Correction (Calibration)|calibration guide]].
For more information about calibration, check the [[:Category:UserGuide|user guide]] for your Phidget.


==Interference==
==Interference==
Line 27: Line 28:


===What are likely sources of interference and how are they handled?===
===What are likely sources of interference and how are they handled?===
[[image:nodistortion.gif|thumb|310px|2 axis magnetic field data with 0 distortion.]]
[[image:nodistortion.gif|thumb|400px|link=|Approximation of 2 axis magnetic field data with 0 distortion.<br>[[Media:nodistortion.gif|Full-Sized Image]]]]
There are 4 main categories of interference.  Some are easy to get rid of via [[Compass Primer#Calibration|calibration]], others are not easy or even impossible to get rid of.   
There are 4 main categories of interference.  Some are easy to get rid of via calibration, others are not easy or even impossible to get rid of.   


If you rotate the compass through a full 360 degrees and plot the resulting data on an x-y plane you will get a circle centered at the origin. This can be extended to a 3 axis compass which would generate a sphere.  However, certain characteristics of the vehicle and environment can have a distorting effect on this magnetic data often in combination with one another to create significant interference.  In fact the magnitude of the interference is regularly many times the that of the actual signal.
If you rotate the compass through a full 360 degrees and plot the resulting data on an x-y plane you will get a circle centered at the origin. This can be extended to a 3 axis compass which would generate a sphere.  However, certain characteristics of the environment can have a distorting effect on this magnetic data.  In fact, the magnitude of the distortion is regularly many times the that of the actual signal.
<br clear = "all">
<br clear = "all">
===="Hard Iron" Interference====
===="Hard Iron" Interference====
[[image:harddistortion.gif|thumb|310px|Magnetic field with hard iron distortion]]
[[image:harddistortion.gif|thumb|link=|400px|Approximation of magnetic field with hard iron distortion.<br>[[Media:harddistortion.gif|Full-Sized Image]]]]
So called ''hard iron'' interference is caused by static magnetic fields associated with the vehicle. This can include any minor (or major) magnetism in the metal chassis or frame of the vehicle, any actual magnets attached such as speakers etc...  This interference pattern is unique to the vehicle but is constant.  If you have your compass in an enclosure that is held together with metal screws even these relatively small amounts of ferromagnetic material can cause issues.  If we consider the magnetic data circle, hard iron interference has the effect of x/y(/z in the case of 3 axes) shifting the entire circle away from the origin by some amount.  The amount is dependent on any number of different factors and can be very large.  The important part is that this shift is the same for all points in time so it can be calibrated out very easily with a numeric offset which is taken care of by the [[Compass Primer#Calibration|calibration process]].
So called ''hard iron'' interference is caused by static magnetic fields associated with the enviornment. For example, this could include any minor (or major) magnetism in the metal chassis or frame of a vehicle, any actual magnets such as speakers, etc...  This interference pattern is unique to the environment but is constant.  If you have your compass in an enclosure that is held together with metal screws even these relatively small amounts of ferromagnetic material can cause issues.  If we consider the magnetic data circle, hard iron interference has the effect of x/y(/z in the case of 3 axes) shifting the entire circle away from the origin by some amount.  The amount is dependent on any number of different factors and can be very large.  The important part is that this shift is the same for all points in time so it can be calibrated out very easily with a numeric offset which is taken care of by the calibration process.
<br clear = "all">
<br clear = "all">


===="Soft Iron" Interference====
===="Soft Iron" Interference====
{|align = "right"
[[image:softdistortion.gif|link=|right|thumb|350px|Approximation of magnetic field with soft iron distortion.<br>[[Media:softdistortion.gif|Full-Sized Image]]]]
|[[image:Magnetic_distortion.jpg|thumb|310px|Example of soft iron distortion]]||[[image:softdistortion.gif|thumb|310px|Magnetic field with soft iron distortion]]
''Soft iron'' interference is caused by distortion of the Earth's magnetic field due to materials in the environment.  Think of it like electricity - the magnetic field is looking for the easiest path to get to where it is going.  Since magnetic fields can flow more easily through ferromagnetic materials than air, more of the field will flow through the ferromagnetic material than you would expect if it were just air.  This distortion effect causes the magnetic field lines to be bent sometimes quite a bit.  Note that unlike hard iron interference which is the result of materials which actually have a magnetic field of their own, soft iron interference is caused by non-magnetic materials distorting the Earth's magnetic field.  This type of interference has a squishing effect on the magnetic data circle turning it into more of an ellipsoid shape.  The distortion in this case depends on the direction that the compass is facing.  Because of this, the distortion cannot be calibrated out with a simple offset, more complicated math will still let the compass account for this type of interference though.
|}
''Soft iron'' interference is caused by distortion of the Earth's magnetic field due to materials in the vehicle's construction.  Think of it like electricity, the magnetic field is looking for the easiest path to get to where it is going.  Since magnetic fields can flow more easily through [[Glossary#Ferromagnetism|ferromagnetic]] materials than air, more of the field will flow through the ferromagnetic material than you would expect if it were just air.  This distortion effect causes the magnetic field lines to be bent sometimes quite a bit.  Note that unlike hard iron interference which is the result of materials which actually have a magnetic field of their own, soft iron interference is caused by non-magnetic materials distorting the Earth's magnetic field.  This type of interference has a squishing effect on the magnetic data circle turning it into more of an ellipsoid shape.  The distortion in this case depends on the direction that the compass is facing.  Because of this the distortion cannot be [[Compass Primer#Calibration|calibrated]] out with a simple offset, more complicated math will still let the compass account for this type of interference though.
 
<br clear = "all">
<br clear = "all">
[[image:Magnetic_distortion.jpg|link=|thumb|center|250px|Example of soft iron distortion caused by an ''external'' object.<br>]]


====Dynamic magnetic fields associated with application====
====Dynamic magnetic fields associated with application====
Most robots and vehicles have all manner of devices running on them other than just the compass.  The wiring associated with these devices will create its own, dynamic magnetic field.  Particularly bad offenders are electric motors that are running.  The magnetic fields of these devices are constantly changing and [[Compass Primer#Calibration|calibration]] will only provide small (if any) benefits.  The best thing that can be done to avoid this sort of interference is to mount the compass as far away from other electronics as possible.
If you are using a compass in a robot or vehicle, there is likely many more devices running at the same time.  The wiring associated with these devices will create its own, dynamic magnetic field.  Particularly bad offenders are electric motors that are running.  The magnetic fields of these devices are constantly changing and calibration will only provide small (if any) benefits.  The best thing that can be done to avoid this sort of interference is to mount the compass as far away from other electronics as possible.


====Static or Dynamic fields not associated with application====
====Static or Dynamic fields not associated with application====
These are the most difficult to handle.  A very common cause of this sort of interference is steel or iron re-bar in concrete floors.  Steel studs in office or commercial buildings are also a problem.  In general any environmental (i.e. external) sources of hard or soft iron distortion can cause issues as the vehicle moves about.  There is no practical way to account for this.  The best thing you can do is to take a simple handheld compass and move it around in the area you intend the electronic compass to be operating.  Does the compass report reasonable directions?  If so then your system will likely function well.  If not, the area is simply too noisy to use an electronic compass.
These are the most difficult to handle.  A very common cause of this sort of interference is steel or iron re-bar in concrete floors.  Steel studs in office or commercial buildings are also a problem.  In general any environmental (i.e. external) sources of hard or soft iron distortion can cause issues (especially if the compass is in a vehicle, which is moving around).  There is no practical way to account for this.  The best thing you can do is to take a simple handheld compass and move it around in the area you intend the electronic compass to be operating.  Does the compass report reasonable directions?  If so then your system will likely function well.  If not, the area is simply too noisy to use an electronic compass.


====Combinations====
====Combinations====
In practical applications most of these types of interference work in combination.  Because of all of this, to use a [[Compass Primer#Calibration|compass calibration]] is required.
In practical applications most of these types of interference work in combination.  Because of all of this, to use a compass calibration is required.


==Declination==
==Declination==
*declination map &rarr;  
Over long periods of time, changes in the Earth's magnetic core have an effect on the Earth's magnetic field.  What this means is that the North magnetic pole shifts and is not the same as ''true'' North.  The time it takes for magnetic North to shift significantly is very long so for all intents and purposes the magnetic North pole is stationary, it is just important to note that it is not in the same place as the actual North pole.  Since compasses measure direction as a function of magnetic field strength, the direction a compass tells you is North is in fact magnetic North.  As you might imagine the amount that magnetic North and true North differ is a function of your location.  It is technically possible for your compass to point in the exact opposite direction from North if you are directly between the North pole and the magnetic North pole.  The most common way to get declination is from a map that has isogonic lines.  Isogonic lines are lines of equal declination similar to contour lines on topographic maps that show elevation.  The lines will give a reasonable approximation of the declination for your region.  For example, take a look at the map below and try to determine what the declination is in your city:
*difference between true north and magnetic north
[[File: declination_small.jpg|link={{SERVER}}/docs/images/f/f3/Declination.jpg]]
*a simple correction factor to fix it
 
* Need to explain this image - it's very simple, but non-intuitive.
 
For the purposes of calculation assume that East declination is positive and West is negative.  This means that you need to subtract the declination from your heading when your declination is to the East and vice versa when it is West.
 
==Noise==
As with all electronics, electronic compasses will be effected by some amount of noise, usually caused by thermal and mechanical fluctuations inside the sensor. Noise levels will be different for each axis, because each orientation of sensor is build slightly differently (for example, it is much more difficult to get low noise from the vertical axis because the sensor is a flat piece of silicon. Two of the major types of noise are white noise and random walk.
 
===White Noise===
White noise is the short-term noise that is contributed to from a number of internal and external factors. For example, when a compass is stationary on your desk, it might read 100&mu;G one sample, 101&mu;G another sample, and 97&mu;G in yet another sample. Luckily, white noise is usually fairly consistent which means it can be mitigated quite effectively if you average multiple samples together. With a Phidgets compass, if you select a data rate slower than its maximum it will automatically average as many samples as it can within that time frame for each value. In applications where you need an extremely fast sampling rate, and can't afford to spend time averaging samples, you should look for a compass with low white noise. Also keep in mind the noisiness of your environment- For example, the engine and road noise on a bus will easily be more noisy than the compass - so there is no point in paying for a low noise device. See the [[Allan Deviation Primer]] for more information on spatial noise characteristics.
 
 
===Random Walk===
Often called drift, random walk is the long-term noise that causes samples to gradually become further and further away from their true values. This type of noise is less important for applications with constant movement and a fast sampling rate, but for applications where values are averaged over longer periods of time, it can cause severe inaccuracies. See the [[Allan Deviation Primer]] for more information on noise characteristics.
 
==Calculating Heading from Magnetometer Data==
The three axis magnetometer data can be used, along with pitch and roll angles calculated from the accelerometer, to find a magnetic bearing, just like a physical compass. In order for the compass to be accurate at all, calibration must have been done to factor out hard and soft iron offsets.
 
The magnetic field is represented as a vector. If the Phidget is completely level with respect to gravity, then we can calculate magnetic bearing easily by looking at the x and y components of the vector, which simply represents an angle between 0 and 360 degrees on the unit circle.
 
However, when the board is not level, the magnetic field vector components in each axis will be skewed, or is other words, what we need to know is the magnetic field with respect to the earth frame, and what we have is the magnetic field with respect to the Phidget, which is rotated with respect to the earth. What needs to be done is to rotate the magnetic field vector back into the earth frame, we can then use the x-y components to again determine bearing. The easiest way to do this is to find the board pitch and roll angles using the accelerometer, and then build a rotation matrix using these angles to rotate the magnetic field vector into the earth frame (ie aligned with gravity). For example, here is an excerpt from our C# example code which shows how this is done:


Due to the nature of the Earth's magnetic field there is a difference between magnetic north, and what is known as "true north".  What this means is that there is a corrective factor that needs to be included in order to get accurate readings.


==Glossary==
<syntaxhighlight lang=csharp>
====resolution====
//This finds a magnetic north bearing, correcting for board tilt and roll as measured by the accelerometer
* resolution indicates how fine of a change in the magnetic field the magnetometer is capable of reading.  in the case of the 1056 it is 400&mu;G minimum. this means that any change in magnetic field smaller than 400&mu;G will not get measured.
//This doesn't account for dynamic acceleration - ie accelerations other than gravity will throw off the calculation
//Calculations based on AN4248
double[] lastAngles = { 0, 0, 0 };
void calculateCompassBearing(SpatialSpatialDataEventArgs e) {
double[] gravity = {
e.Acceleration[0],
e.Acceleration[1],
e.Acceleration[2]};


====offset from north====
double[] magField = {
* Offset from north is only valid on a compass that has been well calibrated.
e.MagneticField[0],
* This figure assumes a long term measurement - so noise has been greatly reduced / averaged out.
e.MagneticField[1],
* Offset from North error comes from non-linearities in the compass, and each axis in the compass being somewhat sensitive to magnetic fields aligned with other axes.
e.MagneticField[2]};
* There is very little that can be done to reduce this error.


//Roll Angle - about axis 0
//  tan(roll angle) = gy/gz
//  Use Atan2 so we have an output os (-180 - 180) degrees
double rollAngle = Math.Atan2(gravity[1], gravity[2]);


//Pitch Angle - about axis 1
//  tan(pitch angle) = -gx / (gy * sin(roll angle) * gz * cos(roll angle))
//  Pitch angle range is (-90 - 90) degrees
double pitchAngle = Math.Atan(-gravity[0] / (gravity[1] * Math.Sin(rollAngle) + gravity[2] * Math.Cos(rollAngle)));


====declination====
//Yaw Angle - about axis 2
[[File:declination.jpg|thumb|310px]]
//  tan(yaw angle) = (mz * sin(roll) – my * cos(roll)) /
*declination map &rarr;
//                  (mx * cos(pitch) + my * sin(pitch) * sin(roll) + mz * sin(pitch) * cos(roll))
*difference between true north and magnetic north
//  Use Atan2 to get our range in (-180 - 180)
*a simple correction factor to fix it
//
* Need to explain this image - it's very simple, but non-intuitive.
//  Yaw angle == 0 degrees when axis 0 is pointing at magnetic north
double yawAngle = Math.Atan2(magField[2] * Math.Sin(rollAngle) - magField[1] * Math.Cos(rollAngle),
magField[0] * Math.Cos(pitchAngle) + magField[1] * Math.Sin(pitchAngle) * Math.Sin(rollAngle) + magField[2] * Math.Sin(pitchAngle) * Math.Cos(rollAngle));


===Measurement Range===
double[] angles = { rollAngle, pitchAngle, yawAngle };
* Maximum field strength (in gauss) that the compass can measure.  Note that hard iron offsets can be calibrated out, but they will still reduce the range of the sensor, as the earth's magnetic field is pushed away from the center.  (May be could use a simple image).


===Noise===
//we low-pass filter the angle data so that it looks nicer on-screen
* This will be a graph of noise (in gauss, std. deviation) compared to sample rate.
try {
* Could also be allen deviation graph
//make sure the filter buffer doesn't have values passing the -180<->180 mark
//Only for Roll and Yaw - Pitch will never have a sudden switch like that
for (int i = 0; i < 3; i += 2) {
if (Math.Abs(angles[i] - lastAngles[i]) > 3)
foreach (double[] value in compassBearingFilter)
if (angles[i] > lastAngles[i])
value[i] += 360 * Math.PI / 180.0;
else
value[i] -= 360 * Math.PI / 180.0;
}


===Sample (Data) Rate===
lastAngles = (double[])angles.Clone();
* This is set for the entire PhidgetSpatial
* The compass is not able to provide data at this rate
* Do not forget that the primer is not here to describe individual products.


====units====
compassBearingFilter.Add((double[])angles.Clone());
*occasional confusion, people use gauss and tesla interchangeably in the application of magnetometers. 1 tesla is 10<sup>4</sup> gauss.
if (compassBearingFilter.Count > compassBearingFilterSize)
compassBearingFilter.RemoveAt(0);


===Use Within Code===
yawAngle = pitchAngle = rollAngle = 0;
foreach (double[] stuff in compassBearingFilter) {
rollAngle += stuff[0];
pitchAngle += stuff[1];
yawAngle += stuff[2];
}
yawAngle /= compassBearingFilter.Count;
pitchAngle /= compassBearingFilter.Count;
rollAngle /= compassBearingFilter.Count;


* Mention how atan() and atan2() are different when using trigonometry in your code, and how they get used.  I.e. atan() is the mathematically correct atan, which goes from -90 to 90 degrees.  The function atan2() was developed because humans don't think this way, they think in a whole circle from 180 to -180 degrees. So atan2() takes the signs of the two arguments into account to figure out the quadrant.
//Convert radians to degrees for display
compassBearing = yawAngle * (180.0 / Math.PI);


===How to calculate bearing===
bearingTxt.Text = compassBearing.ToString("F1") + "°";
*currently we only show how to calculate compass heading in the C# examples.
pitchAngleTxt.Text = (pitchAngle * (180.0 / Math.PI)).ToString("F1") + "°";
rollAngleTxt.Text = (rollAngle * (180.0 / Math.PI)).ToString("F1") + "°";
} catch { }
}
</syntaxhighlight>

Revision as of 16:51, 29 November 2017

Introduction

1042 0.jpg

A compass is a navigational instrument that is used to determine direction relative to the surface of the earth. Compasses measure direction with respect to the 4 cardinal directions (North, East, South, West) with 0 degrees indicating straight North and 180 degrees indicating straight South.

Electronic compasses work in exactly the same way as any other compass: They detect the Earth's magnetic field and respond to it. The difference is that an electronic compass uses a magnetometer to detect the field as opposed to a small magnet. This allows them to be much more accurate and allows them to respond more quickly to changes in direction than a traditional compass ever could.

How Compasses Work

Small compasses found in clocks, mobile phones, and other electronic devices are solid-state compasses, usually built out of two or three magnetic field sensors that provide data for a microprocessor. The correct heading relative to the compass is calculated using trigonometry. Often, the device is a discrete component which outputs either a digital or analog signal proportional to its orientation. This signal is interpreted by a controller or microprocessor and used either internally, or sent to a display unit. The sensor uses highly calibrated internal electronics to measure the response of the device to the Earth's magnetic field.

Basic Use

Compasses are typically used to measure the direction of the object they're mounted on. In order to get the most accurate readings it is recommended to mount the compass to the object in such a way that there is minimal room for shifting around. You will need to calibrate the compass as well.

Calibration

Calibration involves setting the compass up to handle the magnetic field characteristics in your particular region of the world as well as interference caused by a more localized magnetic fields such as speaker magnets. To calibrate effectively you will need to determine the Earth's magnetic field strength at your location. This data is available on line in the form of magnetic field calculators; all you have to do is enter in your zip or postal code.

Calibration is required for any applications needing more accuracy than being able to discern a simple North, East, South, or West direction. If you have already gathered compass data, you can retroactively calibrate that data by applying the same calibration formula to each data point.


For more information about calibration, check the user guide for your Phidget.

Interference

The most common problem compass users can expect to face is interference. Interference will generally cause the compass to behave poorly. Usually this means inconsistent, fluctuating, or inaccurate readings. Luckily most forms of interference are fairly easy to deal with.

What are likely sources of interference and how are they handled?

Approximation of 2 axis magnetic field data with 0 distortion.
Full-Sized Image

There are 4 main categories of interference. Some are easy to get rid of via calibration, others are not easy or even impossible to get rid of.

If you rotate the compass through a full 360 degrees and plot the resulting data on an x-y plane you will get a circle centered at the origin. This can be extended to a 3 axis compass which would generate a sphere. However, certain characteristics of the environment can have a distorting effect on this magnetic data. In fact, the magnitude of the distortion is regularly many times the that of the actual signal.

"Hard Iron" Interference

Approximation of magnetic field with hard iron distortion.
Full-Sized Image

So called hard iron interference is caused by static magnetic fields associated with the enviornment. For example, this could include any minor (or major) magnetism in the metal chassis or frame of a vehicle, any actual magnets such as speakers, etc... This interference pattern is unique to the environment but is constant. If you have your compass in an enclosure that is held together with metal screws even these relatively small amounts of ferromagnetic material can cause issues. If we consider the magnetic data circle, hard iron interference has the effect of x/y(/z in the case of 3 axes) shifting the entire circle away from the origin by some amount. The amount is dependent on any number of different factors and can be very large. The important part is that this shift is the same for all points in time so it can be calibrated out very easily with a numeric offset which is taken care of by the calibration process.

"Soft Iron" Interference

Approximation of magnetic field with soft iron distortion.
Full-Sized Image

Soft iron interference is caused by distortion of the Earth's magnetic field due to materials in the environment. Think of it like electricity - the magnetic field is looking for the easiest path to get to where it is going. Since magnetic fields can flow more easily through ferromagnetic materials than air, more of the field will flow through the ferromagnetic material than you would expect if it were just air. This distortion effect causes the magnetic field lines to be bent sometimes quite a bit. Note that unlike hard iron interference which is the result of materials which actually have a magnetic field of their own, soft iron interference is caused by non-magnetic materials distorting the Earth's magnetic field. This type of interference has a squishing effect on the magnetic data circle turning it into more of an ellipsoid shape. The distortion in this case depends on the direction that the compass is facing. Because of this, the distortion cannot be calibrated out with a simple offset, more complicated math will still let the compass account for this type of interference though.

Example of soft iron distortion caused by an external object.

Dynamic magnetic fields associated with application

If you are using a compass in a robot or vehicle, there is likely many more devices running at the same time. The wiring associated with these devices will create its own, dynamic magnetic field. Particularly bad offenders are electric motors that are running. The magnetic fields of these devices are constantly changing and calibration will only provide small (if any) benefits. The best thing that can be done to avoid this sort of interference is to mount the compass as far away from other electronics as possible.

Static or Dynamic fields not associated with application

These are the most difficult to handle. A very common cause of this sort of interference is steel or iron re-bar in concrete floors. Steel studs in office or commercial buildings are also a problem. In general any environmental (i.e. external) sources of hard or soft iron distortion can cause issues (especially if the compass is in a vehicle, which is moving around). There is no practical way to account for this. The best thing you can do is to take a simple handheld compass and move it around in the area you intend the electronic compass to be operating. Does the compass report reasonable directions? If so then your system will likely function well. If not, the area is simply too noisy to use an electronic compass.

Combinations

In practical applications most of these types of interference work in combination. Because of all of this, to use a compass calibration is required.

Declination

Over long periods of time, changes in the Earth's magnetic core have an effect on the Earth's magnetic field. What this means is that the North magnetic pole shifts and is not the same as true North. The time it takes for magnetic North to shift significantly is very long so for all intents and purposes the magnetic North pole is stationary, it is just important to note that it is not in the same place as the actual North pole. Since compasses measure direction as a function of magnetic field strength, the direction a compass tells you is North is in fact magnetic North. As you might imagine the amount that magnetic North and true North differ is a function of your location. It is technically possible for your compass to point in the exact opposite direction from North if you are directly between the North pole and the magnetic North pole. The most common way to get declination is from a map that has isogonic lines. Isogonic lines are lines of equal declination similar to contour lines on topographic maps that show elevation. The lines will give a reasonable approximation of the declination for your region. For example, take a look at the map below and try to determine what the declination is in your city: Declination small.jpg


For the purposes of calculation assume that East declination is positive and West is negative. This means that you need to subtract the declination from your heading when your declination is to the East and vice versa when it is West.

Noise

As with all electronics, electronic compasses will be effected by some amount of noise, usually caused by thermal and mechanical fluctuations inside the sensor. Noise levels will be different for each axis, because each orientation of sensor is build slightly differently (for example, it is much more difficult to get low noise from the vertical axis because the sensor is a flat piece of silicon. Two of the major types of noise are white noise and random walk.

White Noise

White noise is the short-term noise that is contributed to from a number of internal and external factors. For example, when a compass is stationary on your desk, it might read 100μG one sample, 101μG another sample, and 97μG in yet another sample. Luckily, white noise is usually fairly consistent which means it can be mitigated quite effectively if you average multiple samples together. With a Phidgets compass, if you select a data rate slower than its maximum it will automatically average as many samples as it can within that time frame for each value. In applications where you need an extremely fast sampling rate, and can't afford to spend time averaging samples, you should look for a compass with low white noise. Also keep in mind the noisiness of your environment- For example, the engine and road noise on a bus will easily be more noisy than the compass - so there is no point in paying for a low noise device. See the Allan Deviation Primer for more information on spatial noise characteristics.


Random Walk

Often called drift, random walk is the long-term noise that causes samples to gradually become further and further away from their true values. This type of noise is less important for applications with constant movement and a fast sampling rate, but for applications where values are averaged over longer periods of time, it can cause severe inaccuracies. See the Allan Deviation Primer for more information on noise characteristics.

Calculating Heading from Magnetometer Data

The three axis magnetometer data can be used, along with pitch and roll angles calculated from the accelerometer, to find a magnetic bearing, just like a physical compass. In order for the compass to be accurate at all, calibration must have been done to factor out hard and soft iron offsets.

The magnetic field is represented as a vector. If the Phidget is completely level with respect to gravity, then we can calculate magnetic bearing easily by looking at the x and y components of the vector, which simply represents an angle between 0 and 360 degrees on the unit circle.

However, when the board is not level, the magnetic field vector components in each axis will be skewed, or is other words, what we need to know is the magnetic field with respect to the earth frame, and what we have is the magnetic field with respect to the Phidget, which is rotated with respect to the earth. What needs to be done is to rotate the magnetic field vector back into the earth frame, we can then use the x-y components to again determine bearing. The easiest way to do this is to find the board pitch and roll angles using the accelerometer, and then build a rotation matrix using these angles to rotate the magnetic field vector into the earth frame (ie aligned with gravity). For example, here is an excerpt from our C# example code which shows how this is done:


//This finds a magnetic north bearing, correcting for board tilt and roll as measured by the accelerometer
//This doesn't account for dynamic acceleration - ie accelerations other than gravity will throw off the calculation
//Calculations based on AN4248
double[] lastAngles = { 0, 0, 0 };
void calculateCompassBearing(SpatialSpatialDataEventArgs e) {
	double[] gravity = {
		e.Acceleration[0],
		e.Acceleration[1],
		e.Acceleration[2]};

	double[] magField = {
		e.MagneticField[0],
		e.MagneticField[1],
		e.MagneticField[2]};

	//Roll Angle - about axis 0
	//  tan(roll angle) = gy/gz
	//  Use Atan2 so we have an output os (-180 - 180) degrees
	double rollAngle = Math.Atan2(gravity[1], gravity[2]);

	//Pitch Angle - about axis 1
	//  tan(pitch angle) = -gx / (gy * sin(roll angle) * gz * cos(roll angle))
	//  Pitch angle range is (-90 - 90) degrees
	double pitchAngle = Math.Atan(-gravity[0] / (gravity[1] * Math.Sin(rollAngle) + gravity[2] * Math.Cos(rollAngle)));

	//Yaw Angle - about axis 2
	//  tan(yaw angle) = (mz * sin(roll) – my * cos(roll)) /
	//                   (mx * cos(pitch) + my * sin(pitch) * sin(roll) + mz * sin(pitch) * cos(roll))
	//  Use Atan2 to get our range in (-180 - 180)
	//
	//  Yaw angle == 0 degrees when axis 0 is pointing at magnetic north
	double yawAngle = Math.Atan2(magField[2] * Math.Sin(rollAngle) - magField[1] * Math.Cos(rollAngle),
		magField[0] * Math.Cos(pitchAngle) + magField[1] * Math.Sin(pitchAngle) * Math.Sin(rollAngle) + magField[2] * Math.Sin(pitchAngle) * Math.Cos(rollAngle));

	double[] angles = { rollAngle, pitchAngle, yawAngle };

	//we low-pass filter the angle data so that it looks nicer on-screen
	try {
		//make sure the filter buffer doesn't have values passing the -180<->180 mark
		//Only for Roll and Yaw - Pitch will never have a sudden switch like that
		for (int i = 0; i < 3; i += 2) {
			if (Math.Abs(angles[i] - lastAngles[i]) > 3)
				foreach (double[] value in compassBearingFilter)
					if (angles[i] > lastAngles[i])
						value[i] += 360 * Math.PI / 180.0;
					else
						value[i] -= 360 * Math.PI / 180.0;
			}

		lastAngles = (double[])angles.Clone();

		compassBearingFilter.Add((double[])angles.Clone());
		if (compassBearingFilter.Count > compassBearingFilterSize)
			compassBearingFilter.RemoveAt(0);

		yawAngle = pitchAngle = rollAngle = 0;
		foreach (double[] stuff in compassBearingFilter) {
			rollAngle += stuff[0];
			pitchAngle += stuff[1];
			yawAngle += stuff[2];
		}
		yawAngle /= compassBearingFilter.Count;
		pitchAngle /= compassBearingFilter.Count;
		rollAngle /= compassBearingFilter.Count;

		//Convert radians to degrees for display
		compassBearing = yawAngle * (180.0 / Math.PI);

		bearingTxt.Text = compassBearing.ToString("F1") + "°";
		pitchAngleTxt.Text = (pitchAngle * (180.0 / Math.PI)).ToString("F1") + "°";
		rollAngleTxt.Text = (rollAngle * (180.0 / Math.PI)).ToString("F1") + "°";
	} catch { }
}