Having evaluated a couple of AHRS (attitude heading reference system) solutions, and after wondering if a compass was even necessary at all, another fundamental question arose. Suppose I do use a compass...
Do I really need a 3D, tilt-compensated compass in the first place? Or can I get away with a much simpler 2D compass? How much heading error can I expect from a 2D versus 3D solution?
Scott (Team Tobor), AVC winner for the last two years, employed a 2D compass heading calculation. Of course he also fed this value into an Extended Kalman Filter (EKF) along with odometry heading calculations.
What should I do? Time for more math, Octave (Matlab), and Gnuplot fun. Let's figure this out...
I'm not building an airplane, I'm building a car that will drive on relatively flat ground. That's important because it's substantially easier to compute a 2D compass heading than a 3D one for a moving vehicle.
A 2D heading is calculated by taking the arctangent of the ratio of the y-axis and x-axis readings: atan2(mag_y/mag_x). That's really easy.
Computing an accurate 3D compass heading for a moving vehicle requires an orientation estimate which relies on an IMU and complex algorithms to fuse gyro and accelerometer signals. In some cases one must consider the effects of acceleration on the accelerometers.
Is it worth the effort? I mean, how bad can the error of a 2D compass be?
Let's say the maximum tilt of the sensor with respect to "level" is 10 degrees of pitch. What's the error of a 2d heading for a range of true headings from 0 to 180 degrees?
I wrote an Octave script that iterated from 0 to 180 degrees of actual heading and computed the error between actual heading and 2d heading for a range of vehicle tilt angles.
How to model this? Start with a magnetic field vector of magnitude 1 pointing north and rotate it by 50° of pitch to represent magnetic inclination. Then rotate it counterclockwise about the z-axis in the direction by the true heading.
The result is the normalized vector that would be read by our simulated magnetometer. For example, if the magnetometer is at a 45° heading, the vector will point approximately 50° down and 45° counterclockwise. In other words, this new vector is the magnetic vector is expressed in the sensor frame of reference.
To simulate a pitching magnetometer, I rotated the magnetic vector about the y-axis, then calculated the heading using only the x and y components of the magnetic field vector. Finally, I determined the error between the actual heading and the 2d heading.
Below is a plot of heading error versus heading versus tilt, with tilt values of 0-10 where roll=pitch=tilt, and heading from 0 to 360 degrees.
You can see that above about 10 degrees, error starts to grow pretty quickly.
Somewhere around 5° is probably a safe limit and that's a lot of tilt. It even looks steep. A 6% grade is only a 3.4° while 5° would be an 8.7% grade. In fact, I surveyed the slopes at the Sparkfun building parking lot; they looked terrible, but the angles are generally 3° or less.
|Informal SFE site survey, most angles are 0-3°|
But that's not the whole story because the compass isn't the only possible heading sensor anyway. In fact, relying on compass as the only heading sensor is probably unwise as it's subject to local field disturbances from ferrous objects like the drainage covers on the sidewalk around the SFE building.
|Source of compass distortion. Don't drive too close!|
In such a scenario, tilt will have a less significant and less immediate effect on heading error so that, for brief maneuvers or short stretches of uneven ground, the error may be acceptably low.
Here's the Octave script if you're interested: TiltCompTest.m