The above picture shows an interesting side effect of magnetometer misalignment: complete reversal of the measured horizontal component of the earth's magnetic field.

The effect was discovered by Peter Hollands during his analysis of the data from one of Ric Kuebler's flights. Ric was using the UAV DevBoard (UDB) with MatrixPilot autopilot software. The airplane icons in the picture indicate the estimated orientation of the plane. The blue arrows indicate the estimated direction of the horizontal component of the measured magnetic field, in the earth frame of reference.

The magnetometer is mounted separately from the UDB. It turns out that there was a small roll misalignment of the magnetometer in Ric's setup. Ric aligned his magnetometer more carefully, and the flipping disappeared on subsequent flights. But there was a nagging issue.

Because of the large vertical component of the earth's magnetic field in many parts of the world, magnetometer-based yaw information is particularly vulnerable to misalignment. In my neighborhood, the vertical magnetic field is three times as large as the horizontal field. As a result, a little bit of mechanical misalignment of the magnetometer, and certain attitudes of the aircraft can cause complete flipping of the measured magnetic field.

It is not all that easy to align a separately mounted magnetometer. Even a 5 degree misalignment is too much. I wondered if there might be a way to solve the problem with software....

While I was analyzing the data from Ric's flight, on a hunch, I plotted the measured horizontal magnetic field against the heading:

The plot told me two things. First, it told me that there was a misalignment issue. But it also told me how much the misalignment was. If that could be determined from the flight data from a post-flight analysis, it should be possible to figure it out in flight as well. So I set out to find a way to determine magnetometer misalignment in flight, and automatically compensate for it. The theory, implementation, and testing are reported here.

The method works much better than I originally thought it might. It will automatically and exactly compensate for any amount of roll, pitch, and yaw misalignment between a magnetometer and its partner IMU, including 180 degrees.

Best regards,

Bill Premerlani


Views: 7526

Comment by Michael Pursifull on October 16, 2011 at 3:16pm

Very nice report. I notice, however, that you removed the code which you had originally introduced in the implementation, was there an issue with the source code presented as an example?

Comment by Michael Pursifull on October 16, 2011 at 3:25pm

I also wonder about the potential application of this sort of calculation in the calibration of gyros or other sensors... for example, if one includes a second set of gyros which are not aligned with those on the IMU? I do not pretend to understand the math, but my limited understanding is suggestive that we can use one set of sensors to calibrate another... I will leave the practical applications possible with three or five separate IMUs to the imagination of the reader. 

Comment by William Premerlani on October 16, 2011 at 3:42pm

Hi Mike,

I previouslyreported how to achieve inflight calibration of gyros.

Regarding the source code, I decided to leave it out of the report mostly because I thought a verbal explanation would be easier to follow. If there is sufficient interest, I will also publish the code. Does anyone else want to see the code?

Best regards,


Comment by William Premerlani on October 16, 2011 at 3:58pm

Hi Mike,

Here is a link to the portion of the MatrixPilot code that does the magnetic field calculations. There are several calculations that are being done, including yaw drift correction, offset removal, and alignment.


Best regards,


Comment by Michael Pursifull on October 16, 2011 at 4:16pm



Thank you. I'm not feeling sufficiently big brained to grok this at the moment; I will study it and hope that someone here beats me to the punch if munging this into ArduCopter, with your permission. I do not know, also, if we will need to await the next gen hardware for this. If I recall correctly, MatrixPilot is 32bit?

Comment by Michael Pursifull on October 16, 2011 at 4:22pm

Sorry, I was thinking of multipilot, if anything, it makes MatrixPilot more remarkable. I've added one to my shopping cart, I need to take a much closer look at everything you have been up to. Thank you.

Comment by Randy on October 16, 2011 at 6:24pm

This is great stuff Bill!

I'm pretty sure we will integrate this into the Arducopter and ArduPilot code.  Rate determining factor is mostly putting in the time to understand the method Bill's come up with!

It doesn't need to be someone in the core dev team though, with the magic of git anybody can implement in their own branch and then (apparently) we can merge it in easy to trunk while keeping the credit for who did the original code changes intact...that's my understanding anyway. :-)

Comment by Mike on October 17, 2011 at 3:02am

Bill, your elegant use of mathematics to solve such problems as this and wind compensation are impressive to say the least and a major contribution to the development of autopilot systems - not just UDB! Thank you for your immense contribution to the hobby.



Comment by Gary Mortimer on October 17, 2011 at 3:24am

Here here, implementing the wind model should be a no brainer for every project out there that can.

Its a shame that there is not one platform that you could try all the different methods out on.

Comment by ionut on October 18, 2011 at 5:38am

I'm not sure I fully understand the problem here, but if you have a missalignement between 2 coordinates system like magnetic space and Gyro space, you will need only 3 points in both spaces to determine the transformation between the 2 spaces.Horn's closed form algorithm here


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

Join DIY Drones


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

© 2019   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service