# LSM303D -- where is inclination compensation?

The library for the LSM303D doesn’t seem to compensate for magnetic inclination. This seems to lead to headings that can be 20 degrees off!

Here’s the description from the code:

Description of heading algorithm: Shift and scale the magnetic reading based on calibration data to find the North vector. Use the acceleration readings to determine the Up vector (gravity is measured as an upward acceleration). The cross product of North and Up vectors is East. The vectors East and North form a basis for the horizontal plane. The From vector is projected into the horizontal plane and the angle between the projected vector and horizontal north is returned.

The problem is that the projection of the magnetic field through the ground plane, when rotated a full circle, is an ellipse.
Also, the code doesn’t actually “scale” the readings; it just “offsets” them (and, evenso, it “offsets” them by the default of zero unless you hack at the code to set another calibration.)

I imagine that the correct way to deal with this is to measure inclination based on the down vector, and then rotate the reading back to ground plane, rather than reconstruct it simply using cross products (which will apply a skew based on the elongation of the ellipse.)

However, I could be off here – is there some other way this is supposed to work?

[quote]The problem is that the projection of the magnetic field through the ground plane, when rotated a full circle, is an ellipse.[/quote]If you mean the measured magnetic field, then the magnetometer is not properly calibrated and has a significant relative gain error on one or more axes.

Otherwise, the comment doesn’t make much sense. The projection of a vector on the horizontal plane is just another vector. If the coordinate system is rotated in the horizontal plane, the tip of that vector describes a circle about the origin.

For calibration of magnetometers, I think this is currently the best approach: sailboatinstruments.blogspot.com … ation.html

You are right. The one that matters is the down vector -- which the library does take care of. As long as the bot isn't accelerating/turning and skewing the down vector...

It turns out I was having some soft and hard iron sources -- a 18" pole away, but still enough.

You are right. The one that matters is the down vector – which the library does take care of. As long as the bot isn’t accelerating/turning and skewing the down vector…

It turns out I was having some soft and hard iron sources – a 18" pole away, but still enough.