0

Skewed Compass Heading from LSM303 (9-DOF IMU)
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Skewed Compass Heading from LSM303 (9-DOF IMU)

by webkris on Sat Nov 22, 2014 4:02 pm

Greetings!

This one has me stumped.
Hardware is a simple Teensy 2.0 two pins (I2C) power and ground.
After getting some weird readings for heading while roll and pitch were working fine I loaded the "Compass Heading" into my AHRS sketch:
Code: Select all | TOGGLE FULL SIZE
sensors_event_t event;
    mag.getEvent(&event);
    float Pi = 3.14159;

  float heading = (atan2(event.magnetic.y, event.magnetic.x) * 180) / Pi;
  if (heading < 0) {
    heading = 360 + heading;
  }
  Serial.print("Compass heading: ");
  Serial.println(heading);
  Serial.print(F(""));


Results:
On the table facing North at 0° heading = 0
I rotate clockwise and at 90° it reads 50
Rotate clockwise again to 180° and it reads only 98
When I get to 270° the heading reads only 185
Going from 270° to 0° the readings go much faster - they count from 185 to 360 in the final 90° turn?!
Just to be clear - they count all the way and don't skip - at first I thought I had a math problem where 360 goes back to 0.

Calibration:
I'm somewhat frustrated here - because I've read that these are "pretty good without calibration" but the calibration sketch does not make it clear to me where the calibration numbers need to go. I need some help implementing the basic Min Max arduino calibration into the AHRS sketch. I've seen it in the pololu/lsm303-arduino library, but I'd like to stick with the adafruit libraries.

Note on magnetic interference:
I checked the table and area with my phones magnetometer. I moved the hardware and got the same readings in the air about 6 feet from my desk. I am pretty sure my neighbor doesn't have an MRI machine in his garage, but you never know these days. :p

Thanks!
- Kris

webkris
 
Posts: 2
Joined: Mon Nov 17, 2014 11:03 pm

Re: Skewed Compass Heading from LSM303 (9-DOF IMU)

by adafruit_support_mike on Sun Nov 23, 2014 1:50 am

You don't need an MRI next door.. a bottle cap 6" from the sensor can skew the readings significantly.

We're looking for ways to compensate for that, but it's a tricky problem.. there's no way to extract 'true north' from the overall sensor reading. Techniques for solving the problem do exist, but they're proprietary and valuable to the companies that have them.

The solution we know about involves rotating the sensor and taking the average of all the readings. The readings will all fall on the perimeter of a circle, but the center of the circle will be offset from (0,0) by the amount of the interference. Once you know that vector, you can use it to adjust later readings.

adafruit_support_mike
 
Posts: 64984
Joined: Thu Feb 11, 2010 2:51 pm

Re: Skewed Compass Heading from LSM303 (9-DOF IMU)

by marke3 on Mon Nov 24, 2014 4:50 am

I can vouch for this. I bashed my head against the wall and scoured every forum comment just bench testing. Then found one of those levitating pen desk ornaments in the drawer.

Then happy with the accuracy level It went on a rover and kept course nicely through the house until below a large electric motor 4 metres above in the ceiling and it went haywire.

But when one sensor is not enough there's plenty of others to add and usually a combination will get you there. I reckon I need ultrasonics and accelerometer to get to the back doors and then GPS can lend a hand.

marke3
 
Posts: 205
Joined: Sat Feb 08, 2014 5:24 pm

Re: Skewed Compass Heading from LSM303 (9-DOF IMU)

by adafruit_support_bill on Mon Nov 24, 2014 8:04 am

These days, tiny, yet incredibly powerful rare-earth magnets are inexpensive, and just about everywhere. They drive magnetometers nuts.

adafruit_support_bill
 
Posts: 84355
Joined: Sat Feb 07, 2009 10:11 am

Re: Skewed Compass Heading from LSM303 (9-DOF IMU)

by webkris on Mon Nov 24, 2014 11:57 am

I learned something new - but how I learned it is frustrating.

Two things...
1. I would love to have the ability to load the calibration numbers as asked.
2. I feel that the guide (that makes it seem like this is going to operate exactly like a compass - pictured) is unfinished and could include not only the calibration, but the fact that this doesn't really work like a "Metal needle compass" - and people building the Zax-O-Meter are going to run into every magnet, motor, and solenoid in the house and not actually head west. https://learn.adafruit.com/lsm303-accel ... -breakout/

I hope this is nothing but constructive.
Thanks,
- Kris

UPDATE:
If you're building a head tracker or a IMU with this board - just go here: http://www.pololu.com/product/2468/resources
The "Pololu_Open_IMU by mikeshub" was drag and drop. Even with fumbling the libraries I got accurate and useful compass data in minutes. Action video - http://youtu.be/tsXhuh09hko
Last edited by webkris on Mon Dec 01, 2014 2:59 pm, edited 1 time in total.

webkris
 
Posts: 2
Joined: Mon Nov 17, 2014 11:03 pm

Re: Skewed Compass Heading from LSM303 (9-DOF IMU)

by adafruit_support_bill on Mon Nov 24, 2014 12:07 pm

Thanks for the feedback. Although metal needle compasses are not subject to zero-G offset errors like the MEMs magnetometers, they are subject to local distortions of the magnetic field.

adafruit_support_bill
 
Posts: 84355
Joined: Sat Feb 07, 2009 10:11 am

Re: Skewed Compass Heading from LSM303 (9-DOF IMU)

by bambuino on Thu Mar 12, 2015 5:29 pm

Hello --
Is there an answer as to where the min/max calibration values are plugged in to the Adafruit libraries or the sketch itself? I have a 10-DOF that provided reasonable compass headings. I then had an FTDI board problem when loading the pitchrollheading example sketch to an ATMega1284P, swapped the FTDI board out, reloaded same sketch with slight change of what was getting printed to Serial, and was receiving compass readings 20 degrees off. So the IMU is out of calibration, and want to know how to fix this. Doing something with the min/max values for calibration is pretty straight forward on other brand IMUs. If I run the below sketch, what do I do with the values received?
Thanks.
EDIT: I loaded the ahrs example sketch and find orientation values opposite of what is received from the pitchrollheading example sketch. Swing the IMU west, serial output tells me I am turning east, pitch down and the signs are opposite of that printed out from pitchrollheading example.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_BMP085_U.h>

#include <Adafruit_L3GD20.h>

#include <Adafruit_Sensor.h>

#include <Adafruit_10DOF.h>

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>


/* Assign a unique ID to these sensors */
Adafruit_LSM303_Accel_Unified     accel = Adafruit_LSM303_Accel_Unified(54321);
Adafruit_LSM303_Mag_Unified       mag = Adafruit_LSM303_Mag_Unified(12345);

float AccelMinX, AccelMaxX;
float AccelMinY, AccelMaxY;
float AccelMinZ, AccelMaxZ;

float MagMinX, MagMaxX;
float MagMinY, MagMaxY;
float MagMinZ, MagMaxZ;

long lastDisplayTime;

void setup(void)
{
  Serial.begin(9600);
  Serial.println("LSM303 Calibration"); Serial.println("");
 
  /* Initialise the accelerometer */
  if(!accel.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    while(1);
  }
  /* Initialise the magnetometer */
  if(!mag.begin())
  {
    /* There was a problem detecting the LSM303 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    while(1);
  }
  lastDisplayTime = millis();
}

void loop(void)
{
  /* Get a new sensor event */
  sensors_event_t accelEvent;
  sensors_event_t magEvent;
 
  accel.getEvent(&accelEvent);
  mag.getEvent(&magEvent);
 
  if (accelEvent.acceleration.x < AccelMinX) AccelMinX = accelEvent.acceleration.x;
  if (accelEvent.acceleration.x > AccelMaxX) AccelMaxX = accelEvent.acceleration.x;
 
  if (accelEvent.acceleration.y < AccelMinY) AccelMinY = accelEvent.acceleration.y;
  if (accelEvent.acceleration.y > AccelMaxY) AccelMaxY = accelEvent.acceleration.y;

  if (accelEvent.acceleration.z < AccelMinZ) AccelMinZ = accelEvent.acceleration.z;
  if (accelEvent.acceleration.z > AccelMaxZ) AccelMaxZ = accelEvent.acceleration.z;

  if (magEvent.magnetic.x < MagMinX) MagMinX = magEvent.magnetic.x;
  if (magEvent.magnetic.x > MagMaxX) MagMaxX = magEvent.magnetic.x;
 
  if (magEvent.magnetic.y < MagMinY) MagMinY = magEvent.magnetic.y;
  if (magEvent.magnetic.y > MagMaxY) MagMaxY = magEvent.magnetic.y;

  if (magEvent.magnetic.z < MagMinZ) MagMinZ = magEvent.magnetic.z;
  if (magEvent.magnetic.z > MagMaxZ) MagMaxZ = magEvent.magnetic.z;

  if ((millis() - lastDisplayTime) > 1000)  // display once/second
  {
    Serial.print("Accel Minimums: "); Serial.print(AccelMinX); Serial.print("  ");Serial.print(AccelMinY); Serial.print("  "); Serial.print(AccelMinZ); Serial.println();
    Serial.print("Accel Maximums: "); Serial.print(AccelMaxX); Serial.print("  ");Serial.print(AccelMaxY); Serial.print("  "); Serial.print(AccelMaxZ); Serial.println();
    Serial.print("Mag Minimums: "); Serial.print(MagMinX); Serial.print("  ");Serial.print(MagMinY); Serial.print("  "); Serial.print(MagMinZ); Serial.println();
    Serial.print("Mag Maximums: "); Serial.print(MagMaxX); Serial.print("  ");Serial.print(MagMaxY); Serial.print("  "); Serial.print(MagMaxZ); Serial.println(); Serial.println();
    lastDisplayTime = millis();
  }
}

bambuino
 
Posts: 24
Joined: Fri Apr 18, 2014 8:03 am

Re: Skewed Compass Heading from LSM303 (9-DOF IMU)

by Adrian_Land on Sun Dec 13, 2015 11:32 pm

Hello Kris,

I was intrigued by your Update on this topic:
"The "Pololu_Open_IMU by mikeshub" was drag and drop. Even with fumbling the libraries I got accurate and useful compass data in minutes."

I'm very much in the 'fumbling with the libraries' mode; looks like the Open_IMU requires older versions of the LSM303 and L3G libraries than currently on github. Any suggestions on how to make the Open_IMU compile would be very much appreciated!

Regards
Adrian Land

Adrian_Land
 
Posts: 14
Joined: Sat Sep 27, 2014 1:00 pm

Please be positive and constructive with your questions and comments.