Maker Portal

View Original

Calibration of a Magnetometer with Raspberry Pi

See this content in the original post

See this content in the original post

The Earth’s magnetic field can be approximated as a dipole centered at the Earth’s core with opposite magnetic poles that intersect the Earth's surface at points called geomagnetic poles (read more about the World Magnetic Model here). When characterizing the Earth’s magnetic field at a given location, a triple-axis magnetometer calculates the horizontal and vertical components of the magnetic field by partitioning the amplitudes by each component of the magnetic field. An example of a triple-axis IMU placed at the surface of the Earth is given below:

The field measured by the magnetometer in the horizontal is considered the ‘heading’ and the vertical is sometimes called the ‘magnetic dip’ or inclination. For most purposes, the heading is used to determine the direction of magnetic north, which can be used for geographic positioning.

Returning to the MPU9250 magnetometer (AK8963) drawing, we can begin to understand how we might approximate the direction of magnetic north and the magnetic field strength at a the geographic location of the IMU:

Similar to Part I of this tutorial series, we can use the magnetic field calculator produced by the National Centers for Environmental Information (NCEI) as a validation of the magnetic field being measured by the magnetometer. For New York City (our lab’s geographic location) the magnetic field components are as follows:

We can see that the magnetic field is being calculated by the World Magnetic Model 2010 for roughly downtown Manhattan, New York City. The declination is the angle between magnetic and geographic north. The inclination is the angle between the surface of the Earth and the vertical magnetic field. The horizontal intensity can be found by computing the amplitude of the component field in the horizontal direction. For example, if the IMU’s z-direction is placed parallel to gravity, the amplitude of the field in the x/y-directions can be found as follows:

The above can be used to find the strength of the magnetic field (horizontal intensity) using the two components of the field in the horizontal plane (x/y or any other combination depending on the orientation of the sensor). Similarly, we can approximate the heading using the arctangent of the two horizontal components as well:

Thus, using the arctangent relationship above the direction of magnetic north can be found using just two components of the magnetic field measured by the magnetometer (assuming the sensors are perpendicular to the surface). In the next section, the arctangent will be used to calibrate what is called the ‘hard iron offset’ for the magnetometer. This will effectively calibrate the offsets inherent in the magnetometer, and ensure that the angles being approximated from the arctangent are not biased by the sensors.

See this content in the original post

See this content in the original post

The IMU will need to be rotated 360 degrees in at least two directions in order to find the hard iron offset of the triple-axis magnetometer. The best way to understand hard iron effects is to first take measurements during a 360° rotation and visualize the magnetic response of the planar magnetometer sensors (sensors parallel to the Earth’s vertical magnetic field). This is shown in the plot below:

It is easy to see the large variability between the three sensor planes before offsetting for the hard iron effect. The 360° rotation of the sensor around each axis should produce a circle centered around the origin (0,0), which we see after offsetting for the hard iron effects. This is essential for improving the accuracy of the magnetometer’s approximation of heading. Thus, in the above we arrive at three offset values that approximate the hard iron interferences of the system. In the calculation of heading using the arctangent given in the above section, the hard iron effects should first be subtracted from the magnetometer values being read. This will increase the accuracy and stability of the tracking/orientation approximation being used for a given IMU application.

More information on hard iron offsets and magnetometer calibration can be found at the links below:

The code used to conduct the hard iron calibration offset is also given below:

See this content in the original post

Once again, all codes are housed in the project repository at the link below:

See this content in the original post

The procedure for calibrating the hard offset for the magnetometer is as follows:

  1. Run the mag_hard_calibration.py code (above)

  2. When prompted, rotate the IMU around the stated axis (the stated axis should be parallel to gravity)

  3. After rotating around the stated axis, press CTRL+C to interrupt the run

  4. Repeat for all three axes

There is another more involved method for calibrating a magnetometer, called soft iron calibration, which scales each axis of the magnetometer to ensure that the shape of the rotation of the IMU maintains its circular form. If soft iron is present, the rotation of the IMU will produce more of an elliptic response rather than a circular one. This is not explored in this tutorial due to the lack of soft iron material present in our system. However, there are many publications that handle soft iron calibration for a magnetometer, below are just a few:

In the next section, the fusion of all three sensors (9-degrees of freedom) will be introduced, which will combine all calibration routines that will output calibration coefficients in one script. Then, a live plotter will be given to permit real-time monitoring of all nine variables.


See this content in the original post

The calibration of the IMU has thus far only involved storing calibration coefficients in the local memory and producing plots and printouts of the variables. In this section, the calibration routines for each of the sensors is assembled into one calibration procedure where the coefficients are saved for future use. This makes the calibration procedure useful for future applications and allows for validation and extended verification of algorithms using the calibration information. This section will also introduce a real-time plotting code which allows the user to test the calibration of all 9-DoF in real time, which further validates the calibration and also helps users understand how the MPU9250 responds under different forcings (magnetic, accelerative, rotational, etc.).

The full calibration implementation code that goes through the calibration of the gyroscope, accelerometer, and magnetometer is given below. The code also outputs a real-time visualization for viewing the calibration of each 9-DoF along with an approximate heading calculation. The visualization allows for checking of calibration and verification of different phenomena in real-time. For example, a magnet could be brought into the IMU’s detection field and the response will be seen by the AK8963 magnetometer. The full code is given as well as on the project’s GitHub page.

See this content in the original post

An example printout of the calibration coefficients outputted by the script above can be seen below:

Another example output of the code above is given below showing a 360° rotation plot of the IMU around the z-axis:

The complete calibration code given above saves the calibration coefficients to a local file entitled 'mpu9250_cal_params.csv' - Upon reloading the parameters in this file, the calibration can be carried on into future applications.


See this content in the original post

This concludes the series entitled “Calibration of an Inertial Measurement Unit (IMU) with Raspberry Pi" (Part I, Part II) which outlined a series of measurement techniques desired around finding meaningful and useful calibration coefficients of an inertial measurement unit (IMU), specifically with an application employing the MPU9250 interfaced through a Raspberry Pi computer. The MPU9250 contains three sensors: an accelerometer, gyroscope (MPU6050), and magnetometer (AK8963), all of which were explored throughout the IMU calibration series. The accelerometer was calibrated using three data points for each axis, centered around Earth’s acceleration due to gravity. The gyroscope was calibrated under steady conditions to approximate the offset coefficients for each axis. In this entry, methods for calibrating the magnetometer were introduced, where the IMU was rotated 360° around each axis. Our IMU Calibration Block was used for all calibration procedures to permit calibration of all 9-degrees-of-freedom. Calibration of inertial measurement units is essential for improving algorithms deployed in applications ranging from autonomous vehicle navigation systems, gesture recognition, projectile guidance, indoor navigation, quadcopter navigation - just to name a few!

See this content in the original post

See More in Raspberry Pi and Engineering:

See this content in the original post