Advanced hard and soft iron magnetometer calibration for dummies

If you bought the cheap magnetomter module like HMC5883L you can not use it without calibration. Measurement of magnetic field will be subjected to distortion. There are two categories of these distortions: the hard iron distortions and the soft iron distortions. The hard iron errors refer to the presence of magnetic fields around the sensor (magnets, power supply wires) and are related to measurement offset errors, while the soft iron errors refer to the presence of ferromagnetic materials around the sensor, which skew the density of the Earth's magnetic field locally and are related to scaling offset errors. You can read more information about these distortions here.

In other words, to get the correct magnetometer data you should get the calibrated magnetometer data. One of the ways to resolve this problem: you should apply the bias to the vector of the non calibrated magnetometer data (X, Y, Z coordinates) and then multiply the transformation matrix by this resulting vector:

Picture 1

In this case the magnetometer calibration is the process of getting the transformation matrix and the bias. To get these data you can use the MagMaster program.

You can download MagMaster here.

Example of using the MagMaster

Hardware:

Software:

  • MagMaster (placed in MagMaster folder)
  • MagViewer (placed in MagMaster folder)

Firmware:

  • Arduino Sketch (placed in MagMaster folder)

Picture 2

Picture 3

Picture 4

Connect the magnetometer module to the arduino board via I2C bus (picture 4). Upload the arduino sketch to the arduino board (see "Arduino_Code" folder in the "MagMaster" folder). This arduino sketch requires the HMC5883L library, copy the folder "HMC5883L" (placed in "Arduino_Code" folder)  to the folder "C:\Program Files\Arduino\libraries".

Then run the MagViewer.exe, select the serial port of the arduino board (the boud rate of the seraial port should be 9600 bps) and click "Run MagViewer". Now you can see the coordinates of the magnetometer data vector in 3D space on a real time (picture 5, video 1, 2). These data are not calibrated yet.

Picture 5

Video 1

Video 2

If you see the points of the magnetometer vector coordinates in 3D space the arduino board and the PC connection works right.

Now close the MagViewer window and run the MagMaster.exe (picture 6). Select the serial port of the arduino board. The green strings X, Y, Z will indicates the coordinates of the magnetometer vector.

Picture 6

Place the magnetometer module as shown on the picture 8.1 and click "Point 0" button of the "Axis X+" groupbox. For the placement of the module you can use the wooden bar or the paper box (picture 7). If you can not connect your magnetometer device to PC then you can use any other way to get to know the magnetometer data. You can enter these data in the program manually.

Picture 7

Place the magnetometer as shown on the picture 8.2 and click "Point 180" button of the "Axis X+" groupbox and so on. You should do in the following way:

  • Picture 8.1: "Point 0", "Axis X+"
  • Picture 8.2: "Point 180", "Axis X+"
  • Picture 8.3: "Point 0", "Axis X-"
  • Picture 8.4: "Point 180", "Axis X-"
  • Picture 8.5: "Point 0", "Axis Y+"
  • Picture 8.6: "Point 180", "Axis Y+"
  • Picture 8.7: "Point 0", "Axis Y-"
  • Picture 8.8: "Point 180", "Axis Y-"
  • Picture 8.9: "Point 0", "Axis Z+"
  • Picture 8.10: "Point 180", "Axis Z+"
  • Picture 8.11: "Point 0", "Axis Z-"
  • Picture 8.12: "Point 180", "Axis Z-"

Picture 8

You should fill the table. After that click "Calculate Transformation Matrix and Bias" and get the required matrix and bias (picture 9).

Picture 9

The transformation matrix and the bias are got. Now you can calculate the calibrated magnetometer data in your device on a real time with using the matrix and the bias as shown on picture 1. The example of the arduino sketch of using this calculation you can find in the "Arduino_Test_Results" folder.

You can apply the sphere radius stabilization algorithm to your program (use for it the Arduino_Radius_Stabilization folder placed in MagMaster folder).

The calibrated magnetometer vector coordinates in 3D space with the radius stabilization shown on the picture 10 and video 3, 4.

Picture 10

Video 3

Video 4

Using of the arduino is only example, you can easy adopt the arduino code for any other system and use the MagMaster program with it.

Views: 83022

Comment by Georacer on June 1, 2014 at 11:43pm

This looks like a very useful piece of software, although I guess I'll keep using Matlab in conjunction with Yuri Petrov's excellent ellipsoid fitting algorithm.

One thing I'd like to note is that hard magnet errors refer to the presence of magnetic fields around the sensor (magnets, power supply wires) and are related to measurement offset errors, while soft magnet errors refer to the presence of ferromagnetic materials around the sensor, which skew the density of the Earth's magnetic field locally and are related to scaling offset errors.

Comment by Georacer on June 1, 2014 at 11:45pm

Also, could you fix the download link? It doesn't appear to be clickable.

Comment by Yury Matselenak on June 2, 2014 at 12:29pm

As far as I know, the Yury Petrov's algorithm does not solve the problem of the not ideal ortogonal orientation of the axis and thier rotation. MagMaster solves this problem)

Link is clickable now!

Comment by Georacer on June 2, 2014 at 12:40pm

Since it fits an ellipsoid onto a distribution, it can handle bad alignment of the sensor board in relation to the plane/quad/heli, but it does not support axis misalignment of the sensor itself.

In that aspect, yes, I agree with you.

However, given that axis misalignment errors will be dwarfed in comparison with background and electrical noise and how useful Matlab is for data manipulation, I wouldn't dismiss this choice.

Comment by Yury Matselenak on June 2, 2014 at 1:06pm

But Matlab is hard for a beginner. MagMaster is simple and fast, it has the good results of calibration.

Comment by Georacer on June 2, 2014 at 1:13pm

Yes, Matlab is indeed hard for a beginner. However, if someone wants to calibrate a magnetometer for soft and hard magnet errors, chances are that he should know how to operate Matlab as well. It's an indispensable tool for serious engineering.

In case that you have a problem with it being commercial and closed-source, Octave is supposed to be compatible with Matlab .m files.

Comment by Jose.L.Jr on June 3, 2014 at 8:01am

Thank you for share Yury.

Your monolithic interface is really good. 

Comment by Yury Matselenak on June 3, 2014 at 1:04pm

Thanks!

I updated the archive with MagMaster. Now it has the arduino code with the sphere radius (length of the magnetometer vector) stabilazation.

Comment by Don Howes on June 12, 2014 at 7:47am

Yury I want to use this method to calibrate the magnetometer in the Pololu MINIMU-9. I can modify the sketch to get the raw data but I am confused as how to orient the chip for the 12 readings. This chip has X pointing to top of board, Y to the right and Z going into/down. Can I turn the chip over and change the sign of the z axis and follow the diagrams?

Comment by Yury Matselenak on June 12, 2014 at 9:55am

Don, the MINIMU-9 and HMC5883L GY-273 modules has the equivalent axis orienations. See the pictures:

Comment

You need to be a member of DIY Drones to add comments!

Join DIY Drones

Groups

Season Two of the Trust Time Trial (T3) Contest 
A list of all T3 contests is here. The current round, the Vertical Horizontal one, is here

© 2017   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service