Hi.
I am using your LSM9DS1 Breakout and the sample Arduino library (LSM9DS1_Basic_I2C.ino) to get the x, y, z value and the heading data from the magnetometer. However, the heading does not become zero when it is facing North. Also, the range of the heading is not from 0-360 degrees and the value does not increase when instead it is supposed to go from 0 to 90 to 180 to 270 to 360. (Please see attached file for the sample output)
Sample Code:
Edited by moderator to add code formatting.
void loop()
{
// Update the sensor values whenever new data is available
if ( imu.gyroAvailable() )
{
// To read from the gyroscope, first call the
// readGyro() function. When it exits, it'll update the
// gx, gy, and gz variables with the most current data.
imu.readGyro();
}
if ( imu.accelAvailable() )
{
// To read from the accelerometer, first call the
// readAccel() function. When it exits, it'll update the
// ax, ay, and az variables with the most current data.
imu.readAccel();
}
if ( imu.magAvailable() )
{
// To read from the magnetometer, first call the
// readMag() function. When it exits, it'll update the
// mx, my, and mz variables with the most current data.
imu.readMag();
}
if ((lastPrint + PRINT_SPEED) < millis())
{
printGyro(); // Print "G: gx, gy, gz"
printAccel(); // Print "A: ax, ay, az"
printMag(); // Print "M: mx, my, mz"
// Print the heading and orientation for fun!
// Call print attitude. The LSM9DS1's mag x and y
// axes are opposite to the accelerometer, so my, mx are
// substituted for each other.
printAttitude(imu.ax, imu.ay, imu.az,
-imu.my, -imu.mx, imu.mz);
Serial.println();
lastPrint = millis(); // Update lastPrint time
}
}
void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
{
float roll = atan2(ay, az);
float pitch = atan2(-ax, sqrt(ay * ay + az * az));
float heading;
if (my == 0)
heading = (mx < 0) ? PI : 0;
else
heading = atan2(mx, my);
heading -= DECLINATION * PI / 180;
if (heading > PI) heading -= (2 * PI);
else if (heading < -PI) heading += (2 * PI);
else if (heading < 0) heading += 2 * PI;
// Convert everything from radians to degrees:
heading *= 180.0 / PI;
pitch *= 180.0 / PI;
roll *= 180.0 / PI;
Serial.print("Pitch, Roll: ");
Serial.print(pitch, 2);
Serial.print(", ");
Serial.println(roll, 2);
Serial.print("Heading: "); Serial.println(heading, 2);
}
I have also tried using MPU9250 Breakout and switching arctan2(mx, my) to arctan2(my, mx) since that is what other people had , but that didn’t fix the problem.
Is there something wrong with the sample formula. I have researched other people’s work, but the majority have gotten the correct heading using the same formula.
It will be great if we can get your help. Thank you very much.
LSM9DS1_Sample_Output.txt (5.53 KB)