title: Strange magnetometer readout
body: <p>I am attempting to readout the data from my MPU-9150 magnetometer and getting some odd numbers. I have accessed the magnetometer within the IMU and am getting data which changes with the orientation of the IMU but is not within the range specified by the product specification guide. I'm think its probably something to do with either the variable type i am using to store the data or the method i am using to manipulate the twos compliment data to make it readable, so here is the code anyway...</p>
  
  <pre><code>void MPU9150::getMag(double* mag_X, double* mag_Y, double* mag_Z){
      uint8_t  asax, asay, asaz; 
  
      I2Cdev::writeByte(MPU9150_ADDRESS, INT_PIN_CFG, 0x02);//Set i2c bypass enable pin to true to access magnetometer.
      I2Cdev::writeByte(MPU9150_MAG_ADDRESS, MPU9150_MAG_CNTRL, 0x0f);//Fuse Rom access mode.
      I2Cdev::readBytes(MPU9150_MAG_ADDRESS, MPU9150_MAG_ASAX, 3, buffer);//Get sensitivity adjustment values.
      asax = buffer[0];
      asay = buffer[1];
      asaz = buffer[2];
  
      // Serial.print("asax = "); Serial.print(asax); Serial.print("\n");
      // Serial.print("asay = "); Serial.print(asay); Serial.print("\n");
      // Serial.print("asaz = "); Serial.print(asaz); Serial.print("\n\n");
  
      I2Cdev::writeByte(MPU9150_MAG_ADDRESS, MPU9150_MAG_CNTRL, 0X01);//Enable the magnetometer.
      delay(10);
      I2Cdev::readBytes(MPU9150_MAG_ADDRESS, MPU9150_MAG_XOUT_L, 6, buffer);//Read magnetometer readings.
      mx = ((((int16_t)buffer[0]) &lt;&lt; 8) | buffer[1]) * 0.3;
      my = ((((int16_t)buffer[2]) &lt;&lt; 8) | buffer[3]) * 0.3;
      mz = ((((int16_t)buffer[4]) &lt;&lt; 8) | buffer[5]) * 0.3;
  
      *mag_X = mx * ((((asax - 128)*0.5)/(128)) + 1);//Adjust readings with sensitivity adjustment values.
      *mag_Y = my * ((((asay - 128)*0.5)/(128)) + 1);
      *mag_Z = mz * ((((asaz - 128)*0.5)/(128)) + 1);
  </code></pre>
  
  <p>}</p>
  
  <p>The decimal range for each axis should be between 4096 and -4096, and there are 6 data registers each containing 8 bit high and low values for each axis. The data that i am getting seems to be in the range >10000 to &lt;-10000 with the highest value i have seen at 9830. If anyone has any ideas they would be appreciated, thanks :)</p>
  
  <p>EDIT: Buffer is a private integer array, used to hold the bytes from the axis registers. </p>
  
  <pre><code>private:
       uint8_t buffer[14];
  </code></pre>
  
  <p>Also reversing the order of the bytes (as i have realised should be done as the first byte in each register is low and the second high) has resulted in the range of the output reduced to a range of about -170 to 170...</p>
  
  <pre><code>mx = (((int16_t)buffer[1]) &lt;&lt; 8) | buffer[0];
  my = (((int16_t)buffer[3]) &lt;&lt; 8) | buffer[2];
  mz = (((int16_t)buffer[5]) &lt;&lt; 8) | buffer[4];
  </code></pre>