  In this work we present a practical algorithm for calibrating a magnetometer
for the presence of magnetic disturbances and for magnetometer sensor errors.
To allow for combining the magnetometer measurements with inertial measurements
for orientation estimation, the algorithm also corrects for misalignment
between the magnetometer and the inertial sensor axes. The calibration
algorithm is formulated as the solution to a maximum likelihood problem and the
computations are performed offline. The algorithm is shown to give good results
using data from two different commercially available sensor units. Using the
calibrated magnetometer measurements in combination with the inertial sensors
to determine the sensor's orientation is shown to lead to significantly
improved heading estimates.
