SEN 10724 - Compass heading?

I am incredibly confused, I've built a robot using an Arduino uno and i'm using a template provided by robotics professor at the university. I have connected my 9dof sensor stick to the robot and can get outputs from the 9dof stick. My problem is, i'm getting three numbers, presumably x,y,z from the magneto readings, how do I turn these into a single heading for my robot with say 0 as magnetic north? I have tried using the HCL5883L library and an online guide for creating a digital compass but I couldn't get this to work either? This is my current code, very very messy as I keep adding bits: [Pastebin link

](http://pastebin.com/hq5eq7np)

Ok, so if we are assuming that Z is negligible because the robot will always remain flat on the ground then I should be receiving opposite values for x and y when i rotate the robot by 180 degrees and then I should be able to write a bit of code to turn this into degree’s, however this does not seem to be the case.

robot facing north:

c, -92, 490, -142

south:

c, -92, 490, -270

east:

c, -21, 497, 194

west:

c, -160, 490, -208

I take it then that negative and positive outputs do not equate to forward and backward so i’m not sure how I would go about transposing these values into a singular compass heading

You should look at the compass chip data sheet and determine how the X and Y axes are oriented with respect to the robot. Then you need to calibrate the compass. Headings will be meaningless until this is done. Here are two procedures, in order of increasing accuracy: http://www.bot-thoughts.com/2011/04/quick-and-dirty-compass-calibration-in.html http://sailboatinstruments.blogspot.com/2011/08/improved-magnetometer-calibration.html

Thanks for the reply, i've been staring at the Datasheet for the past 20 mins actually but I can't work out exactly what i'm looking for. I've also been looking at the example code that i've incorporated into my coding and I can't work out how to get the individual magnetometer readings apart to use them in functions. I believe its something to do with the union XYZBuffer line but i'm not 100% sure

What is sitting next to your sensor ?

have not tried this code… Different gain used

#include <Wire.h>
#define COMPASS 0x1e
int x,y,z;
byte buff[6];
int xn,yn,zn =5000; // miNimun values
int xm,ym,zm = -5000; // Maximum

void setupCompass()
{
  Wire.beginTransmission(COMPASS);
  Wire.write(0x00); Wire.write(0x70);
  Wire.endTransmission();
  Wire.beginTransmission(COMPASS);
  Wire.write(0x01); Wire.write(0x20); //gain +/- 1.3 Ga
  Wire.endTransmission();
  Wire.beginTransmission(COMPASS);
  Wire.write(0x02); Wire.write(0x00); //  Reading
  Wire.endTransmission(); 
  delay(6);
}
void readCompass() 
{
  Wire.beginTransmission(COMPASS);
  Wire.write(0x03); //  Read from byte 3 on
  Wire.endTransmission(); 
  Wire.requestFrom(COMPASS, 6);      
  delay(5);
  for (int i=0;i<6;i++) buff[i]=Wire.read();
  x=buff[0]<<8+buff[1];
  y=buff[2]<<8+buff[3];
  z=buff[4]<<8+buff[5];
  xn=min(xn,x);
  yn=min(yn,y);
  zn=min(zn,z);
  xm=max(xm,x);
  ym=max(ym,y);
  zm=max(zm,z);
}

void setup()
{
  Serial.begin(9600);  // start serial for output
  Wire.begin();        // join i2c bus (address optional for master)
  setupCompass();
}

void loop()
{
  readCompass();
  Serial.print("X :");
  Serial.print(xn);
  Serial.print(" / ");
  Serial.print(xm);
  Serial.print("   Y :");
  Serial.print(yn);
  Serial.print(" / ");
  Serial.print(ym);
  Serial.print("   Z :");
  Serial.print(zn);
  Serial.print(" / ");
  Serial.println(zm);
  delay(200); // ?? short delay
}