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...

**Rationale**

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?

**Calculations**

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.

Here the error is about 1.8° and the maximum error occurs at 90° and 270°. Naturally, I'm curious about the relationship between tilt and error. How much tilt can I get away with before errors get too high?

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.

Let's focus in on just pitch+roll angles versus error for the maximum error at 90 degrees heading..

You can see that above about 10 degrees, error starts to grow pretty quickly.

**Conclusion**

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

Great thorough analysis. Certainly seems like tilt-compensation isn't needed for this application. But there's also what looks like a neat product that does all the math on-board to provide tilt-compensated output for about $35: http://www.robotshop.com/devantech-tilt-magnetic-compass.html That way you don't spend $100's and you don't add the additional trig processing to your main controller. I've never used one, but I've bought one to play with soon. I'd love to know if anyone's used one of these and what their experience has been.

ReplyDelete@ViennaMike: Thanks! That's an interesting device. Looks like a PIC16F on the board? The only downside is the linear and centripetal acceleration but one could simply ignore the compass during turns and when accelerating/decelerating and use it to compensate for gyro drift only when heading rate is < some value. Hmmmmm....

ReplyDeleteP.S. Don't forget that your vehicle's electric motor (which probably has permanent magnets in it) is going to generate a magnetic field that can significantly impact your readings and will vary to the point that you can't do simple measurement compensation for it. You usually have to physically separate the sensor from the motor by many inches.

ReplyDelete!!Dean

@Dean : yes, good point. Don't worry, I didn't forget :) see: http://www.bot-thoughts.com/2011/12/magnetometers-and-motors.html

ReplyDelete