# Magnetometer offsets revisted

The above is a plot of the 3 components of estimated magnetometer offsets computed during spin tests at 78 RPM of an improved method for estimating and removing magnetometer offsets. There is a report available with theory and implementation. The method will work equally well for fixed wing aircraft, multicopters, and helicopters. It makes no assumptions about the dynamics of the airframe.

The idea for the new method came to me while I was recently working on a method to detect and compensate for magnetometer misalignment errors. I realized the method that I had been using for offset compensation was sensitive to misalignment between the magnetometer axes and the gyro axes, so I thought about ways to compute offsets that would ignore misalignment. I found a good way to do it that turned out to be more accurate and easier to implement than the method that I was using.

I also figured out a slick way to detect and adjust for magnetometer misalignment, I will be publishing a report in a few days on that subject. The method will compensate in flight for any amount of magnetometer misalignment, including a 180 degree yaw mounting error. In other words, the algorithm can figure out that you mounted your magnetometer backwards and apply a rotation matrix to the magnetometer vectors to put them into the correct reference frame. Stay tuned....

[Here is the technique for doing inflight magnetometer alignment. - WJP]

Best regards,

Bill Premerlani

Views: 1559

Tags: UDB, magnetometer, offsets

Comment by Bot Thoughts on October 14, 2011 at 11:43pm

Wow, nice! Eager to see if I can implement any of this for my ground vehicle.

Comment by ionut on October 15, 2011 at 12:04am

Rana you can get a picture of him and start worship him.

Developer
Comment by Mark Colwell on October 15, 2011 at 6:14am

Sweet, now we can just mount mag where EMF is lowest and not worry about orientation !

Comment by William Premerlani on October 15, 2011 at 6:44am

Hi Mark,

The amount of time that it takes for the auto-alignment algorithm to converge is proportional to the amount of misalignment. So the best thing to do is to orient the magnetometer at least approximately, within 10 degrees would be a good idea.

Another approach if you really want to be able to orient the magnetometer any way at all would be to rotate the aircraft several times around each axis with the IMU and magnetometer mounted in place, and then record the alignment information reported by the algorithm, and use it as the starting point on subsequent initializations.

Also keep in mind that right now the algorithm is running only in MatrixPilot. I don't know if and when the ArduPilot team will want to incorporate it in ArduPilot.

Best regards,

Bill

Comment by Andrew Zaborowski on April 28, 2012 at 6:33pm

WOW, this is an incredibly simple formula, much less room for programmer error and much easier on my poor flight computer 8bit chip.  It's simple enough that I implemented it in the last 1h in my firmware and it works!

I used a 1.0 gain.  My main issue was when I close the top cover on my airplane model, which is secured with three strong magnets, so independently of what magnetometer constant offset I use, it'll be wrong either with the hatch open or closed.  Now it converges after maybe 5s of moving the airplane around after closing the cover.  I stop integrating the offset values once the (max measured field magnitude / min magnitude) is > 0.95 in a period where the gyro reports at least 60deg pitch and 60deg yaw variation.  If at any later point the magnitude becomes > 1.05 or < 0.95 of the calibration value, the autopilot goes back to integration mode and temporarily stops using the magnetometer input for the AHRS quaternion correction.

Also this method may be very useful on devices without a gyro.  I have a feeling that the previous method you presented (2010) converged a little quicker by using the gyro input.  Both are incredibly clever ideas though, which should probably be taught in robotics classes.

Out of curiosity on the horizontal axis in your graph is this the revolutions count, time in seconds, or the number of integration steps?

Also in the implementation example in the PDF you seem to be saving "magFieldBodyPrevious" and "magFieldBodyMagnitudePrevious" between loop iterations.  But the value of that vector is R * b_0 + R^T  * b_E. The problem I see is that b_0 changes between loop iterations.  Would it not be more correct to save the raw measurement between iterations, and subtract only the current b_0 from b_B1 and b_B2?  This is what I did in my case.

Comment by Andrew Zaborowski on April 28, 2012 at 6:41pm

b_0 * R^T * b_E is what I meant (i.e. offset estimation + raw value from magnetometer)

Comment by William Premerlani on April 29, 2012 at 5:30pm

Hi Andrew Z,

I am very pleased to hear that this method works for you, and that you were able to implement it quickly. I don't know if either method is taught in robotics classes, but this latest method is so accurate and elegant, that I think sooner or later it will catch on widely. It was recently incorporated into ArduPilot.

The previous method, which converged at about the same rate as this one, was more sensitive to magnetometer misalignment, and tended to cross couple with the inflight magnetometer alignment algorithm. This algorithm has less cross coupling.

On my graph, the horizontal axis is the number of magnetometer readings, at 4 readings per second.

Regarding your last question, yes, what you are suggesting would be "more correct", but it would not be any more or less accurate then the implementation example. Both methods will converge to the same result. That is because as the algorithm converges, the values of b_0 stop changing, so both methods converge to the same value.

There are several variations on the basic idea that would work just as well as long as they converge toward the correct value.

The reason that I implemented the algorithm the way that I did was for convenience: I subtract b_0 in the magnetometer driver, so that its removal is transparent to all the computations that depend on the magnetic measurement.

Best regards,

Bill Premerlani

Comment

Join DIY Drones

1

2

3

4

5

6

7

8

9

10

## Contests

Season Two of the Trust Time Trial (T3) Contest has now begun. The fourth round is an accuracy round for multicopters, which requires contestants to fly a cube. The deadline is April 14th.

A list of all T3 contests is here

47 members

24 members

87 members

183 members

692 members