Accelerometer and Magnetometer Joint Calibration and Axes Alignment

In this work, we propose an algorithm for joint calibration and axes alignment of a 3-axis accelerometer and a 3-axis magnetometer. The proposed algorithm applies when the two sensors are fixed on the same rigid platform. It achieves accurate calibration without requiring any external piece of equipment like a turntable for the accelerometer or Gauss magnetic chamber and Maxwell coils setup for the magnetometer. The efficiency and accuracy of the proposed algorithm are evaluated using experimental data.


Introduction
Accelerometers and magnetometers are typically combined in a wide range of application fields including navigation [1], attitude estimation [2], image stabilization [3], and others. In many high-end applications, expensive, factory-calibrated sensors are used in order to achieve high accuracy. However, in applications where cost is of major importance, low-cost sensors, typically in integrated form, are preferred.
Micro-Electro-Mechanical (MEMS) accelerometers are typically used in portable, commercial applications as they combine extremely small size with low cost. However, MEMS accelerometers lack in measurement accuracy due to the inherent imperfections of the MEMS fabrication process [4]. Thus, in applications where accuracy is important, a calibration procedure compensating for the most part of their error is required.
Similarly, low-cost magnetometers suffer from large measurement errors. Except for the common sensor errors, caused by manufacturing imperfections (bias, axes misalignment, etc.), the measured magnetic field is also strongly distorted by nearby materials attached to the sensor's reference frame.
More specifically, the distortion due to magnetic materials in the vicinity of the sensor is called hard-iron distortion and causes a permanent bias to the sensor's output. In addition, ferromagnetic materials attached to the sensor's platform (with the same reference frame) alter the existing magnetic field causing the so-called soft-iron distortion.
Both hard-iron and soft-iron distortions are commonly caused by surrounding electronic components and materials used in the sensor's enclosure and, in many cases, they are the dominant error source of magnetic sensors. Thus, even when a factory-calibrated magnetometer is embedded to a device, a calibration procedure to compensate for such distortions is required.
Factory calibration or standard after-production calibration techniques require using expensive equipment like a turntable for the accelerometer or Gauss magnetic chamber and Maxwell coils setup for the magnetometer. Using such a calibration method would significantly raise the sensor's cost and thus is forbidden. In that sense, a calibration procedure that requires no special piece of equipment is needed when low-cost sensors are concerned. For 3-axis accelerometer calibration, the constant magnitude of the gravity is most commonly used as reference. More specifically, many authors exploit the fact that the measured specific force's magnitude should be constant and independent of the sensor's orientation while it is still. Using this fact, they formulate the sensor's calibration, either as an optimization problem [5][6][7][8] or as an estimation one [9].
For 3-axis magnetometer calibration, the constant magnitude of the earth's magnetic field is exploited. Specifically, if the sensor is placed away of magnetic disturbances, the magnitude of the measured magnetic field should be locally constant, and independent of the sensor's orientation. Similarly to the accelerometer's case, the calibration parameters are calculated by solving a minimization [8,[10][11][12][13][14] or an estimation [15] problem.
In many applications such as navigation and attitude estimation, the measurements of an accelerometer and a magnetometer are combined. This gives rise to the need of alignment between their sensitivity axes. The authors in [15,16] use a gyroscope to align the axes of an accelerometer and a magnetometer. The authors in [17,18] exploit the fact that the measured magnetic inclination angle should be locally constant across different measurements when the accelerometer is still and the magnetometer is away of magnetic disturbances in order to align the axes of the two sensors.
In this work, we propose an algorithm for joint calibration and axes alignment of a 3-axis accelerometer and a 3-axis magnetometer. The proposed algorithm provides accurate calibration and axes alignment without requiring any special piece of equipment or external reference. Its performance and accuracy are evaluated through a series of experimental measurements using a low-cost accelerometer and a low-cost magnetometer.
This paper is organized as follows. In Section 2, the accelerometer's and magnetometer's error sources and measurement models are presented. The proposed calibration algorithm is described in Section 3. In Section 4, the measurement platform developed for the experimental purposes is described. Finally, evaluation of the proposed algorithm using experimental data and conclusions are presented in Sections 5 and 6, respectively.

Accelerometer's and Magnetometer's Error Characteristics and Measurement Model
In this section, the dominant error sources of accelerometers and magnetometers are presented. Then, a model that relates the sensors' measurements with the true value of the specific force and the magnetic field is presented.

Common Accelerometer's and Magnetometer's Error Sources
There are several types of inertial and magnetic sensors based on different operation principles and manufacturing technologies. Although the measurement accuracy varies significantly between different types of sensors, the basic error sources are the same for both inertial and magnetic sensors.
• Bias or offset is a constant error exhibited by all inertial and magnetic sensors. In most cases, it is the dominant term in the overall error of the sensor.
• Scale-factor error is the deviation of the input-output gain from unity.
• Cross-coupling error is caused by the nonorthogonality of the sensor's sensitivity axes due to manufacturing imperfections.
• Random noise is the nondeterministic error caused by both the mechanical and electronic structures of the sensors.

Accelerometer's Measurement Model
The measurement of an accelerometer is modeled as [4,19] y where y a is the 3 × 1 measurement vector, f is the 3 × 1 true specific force vector, T s f is the 3 × 3 diagonal matrix representing the scale-factor error, T cc is the 3 × 3 matrix representing the cross-coupling error, h a is the 3 × 1 accelerometer's bias vector, and ε represents the 3 × 1 random noise vector. Setting T a I 3 + T s f + T cc , where I 3 is the 3 × 3 identity matrix, (1) can be written as

Magnetometer's Measurement Model
The measurement of a magnetometer is modeled as [11,14,17,20] where y m is the 3 × 1 measurement vector, m is the 3 × 1 true magnetic field vector, T s f denotes the 3 × 3 diagonal matrix representing the scale-factor error, T cc is the 3 × 3 matrix representing the cross-coupling error, T si is the 3 × 3 matrix modeling the soft-iron distortion, h b is the 3 × 1 magnetometer's bias vector, h hi is the 3 × 1 bias vector due to hard-iron distortion, and ε denotes the 3 × 1 random error vector. Setting T m T s f T cc T si and h m T s f T cc h si + h b , the magnetometer's measurement model becomes

The Proposed Algorithm
In this work, we propose an algorithm for joint calibration and axes alignment of a 3-axis accelerometer and 3-axis magnetometer. The proposed algorithm is based on the modification of a popular calibration approach [11,14], introduced in [21].

Joint Accelerometer and Magnetometer Calibration
For the accelerometer's and magnetometer's calibration, we exploit the constant magnitude of the gravity and the earth's magnetic field, respectively. Assuming a set of N accelerometer's (y a 1 , y a 2 , . . . , y a N ) and magnetometer's (y m 1 , y m 2 , . . . , y m N ) measurements , from (2) and (4) we have y a k = T a f k + h a + ε and y m k = T m m k + h m + ε, respectively, for k = 1, 2, ..., N. We define the cost function J am , capturing the total measurement error of both sensors and we form the optimization problem below: where λ a and λ m are positive constants. They should be selected to balance the contribution of the two summands. All norms in this work are Euclidean norms unless it is stated otherwise. In (6), without loss of generality, we assume the magnitude of the gravity and of the magnetic field are both one. Note that in (5), both T a and f k for the accelerometer and T m and m k for the magnetometer are unknowns and therefore (5) is a quartic cost function as it involves the square of their product. In order to get a more computationally efficient optimization problem, we apply the modification we introduced in [21]. More specifically, we multiply the accelerometer's and magnetometer's measurement models by T a −1 and T m −1 , respectively. By doing so, the cost function (5) transforms as follows: where Note that (7) is a quadratic cost function.

Accelerometer's and Magnetometer's Axes Alignment
In this section, we modify the optimization problem of (8) to also account for the alignment between the sensor's sensitivity axes. To do so, we derive a rotation matrix R ∈ SO(3) which rotates the magnetometer's axes (x, y, z) into the accelerometer's axes (x, y, z). To that purpose, we exploit the fact that the measured inclination angle should be constant and independent of the sensor's orientation.
Magnetic inclination is the angle between the horizontal plane and the Earth's magnetic field lines, as shown in Figure 1. It can be defined by the normalized inner product of the gravity and the magnetic field vectors as where δ is the magnetic inclination angle, f is the gravity vector, and m is the magnetic field vector.

Magnetic Needle
Horizontal Plane

Figure 1. Magnetic inclination
The inclination angle can be calculated using the measurements of a 3-axis accelerometer and a 3-axis magnetometer. For this to be possible however, both sensors must be individually calibrated and their sensitivity axes must be aligned.
Assuming N measurements of a calibrated accelerometer and a calibrated magnetometer, fixed on the same rigid platform, we write the cost function which implicitly captures the misalignment error between the two sensors, Note that Rm k = m k because R ∈ SO(3). Then, we express the axes alignment between the two sensors as the following optimization problem: where δ is the magnetic inclination angle, f k and m k are the accelerometer and magnetometer measurements, respectively, and R is a rotation matrix that rotates the magnetometer's axes into the accelerometer's axes. Both inclination angle δ and rotation matrix R are unknowns.

Joint Accelerometer's and Magnetometer's Calibration with Simultaneous Axes Alignment
Using (7) and (10), we formulate the following cost function: The joint calibration and axes alignment of a 3-axis accelerometer and a 3-axis magnetometer is formed as the following optimization problem: In order to solve (13), we write the corresponding unconstrained cost-plus-penalty function: where λ a , λ an , λ m , λ mn , λ al , and λ R are positive constants and d = sin(δ). • F denotes the Frobenius norm of a matrix [22] and vec(•) denotes the vector form of a matrix. Note that the term (14) ensures that the resulting R matrix is in SO (3), i.e., it is an orthogonal matrix with a determinant equal to one.
The cost-plus-penalty function in (14) is minimized using the gradient descent method. To that purpose, we define F = [ f 1 , f 2 , ..., f N ] and M = [m 1 , m 2 , ..., m N ]. The gradient of (14) is derived as follows: where where ρ = 1, 2, ..., N and ⊗ denotes the Kronecker's product [22]. A good initial estimate of the unknowns is required to ensure the algorithm's convergence. For the accelerometer, the calibration matrix is initialized as the identity matrix and the offset vector as the zero vector. For the magnetometer, the linear least-squares problem proposed in [11] is used to find an initial estimate of the calibration parameters. The inclination angle is initialized according to the World Magnetic Model (WMM) while the axes alignment matrix R is initialized as the identity matrix.

Measurement Platform
A measurement platform based on low-cost inertial and magnetic sensors was designed and built for experimental purposes. The designed system embeds the STMicroelectronics LSM9DS1 system-in-package (SiP), which contains a 3-axis accelerometer, a 3-axis gyroscope, and a 3-axis magnetometer. The data are handled by a 32-bit microcontroller (STMicroelectronics STM32F746) and stored in flash memory (temporarily) and on an SD card (permanently). The system is powered by a standard 3.7 V Li-ion battery. A system-level diagram and a picture of the implemented measurement platform are shown in Figure 2.  Note that during the experimental measurements, the range of the accelerometer and the magnetometer were set to ±16 g and ±16 Gauss, respectively. In addition, the accelerometer's output data rate was set to 238 Hz while the magnetometer's output data rate was set to 80 Hz.

Experimental Results and Algorithm's Performance
For experimental purposes, we used the measurement platform of Section 4 to record three different calibration datasets. To do so, the measurement platform was placed in 12 different orientations according to the 12-step accelerometer and magnetometer calibration procedure introduced in [8]. The calibration procedure was repeated three times, and three different calibration datasets were recorded. The proposed algorithm was applied to all calibration datasets. Its convergence and accuracy are discussed below.

Algorithm's Convergence
The convergence of the proposed algorithm, evaluated using the cost function J(x), is shown in Figure 3. The goal of the calibration is to derive a vector x such that J(x) becomes zero. However, in practical applications, instead of becoming zero, it converges to a very small value. This happens due to the sensor's nonlinearities and noise, which are not captured by the linear models (2) and (4).
As shown in Figure 3, J(x) converges to a sufficiently small value for all three datasets in a way that appears to be monotonic. Note that the cost function value evolves differently with the iteration step for every one of the three datasets. This is expected and relates with the calibration procedure used. More specifically, to record each calibration dataset, the measurements platform was placed in 12 different still orientations as recommended in [8]. As the placement of the sensor was done by hand, by three different persons, the orientation deviations (even up to 20 degrees in Euler angle) resulted in different datasets which implied the different evolutions of the cost function value.

Calibration Parameters
Applying the proposed calibration algorithm to every dataset, we calculated the calibration parameters for the two sensors (T a , h a and T m , h m for the accelerometer and the magnetometer, respectively) and the axes alignment matrix R. A brief analysis of the form and numerical values of the calculated parameters is presented below.
The calculated calibration matrix T a and offset vector h a for the accelerometer are In the magnetometer's case, the resulting calibration matrix T m has a more complicated form as it also incorporates the soft-iron distortion compensation. The values of the diagonal elements of T m are very close to the required scaling factors for the magnitude normalization (note that the magnitude of the magnetic field at the place of the experiment is about 0.42 Gauss, according to WMM.) and the nondiagonal ones have small absolute values. This indicates that only weak soft-iron distortion is present. In the same vein, the offset vector h m incorporates both the sensor's offset and the hard-iron distortion compensation. As expected, R is close to identity matrix. The total axes misalignment between the magnetometer and the accelerometer, expressed in principal rotation parameters, is about 6.7 • .

Calibration Accuracy
The proposed algorithm exploits the constant magnitude of gravity (for accelerometer's calibration) and the earth's magnetic field (for magnetometer's calibration). Thus, when both sensors are calibrated, the measured magnitude of the specific force and the magnetic field should be constant, independent of the sensor's orientation. In Figure 4, the accelerometer's magnitude while the sensor's platform is placed in several different still positions is presented. Similarly, the magnitude of the raw and calibrated magnetometer's measurements while the sensors' platform is randomly rotated by hand is shown in Figure 5.
As seen in Figures 4 and 5, the magnitude of the calibrated measurements is constant and equal to one, independent of the sensor's orientation, indicating the accurate calibration of both sensors.
As far as the axes alignment is concerned, except from the rotation matrix R, the algorithm also calculates the inclination angle δ. Given that the calibration took place away from any magnetic disturbance (and thus, the measured magnetic field corresponds to the earth's magnetic field), the measured inclination angle should be close to that predicted by WMM. The inclination angle calculated for each dataset is presented in Table 3.
The experimental calibration measurements took place inside the campus of the National Technical University of Athens. According to WMM, the magnetic inclination angle at the location of the experiment is 54.6025 • . The small deviation of the measured inclination angles from that predicted by WMM indicates the accuracy of the calibration algorithm.

Conclusions
In this work, we presented an algorithm for joint calibration and axes alignment of a 3-axis accelerometer and a 3-axis magnetometer. The proposed algorithm applies when the two sensors are fixed on the same rigid platform. It calibrates both sensors and aligns their sensitivity axes without requiring any special piece of equipment or external references. Experimental results demonstrate the algorithm's convergence and accuracy.