Friday, March 18, 2011

Compass With Tilt Compensation

After I get the GPS logger shield working the next objective is the compass. With the HMC6343, I'll get heading and pitch (or heel angle in a sail boat). From reading a little bit around the internet, it seems that this compass works very well, but you really have to watch out for motors and magnets. The interference screws up the heading results.



Compass communicates using the Wire Library

Code to test Compass attached to arduino
this is from a chinese robotics site, but the code is still the same.

Code Robbie Edwards in their sailboat:
// I2C related constants and variables
// Library includes

#include
#define HMC6343Address 0x32
// Shift the device's documented slave address (0x32) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)
int slaveAddress = HMC6343Address >> 1; // This results in the 7 bit address to pass to TWI
// Compass measurement related constants and variables
byte headingData[6];


void init_compass(void)
{
Wire.begin(); // Open I2C to HMC6343
}

void getCompassData(void)
{
int i;
Wire.beginTransmission(slaveAddress);
Wire.send(0x50); // Send a "Get Data" (0x50) command to the HMC6343
Wire.endTransmission();

delay(2); // The HMC6343 needs at least a 1ms (microsecond) delay
// after this command. Using 2ms just makes it safe

// Read the 6 heading bytes, MSB first
// The resulting 3 16bit words are the compass heading, the tilt and roll in 10th's of a degree
// For example: a heading of 1345 would be 134.5 degrees

Wire.requestFrom(slaveAddress, 6); // Request the 6 bytes of data (MSB comes first)
i = 0;
while(Wire.available() && i < headingvalue =" headingData[0]*256" tiltvalue =" headingData[2]*256" rollvalue =" headingData[4]*256" vbthetac="headingValue/10;">

No comments:

Post a Comment