Calibration (USB)

Calibration is an important part of any orientation system. Any magnetometer has two sources of error called Soft Iron Error and Hard Iron Error.

Soft Iron Error is generally caused by local magnetic interference from specific metals, causing a deviation in magnitude or direction. Soft iron errors will cause what should be 'spherical' data to be deformed into a pill shape. Correcting for soft-iron error is the process of transforming the pill shaped output back into a spherical form using matrix multiplication with the soft-iron coefficients against the raw x/y/z data.

Hard Iron Error is a caused by local magnetic fields, which 'add' to the existing magnetic field, causing a specific change in position relative to the ideal, centered spherical output you would see in a perfect environment with a perfect sensor. This is a static set of values for x/y/z that are applied to 'recenter' the magnetometer output.

Calibration should always be done in the final environment and project setup (enclosure, etc.) where the sensor will be used, since much of the calibration process involves compensating out local environmental variables!

Generating Calibration Data

To determine the soft iron and hard iron error coefficients for your specific sensors and environment using a USB cable, you can make use of the MotionCal app from PJRC.

Download and run the appropriate binary for your operating system, and place it somewhere convenient.

When you run the app, by default you will see something like this:

Next, with your development board and the NXP Precision 9DoF connected, load the ahrs_calibration_usb sketch from Adafruit_AHRS (source code on Github):

You should see something resembling the following code:

Make sure that you have the correct sensor target selected via the AHRS_VARIANT macro, which should be set as shown in the code below:

Download: file
// Define your target sensor(s) here based on the list above!
// #define AHRS_VARIANT    ST_LSM303DLHC_L3GD20
#define AHRS_VARIANT   NXP_FXOS8700_FXAS21002

Build and flash the sketch, and then without opening the Serial Monitor go back to the MotionCal app.

In MotionCal select the correct serial port from the drop-down list in the top-left-hand corner:

Once you are connected, start rotating the orientation sensors, and red dots will start to show up on the screen, and the soft iron error (Magnetic Mapping) and hard iron error (Magnetic Offset) will start to populate themselves:

Once you have a reasonably complete sphere (check the error percentages on the bottom of the screen!), make note of the Magnetic field values on the right-hand side:

Make Note of the Compensation Coefficients

The values generated above represent the coefficients that you will need to apply when running the ahrs_fusion_usb sketch (code on Github).

Note these values down (save a screenshot, for example!), and then plug them into ahrs_fusion_usb using the following setup (the values below are taken from the screenshot above):

Download: file
// Offsets applied to raw x/y/z mag values
float mag_offsets[3]            = { -2.20F, -5.53F, -26.34F };

// Soft iron error compensation matrix
float mag_softiron_matrix[3][3] = { {  0.934,  0.005,  0.013 },
                                    {  0.005,  0.948,  0.012 },
                                    {  0.013,  0.012,  1.129 } };

float mag_field_strength        = 48.41F;

// Offsets applied to compensate for gyro zero-drift error for x/y/z
float gyro_zero_offsets[3]      = { 0.0F, 0.0F, 0.0F };
This guide was first published on Apr 14, 2017. It was last updated on Apr 14, 2017. This page (Calibration (USB)) was last updated on May 25, 2019.