Heading can be effectively determined through the use of dual-axis magnetometer and triaxial accelerometer sensors available in an inertial measurement unit (IMU) such as the MEMSense nIMU or µIMU. However, a correctly implemented compass system must compensate for the effect of elevation and bank angle (tilt), as well as calibrate out hard- and soft-iron effects as much as possible. This article discusses the impact of elevation, bank angle, and hard- and soft-iron effects on heading calculations, and discusses methods that may be employed to counter their impact when using magnetometer data from an IMU. Although the methods address corrections in two dimensions (x-y plane), it is possible, and often necessary, to extend the concepts and functions to three dimensions.
The following terms are used throughout this article and are defined here to aid in clarity.
Accelerometer—A sensor that measures linear acceleration.
Gyro/gyroscope—A sensor that measures rotational velocity.
Inertial measurement data—Any data collected from an inertial measurement unit, generally rotational velocity, linear acceleration, and magnetic field strength.
Inertial Measurement Unit (IMU)—A device used to collect inertial measurement data.
Magnetometer—A sensor that measures changes in magnetic field strength.
Microelectromechanical Systems (MEMS)—Micro-size mechanical devices on a silicon wafer built with techniques from the semiconductor industry.
The coordinate system or frame conventions used in this article are shown in Figure 1 and use the Right-Hand Rule that specifies that positive rotation is in the direction in which the fingers of your right hand curl when the thumb is oriented along the positive axis of rotation (away from the origin). Referring to the figure, the positive bank angle will be in the direction of the arrow rotating around the X axis, positive elevation will be in the direction of the arrow rotating around the Y axis, and positive heading will be in the direction of the arrow rotating around the Z axis. Thus, positive rotation is counter-clockwise when looking along the axis of rotation towards the origin.
Figure 1. Reference frame conventions
We will always assume that orientation is relative to a reference coordinate frame where the X axis points North, the Y axis points East, and the Z axis points down—often referred to as NED. Heading and attitude are always determined relative to this standard, or reference, position.
To differentiate between the local coordinate frame and the reference coordinate frame, the reference frame axes will be in uppercase and/or have the subscript r, as in Xr, whereas the local frame axes will be lowercase.
Description of Sensors
A typical IMU will contain three different sensors: a gyroscope, which measures angular rate/velocity; an accelerometer, which measures acceleration; and a magnetometer, which measures magnetic field strength. In addition, each sensor is typically triaxial, meaning it is capable of providing simultaneous measurements along each of the three axes shown in Figure 2. Because of drift and noise components at low angular rates, MEMS-based gyroscopes are typically not used directly in the calculation of heading and so will not be mentioned further. However, both the accelerometer and magnetometer are critical in accurately determining elevation, bank angle, and heading.
Figure 2. Calculation of θ (elevation). Note that this is a negative rotation that will result in a negative elevation angle
An accelerometer measures acceleration, and for the purpose of this discussion is used to measure the acceleration of gravity in g's (1 g = 9.8 m/s2). The structure of an accelerometer is mechanical in nature, often illustrated as a proof mass attached to a spring, which in turn is attached to an outer supporting structure. As the object accelerates, the proof mass will lag the motion, and this differential in the position of the proof mass is then used in calculating the acceleration.
Magnetometers are used to measure the strength of a magnetic field. This can be accomplished in several ways, but a method based on magnetoresistivity is commonly used in MEMS-based magnetometers. First discovered by Lord Kelvin in 1856, magnetoresistance is exhibited by an increase in resistivity when an electrical current is oriented in the same direction as the magnetic field and by a decrease in resistivity when the current is perpendicular to the field. Magnetoresistive-based magnetometers are solid state, not mechanical, and are therefore not susceptible to many of the effects that normally corrupt mechanical systems. It is this effect, also called anisotropic magnetoresistance (AMR), that enables measurement of magnetic fields in a very small semiconductor package.
Elevation and Bank Angle Effect
Elevation is defined as the angle formed between the x axis and the horizon/ground, and bank angle is defined as the angle formed between the y axis and the horizon/ground. Gravity exerts a constant acceleration of 1 g, which may be used to calculate elevation and bank angle. As shown in Figure 2, use of the arc-tangent function enables calculation of elevation following a negative rotation in the X-Z plane. Note that the Z axis is oriented 'down', as specified by the NED convention. (Please see   for further discussion on calculating elevation and bank angle.)
Heading is calculated using the same method as that used in elevation and bank angle, but because gravity cannot be used to calculate changes in heading, the magnetometer data must be used instead.
A naïve approach to calculating heading would simply apply the arctan function to raw magnetometer data. However, because magnetometer sensitivity decreases as elevation and bank angles increase , this approach introduces errors in the heading. Figure 3 shows the change in heading due to the decrease in magnetometer sensitivity as elevation increases.
Figure 3. Change in heading as a result of a 50° change in elevation (tilt). If properly corrected, no change in heading should be exhibited
By realigning the local z axis with the reference frame Z axis, the heading can be corrected as shown in Figure 4, with the magnitude of these errors given in Figure 5. It's also important to note that tilt/sensitivity errors vary with location, thus it is not possible to employ a static correction factor such as a lookup table.
Figure 4. Heading corrected for 50° change in elevation
Figure 5. Heading error as a result of a 50° change in elevation
Therefore, to accurately calculate heading, we must first apply a rotation that removes the bank angle followed by a second rotation that removes elevation (the reverse sequence is also acceptable). Once this sequence of rotations is completed the local x-y plane will be realigned with the reference X-Y plane, corrections to the magnetometer data will have been made, and we may then proceed with compensating for hard- and soft-iron effects. Full discussion on determining the necessary rotation matrices, applying the rotations, and calculating the corrected heading can be found in .
Hard- and Soft-Iron Distortions
Distortions of the earth's magnetic field are a result of external magnetic influences generally classified as either a hard- or soft-iron effect. If no distorting effects are present, rotating a magnetometer through a minimum of 360° and plotting the resulting data as y axis vs. x axis will result in a circle centered around (0, 0), as shown in Figure 6.
Figure 6. A graph of ideal distortion-free magnetometer data. Note the output is centered around (0,0) and circular in shape
However, the presence of hard- and/or soft-iron effects may produce a perturbation of the circle as a simple offset from (0, 0) in the case of a hard-iron effect, or deform the circle to produce an ellipse in the case of a soft-iron effect. It is also possible that both effects will be exhibited simultaneously.
It is also important to recognize that effective compensation of hard- and soft-iron distortions is dependent upon the distorting material(s) rotating/moving with the sensor. An example would be mounting the sensor in an aircraft; any materials that are part of the aircraft that exhibit a distorting effect would move as the aircraft and mounted sensor move, and it would generally be possible to compensate for the associated hard- and soft-iron effects. In contrast, it is much more difficult—if not impossible—to compensate for distorting effects exhibited by material external to the aircraft/sensor platform. Thus, it is important to understand not only how compensation may be applied, but also to recognize those conditions under which effective compensation techniques are not possible.
Hard-Iron Distortion. Hard-iron distortion is produced by materials that exhibit a constant, additive field to the earth's magnetic field, thereby generating a constant additive value to the output of each of the magnetometer axes. A speaker magnet, for example, will produce a hard-iron distortion. As long as the orientation and position of the magnet relative to the sensor is constant the field and associated offsets will also be constant. A hard-iron distortion can be visibly identified by an offset of the origin of the ideal circle from (0, 0), as shown in Figure 7.
Figure 7. A graph of magnetometer data displaying hard-iron distortion, exhibited by a constant offset in both X and Y
Compensating for hard-iron distortion is straightforward, accomplished by determining the x and y offsets and then applying these constants directly to the data. It is important to note that tilt compensation must be applied prior to determining hard-iron corrections.
Hard-iron corrections are typically determined by rotating the sensor through a minimum of 360°, then determining the distance from (0, 0) to the center of the circle by identifying the average of the maximum and minimum values for each of the axes, as shown in Equations 1 and 2, respectively,
|α||=||X axis offset|
|xmax||=||Maximum x value|
|xmin||=||Minimum x value|
|β||=||Y axis offset|
|ymax||=||Maximum y value|
|ymin||=||Minimum y value|
These offsets are then subtracted from the raw x and y magnetometer data, thus largely eliminating the hard-iron distortion.
Hard-iron effects are constant regardless of orientation or position of the sensing platform. These constant offsets can be stored once calculated and subtracted from the raw magnetometer data.
Soft-Iron Distortion. Unlike hard-iron distortion where the magnetic field is additive to the earth's field, soft-iron distortion is the result of material that influences, or distorts, a magnetic field—but does not necessarily generate a magnetic field itself, and is therefore not additive. Iron and nickel, for example, will generate a soft-iron distortion. While hard-iron distortion is constant regardless of orientation, the distortion produced by soft-iron materials is dependent upon the orientation of the material relative to the sensor and the magnetic field. Thus, soft-iron distortion cannot be compensated with a simple constant; instead, a more complicated procedure is required.
As shown in Figure 8, a soft-iron distortion is typically exhibited as a perturbation of the ideal circle into an ellipse. Plotting the magnitudes also shows the characteristic two-cycle error.
Figure 8. Soft-iron effect distorting the ideal circle into an elliptical shape (A). The corresponding magnitude (B) illustrates the characteristic two-cycle error where the peaks represent the major axis and the valleys the minor axis
Compensating for soft-iron distortion is more compute-intensive than compensating for hard-iron distortion, and it may be more effective from a cost and efficiency perspective—particularly if designing or implementing an embedded system—to eliminate the soft-iron material(s) from the proximity of the sensor. In many cases this is not an option, and implementing a soft-iron compensation method is required.
To simplify the following discussion we assume that all tilt effects and hard-iron distortions are either not present in the application environment, or that tilt and hard-iron compensation methods have previously been applied. Furthermore, if tilt and hard-iron effects are present, compensation for these distortions must be applied prior to correcting for soft-iron distortions. As such, it is safe to assume that the origin of the ellipse is at (0, 0), as shown in Figure 9, and is exhibiting a rotation of θ degrees from the X axis.
Figure 9. An ellipse generated as a result of soft-iron distortion, centered at (0,0) with rotation θ
Identifying θ in Figure 9 is accomplished by using Equation 3 to calculate the magnitude of the line segment r, followed by Equation 4 to determine θ:
From a computing perspective, and assuming relatively clean data, one method of identifying r is to calculate the magnitude of each data point and then identify the maximum of these computed values; the coordinates of this value will correspond with the major axis. Similarly, the minimum value will correspond to the minor axis, q. Once θ has been identified, the rotation matrix given in Equation 5 is applied to the vector of magnetometer x and y values, v, using Equation 6. (For further discussion on rotation matrices and additional material on the derivation of Equations 5 and 6 please see .)
After the rotation, the major axis of the ellipse will be aligned with the reference frame X axis and the minor axis will be aligned with the Y axis, as shown in Figure 10.
Figure 10. Alignment of the ellipse's major and minor axes with the coordinate system's x and y axes, respectively, following the rotation
Following the rotation, we can now properly scale the major axis such that the ellipse is converted to an approximate circle. The scale factor, σ, is determined using Equation 7, and is the ratio of the length of the major axis to that of the minor axis. Each magnetometer x value is then divided by this scale factor to produce the desired circle.
Once scaling is completed a final rotation must be made to rotate the data back to their original position, thus compensating for the soft-iron distortion. This is achieved by applying Equations 5 and 6, but with a negative θ, to align the ellipse and coordinate system axes.
Substantial error can be present in heading calculations unless adequate detection and calibration methods are applied. The several methods presented here may be used to correct for the tilt, hard-iron, and soft-iron distortion present in data collected via a dual-axis magnetometer. In addition, realizing improved results in heading calculation requires these methods to be applied in proper order of application. Although the discussion and corresponding functions are specific to corrections in 2D or the x-y plane, with the integration of a triaxial magnetometer it is possible to extend these concepts to three dimensions and to correct for distortions present in all axes.
2. "Tilt-Sensing with Kionix MEMS Accelerometers," (PDF) Kionix Corp., NY. Retrieved August 4, 2008 from http://www.kionix.com.
3. Caruso, M.J. "Applications of Magnetic Sensors for Low Cost Compass Systems," Position Location and Navigation Symposium, IEEE 2000, 13-16 March 2000, pp. 177-184.
4. Jack B. Kuipers, Quaternions and Rotation Sequences, a Primer with Applications to Orbits, Aerospace, and Virtual Reality, Princeton University Press, Princeton, 2002.