Wednesday, March 9, 2011

AVC Bot: Adding a Compass

From previous posts you probably deduced that Data Bus, my 2011 Sparkfun AVC entry, now has a compass. I forgot to write about its selection and installation.

Experience with gyro data collection led me to the conclusion that a compass would be really helpful with vehicle navigation. Why?

A gyro tends to have drift that can be hard to predict. GPS updates come too infrequently and are too unreliable as a reference source. A compass offers frequent updates (20Hz  in this case) and a reliable source of steady state heading information.

Selecting a Compass

Having spent a fairly big pile of money on this robot so far and with my hobby fund dwindling rapidly, a high end compass was out of the question.

Two cheap options presented themselves, a simple Honeywell HMC6352 two-axis magnetometer/compass and a 3-axis magnetometer from Sparkfun.

I had (and still have) my hands full with other mathematical problems that are smacking me to the curb, so having to figure out an algorithm for a tilt-compensated compass was very unappealing.

Plus, the robot is going to be on flat ground at all times and body roll should be minimal. A tilt-compensating compass would be nice but the robot can live without one.

The Honeywell compass won out. It's ridiculously simple to use.

Interfacing the Compass to mbed

The compass is an I2C based device. At it's simplest, you query and it tells you your magnetic heading. To make matters easier, someone had already written a nice, full-function, mbed driver library. So I used it. Like this:

#include "HMC6352.h"

HMC6352 compass(p28, p27);              // Driver for compass

int main() {
    // Initialize compass; continuous mode, 
    // periodic set/reset, 20Hz measurement rate.
    compass.setOpMode(HMC6352_CONTINUOUS, 1, 20);

    // read compass, convert to float
    compassHdg = compass.sample() / 10.0;       

    // Correct for local declination
    compassHdg -= declination;

    // Clamp to 0-360
    if (compassHdg < 0.0) compassHdg += 360;    

    // ...
}

Mounting

Compasses are sensitive to iron and current. I mounted mine as far away from screws, the main battery cables, and the steering servo as possible. In fact, I rotated the battery 180° so that the main cables exit the back of the robot, far, far away from the compass.

The compass is currently affixed with double-sided tape to a 2" high "bridge" made of brass that's mounted to the main lexan base. It's final location and mounting scheme are still in the works.

Magnetic Declination

If you've played with compasses at all, you probably know that the error between magnetic and true heading varies all over the globe because the Earth's magnetic field isn't perfectly uniform. The error is called magnetic declination.

If only one could figure out how much the error was at any geographic location... but wait: you can!

Not only is there an online calculator available, but also Geomagix, a downloadable trial application for Windows.

There's more. A magnetic survey was conducted just a year ago in 2010, meaning the utmost in accurate information. How cool is that?

Download the Geomagix code and the 2010 cof file and you're good to go.  Declination at my house: +8.91305°

Now I can configure the robot with the correct declination at the Sparkfun Building or wherever else Data Bus runs.

1 comment:

  1. Just a followup note: I ended up not liking this compass. You can search for info on its built-in calibration. I don't recall ever getting it to work very well. I think a better choice is an HMC5843 although these are now replaced by the HMC5883. You have more control and flexibility. I'm still struggling with compass issues, so if I ever feel like I have the compass issue nailed, I'll be sure to post up an article.

    ReplyDelete