Tuesday, August 30, 2011

Improved magnetometer calibration (Part 1)

Note: the MagCal software is no longer available on the University of Calgary site. See Part 2 of this post for the replacement Magneto software.

After looking at a number of recent papers on magnetometer calibration, I have decided on the following path to improve the calibration of a custom compass under development.

It is based on the availability of a PC software tool called MagCal.exe, developed by the PLAN Research Group at the University of Calgary, Alberta, Canada.

First, here is how to get the software:
     1. Go to their website: http://plan.geomatics.ucalgary.ca/
     2. On their menu bar, select Research, then Publications
     3. Type MagCal in the Search Criteria textbox, then click on the ‘Search’ button.
     4. Right click on ‘Download Executable’, then pick ‘Save target as…’ in the pop-up menu
     5. Save the MagCal.zip file on your machine
     6. Unzip, and you are ready to go.

You can also download the PDF paper on which the software is based at the same location.

So what MagCal exactly does? As stated in the about box: “This code computes the calibration parameters of a tri-axis magnetometer”.

From an input file containing raw measurement of the magnetometer, it finds 12 calibration values that correct for a whole set of errors: bias, hard iron, scale factor, soft iron and misalignments.  Note that you will have to invert a 3x3 matrix result (there are online tools for that). What they don’t tell is that the same software can also be used to calibrate the accelerometer for bias, scale factor AND misalignments.

Here is a rendering (using Google SketchUp) of the example raw data file provided with the MagCal software.

The whole idea is to first make this calibration on the completed compass in a magnetic clean environment (like an exterior isolated field).

The whole calibration procedure will be like this:
     1. Record a set of static measurements (magnetometer and accelerometer) by positioning the compass in a large number of orientiations covering a whole sphere
     2. Process the data in MagCal to find the calibration values (12 each for the magnetometer and the accelerometer)
     3. Carefully position the compass assembly completely horizontal (use a precision level).
     4. Record the accelerometer output at this position, to find the orientation of the accelerometer versus the whole compass assembly. This will be used to apply a final correction to the calibrated accelerometer values before applying the tilt compensation equations.

Note that once the compass will be installed in the boat, there may be additional hard and soft iron errors induced by the boat structure and equipment.  These will be later identified with the help of the GPS, as shown in a previous post.

In Part 2 of this post, I present 'Magneto', a similar software tool which has some practical advantages compared to 'MagCal'.


  1. Hi, probably our entry may be off topic but anyways, I have been surfing around your blog and it looks very professional. It’s obvious you know your topic and you appear fervent about it. I’m developing a fresh blog plus I’m struggling to make it look good, as well as offer the best quality content. I have learned much at your web site and also I anticipate alot more articles and will be coming back soon. Thanks you.

  2. hello!!!amazing post and excellent!is the source code of the algorithm for the calibration available for matlab!since i am most familiar with it!thanx :)

    1. See :
      for a link to a Matlab implementation of the Li's
      ellipsoid fitting algorithm. You will to complete with the calculation of the correction matrix as shown in:

    2. thanx!when you say i have to complete you mean to do the last 5 steps?

    3. a i found it !!!just 2 last questions!!!!
      double hm;
      double* A_1 = new double[3*3];

      for(i = 0; i < 9; i++)
      A_1[i] = SQ[i] * hm / hmb;

      what does hm stands for?

      and :

      ***Calculate A to permit comparison with MagCal***

      what does the part of the code do here?calcualtes A-1?

    4. i asked silly questions!i completed it!thanx a lot !!!this calibration technic offered me the only correct results :)!!!!

  3. Really interesting subject, as I struggled with this for some time now with little luck. Ellipsoid fitting is not an easy task...
    Anyway - in order to see what the output would be when applying the correction matrix on the data provided in your sample, I wrote a small program that applies the correction and plots the data.
    However I didn't get the perfect sphere I had hoped for and wonder if I made something wrong.
    I could publish the application and results here If I only knew how to attach files and pictures...

  4. Thank you! This post is an excellent tutorial for calibrating magnometer such HMC-5883L sensor.

  5. Hi,I want to ask a question of the procedure 1,
    record a set of "static" measurement by positioning the compass in a large number of orientiations covering a whole sphere. How to record static measurement when position whole orientations to cover a whole sphere when rotate the compass? and how many sets should I record is enough?

    1. In practice, I only rotate the compass around 3 mutually perpendicular axis, as this is enough to define an ellipsoid of measurements. Using 10 degrees steps for each measurement is quite enough. It is particularly important that the compass does not move during the accelerometer measurements, as any vibration will affect the results (hence the 'static' recommendation). I fix the compass to a wooden peg
      leaning on a support to make the measurements.

    2. Hi,Merlin
      I've done lots of times,after the calibration seems very effective,but as time goes by(2~3hours) I found that magnetometer would become inaccurate even in the same environment,is that normal? Do I need to calibration maybe every couples of hours?

    3. Definitely not normal if the environment has not changed.

    4. I am in the office,so there are computers and steel inside the concrete skeleton,guess maybe the problem here.

    5. Might be drifting temperature effects over the time period? Fluxgates do drift with temperature changes.

  6. Hi Merlin, thanks for this great blogPost. I am trying to find the value for the Norm of the Earths Magnetic Field. Your link is a 404 but i tracked it down here: https://www.ngdc.noaa.gov/geomag-web/#igrfwmm
    The issue is that this outputs 8 fields, but none of them are called 'norm'.
    # (1) Date in decimal years
    # (2) Declination in decimal degrees
    # (3) Inclination in decimal degrees
    # (4) Horintensity in nanoTesla (nT)
    # (5) Totalintensity in nanoTesla (nT)
    # (6) Xcomponent in nanoTesla (nT)
    # (7) Ycomponent in nanoTesla (nT)
    # (8) Zcomponent in nanoTesla (nT)
    What do you suggest i use as 'norm' for the MagCal input??

  7. Use Total Intensity aka TMI aka TF aka field aka total field value for your location.

  8. I'm working with the HMC5883L, I succeeded to convert the raw data to degrees but I don't know much about the calibration and how to know if the device is really pointing to the north.