So I'm testing LSM303DLH pololu that is connected to Arduino Uno
Here is my code below (I'm trying to get the LSM303DLH to read the correct heading value)
#include <Wire.h>
#include <LSM303DLH.h>
LSM303DLH compass;
void setup() {
Serial.begin(9600);
Wire.begin();
compass.enableDefault();
// Calibration values. Use the Calibrate example program to get the values for
// your compass.
compass.m_min.x = -90; compass.m_min.y = -90; compass.m_min.z = 0;
compass.m_max.x = +90; compass.m_max.y = +90; compass.m_max.z = 359;
}
void setOffset ( float x,
float y,
float z
) {}
void loop() {
compass.read();
int heading = compass.heading((LSM303DLH::vector){0,-1,0});
Serial.println(heading);
delay(3000);
}
And I try to verify with a magnetic compass that I bought from local store. When the magnetic compass reads the north value of 0 degrees, my LSM303DLH reads the north value as 30 degrees.
Is there a way to get my LSM303DLH heading values to match up to my magnetic compass and have them both spit out the same heading values?
For my LSM303DLH, the north starts at the corner tip but that is not what I want... I want the north to start 0 degrees when I stand my LSM303DLH in vertical position like this...
I don't want my LSM303DLH to give me reading of 0 degrees at the corner tip... I want it to read 0 degrees value when I stand it up like the one in the link...
Is there any way possible for me to do that? like any specific changes in the code I have to do?
Help would be really appreciated. Thank you in advance.
I'd have expected it to have the X/Y/Z axes aligned with the board geometry and give a zero (North) heading when one of the edges of the board was point North.
Clearly it isn't doing that, so I wonder whether it's actually set up and calibrated correctly.
Apart from zero not matching North, is it producing sensible values? I mean, if you turn it through 90, 180, 270, 360 degrees does the heading output go up by the corresponding amount? In particular, does it go through 358, 359, 0, 1 2 ... and the same in reverse when you turn it the other way? If it is, then you could get the heading you want just by aligning the board with your North, see what reading it gives, and then change your code to subtract that value from the reported heading to give you the true heading.
This will give you the right heading, but it won't be a value between 0 - 360. If you want to end up with a value between 0 - 360 then use the mod operator ('%'). So your code fragment would look something like this:
// NORTH_HEADINGĀ is whatever heading the unit reports when you point it North
#define NORTH_HEADING 17
reportedHeading = compass.heading(...);
trueHeading = ((reportedHeading - NORTH_HEADING) + 360) % 360;
// trueHeading is now a number between 0 - 360 with 0 corresponding to North
Different implementations of mod (%) treat negative values in different ways. Since I don't know what the Arduino implementation does (and the language reference doesn't say either) I added 360 to make sure the value was never negative.
Thank you all to improve code. I tried it and I have one question. The code is working very good, but when is around 0 is bouncing for about +- 50 degrees. Anybody of you know why?