LSM303DLHC Problems with Calibration

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
daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

LSM303DLHC Problems with Calibration

Post by daz1761 »

I'm having trouble trying to set my LSM303DLHC as a tilt compensated compass. Firstly, I looked at the official tutorial that involved a using a servo motor. This wasn't what I had in mind so I followed it so far up until the calibration page, but got a little confused as there seems to be a sketch that lets you gather the mins and max's of all the axis etc. but has no sketch where you can drop them constants in.

I had a search and found this library that collects the mins and max's etc. then provides a sketch so you can drop them constants it:

https://github.com/pololu/LSM303

I've gone through many calibrations (rotate every axis slowly without adding acceleration) then got my iPhone compass up and running and compared my results from the serial monitor to the iPhone's North headings.

Results:

iPhone heading pointing North = 0 deg
X axis is inline with gravity pointing North = ~15 deg

iPhone heading pointing North = 0 deg
Y axis is inline with gravity pointing North = ~285 deg

iPhone heading pointing North = 0 deg
Z axis is inline with gravity pointing North = ~90 deg

I wasn't expecting 100% precision as its not fatal, but to be close would be nice as this project does need to simulate a mobile phone compass.

My next option was to explore the Fabio V link, but sadly after numerous clicking I could not actually download anything. I found he has a video on You Tube which walks you through how to setup a Processing sketch with one of his Arduino sketches, but all his version of the compass did was just control a 3d square. I also cant try the app (although he states is buggy) as I am using a mac and wouldn't know what to do with the values anyway.

Image

Code: Select all

#include <Wire.h>
#include <LSM303.h>

LSM303 compass;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  compass.init();
  compass.enableDefault();
  
  // Calibration values. Use the Calibrate example program to get the values for
  // your compass.
  compass.m_min.x = -371; compass.m_min.y = -469; compass.m_min.z = -486;
  compass.m_max.x = 757; compass.m_max.y = 645; compass.m_max.z = 475;
}

void loop() {
  compass.read();
  int heading = compass.heading((LSM303::vector){0,-1,0});
  Serial.println(heading);
  delay(100);
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: LSM303DLHC Problems with Calibration

Post by adafruit_support_bill »

One question to ask is whether the phone compass automatically compensates for magnetic declination. Depending on your location, this can be quite a bit off from magnetic north.

This thread has some code for utilizing the calibration constants with the LSM303
http://forums.adafruit.com/viewtopic.ph ... te#p219276

daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Re: LSM303DLHC Problems with Calibration

Post by daz1761 »

Thanks

I'm not 100% sure about it compensating for magnetic declination. I know I've had a real life compass in here on another day and magnetic North always seems to point in the same direction.

I've just tried copying and pasting the code from the other thread, and the compiler is throwing a wobbler saying that "Adafruit_LSM303 lsm;" was not declared in this scope? I do have the library in my examples list. Also my test readings from the initial calibration sketch are way different to mine.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: LSM303DLHC Problems with Calibration

Post by adafruit_support_bill »

Hmmm. Looks like he's using the old Adafruit_LSM303.h library. I believe that gives raw output, not scaled to SI units like the DLHC version of the library.

daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Re: LSM303DLHC Problems with Calibration

Post by daz1761 »

Sounds ominous, I thought it was too good to be true haha. Is the older library available and compatible with the newer version of the device?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: LSM303DLHC Problems with Calibration

Post by adafruit_support_bill »

Yes. The old library is still here: https://github.com/adafruit/Adafruit_LSM303
It works with all versions of the device.

daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Re: LSM303DLHC Problems with Calibration

Post by daz1761 »

Cool, I'll give it a try :)

daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Re: LSM303DLHC Problems with Calibration

Post by daz1761 »

Weird. I've moved the up to date LSM303 library from my examples folder to my desktop as it was named the same as the older one you just linked me up to, and all it contains is a sketch called 'test', which just prints what looks like RAW values. However, totoro17's has put a link in his thread where he got his min / max's, and the link takes you to the same calibration page where I got my fractional mins and max's :?

I wonder how he got them large values and if so, are they the accelerometer or the magnetometer mins and max's.

On a positive note, I don't get a compiler error with his code now.

Edit:

In fact I've just run the sketch as it was (copied and pasted) and I'm getting 4 readings mapped into 4 columns with no idea what they correlate to.

Totoro17
 
Posts: 12
Joined: Sun Nov 04, 2012 7:49 pm

Re: LSM303DLHC Problems with Calibration

Post by Totoro17 »

Holy moly someone used my code and found it useful! That's awesome!

The min/max values I posted in that thread were accelerometer values.

I've used the same code for the LSM303 magnetometer, but I haven't had as much luck on that front yet because I've found that the min and max magnetometer values are not as stable: they change significantly when the magnometer is close to the AAA battery pack and anything metallic. Readings are very sensitive to proximity to my laptop and cell phone. And my min/max values change from day to day for other reasons I haven't figured out yet, but I suspect that I'm doing something silly.

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

Return to “Other Arduino products from Adafruit”