Azimuth Calculation applications - tilt compensation
When the electric compass is level, it is possible to calculate using a simple calculation method.
However, in the case of a smartphone device, it often isn't level but tilted.
Because the electronic compass contained in the device is tilted, it is unable to measure the horizontal component of the earth's magnetic field.
To correctly measure azimuth when not level, you must measure the angle of tilt with an accelerometer and perform a tilt compensation calculation.
To measure tilt with an accelerometer
Use the accelerometer measurements for gravitational acceleration in the direction 180 degree opposite of gravity.
Using the 2 vectors of gravity's actual measurements (Ax, Ay, Az) and the values when the device is level (Ax0, Ay0, Az0), calculate the rotation (rotational angle, rotational axis) for the 2 vectors before and after rotation.
Using the angle α formed by the two vectors, the rotational axis (nx, ny, nz) can be calculated according to the following equation.
(2)Calculating the tilt compensation
Using the rotation (rotational angle, rotational axis) calculated in the previous step, calculate the rotational matrix Mrot.
Compared to the magnetic field (Hx, Hy, Hz) measured while tilted, to perform the rotation from the rotation matrix to the direction to reverse the tilt, use the rotation matrix Mrot' where α'= -α in the opposite direction of rotation.
The formula for the compensation is below.
From the above equation, the slope corrected magnetic field (Hx', Hy', Hz') can be found.
From the slope corrected magnetic field, the azimuth can be calculated using the level magnetic field's Hx' and Hy'.