cd00269797

AN3192
Application note
Using LSM303DLH for a tilt compensated electronic compass
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.
August 2010
Doc ID 17353 Rev 1
1/34
www.st.com
Contents
AN3192
Contents
1
2
3
Electronic compass basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1
Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2
Heading calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3
Tilt compensation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4
Electronic compass system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5
Getting started with LSM303DLH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
LSM303DLH configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1
Typical hardware connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2
LSM303DLH accelerometer data acquisition . . . . . . . . . . . . . . . . . . . . . . . 9
2.3
LSM303DLH magnetic sensor data acquisition . . . . . . . . . . . . . . . . . . . . 10
Calibrating the LSM303DLH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.1
LSM303DLH accelerometer calibration . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.2
LSM303DLH magnetic sensor calibration . . . . . . . . . . . . . . . . . . . . . . . . 12
3.2.1
4
Testing the electronic compass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.1
4.2
5
Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Lab testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.1.1
Absolute heading testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.1.2
Tilt compensation testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Field testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.1
Relative heading testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.2.2
Tilt compensation testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Firmware implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Appendix A LSM303DLH pitch/roll/heading calculation . . . . . . . . . . . . . . . . . . . 19
A.1
Pitch/roll calculation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
A.2
Heading calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Appendix B Accelerometer calibration method. . . . . . . . . . . . . . . . . . . . . . . . . . 24
2/34
Doc ID 17353 Rev 1
AN3192
Contents
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
Doc ID 17353 Rev 1
3/34
List of figures
AN3192
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
Figure 12.
Figure 13.
4/34
Body coordinates and attitude angles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Heading calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Handheld device at tilted position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Block diagram of electronic compass system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Typical hardware connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Lab testing setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Field testing setup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Electronic compass coordinate system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Rotation procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Heading calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3D rotations plus three 2D full round rotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
After hard-iron, soft-iron, and scale factor compensation . . . . . . . . . . . . . . . . . . . . . . . . . . 28
After misalignment compensation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Doc ID 17353 Rev 1
AN3192
1
Electronic compass basics
Electronic compass basics
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 3axis 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.
Figure 1.
Body coordinates and attitude angles
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.
Doc ID 17353 Rev 1
5/34
Electronic compass basics
1.2
AN3192
Heading calculation
When the device is at a leveled position, pitch and roll angles are 0°. Then the heading
angle can be determined as shown in Figure 2.
Figure 2.
Heading calculation
;E
;K
+K
(ORIZONTALCOMPONENTOF+
(EADING
<E
<K
=E + ,OCALEARTHMAGNETICFIELD
!-V
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.
1.3
Tilt compensation
If the handheld device is tilted, then the pitch and roll angles are not equal to 0° as shown in
Figure 3, where the pitch and roll can be measured by a 3-axis accelerometer. Therefore,
the magnetic sensor measurements XM, YM, and ZM need to be compensated to obtain Xh
and Yh as shown in Equation 2. And then apply Equation 1 for the heading calculation.
6/34
Doc ID 17353 Rev 1
AN3192
Electronic compass basics
Figure 3.
Handheld device at tilted position
$FFHOHURPHWHU
3LWFK
;
;K
+RUL]RQWDOSODQH
<
5ROO
<K
!-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
Where, XM, YM, and ZM are magnetic sensor measurements.
1.4
Electronic compass system
Figure 4 below shows the block diagram of an electronic compass system. A microcontroller
(MCU) is used to collect the 3-axis accelerometer raw data for the pitch and roll calculation
and collect the 3-axis magnetic sensor raw data for the heading calculation. The following is
the procedure for building a working electronic compass system.
●
Hardware design to make sure the MCU can get clean raw data from the accelerometer
and the magnetic sensor
●
Accelerometer calibration to obtain parameters to convert accelerometer raw data to
normalized values for pitch and roll calculation
●
Magnetic sensor calibration to obtain parameters to convert magnetic sensor raw data
to normalized values for the heading calculation
●
Test the performance of the electronic compass system.
Figure 4.
Block diagram of electronic compass system
3LWFK
D[LV
DFFHOHURPHWHU
D[LV
PDJQHWLFVHQVRU
$QDORJWR
GLJLWDO
FRQYHUWHU
5ROO
0LFURFRQWUROOHU
+HDGLQJ
!-V
Doc ID 17353 Rev 1
7/34
Electronic compass basics
1.5
AN3192
Getting started with LSM303DLH
The LSM303DLH from STMicroelectronics is a 6D sensor module that contains a 3D
accelerometer and a 3D magnetic sensor. It has an I2C digital interface so that the analog to
digital converter is avoided. The MCU can collect 6D sensor data directly through the I2C
interface.
After understanding the electronic compass basics, it is time to get started on building a
working electronic compass system based on the LSM303DLH 6D sensor module. The
detailed definition and pitch/roll/heading calculation are described in Appendix A.
8/34
Doc ID 17353 Rev 1
AN3192
LSM303DLH configuration
2
LSM303DLH configuration
2.1
Typical hardware connection
Figure 5 shows a typical hardware connection between the LSM303DLH and a
microcontroller. The +3 V power supply is used to power the LSM303DLH and the +1.8 V
power supply is used for digital IO lines.
Figure 5.
Typical hardware connection
9
&
ȝ)
9
9GG
5HVHUYHG
.
9&&
6&/B0
6&/
6&/B$
&
ȝ)
6'$B0
5HVHUYHG
&
ȝ)
.
6(7
6'$B$
6'$
9GGB,2B$
9GGBGLJB0
6(7
5HVHUYHG
&
'5'<B0
6$B$
,17
,17
*1'
9
0LFUR
FRQWUROOHU
&
ȝ)
966
/60'/+
,&6ODYH
,&0DVWHU
!-V
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.
2.2
LSM303DLH accelerometer data acquisition
SA0_A pin 4 in Figure 5 is tied to ground. So the 7-bit I2C slave address for the
accelerometer, accordingly, is 0011000b or 0x18. For electronic compass applications, a full
scale range of ±2 gauss and bandwidth of 10 Hz should be sufficient.
Doc ID 17353 Rev 1
9/34
LSM303DLH configuration
AN3192
After power-on of the LSM303DLH, two registers, CTRL_REG1_A (20h) and
CTRL_REG4_A (23h) need to be configured. Write 0x27 to the CTRL_REG1_A register to
bring the accelerometer into normal operation mode with ODR 50Hz. Write 0x40 to the
CTRL_REG4_A register to keep a full scale range ±2 gauss in continuous data update
mode and change the little-endian to a big-endian structure.
The following sample C code shows how to acquire accelerometer data:
unsigned char ACC_Data[6];
int Ax, Ay, Az;
void main(void)
{
Write(0x18, 0x20, 0x27);//set CTRL_REG1_A register
Write(0x18, 0x23, 0x40);//set CTRL_REG4_A register
While (1)
{
ACC_Data[0] = Read(0x18, 0x28);//read OUT_X_L_A (MSB)
ACC_Data[1] = Read(0x18, 0x29);//read OUT_X_H_A (LSB)
ACC_Data[2] = Read(0x18, 0x2A);//read OUT_Y_L_A (MSB)
ACC_Data[3] = Read(0x18, 0x2B);//read OUT_Y_H_A (LSB)
ACC_Data[4] = Read(0x18, 0x2C);//read OUT_Z_L_A (MSB)
ACC_Data[5] = Read(0x18, 0x2D);//read OUT_Z_H_A (LSB)
Ax = (int) (ACC_Data[0] << 8) + ACC_Data[1];
Ay = (int) (ACC_Data[2] << 8) + ACC_Data[3];
Az = (int) (ACC_Data[4] << 8) + ACC_Data[5];
}
}
2.3
LSM303DLH magnetic sensor data acquisition
The 7-bit I2C slave address for the magnetic sensor is 0011110b or 0x1E. For electronic
compass applications, a full scale range of ±1.3 gauss and ODR at 30Hz should be
sufficient.
After power-on of the LSM303DLH, two registers CRA_REG_M (00h) and MR_REG_M
(02h), need to be configured. Write 0x14 to the CRA_REG_M register to change the ODR
from 15 Hz to 30 Hz. Write 0x00 to the MR_REG_M register to put the magnetic sensor into
continuous mode from sleep mode.
The following sample C code shows how to acquire magnetic sensor data:
unsigned char temp, MR_Data[6];
int Mx, My, Mz;
void main(void)
10/34
Doc ID 17353 Rev 1
AN3192
LSM303DLH configuration
{
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.
Doc ID 17353 Rev 1
11/34
Calibrating the LSM303DLH
AN3192
3
Calibrating the LSM303DLH
3.1
LSM303DLH accelerometer calibration
All ST MEMS accelerometers are factory calibrated, allowing the user to avoid any further
calibration for most of the applications now present in the market. However, to reach a
heading accuracy of below 2°, an easy calibration procedure is hereafter described. After
the LSM303DLH is installed in the handheld device, it is necessary to calibrate the
accelerometer part again at the handheld device's manufacturers in order to determine the
offset, the scale factor, and the misalignment matrix with respect to the device body axes
Xb/Yb/Zb. After the device is released to the market, end users don't need to perform further
accelerometer calibration in field.
The relationship between the normalized Ax1, Ay1, and Az1 and the accelerometer raw
measurements Ax, Ay, and Az can be expressed as,
Equation 3
0
0
⎡ A x1 ⎤
⎡1/ A _ SC x
⎤ ⎡A x − A _ OS x ⎤
⎢
⎥ [
⎢
⎥ ⎢
⎥
]
=
A
A
_
m
0
1
/
A
_
SC
0
y
3x 3 ⎢
⎢ y1 ⎥
⎥ ⋅ ⎢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.
3.2
LSM303DLH magnetic sensor calibration
LSM303DLH has a magnetic field resolution of 8 mGauss at Vdd = +3 V. And the average
magnitude of the horizontal magnetic field component is in about the 200 mGauss range
(more at the equator, less near the magnetic poles). Therefore, the expected heading
accuracy is about 2.3° [=arctan (8/200)].
At a full scale range of ±1.3 gauss, the sensitivity of the LSM303DLH's magnetic sensor is
1055 LSB/gauss for the X/Y axis and 950 LSB/gauss for the Z axis. For example in San
Francisco, California, the earth’s magnetic field strength is 0.49932 gauss with a declination
12/34
Doc ID 17353 Rev 1
AN3192
Calibrating the LSM303DLH
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
0
0
⎡M x1 ⎤
⎡1/ M _ SC x
⎤
⎡M x − M _ OS x ⎤
⎢
⎥ [
⎢
⎥
⎢
⎥
0
1/ M _ SC y
0
⎢M y1 ⎥ = M _ m]3 x 3 ⎢
⎥ ⋅ [M _ si]3 x 3 ⎢M y − M _ OS y ⎥
⎢⎣Mz1 ⎥⎦
⎢⎣
⎢⎣Mz − M _ OS z ⎥⎦
0
0
1/ M _ SC z ⎥⎦
⎡MR11 MR12
⎢
= ⎢MR 21 MR 22
⎢⎣MR 31 MR 32
MR13 ⎤ ⎡M x − MR10 ⎤
⎥ ⎢
⎥
MR 23 ⎥ ⋅ ⎢M y − MR 20 ⎥
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
Doc ID 17353 Rev 1
13/34
Calibrating the LSM303DLH
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
Doc ID 17353 Rev 1
AN3192
4
Testing the electronic compass
Testing the electronic compass
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.
Table 1.
4.1
Expected pitch/roll/heading accuracy
Parameter
Value
Heading accuracy
< 2° RMS (tilt within ±50°), range: 0° ~ 359.9°
Pitch and roll accuracy
< 1° RMS (tilt within ±50°), range: -90° ~ +90°
Resolution
0.1° for heading, pitch, and roll
Lab testing
A convenient setup for accurate lab testing is a wooden platform with 3 degrees of rotation
freedom, as shown in Figure 6.
Figure 6.
Lab testing setup
+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.
4.1.1
Absolute heading testing
●
Level the wooden platform
●
Align the handheld device Xb axis to the local magnetic north direction mark on the wall
●
Check the heading output of the electronic compass. It should be 0º ± 2º. This is the
absolute heading accuracy at 0º
●
Rotate the wooden platform horizontally clockwise or counterclockwise at a random
angle which can be read from the marks on the platform. Then compare the compass
Doc ID 17353 Rev 1
15/34
Testing the electronic compass
AN3192
heading output with the known heading angle. The difference should be within ± 2º.
This is the absolute heading accuracy at random position.
●
4.1.2
4.2
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º.
Tilt compensation testing
●
Level the wooden platform
●
Align the handheld device Xb axis to any direction. Record the compass heading output
value
●
Rotate the platform around the Yb axis to generate a plus or minus pitch angle which
can be read from the marks on the wooden platform. Compare the compass pitch
output with the known pitch angle. The difference should be within ± 1º (see Table 1).
At the same time the change of the compass heading output should be within ± 2º
which means the compass is tilt compensated
●
Level the wooden platform
●
Align the handheld device Xb axis to any direction. Record the compass heading output
value
●
Rotate the platform around the Xb axis to generate a plus or minus roll angle which can
be read from the marks on the platform. Compare the compass roll output with the
known roll angle. The difference should be within ± 1º (see Table 1). At the same time
the change of the compass heading output should be within ± 2º which means the
compass is tilt compensated.
Field testing
In any physical situations outside the lab, rough field testing can be performed. A wooden
table with a smooth surface is required. The surface does not have to be leveled. Draw
some lines, for example, 20º apart on a white sheet of paper as shown in Figure 7.
Figure 7.
Field testing setup
ƒ
ƒ
ƒ
ƒ
ƒ
ƒ
ƒ
16/34
ƒ
ƒ
Doc ID 17353 Rev 1
!-V
AN3192
4.2.1
4.2.2
Testing the electronic compass
Relative heading testing
●
Place the handheld device on the paper which is taped to the wooden table
●
Align the device Xb axis to any direction
●
Align the left edge of the device to any line in Figure 7
●
Record the compass heading output value
●
Use one hand or both hands to rotate the device to align its left edge to any other line.
Then the difference between the compass new heading output and the previous one
should be the same as the difference of the line degree change. The error should be
within ± 2º
●
Another quick and easy way to check the relative heading accuracy is to align the left
edge of the device to the wooden table edge. Record the compass heading output
value. Then rotate the device and align the top edge, bottom edge, or right edge to the
same table edge. The difference between the new heading output and the previous
output should be either ± 90º or 180º with an error of ± 2º.
Tilt compensation testing
●
Place the handheld device on the wooden table with the Xb axis pointing in any
direction
●
Use two hands to hold the device and tilt the device along its top or bottom edge
carefully
●
While the compass pitch output is changing, the heading output should remain the
same with an error of ± 2º. This means the compass is tilt compensated
●
Use two hands to hold the device and tilt the device along its left or right edge
●
While the compass roll output is changing, the heading output should remain the same
with an error of ± 2º. This means the compass is tilt compensated.
Doc ID 17353 Rev 1
17/34
Firmware implementation
5
AN3192
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:
18/34
●
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
Doc ID 17353 Rev 1
AN3192
LSM303DLH pitch/roll/heading calculation
Appendix A
LSM303DLH pitch/roll/heading calculation
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.
Figure 8.
Electronic compass coordinate system
;E
5ROO
=$0
/60'/+
;$0
3LWFK
<$0
<E
+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 topview 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.
Doc ID 17353 Rev 1
19/34
LSM303DLH pitch/roll/heading calculation
Table 2.
Sign definition of LSM303DLH sensor raw measurements
Accelerometer (signed integer)
Stationary
position
A.1
AN3192
Magnetic sensor (signed integer)
Ax
Ay
Az
Mx
My
Mz
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 -
Pitch/roll calculation
When the device is at an arbitrary 3D position X'b, Y'b, and Z'b, there are a few rotation
procedures to rotate the device from the local level frame Xb, Yb, and Zb, shown in Figure 8,
to that 3D position. Different rotation procedures result in different rotation matrix. The
aircraft convention of angle rotation is used in this application note.
Firstly, rotate the handheld device around the Zb axis clockwise at an angle (ψ) with the view
from the origin to downwards. Then rotate the device around Yb at an angle (ρ) with Xb
moving upwards. Then rotate the device around Xb at an angle (γ) with Yb moving
downwards. The new device body axes become X'b, Y'b, and Z'b, as shown in Figure 9.
Figure 9.
Rotation procedures
;E
+HDGLQJ
3LWFK
;
5ROO
;;¶E
;
;
<E
<
<<
<
=E=
<¶E
=
=
= =¶E
!-V
Then each rotation matrix is:
Equation 5
⎡ cos ψ sin ψ 0⎤
⎢
⎥
R ψ = ⎢− sin ψ cos ψ 0⎥
⎢⎣ 0
0
1⎥⎦
20/34
Doc ID 17353 Rev 1
AN3192
LSM303DLH pitch/roll/heading calculation
Equation 6
⎡cos ρ 0 − sin ρ⎤
⎢
⎥
Rρ = ⎢ 0
1
0 ⎥
⎢⎣ sin ρ 0 cos ρ ⎥⎦
Equation 7
0
0 ⎤
⎡1
⎢
⎥
R γ = ⎢0 cos γ sin γ ⎥
⎢⎣0 − sin γ cos γ ⎥⎦
And the relationship between X'b/Y'b/Z'b and Xb/Yb/Zb is:
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 ⎥⎦
In the local horizontal plane, as shown in Figure 8, Xb = Yb = 0, Zb = +1g. At X'b/Y'b/Z'b, the
LSM303DLH accelerometer raw measurements are Ax, Ay, and Az which are signed integer
in terms of LSBs. Let Ax1, Ay1, and Az1 be the normalized values after applying
accelerometer calibration parameters into Ax, Ay, and Az. So Ax1, Ay1, and Az1 become
floating point values less than 1 in terms of g (earth gravity), and the root sum of their
squared values should be equal to 1 when the accelerometer is still. Then Equation 8
becomes:
Equation 9
cos ρ cos ψ
cos ρ sin ψ
− sin ρ ⎤ ⎡0⎤
⎡ A x1 ⎤ ⎡
⎢
⎥ ⎢
⎥ ⎢ ⎥
A
cos
sin
sin
cos
sin
cos
cos
sin
sin
sin
cos
=
ψ
ρ
γ
−
γ
ψ
γ
ψ
+
ρ
γ
ψ
ρ sin γ ⎥ ⋅ ⎢0⎥
⎢ y1 ⎥ ⎢
⎢⎣ A z1 ⎥⎦ ⎢⎣cos ψ sin ρ cos γ + sin γ sin ψ − sin γ cos ψ + sin ρ cos γ sin ψ cos ρ cos γ ⎥⎦ ⎢⎣ 1⎥⎦
Therefore, pitch and roll angle can be calculated as:
Equation 10
Pitch = ρ = arcsin(− A x1 )
Roll = γ = arcsin(A y1 / cos ρ)
Note:
When pitch = ±90º, roll should be set to 0º to avoid singularity
The arcsin function has good linearity between about -45º to +45º, so the accuracy of the
pitch and roll calculation degrades when tilt angles exceed this range
Normalized accelerometer measurement Az1 is not used for the pitch and roll calculation.
But it can be used to check if the magnitude A = A 2x1 + A 2y1 + A 2z1 is equal to 1. If not, then it
means linear acceleration or angular acceleration is detected.
Doc ID 17353 Rev 1
21/34
LSM303DLH pitch/roll/heading calculation
A.2
AN3192
Heading calculation
For the heading calculation, 3-axis magnetic sensor measurements need to be normalized
by applying magnetic sensor calibration parameters and then reflected onto the horizontal
plane by tilt compensation, as shown in Figure 10.
Figure 10. Heading calculation
;E
(ISTHEHORIZONTALCOMPONENTOFTHE
0[
%ARTHSMAGNETICFIELDTOGETHERWITHTHE
NEARBYINTERFERENCEMAGNETICFIELD
ȥ
+
=EPOINTINGINTOPAGE
0\
<E
!-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
0
sin ρ
⎡X b ⎤
⎡X' 'b ⎤ ⎡ cos ρ
⎤ ⎡X' 'b ⎤
⎢ ⎥
⎥ ⎢
⎥ ⎢
⎥
−1 −1 ⎢
Y
R
R
Y
'
'
sin
sin
cos
sin
cos
=
=
γ
ρ
γ
−
γ
ρ
γ
ρ ⎢
b⎥
⎢ b⎥
⎢
⎥ ⋅ ⎢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
Doc ID 17353 Rev 1
AN3192
LSM303DLH pitch/roll/heading calculation
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
The magnitude M = M2 + M2 + M2 should also be equal to 1. If not, it means that the
x2
y2
z2
external magnetic interference field is detected or a pitch/roll error is present.
Because the accelerometer measurements cannot distinguish earth’s gravity from linear
acceleration or angular acceleration, fast motion causes pitch/roll calculation error which
then directly introduces an error to the heading calculation. In most cases, the fast motion
doesn't last long and the device goes back to a stationary position. So the heading accuracy
in an electronic compass means static accuracy when the device is still or without
acceleration.
Doc ID 17353 Rev 1
23/34
Accelerometer calibration method
Appendix B
AN3192
Accelerometer calibration method
Let's consider accelerometer calibration at 6 stationary positions, as shown in Table 2.
Equation 3 can be rewritten as:
Equation 14
[A x1
] [
A y1 A z1 = A x
Ay
Az
⎡ ACC11
⎢
ACC12
1⋅⎢
⎢ACC13
⎢
⎣ACC10
ACC 21
ACC 22
ACC 23
ACC 20
]
ACC 31 ⎤
⎥
ACC 32 ⎥
ACC 33 ⎥
⎥
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
At Yb down position (P3 position), [A x1 A y1 A z1 ] = [0 1 0] and assume that at Yb down
position, n3 sets of accelerometer raw data Ax, Ay, and Az have been collected. Then,
Equation 18
Y3 = [0 1 0]n3 x 3
[
w 3 = A xP3
24/34
A yP3
Doc ID 17353 Rev 1
]
A zP3 1 n3 x 4
AN3192
Accelerometer calibration method
●
At Yb up position (P4 position), [A x1 A y1 A z1] = [0 − 1 0] and assume that at Yb up
position, n4 sets of accelerometer raw data Ax, Ay, and Az have been collected. Then,
Equation 19
Y4 = [0 − 1 0]n4 x 4
[
w 4 = A xP4
●
]
A XP4 1 n4 x 4
At Xb down position (P5 position), [Ax1 A y1 Az1 ] = [1 0 0 ] and assume that at Xb
down position, n5 sets of accelerometer raw data Ax, Ay, and Az have been collected.
Then,
Equation 20
Y5 = [1 0 0]n5 x 3
[
w 5 = A xP5
●
A yP4
A yP5
]
A zP5 1 n5 x 4
At Xb up position (P6 position), [A x1 A y1 A z1 ] = [− 1 0 0] and assume that at Xb up
position, n6 sets of accelerometer raw data Ax, Ay, and Az have been collected. Then,
Equation 21
Y6 = [− 1 0 0]n6 x 3
[
w 6 = A xP6
A yP6
]
A zP6 1 n6 x 4
Combine Equation 16 with Equation 21 and let n = n1 + n2 + n3 + n4 + n5 + n6, then
Equation 15 becomes,
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,
Doc ID 17353 Rev 1
25/34
Accelerometer calibration method
AN3192
Equation 24
[
X = wT ⋅ w
]
−1
⋅ wT ⋅ Y
Where,
means matrix transpose
wT
[w
26/34
T
⋅w
]
−1
means matrix inverse
Doc ID 17353 Rev 1
AN3192
Magnetic sensor calibration method
Appendix C
C.1
Magnetic sensor calibration method
Step 1: Soft-iron effect verification
It is always good to know if the device has soft-iron interference before choosing which
model for the identification of the calibration parameters, tilted ellipsoid, or non-tilted
ellipsoid. This can be done by performing 3D rotations in a clean environmental area. Then
plot the collected magnetic sensor raw data to check if there is a soft-iron interference field
inside the device. This set of data is not used for the following magnetic sensor calibration.
However, in practical situations, three 2D full round rotations may not be easy to perform.
Then an amount of 3D rotations data can be used for rough field calibration.
An example plot of 3D rotations data and three 2D full round rotations data is shown in
Figure 11. It is obvious that this electronic compass has a built-in soft-iron effect.
Figure 11. 3D rotations plus three 2D full round rotations
C.2
Step 2: Hard-iron, soft-iron and scale factor compensation
If there is soft-iron distortion, the 3D rotations show a tilt ellipsoid which can be described as
the following equation:
Equation 25
(x − x 0)2 + (y − y0)2 + (z − z0)2 + (x − x 0)(y − y0) + (x − x 0)(z − z0) + (y − y0)(z − z0) = R2
a2
b2
c2
d2
e2
f2
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.
Doc ID 17353 Rev 1
27/34
Magnetic sensor calibration method
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
a
2
b
2
c
2
= R2
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.
Figure 12. After hard-iron, soft-iron, and scale factor compensation
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
[
x2 = x y z − y2
− z2
2x 0
⎡
⎤
⎢
⎥
a2
⎢
⎥
2y 0
2
⎢
⎥
b
2
⎢
⎥
a
2z 0
⎢
⎥
2
c
⎢
⎥
2
⎥
1⋅⎢
a
⎢
⎥
2
b
⎢
⎥
2
a
⎢
⎥
⎢
⎥
2
c
⎢
⎥
2
2
⎢a 2R 2 − x 2 − a y 2 − a z 2 ⎥
0
0
0
⎢⎣
b2
c 2 ⎥⎦
]
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
Doc ID 17353 Rev 1
AN3192
Magnetic sensor calibration method
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
Then Equation 26 becomes,
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
Doc ID 17353 Rev 1
29/34
Magnetic sensor calibration method
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.
C.3
Step 3: Misalignment error compensation
Misalignment error compensation is to align the magnetic sensor sensing axes to the device
body axes based on three 2D full round rotations, as shown in Figure 12. The vector dotproduct method can be used to find each normalized vector that rotates, corrected, three 2D
full round rotation circles to their corresponding body axes. The normalized vector means
the magnitude is equal to 1. These 3 vectors form the [M_m] 3x3 misalignment matrix
described in Equation 4.
Applying the [M_m] 3x3 misalignment matrix to the above unit sphere and three 2D circles,
the plot is shown in Figure 13. Now three 2D full round rotations are aligned to the device
body axes. For example, the red color Zb down rotation is parallel to Xb - Yb plane.
Let's consider the Zb down rotation. After hard-iron and scale factor correction, the rotation
becomes a centered circle. If the circle is aligned to the device Zb axis, then all zzzz values
of the Zb down rotation circle should be the same, less than 1. If it is not the case, then a
vector can be found to rotate this circle to align to the device Zb axis.
Let,
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
Doc ID 17353 Rev 1
)
AN3192
Magnetic sensor calibration method
So the normalized rotation vector for Zb down rotation is,
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
Doc ID 17353 Rev 1
31/34
References
6
AN3192
References
1.
STMicroelectronics, Inc. LSM303DLH sensor module datasheet
http://www.st.com/stonline/products/families/sensors/motion_sensors/lsm303dlh.htm
2.
Honeywell, Inc. Applications of Magnetoresistive Sensors in Navigation Systems
3.
Honeywell, Inc. Applications of Magnetic Sensors for Low Cost Compass Systems
http://www.ssec.honeywell.com/position-sensors/datasheets/sae.pdf
http://www.ssec.honeywell.com/magnetic/datasheets/lowcost.pdf
32/34
Doc ID 17353 Rev 1
AN3192
7
Revision history
Revision history
Table 3.
Document revision history
Date
Revision
02-Aug-2010
1
Changes
Initial release.
Doc ID 17353 Rev 1
33/34
AN3192
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.
© 2010 STMicroelectronics - All rights reserved
STMicroelectronics group of companies
Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America
www.st.com
34/34
Doc ID 17353 Rev 1