LSM303 Heading Calculations Problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
K1JOS
 
Posts: 13
Joined: Sat May 24, 2014 8:38 am

LSM303 Heading Calculations Problem

Post by K1JOS »

I am running the example shown in the LSM303 Adafruit webpage documentation for calculating compass heading. There are two problems: 1) the heading shows values greater than 360, so normalization to 0-360 is not working, and 2) the code is printing out the other values besides compass heading... seems to be z value plus a negative value.

Here are the SerialMonitor:
Compass Heading: 388.65466.00 248.00 -507.00
Compass Heading: 388.02464.00 249.00 -507.00
Compass Heading: 388.22462.00 248.00 -508.00
Compass Heading: 388.23467.00 252.00 -508.00

Here is the code I am using:

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

/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);


void setup(void)
{
Serial.begin(9600);

/* Enable auto-gain */
mag.enableAutoRange(true);

/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the LSM303 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
}

void loop(void)
{
/* Get a new sensor event */
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.print(heading);
delay(500);
}

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

Re: LSM303 Heading Calculations Problem

Post by adafruit_support_bill »

The code posted prints a single value after the string "Compass Heading". Your output is showing 3 values. That can't be the code that is actually running.

User avatar
K1JOS
 
Posts: 13
Joined: Sat May 24, 2014 8:38 am

Re: LSM303 Heading Calculations Problem

Post by K1JOS »

I think I figured it out. The example magsensor code in the Adafruit_LSM303DLHC library has:

void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);

/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
delay(500);
}

If I comment out all of the print Serial.print lines, I still get the three sets of numbers, so it seems that "mag.getEvent(&event)" puts it all onto the serial bus even without any Serial.print command. Since I have it now all going to an LCD I am getting a clean heading print onto the LCD.

I have another minor finding with the library code which i will post on a new Topic thread.

thanks
jerry

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

Return to “Arduino”