Using a magnetometer is a common way to obtain a system's heading, particularly in applications that do not have access to GNSS. While seemingly straightforward, using a magnetometer to accurately estimate the heading can actually prove to be quite challenging. The Earth's magnetic field is quite weak and there often exists a number of different error sources that can impact the heading accuracy.
To mathematically model the various error sources that can be present in a set of measurements, a sensor model is often established. Magnetometers are commonly modeled using Equation \ref{eq:magmeasmodel}, where the measurement vector on the left of the equation, $\tilde{m}$, contains the magnetic readings as measured by the magnetometer.
\begin{equation} \label{eq:magmeasmodel} \tilde{\boldsymbol{m}} = S_I^{-1}(\boldsymbol{m}_E + \boldsymbol{m}_{e(t)}) + \boldsymbol{b}_{HI} + \boldsymbol{m}_{i(t)}\end{equation}
In a perfectly clean magnetic environment, this measurement vector would simply be Earth's magnetic field ($\boldsymbol{m}_E$). However, in a majority of real-world applications, the local magnetic field measured by the magnetometer often consists of a combination of Earth's magnetic field and magnetic fields created by nearby objects, known as magnetic disturbances. These magnetic disturbances include objects that are external to the system in the surrounding environment ($\boldsymbol{m}_{e(t)}$) as well as objects that are fixed with respect to the sensor ($\boldsymbol{m}_{i(t)}$). Hard iron distortions ($\boldsymbol{b}_{HI}$) and soft iron distortions ($S_I$) can also be present in the magnetic measurements, which bias and distort Earth's magnetic field.To account for error sources present in the magnetometer measurements, a compensation model can be constructed by rearranging Equation \ref{eq:magmeasmodel}.
\begin{equation} \label{eq:magsensormodel} \boldsymbol{m}_c = S_I(\tilde{\boldsymbol{m}} - \boldsymbol{b}_{HI})\end{equation}
This model is often utilized in what is known as a hard and soft iron (HSI) calibration to remove the impact of hard and soft iron distortions present in the magnetic readings. Hard and soft iron distortions as well as the calibration used to account for them are discussed more in depth in Section 3.6.
Earth's magnetic field is a self-sustaining magnetic field that resembles a magnetic dipole with one end near Earth's geographic North Pole and the other near Earth's geographic South Pole. This magnetic field is characterized by both a strength and direction. While the direction of Earth's magnetic field contains both a horizontal component and a vertical component, magnetic-based heading is calculated from only the horizontal component of the magnetic field as the horizontal component points to the magnetic North Pole of the earth.
The strength of this magnetic field varies across the earth with strengths as low as 0.3 Gauss in South America to over 0.6 Gauss in northern Canada. Although the Earth's magnetic field is relatively stable over time, electric currents in the ionosphere can cause daily alterations which can deflect surface magnetic fields by as much as 1°. Normally daily variations in field strength are on the order of 0.25 mGauss, which would equate to about a 0.03° variation in heading. This small of a change in heading is on the same order of magnitude as the resolution of most MEMS-based magnetometers, so in most cases the Earth's magnetic field can be considered constant with respect to time.
In an ideal scenario, a magnetometer would purely measure Earth's magnetic field and very accurately estimate the heading. Unfortunately, most applications consist of nearby objects that bias and distort Earth's background magnetic field, leading to errors in the calculated heading. These objects are commonly referred to as magnetic disturbances and can be characterized in a few different ways.
Magnetic disturbances can be classified as either a hard iron distortion or a soft iron distortion. Hard iron distortions are created by objects that produce a magnetic field (i.e. speaker, magnet, etc.) and cause a permanent bias in the local magnetic field. On the other hand, soft iron distortions are commonly caused by metals such as nickel and iron (i.e. batteries) and distort and stretch the local magnetic field. Refer to Section 3.6 for more information on hard and soft iron distortions.
Regardless of the type of distortion, magnetic disturbances are considered to be either internal or external disturbances. Internal magnetic disturbances are caused by objects that are fixed with respect to the magnetometer. This would include electronics, other sensors, and structures that are rigidly mounted with respect to the magnetometer. External magnetic disturbances include any objects producing a magnetic field in the surrounding environment that are not fixed with respect to the magnetometer. Things like keys, phones, computers, electronics, office furniture, and vehicles in the surrounding environment that are not rigidly attached to the sensor are common sources of external magnetic disturbances.
Magnetic disturbances can be further characterized based on whether or not they vary over time. Time-invariant disturbances are constant over time and include things like ferrous metals or electronics that are constantly running. On the other hand, time-varying disturbances change their magnetic field over time. Electric motors running at different speeds, such as the rotors on a quad-copter, or powering on and off of electronics are common examples of time varying disturbances.
Internal magnetic disturbances that are non-time varying can be accounted for using a calibration known as a hard & soft iron (HSI) calibration. For more information on an HSI calibration, see Section 3.6. Advanced filtering techniques can be used to mitigate the impact of external disturbances in the environment, but their effectiveness varies by manufacturer and application.
When using a magnetometer, the sensor is often oriented in such a way that the pitch and roll angles are non-zero (in contrast to the use of a handheld magnetic compass). Since the Earth's magnetic field contains both a horizontal and vertical component, a magnetometer will measure a portion of Earth's vertical magnetic field component in each of its axes when tilted. To remove the impact of the vertical component of Earth's magnetic field, a technique known as tilt-compensation must be utilized in which the estimated pitch and roll angles are used. The use of tilt-compensation is critical, as many places around the world (including most of the U.S. and Europe) contain a vertical component of the magnetic field that is stronger than the horizontal component.
The following equations apply tilt-compensation to the magnetometer data and calculate the corrected magnetic heading angle, where $\tilde{m}_x$, $\tilde{m}_y$, and $\tilde{m}_z$ are the measured magnetometer data in the body frame.
\begin{equation} X = \tilde{m}_x\cos{\theta} + \tilde{m}_y\sin{\theta}\sin{\phi} + \tilde{m}_z\sin{\theta}\cos{\phi}\end{equation}
\begin{equation} Y = \tilde{m}_y\cos{\phi} -\tilde{m}_z\sin{\phi}\end{equation}
\begin{equation} \psi_c = \tan^{-1}{\frac{-Y}{X}}\end{equation}
Due to the dependence on pitch and roll, tilt-compensation couples errors in pitch and roll into errors in heading. If a magnetometer is to be used in an application experiencing non-zero pitch and roll angles, an accelerometer or tilt-sensor will be needed at the very least to obtain estimates of the pitch and roll angles. In such applications, a full Attitude and Heading Reference System (AHRS) or Inertial Navigation System (INS) is recommended for use, which calculates the pitch and roll angles and automatically applies tilt-compensation to the magnetic measurements.
Near the equator, pitch and roll errors do not have as much of an impact on the magnetic heading since the inclination angle is small and the majority of the magnetic field is in the horizontal plane. However, for operation near the Earth's magnetic poles, the inclination angle is near 90° and the vertical magnetic field component is much stronger than the horizontal. In this case, the accuracy of the pitch and roll angles becomes much more important in correctly applying tilt-compensation to obtain an accurate magnetic heading. A pitch or roll error of 0.5° equates to about a 1° error in the magnetic heading in the southern part of the U.S. Comparatively, at the Earth's magnetic poles, this same 0.5° pitch or roll error causes a 2.5° error in the heading.