Coding for a non-leveled HMC5883L compass sensor?

The compass sensor returns X,Y,Z angles.

My embedded sensor board won't be lying horizontal inside my project, so I'd want basically to factor that out. I will move on a horizontal plane. I'd like to report the bearing from 0 to 360 degrees.

Some ideas come to mind:

  • Assume the chip's orientation is more or less fixed, somehow detect which (x,y,y) does the closest to a 360 when I turn the rig around, and use that as my rotation axis?

  • Build a coordinate system where I can get the angle between the before and after position vector. In math it would be defining a plane parallel to the 2 vectors, and computing the angle, based on the dX,dY,dZ angles. Caveat, I still want the angle of rotation oriented so a left turn and a right turn have different signs.

  • Fudge the 3 values by summing them or their squares, and using that as the rotation angle, if so what's the best math?

Of course I want to do this in a few cycles so fixed math or no math :stuck_out_tongue:

Thanks for any insight you may have.

Something about vector calculations is ringing a vague bell in my memory.
But methods are long lost, as I have celebrated my 43yr graduation anniversary now ...

Does it ring a louder bell on you ? :slight_smile:

An accelerometer can be used to correct a magnetometer for the deviation from level. This application note explains in great detail how to create a tilt-compensated compass using trigonometry. However, it is much easier to use vector methods, as described in the Pololu Arduino library here.

It is absolutely required to calibrate the magnetometer in its final environment. Two methods are
http://www.bot-thoughts.com/2011/04/quick-and-dirty-compass-calibration-in.html and
Sailboat Instruments: Improved magnetometer calibration (Part 1) (much better).

Thanks. The first "dirty" calibration URL gives me an idea on how to look at it...

I need something very approximate, as it's for art not science :stuck_out_tongue: