ST AN3192
ST AN3192
Application note
Introduction
This application note describes the method for building a tilt compensated electronic
compass using an LSM303DLH sensor module.
The LSM303DLH is a 5 x 5 x 1 mm with LGA-28L package IC chip that includes a 3D digital
linear acceleration and a 3D digital magnetic sensor. It has a selectable linear acceleration
full scale range of ±2g / ±4g / ±8g and a selectable magnetic field full scale range of ±1.3 /
±1.9 / ±2.5 / ±4.0 / ±4.7 / ±5.6 / ±8.1 gauss. Both the magnetic sensor and the
accelerometer parts can be powered down separately to reduce power consumption.
Sensor measurements can be acquired by a microcontroller through an I2C serial bus
interface.
The key features of the system are:
■ One single chip solution
■ I2C communication interface
■ Tilt compensation
Section 1 describes the basics of the electronic compass. Section 2 presents a typical
hardware connection between the LSM303DLH and a microcontroller and sample code for
sensor data acquisition. Section 3 focuses on the methods of the determination of sensor
calibration parameters. Section 4 shows the methods of lab testing and field testing for
checking the electronic compass performance. Section 5 gives recommendations for
microcontroller firmware implementation when designing a standalone tilt compensated
electronic compass.
1/34
AN3192
Contents
2 LSM303DLH configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1 Typical hardware connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 LSM303DLH accelerometer data acquisition . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 LSM303DLH magnetic sensor data acquisition . . . . . . . . . . . . . . . . . . . . 10
5 Firmware implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2/34
Appendix C Magnetic sensor calibration method . . . . . . . . . . . . . . . . . . . . . . . . 27
C.1 Step 1: Soft-iron effect verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
C.2 Step 2: Hard-iron, soft-iron and scale factor compensation . . . . . . . . . . . 27
C.3 Step 3: Misalignment error compensation. . . . . . . . . . . . . . . . . . . . . . . . . 30
6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
7 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3/34
List of figures
4/34
AN3192
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. In the
northern hemisphere, this field points down. At the equator, it points horizontally and in the
southern hemisphere, it points up. This angle between the earth’s magnetic field and the
horizontal plane is defined as an inclination angle. Another angle between the earth's
magnetic north and geographic north is defined as a declination angle in the range of ± 20º
depending on the geographic location.
A tilt compensated electronic compass system requires a 3-axis magnetic sensor and a 3-
axis accelerometer sensor. The accelerometer is used to measure the tilt angles of pitch
and roll for tilt compensation. And the magnetic sensor is used to measure the earth’s
magnetic field and then to determine the heading angle with respect to the magnetic north.
If the heading with respect to the geographic north is required, the declination angle at the
current geographic location should be compensated to the magnetic heading.
1.1 Definitions
For compass applications in a handheld device such as a cell phone or a PDA, the aircraft
convention is widely used to define the device body coordinates and three attitude angles
pitch, roll and heading as shown in Figure 1.
From Figure 1, the device (or aircraft) body coordinates Xb/Yb/Zb are defined as
forward/right/down based on the right-hand rule.
Three attitude angles are referenced to the local horizontal plane which is perpendicular to
earth’s gravity.
Heading is defined as the angle between the Xb axis and the magnetic north on the
horizontal plane measured in a clockwise direction when viewing from the top of the device
(or aircraft).
Pitch is defined as the angle between the Xb axis and the horizontal plane. When rotating
the device around the Yb axis with the Xb axis moving upwards, pitch is positive and
increasing.
Roll is defined as the angle between the Yb axis and the horizontal plane. When rotating the
device around the Xb axis with the Yb axis moving downwards, roll is positive and
increasing.
5/34
AN3192
(EADING <E
<K
Local earth magnetic field H has a fixed component Hh on the horizontal plane pointing to
the earth’s magnetic north. This component can be measured by the magnetic sensor
sensing axes XM and YM that are named as Xh and Yh. Then the heading angle is calculated
as:
Equation 1
Heading = arctan( Yh / X h )
In Figure 2, when the device body Xb axis is parallel to Hh which is pointing to the magnetic
north, then Xh = max and Yh = 0 so that heading = 0°. Rotating the device clockwise on the
horizontal plane, the heading increases. When Xh = 0 and Yh = min, then heading = 90°.
Keep rotating until Xh = min and Yh = 0, then heading = 180°. And so on. After a full round
360° rotation, the user sees a centered circle if plotting Xh and Yh values coming from the
magnetic sensor measurements.
6/34
]°°
AN3192
$FFHOHURPHWHU
3LWFK ;
;K
<K
< 5ROO
+RUL]RQWDOSODQH
!-V
Equation 2
X h = X M cos Pitch + Z M sin Pitch
Yh = X M sin Roll sin Pitch + YM cos Roll − Z M sin Roll cos Pitch
3LWFK
D[LV
DFFHOHURPHWHU
$QDORJWR
5ROO
GLJLWDO 0LFURFRQWUROOHU
D[LV FRQYHUWHU
+HDGLQJ
PDJQHWLFVHQVRU
!-V
7/34
]°°
AN3192
8/34
AN3192
2 LSM303DLH configuration
/60'/+
The LSM303DLH is an I2C slave and the microcontroller is an I2C master. When designing
the hardware, the user should pay attention to the following:
● Some reserved pins not shown in Figure 5 should be connected to their corresponding
pins according to the LSM303DLH datasheet
● Power supply decoupling ceramic capacitors C3 (10 µF) and C4 (0.1 µF) should be
placed as near as possible to the Vdd pin 6
● Choose a microcontroller that has a built-in I2C controller. If a bit-banging scheme
implemented in the microcontroller's firmware is used, pay attention to the I2C
communication timing specifications
● The +3 V and +1.8 V power supply should be regulated and clean to reduce the noise
In addition, the handheld device may have high current active components, for example, an
RF amplifier circuit. It may also have ferromagnetic metal materials that a magnet would
stick to. These generate magnetic field distortion to the earth’s magnetic field. Even though
the magnetic sensor calibration can compensate these hard-iron and soft-iron distortions, it
is recommended to place the LSM303DLH onto the device PCB as far away as possible
from the above distortions.
9/34
AN3192
10/34
AN3192
{
Write(0x1E, 0x00, 0x14);//set CRA_REG_M register
Write(0x1E, 0x02, 0x00);//set MR_REG_M register
While (1)
{
Temp = Read(0x1E, 0x02); //read MR_REG_M
MR_Data[0] = ReadCurrentAddress(); //read OUT_X_H_M (MSB)
MR_Data[1] = ReadCurrentAddress(); //read OUT_X_L_M (LSB)
MR_Data[2] = ReadCurrentAddress(); //read OUT_Y_H_M (MSB)
MR_Data[3] = ReadCurrentAddress(); //read OUT_Y_L_M (LSB)
MR_Data[4] = ReadCurrentAddress();//read OUT_Z_H_M (MSB)
MR_Data[5] = ReadCurrentAddress(); //read OUT_Z_L_M (LSB)
Mx = (int) (MR_Data[0] << 8) + MR_Data[1];
My = (int) (MR_Data[2] << 8) + MR_Data[3];
Mz = (int) (MR_Data[4] << 8) + MR_Data[5];
}
}
The pointer address of the magnetic sensor of the LSM303DLH has an automatic update
feature. After a read of the MR_REG_M (02h) register, the address pointer automatically
increases 1 to 03h which is the OUT_X_H_M register. After a read of the OUT_X_H_M
register, the address pointer increases 1 to the OUT_X_L_M register. So one function
ReadCurrentAddress() can be used to read 6 bytes of magnetic sensor X-Y-Z data
continuously. Please refer to the LSM303DLH datasheet; Sensor module: 3-axis
accelerometer and 3-axis magnetometer, for more details.
11/34
AN3192
Equation 3
⎡ A x1 ⎤ ⎡1/ A _ SC x 0 0 ⎤ ⎡A x − A _ OS x ⎤
⎢ ⎥ [ ] ⎢ ⎥ ⎢ ⎥
A
⎢ y1 ⎥ = A _ m 3x 3 ⎢ 0 1 / A _ SC y 0 ⎥ ⋅ ⎢A y − A _ OS y ⎥
⎢⎣ A z1 ⎥⎦ ⎢⎣ 0 0 1/ A _ SC z ⎥⎦ ⎢⎣ A z − A _ OS z ⎥⎦
⎡ ACC11 ACC12 ACC13 ⎤ ⎡A x ⎤ ⎡ ACC10 ⎤
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
= ⎢ACC 21 ACC 22 ACC 23 ⎥ ⋅ ⎢A y ⎥ + ⎢ACC 20 ⎥
⎢⎣ACC 31 ACC 32 ACC 33 ⎥⎦ ⎢⎣ A z ⎥⎦ ⎢⎣ACC 30 ⎥⎦
where [A_m] is a 3x3 misalignment matrix between the accelerometer sensing axes and the
device body axes; A_SCi (i = x, y, z) is the scale factor and A_OSi is the offset.
The goal of the accelerometer calibration is to determine 12 parameters from ACC10 to
ACC33 so that with any given raw measurements at arbitrary positions, the normalized
values can be obtained. Therefore, pitch and roll can be calculated by Equation 10 in
Appendix A.
The calibration can be performed at 6 stationary positions as shown in Table 2 in Appendix
A. Collect 5 to 10 second accelerometer raw data at each position with known Ax1, Ay1, and
Az1. Then apply the least square method to obtain the optimal 12 accelerometer calibration
parameters. Please refer to Appendix B for the accelerometer calibration.
In order to get better pitch/roll accuracy, the user can add 4 more stationary positions for
accelerometer calibration. They are: 2 positions with Ax = 0, Ay = ±0.707g, Az = -0.707g, and
2 positions with Ax = ±0.707g, Ay = 0, Az = -0.707g.
12/34
]°°
AN3192
angle of 15.5 º and an inclination angle of 61.4º. Then the Zb component, in Figure 1, is
0.438 gauss. So the Mz raw measurement should be around 950 x 0.438 = +416 LSBs.
The relationship between the normalized Mx1, My1, and Mz1 and the magnetic sensor raw
measurements Mx, My, and Mz can be expressed as,
Equation 4
⎡M x1 ⎤ ⎡1/ M _ SC x 0 0 ⎤ ⎡M x − M _ OS x ⎤
⎢ ⎥ [ ⎢ ⎥ ⎢ ⎥
⎢M y1 ⎥ = M _ m]3 x 3 ⎢ 0 1/ M _ SC y 0 ⎥ ⋅ [M _ si]3 x 3 ⎢M y − M _ OS y ⎥
⎢⎣Mz1 ⎥⎦ ⎢⎣ 0 0 1/ M _ SC z ⎥⎦ ⎢⎣Mz − M _ OS z ⎥⎦
⎡MR11 MR12 MR13 ⎤ ⎡M x − MR10 ⎤
⎢ ⎥ ⎢ ⎥
= ⎢MR 21 MR 22 MR 23 ⎥ ⋅ ⎢M y − MR 20 ⎥
⎢⎣MR 31 MR 32 MR 33 ⎥⎦ ⎢⎣Mz − MR 30 ⎥⎦
where [M_m] is a 3x3 misalignment matrix between the magnetic sensor sensing axes and
the device body axes; M_SCi (i = x, y, z) is the scale factor and M_OSi is the offset caused
by hard-iron distortion; [M_si] is a 3x3 matrix caused by soft-iron distortion.
The goal of the magnetic sensor calibration is to determine the parameters from MR10 to
MR33 so that with any given raw measurements at arbitrary positions, the normalized
values can be obtained. Therefore, the heading can be calculated by Equation 12 and 13 in
Appendix A for tilt compensation.
3.2.1 Terminology
Hard-iron interference magnetic field is normally generated by ferromagnetic materials with
permanent magnetic fields that are part of the handheld device structure. These materials
could be permanent magnets or magnetized iron or steel. They are time invariant. These
unwanted magnetic fields are superimposed on the output of the magnetic sensor
measurements of the earth's magnetic field. The effect of this superposition is to bias the
magnetic sensor outputs. It is described as M_OSi (i = x, y, z) or MR10, MR20 and MR30 in
Equation 4.
A soft-iron interference magnetic field is generated by the items inside the handheld device.
They could be current carrying traces on the PCB or magnetically soft materials. They
generate a time varying magnetic field that is superimposed on the magnetic sensor output
in response to the earth's magnetic field. The effect of the soft-iron distortion is to make a
full round rotation circle become a tilted ellipse. It is described as the [M_si] 3x3 matrix in
Equation 4.
Scale factor error is defined as the mismatch of the sensitivity of the magnetic sensor
sensing axes. Ideally, the 3-axis magnetic sensors that make up the triad are identical. In
reality, however, this may not be the case. Each magnetic sensor channel may have different
sensitivities. The effect of the scale factor error causes the full round rotation circle to
become an ellipse. It is described as M_SCi (i = x, y, z) in Equation 4.
Misalignment error is defined as the angles between the magnetic sensor sensing axes and
the device body axes. When assembling the LSM303DLH in the handheld device, these
small angles always exist and need to be compensated. It is described as [M_m] 3x3 matrix
in Equation 4.
Magnetic sensor calibration can be performed by 3 full round rotations along with device
body axis Zb down, Yb down, and Xb down respectively, at a leveled smooth surface without
a nearby interference magnetic field. They are 2D rotations. The rotation speed should be
13/34
]°°
AN3192
slow in order to collect as many data points as possible. But it does not require constant
rotation speed and an accurate sampling time interval. The full round rotation can be
clockwise or counterclockwise. Collected magnetic sensor raw data of 3 full round rotations
is used to accurately determine the 12 magnetic sensor calibration parameters.
3D random rotations are performed by rotating the handheld device in random directions. If
the handheld device doesn't have hard-iron and soft-iron interference magnetic fields, and
the scale factor of each axis is identical, and the LSM303DLH magnetic sensor's sensing
axes are aligned to the device body axes, then each full round rotation forms a centered
circle with the same radius and the 3D rotations form a centered sphere. However, due to
the hard-iron and soft-iron magnetic field distortions, and the errors of the scale factor and
the misalignment, the centered sphere becomes a shifted, tilted ellipsoid when plotting the
collected magnetic sensor raw data.
The 3 steps for magnetic sensor calibration are presented in Appendix C.
Please note that the magnetic sensor calibration can only compensate the hard-iron and
soft-iron interference magnetic field generated by the handheld device itself. This means
that during full round rotations of the calibration, the hard-iron and soft-iron fields also rotate
with the device.
The electronic compass is sensitive to environmental magnetic interference fields outside of
the handheld device. A single Z-axis gyro chip can be used to aid the compass when
environmental magnetic interference is detected.
14/34
AN3192
After the calibration parameters for the accelerometer and the magnetic sensor of the
LSM303DLH have been determined, it is necessary to check the performance of the
electronic compass. This could be carried out with accurate lab testing and rough field
testing. The expected pitch/roll/heading accuracy is shown in Table 1.
+DQGKHOG
GHYLFH
0DJQHWLF
QRUWKPDUN
:RRGHQ
SODWIRUP
!-V
There should be no strong external interference magnetic field close to the wooden
platform. CRT monitors, power cords, electrical equipment, and metallic frames, sometimes
hidden in the structure below the wooden platform, have to be removed during testing. If the
local magnetic north direction is known by means of a reference magnetometer, a sign can
be placed on a side panel and the handheld device fixed to the top of the platform. An
optical head may be used to align Xb to the mark.
15/34
AN3192
heading output with the known heading angle. The difference should be within ± 2º.
This is the absolute heading accuracy at random position.
● Fix a certain position. Rotate the platform back and forth and then stop at the same
position. The repeatability error also should be within ± 2º.
!-V
16/34
AN3192
17/34
AN3192
5 Firmware implementation
Some microcontrollers may not support floating point operation and are timing critical. In
order to build a standalone tilt compensated electronic compass, the following
recommendations may be helpful:
● Use look-up tables for sin, cos, arcsin, and arctan functions to reduce clock cycles
● Use assembly code to implement signed integer multiplication and division subroutines
to reduce clock cycles
● If some sensor calibration parameters are very small, the user can multiply the whole
accelerometer and magnetic sensor calibration parameter matrix with a big constant
integer, then divide it before the pitch/roll/heading calculation
● Use internal EEPROM to save sensor calibration parameters
● Implement some kind of digital filtering or simple moving average function onto the
sensor raw measurements to reduce the noise level and improve the pitch/roll/heading
accuracy
18/34
AN3192
Assume that the LSM303DLH chip has been installed in a handheld device, such as a cell
phone, or a PDA or simply on a PCB board as shown in Figure 8.
5ROO
=$0
/60'/+
;$0
<E
3LWFK
<$0
+HDGLQJ
+DQGKHOGGHYLFH
=E !-V
Xb, Yb, and Zb are the handheld device body axes with forward-right-down configuration.
XA,M, YA,M, and ZA,M are the accelerometer and magnetic sensor sensing axes,
respectively. Note that the sign of YA,M and ZA,M from the sensor measurements needs to be
reversed to make the sensing axes the same direction as the device body axes.
Pitch/roll/heading angles are referenced to the local horizontal plane which is perpendicular
to the earth's gravity.
● Heading (ψ) or azimuth is defined as the angle with respect to the magnetic north pole.
It is always positive from 0º to +359º when rotating around the Zb axis clockwise top-
view with right-hand rule. If the heading with respect to the geographic north is
required, then the declination angle at the user's current geographic location should be
added or subtracted from the magnetic heading
● Pitch (ρ) is defined as the angle between the Xb axis and the horizontal plane. It goes
from 0º to +90º when rotating around the Yb axis with the Xb axis moving upwards.
When the Xb axis is moving downwards, the pitch angle goes from 0º to -90º
● Roll (γ) is defined as the angle between the Yb axis and the horizontal plane. It goes
from 0º to +90º when rotating around the Xb axis with the Yb axis moving downwards.
When the Yb axis is moving upwards, the roll angle goes from 0º to -90º.
Let Ax, Ay, Az be the accelerometer raw measurement, and Mx, My, Mz the magnetic sensor
raw measurement. Table 2 shows the sign definition of the sensor raw data at 6 stationary
positions. For example, in Figure 8, Xb and Yb are level, Zb is pointing down. Therefore, Ax =
Ay = 0, Az = +1g. Mx and My can be positive (+) or negative (-), while Mz should be positive
if the current location is in the northern hemisphere.
19/34
]°°
AN3192
Zb down 0 0 +1g + or - + or - +
Zb up 0 0 -1g + or - + or - -
Yb down 0 +1g 0 + or - + + or -
Yb up 0 -1g 0 + or - - + or -
Xb down +1g 0 0 + + or - + or -
Xb up -1g 0 0 - + or - + or -
<E
<
< <<
<¶E
= =
=E= = =¶E
!-V
Equation 5
⎡ cos ψ sin ψ 0⎤
⎢ ⎥
R ψ = ⎢− sin ψ cos ψ 0⎥
⎢⎣ 0 0 1⎥⎦
20/34
]°°
AN3192
Equation 6
⎡cos ρ 0 − sin ρ⎤
⎢ ⎥
Rρ = ⎢ 0 1 0 ⎥
⎢⎣ sin ρ 0 cos ρ ⎥⎦
Equation 7
⎡1 0 0 ⎤
⎢ ⎥
R γ = ⎢0 cos γ sin γ ⎥
⎢⎣0 − sin γ cos γ ⎥⎦
Equation 8
⎡X'b ⎤ ⎡X b ⎤
⎢ ⎥ ⎢ ⎥
⎢Y'b ⎥ = R γRρR ψ ⎢ Yb ⎥
⎢⎣ Z'b ⎥⎦ ⎢⎣ Z b ⎥⎦
⎡ cos ρ cos ψ cos ρ sin ψ − sin ρ ⎤ ⎡X b ⎤
⎢ ⎥ ⎢ ⎥
= ⎢cos ψ sin ρ sin γ − cos γ sin ψ cos γ cos ψ + sin ρ sin γ sin ψ cos ρ sin γ ⎥ ⋅ ⎢ Yb ⎥
⎢⎣cos ψ sin ρ cos γ + sin γ sin ψ − sin γ cos ψ + sin ρ cos γ sin ψ cos ρ cos γ ⎥⎦ ⎢⎣ Z b ⎥⎦
Equation 9
⎡ A x1 ⎤ ⎡ cos ρ cos ψ cos ρ sin ψ − sin ρ ⎤ ⎡0⎤
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
A
⎢ y1 ⎥ ⎢ = cos ψ sin ρ sin γ − cos γ sin ψ cos γ cos ψ + sin ρ sin γ sin ψ cos ρ sin γ ⎥ ⋅ ⎢0⎥
⎢⎣ A z1 ⎥⎦ ⎢⎣cos ψ sin ρ cos γ + sin γ sin ψ − sin γ cos ψ + sin ρ cos γ sin ψ cos ρ cos γ ⎥⎦ ⎢⎣ 1⎥⎦
Equation 10
Pitch = ρ = arcsin(− A x1 )
Roll = γ = arcsin(A y1 / cos ρ)
21/34
]°°
AN3192
ȥ +
<E
0\
=EPOINTING INTO PAGE
!-V
If the device rotates from Xb/Yb/Zb to X''b/Y''b/Z''b by roll angle rotation followed by pitch
angle rotation, then
Equation 11
⎡X b ⎤ ⎡X' 'b ⎤ ⎡ cos ρ 0 sin ρ ⎤ ⎡X' 'b ⎤
⎢ ⎥ −1 −1 ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
Y
⎢ b⎥ = R γ R ρ ⎢ Y' ' b⎥ = ⎢ sin γ sin ρ cos γ − sin γ cos ρ ⎥ ⋅ ⎢Y' 'b ⎥
⎢⎣ Z b ⎥⎦ ⎢⎣ Z' 'b ⎥⎦ ⎢⎣− cos γ sin ρ sin γ cos γ cos ρ ⎥⎦ ⎢⎣ Z' 'b ⎥⎦
Let Mx1, My1, and Mz1 be the normalized magnetic sensor measurements after applying
calibration parameters correction into magnetic sensor raw measurements Mx, My, and Mz
at new positions X''b/Y''b/Z''b. Mx, My and Mz which are signed integer in terms of LSBs,
while Mx1, My1, and Mz1 are floating point values less than 1 in terms of the magnetic field
strength, and the square root of the sum squared values should be equal to 1 when the
there is no external interference magnetic field. Then from Equation 11, tilt compensated
magnetic sensor measurements Mx2, My2, and Mz2 can be obtained as:
Equation 12
M x 2 = M x1 cos ρ + Mz1 sin ρ
M y 2 = M x1 sin γ sin ρ + M y1 cos γ − Mz1 sin γ cos ρ
Mz 2 = −M x1 cos γ sin ρ + M y1 sin γ + Mz1 cos γ cos ρ
22/34
]°°
AN3192
Therefore,
Equation 13
⎛M ⎞
Heading = ψ = arctan⎜ y 2 ⎟ for M x2 > 0 and M y2 >= 0
⎝ M x 2 ⎠
⎛M ⎞
= 180° + arctan⎜ y 2 ⎟ for M x2 < 0
⎝ M x 2 ⎠
⎛ My2 ⎞
= 360° + arctan⎜ ⎟ for M x2 > 0 and M y2 <= 0
⎝ M x 2 ⎠
= 90° for M x2 = 0 and M y2 < 0
= 270° for M x2 = 0 and M y2 > 0
23/34
]°°
AN3192
Equation 14
⎡ ACC11 ACC 21 ACC 31 ⎤
⎢ ⎥
[A x1 ] [
A y1 A z1 = A x Ay Az ]
1⋅⎢
ACC12
⎢ACC13
ACC 22
ACC 23
ACC 32 ⎥
ACC 33 ⎥
⎢ ⎥
⎣ACC10 ACC 20 ACC 30 ⎦
Or
Equation 15
Y = w⋅X
Where,
Matrix X is the 12 calibration parameters that need to be determined
Matrix w is sensor raw data LSBs collected at 6 stationary positions
Matrix Y is the known normalized earth gravity vector
For example,
● At Zb down position (P1 position), [A x1 A y1 A z1 ] = [0 0 1] and assume that at Zb down
position, n1 sets of accelerometer raw data Ax, Ay, and Az have been collected. Then,
Equation 16
Y1 = [0 0 1]n1x 3
[
w1 = A xP1 A yP1 A zP1 1 n1x 4 ]
Where,
The matrix Y1 has the same row of [ 0 0 1 ]
The matrix W1 contains raw data in the format of LSBs
● At Zb up position (P2 position), [A x1 A y1 A z1 ] = [0 0 − 1] and assume that at Zb up
position, n2 sets of accelerometer raw data Ax, Ay, and Az have been collected. Then,
Equation 17
Y2 = [0 0 − 1]n2 x 3
[
w 2 = A xP2 A yP2 ]
A zP2 1 n2 x 4
Equation 18
Y3 = [0 1 0]n3 x 3
[
w 3 = A xP3 A yP3 ]
A zP3 1 n3 x 4
24/34
]°°
AN3192
Equation 19
Y4 = [0 − 1 0]n4 x 4
[
w 4 = A xP4 A yP4 ]
A XP4 1 n4 x 4
Equation 20
Y5 = [1 0 0]n5 x 3
[
w 5 = A xP5 A yP5 ]
A zP5 1 n5 x 4
Equation 21
Y6 = [− 1 0 0]n6 x 3
[
w 6 = A xP6 A yP6 ]
A zP6 1 n6 x 4
Equation 22
Ynx 3 = w nx 4 ⋅ X 4 x 3
Where,
Equation 23
⎡Y1 ⎤
⎢ ⎥
⎢Y2 ⎥
⎢Y3 ⎥
Y=⎢ ⎥
⎢Y4 ⎥
⎢Y ⎥
⎢ 5⎥
⎢⎣Y6 ⎦⎥ nx 3
⎡w1 ⎤
⎢ ⎥
⎢w 2 ⎥
⎢w 3 ⎥
w=⎢ ⎥
⎢w 4 ⎥
⎢w ⎥
⎢ 5⎥
⎢⎣w 6 ⎥⎦ nx 4
Therefore, the calibration parameter matrix X can be determined by the least square method
as,
25/34
]°°
AN3192
Equation 24
X = wT ⋅ w[ ]−1
⋅ wT ⋅ Y
Where,
wT means matrix transpose
[w T
⋅w ]
−1 means matrix inverse
26/34
]°°
AN3192
Equation 25
where:
● x0, y0, z0 are the offsets M_OSi (i = x, y, z) caused by hard-iron distortion
● x, y, z are magnetic sensor raw data Mx, My and Mz
● a, b, c are the semi-axes lengths,
● d, e, f are cross axis effect to make the ellipsoid tilted,
● R is a constant of the earth’s magnetic field strength.
27/34
]°°
AN3192
If there is no soft-iron distortion inside the device, or the soft-iron effect is very small and can
be ignored, then the ellipsoid from 3D rotations is not tilted. So the soft-iron matrix [M_si] is
a 3x3 identity matrix and Equation 25 can be simplified as:
Equation 26
(x − x 0)2 + (y − y0)2 + (z − z0)2 = R2
2 2 2
a b c
Therefore, the least square fitting ellipsoid method can be used to discover the parameters
of M_SCi, M_OSi (i = x, y, z) and [M_si]. The magnetic sensor raw data used here could be
three 2D full round rotations, or 3D rotations, or both.
Applying these parameters to the collected 3D rotations data and three 2D full round
rotations, the plot is shown in Figure 12. Now the shifted tilted ellipsoid becomes a centered
unit sphere.
Let's assume there is no soft-iron distortion. The soft-iron matrix [M_si] is a 3x3 identity
matrix. Then Equation 26 can be rewritten as:
Equation 27
⎡ 2x 0 ⎤
⎢ a2 ⎥
⎢ 2y 0 ⎥
⎢ b 2
⎥
⎢ a 2 ⎥
⎢ 2
2z 0 ⎥
⎢ ⎥
[ ]
c
x2 = x y z − y2 − z2 1⋅⎢ a 2 ⎥
⎢ 2 ⎥
⎢ b ⎥
⎢ a 2
⎥
⎢ 2 ⎥
⎢ c ⎥
2 2
⎢a 2R 2 − x 2 − a y 2 − a z 2 ⎥
⎢⎣ 0
b2
0
c 2 ⎥⎦
0
After three 2D full round rotations magnetic sensor raw data have been collected, it is
possible to combine Mx, My, and Mz as column vector and row vector. Then Equation 27
becomes,
28/34
]°°
AN3192
Equation 28
w nx1 = [H]nx 6 ⋅ X 6 x1
The least square method can be applied to determine the parameters X vector as:
Equation 29
[ ] −1
X = H T H HT ⋅ w
Then,
Equation 30
M _ OS x = x 0 = X(1) / 2
M _ OS y = y 0 = X(2) /(2 ⋅ X(4))
M _ OS z = z 0 = X(3) /(2 ⋅ X(5))
And,
Equation 31
A = a 2R 2 = X(6) + x 20 + X(4) ⋅ y 20 + X(5) ⋅ z 20
B = A / X(4)
C = A / X(5)
Let,
Equation 32
xx = M x − M _ OS x
yy = M y − M _ OS y
zz = Mz − M _ OS z
Equation 33
xx 2 yy 2 zz 2
+ + =1
A B C
Therefore,
Equation 34
M _ SC x = A
M _ SC y = B
M _ SC z = C
29/34
]°°
AN3192
Let,
Equation 35
xxx = xx / M _ SC x
yyy = yy / M _ SC y
zzz = zz / M _ SC z
Then,
Equation 36
xxx 2 + yyy 2 + zzz 2 = 1
Up to now, M_SCi (i = x, y, z), the scale factor, M_OSi, the offset caused by hard-iron
distortion, and [M_si], 3x3 matrix caused by soft-iron distortion have been determined.
Applying these parameters to three 2D full round rotations and 3D random rotations, the 3D
random rotations become a centered unit sphere with three 2D full round rotations sitting on
its surface. Similarly, the least square method can be used to determine the [M_si] 3x3
matrix when there is soft-iron distortion.
Equation 37
Hmx 3 = [xxx z yyy z zzz z ]
be the Zb down rotation circle data after scale factor, hard-iron and soft-iron correction.
Equation 38
w mx1 = (xxx 2
z + yyy 2z + zzz 2z )
Then,
Equation 39
[ ] −1
X 3 x1 = H T H H T ⋅ w
30/34
]°°
AN3192
Equation 40
Rz = X / (X(1) 2
+ X(2)2 + X(3)2 )
Similarly, the normalized rotation vectors Rx and Ry for Xb down rotation and Yb down
rotation can be found. Then the final misalignment compensation matrix is,
Equation 41
M _ m3x 3 = R x[ Ry Rz ]
Figure 13. After misalignment compensation
31/34
]°°
AN3192
6 References
32/34
AN3192
7 Revision history
33/34
AN3192
34/34