Tilt compensation when reading a digital compass

If you’re familiar with using a compass (the tool that points to magnetic north, not the one that makes circles) the concept of holding the device level makes sense. It must be level for the needle to balance and rotate freely. You just use your eyes to make sure you’re holding the thing right. Now think of a digital compass. They work by measuring the pull of a magnetic field, and have no visual method of showing whether they’re level or not. To ensure accurate readings you might use an accelerometer to compensate for a tilted magnetometer.

The process involves taking measurements from both an accelerometer and a magnetometer, then performing calculations with that data to get a true reading. Luckily the equations have been figured out for us and we don’t need to get too deep into trigonometry. You will, however, need to use sine, cosine, and arctangent in your calculations. These should be available in your programming language of choice. Arduino (used here) makes use of the avr-libc math library to perform the calculations.

How fortunate that this article came up! I’m beginning work on a project for exactly this, and I have a question…. Most applications are dealing with relatively static forces such as an RC pan/tilt platform which comes to mind for some reason. A motorcycle both tilts and experiences G-force (acceleration) through a corner, and I’m building such a compass for next spring. My problem is that the gauges have too strong a field to mount a typical magnet compass anywhere near the gauges, hence it would not be within sight if placed where it works. So I have to place the magnetometer/accelerometer remotely and put only a display at the gauges. I have NOT gone through the math in detail as yet as I’m still selecting components, but the question does arise that cornering G-force may or may not be compensated for by the accelerometer. At face value it seems it will be, but then again as applied it seems the accelerometer is only measuring direction of gravity (tilt) and G-forces add a whole new complexity that may require speedometer input to compensate for.
Opinions? Any lucid experts on the subject?

Sounds to me like its going to take adding a rate gyro to note the cycle’s change in direction and use that to correct the accelerometer output to establish a correction factor and derive the actual tilt of the system for the compass. Then again I note that a magnet compass is “upset” during a corner and gives unusable readings till you settle out to level and straight. This means that for my application use of an accelerometer overcomplicates things and adds a rate gyro to compensate, when a plain old magnet compass has problems during the very same turn. Hence, best bet would be to just use a plain 2 or 3 axist magnetometer to get the same accuracy as a magnet compass and accept that it’s valid only during straight line travel.

Somebody has to add, that accelerometers can’t distinguish tilt from acceleration. So if acceleration may be present, you need to add rate-sensors (gyros) and use them for the calculations. …my 2 cents.

@JohnnyMax
I must admit that I did not read the article before posting my comment…
Best wishes to loveelectronics to get it right : the AHRS algorithm in standard Arduino programming will be a really nice thing.

Magnetic declination compensation requires knowing your position on the globe and looking up the declination correction factor on a reference map. It is not not compensated for in the magnetometer. It would require GPS and a lookup table.

Being someone who has used trig functions on both AVR and arduino, they introduce a rather large error due to rounding in the math library. To do this more accurately, you need a lookup table, math co-processor, or a 32bit platform, imho.