I am trying to use an LSM303DLHC as a compass, but I the readings are not correct. I am not sure if the problem is with my setup or the math. I am using the Compass example sketch from Adafruit.\
I have the LSM303 attached to a Stepper motor. The LSM303 is mounted horizontally, so the sensor chip is facing down. I would say the LSM303 is not perfectly level, nor perfectly centered on the axis of the Stepper Motor, but it's close. The motor turns 180*, pauses for 3 seconds and I record the x, y, z and calculated heading readings. Visually, it looks like the stepper is turning 180* - it may not be perfect, but it's very close. I can see it lands in the same spot with each rotation.
Here are the mag bits:
#include <Adafruit_LSM303DLH_Mag.h>
#include <Adafruit_Sensor.h>
Adafruit_LSM303DLH_Mag_Unified mag = Adafruit_LSM303DLH_Mag_Unified(12345);
/* Initialise the sensor /
if (!mag.begin()) {
/ There was a problem detecting the LSM303 ... check your connections */
Serial.println("problem...");
while (1)
;
}
}
void loop(void) {
sensors_event_t event;
mag.getEvent(&event);
float Pi = 3.14159;
// Calculate the angle of the vector y,x
Serial.print(event.magnetic.x);
Serial.print(" ");
Serial.print(event.magnetic.y);
Serial.print(" ");
Serial.print(event.magnetic.z);
Serial.print(" ");
float heading = (atan2(event.magnetic.y, event.magnetic.x) * 180) / Pi;
// Normalize to 0-360
if (heading < 0) {
heading = 360 + heading;
}
Serial.print(heading);
Serial.println();
}
The problem I'm having, it it takes 6 readings (3 rotations) for the readings to be the same. I would think that turning 180*, the readings should be the same every other pause. This makes me think it's the math, but again - I took this code from the Adafruit Compass example.
I have included the output below. You can see that it repeats the same 6 readings (within a degree or so). If it starts at 180*, i would expect the next reading to be 0*, and the third to be 180*, then 0*, etc...
Again, I'm trying to use this as a fairly accurate compass. Any suggestions on what is wrong?
Output Readings:
X Y Z Heading
-33.09 -0.18 63.88 180.31
-21.27 -101.82 71.63 258.2
-23.91 31.82 94.59 126.92
11.55 -33.91 63.57 288.8
0.36 67.45 67.04 89.69
9.36 -67.09 90.1 277.95
-33.73 0 63.98 180
-21.55 -101.55 71.84 258.02
-23.36 32.27 93.98 125.9
11.09 -33.27 61.22 288.43
0.45 68.36 66.73 89.62
8.64 -65.73 90.31 277.49
-33.45 -0.36 63.78 180.62
-21.27 -101.82 71.53 258.2
-23.82 31.82 94.59 126.82
11 -34.18 64.29 287.84
0.73 68.18 67.04 89.39
9.09 -67.36 90.2 277.69
-32.73 0.45 64.18 179.2
-21.73 -101.18 72.24 257.88
-23 32.36 94.49 125.4
11.91 -33.45 61.02 289.59
0.09 68.36 66.43 89.92
9.27 -65.91 91.12 278.01
-33.27 0.09 63.78 179.84
-21.18 -101.73 71.33 258.24
-24.18 31.73 94.59 127.31
11.73 -34.09 64.08 288.98
0.36 67.73 66.63 89.69
9.55 -67.18 90 278.09
-33.82 -0.27 63.78 180.46
I did another test, and rather than rotating 180*, pausing/reading and then continuing another 180*, I instead rotate 180*, then rotate backwards 180*.
In this test, the numbers are consistent. For example:
Rotate forward 180*: x = 32, y = -15, z = -84
Rotate back 180*: x = -42, y = -38, z = -45
Rotate forward 180*: x = 32, y = -15, z = -84
Rotating back and forth 180* gives me the same sets of readings at teach location.
So it is consistent, but if it takes 3 full rotations to get back to the same number, how can I use it as a compass?
Thanks,
-Matt