LSM303 Magnet 2's complement

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Mamba
 
Posts: 17
Joined: Mon Mar 02, 2015 12:52 am

LSM303 Magnet 2's complement

Post by Mamba »

I'm integrating the LSM 303 into MyRIO
But with the 2's complement value I'm having problems.
I tried out my 2's complement transformation it works fine.
But I don't get the same values when I compare it to the arduino readings. My values are in between -32768 and 32767.
I looked up in the sensor libaries but didn't find the conversion. Where is it written?
Am I right that the first byte is the higher one?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 Magnet 2's complement

Post by adafruit_support_rick »

The LSM303 sends the low-order byte first.

User avatar
Mamba
 
Posts: 17
Joined: Mon Mar 02, 2015 12:52 am

Re: LSM303 Magnet 2's complement

Post by Mamba »

Thanks for the Reply Rick, but this order I already took into account. Is there no other data manipulation?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 Magnet 2's complement

Post by adafruit_support_rick »

The library does this:

Code: Select all

void Adafruit_LSM303::read()
{
  // Read the accelerometer
  Wire.beginTransmission((byte)LSM303_ADDRESS_ACCEL);
  Wire.write(LSM303_REGISTER_ACCEL_OUT_X_L_A | 0x80);
  Wire.endTransmission();
  Wire.requestFrom((byte)LSM303_ADDRESS_ACCEL, (byte)6);

  // Wait around until enough data is available
  while (Wire.available() < 6);

  uint8_t xlo = Wire.read();
  uint8_t xhi = Wire.read();
  uint8_t ylo = Wire.read();
  uint8_t yhi = Wire.read();
  uint8_t zlo = Wire.read();
  uint8_t zhi = Wire.read();

  // Shift values to create properly formed integer (low byte first)
  accelData.x = (xlo | (xhi << 8)) >> 4;
  accelData.y = (ylo | (yhi << 8)) >> 4;
  accelData.z = (zlo | (zhi << 8)) >> 4;
  
  // Read the magnetometer
  Wire.beginTransmission((byte)LSM303_ADDRESS_MAG);
  Wire.write(LSM303_REGISTER_MAG_OUT_X_H_M);
  Wire.endTransmission();
  Wire.requestFrom((byte)LSM303_ADDRESS_MAG, (byte)6);
  
  // Wait around until enough data is available
  while (Wire.available() < 6);

  // Note high before low (different than accel)  
  xhi = Wire.read();
  xlo = Wire.read();
  zhi = Wire.read();
  zlo = Wire.read();
  yhi = Wire.read();
  ylo = Wire.read();
  
  // Shift values to create properly formed integer (low byte first)
  magData.x = (xlo | (xhi << 8));
  magData.y = (ylo | (yhi << 8));
  magData.z = (zlo | (zhi << 8));  
  
  // ToDo: Calculate orientation
  magData.orientation = 0.0;
}

User avatar
Mamba
 
Posts: 17
Joined: Mon Mar 02, 2015 12:52 am

Re: LSM303 Magnet 2's complement

Post by Mamba »

accelData.x = (xlo | (xhi << 8)) >> 4;
The “>>4” shifts the two bytes 4 bit to the right.
But what happens with those 4 bits.
Is the accel information just in 12 bits?


_______
Magnetometer
In a previous try, I had this shifting.
But the readings were too low (see attachment).
Placing a second sensor next to it and reading it out via an arduino: MAG X: 7.91 Y: -31.27 Z: 28.47 uT

I checked out my 2's complement. It works fine. Therefore I can't explain my self the difference.
Attachments
magnet_reading.gif
magnet_reading.gif (347.74 KiB) Viewed 532 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 Magnet 2's complement

Post by adafruit_support_rick »

Are you using unsigned bytes for the raw data? The right shifting should be an unsigned operation.

User avatar
Mamba
 
Posts: 17
Joined: Mon Mar 02, 2015 12:52 am

Re: LSM303 Magnet 2's complement

Post by Mamba »

accel_Output.gif
accel_Output.gif (334.34 KiB) Viewed 513 times
So this my accel Output. As you can see 1 G is around 1000 why is that?

And referring to your last post. The right shifting is just used for accel data, but nevertheless I use unsigned bytes for both accel and magnet raw data.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 Magnet 2's complement

Post by adafruit_support_rick »

BANNED wrote:As you can see 1 G is around 1000 why is that?
The sensor reports milliG. So 1000 is 1G

User avatar
Mamba
 
Posts: 17
Joined: Mon Mar 02, 2015 12:52 am

Re: LSM303 Magnet 2's complement

Post by Mamba »

Thanks a lot Rick for your help.
Now the accel part is done.
Could you tell me where you've found out, that it's a 12 bit 2's complement?
I see nowhere a hint in the data sheet.

For the magnet and the gyro:
The only data manipulation there, is the left shifting (16 bit 2's complement), isn't it?
Do you have another idea besides the unsigned operation, why I don't get the correct values?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 Magnet 2's complement

Post by adafruit_support_rick »

No, the data sheet doesn't seem to have anything to say about the data format for the accelerometer.
I just looked at the Adafruit library. I don't know where the library author got the information.

I don't know why you don't get the correct values for the magnetometer. Again, all I have for reference is the datasheet and the adafruit library.

We actually have two versions of the library. You can check them out - maybe you'll find a hint:
https://github.com/adafruit/Adafruit_LSM303DLHC
https://github.com/adafruit/Adafruit_LSM303

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”