Interface library for STMicro LSM303DLH 3-axis magnetometer w/ 3-axis acceleromter. Computes magnetic heading.

Fork of LSM303DLH by Michael Shimniok

Embed: (wiki syntax)

« Back to documentation index

LSM303DLH Class Reference

LSM303DLH Class Reference

Tilt-compensated compass interface Library for the STMicro LSM303DLH 3-axis magnetometer, 3-axis acceleromter. More...

#include <LSM303DLH.h>

Public Member Functions

 LSM303DLH (PinName sda, PinName scl)
 Create a new interface for an LSM303DLH.
void setOffset (float x, float y, float z)
 sets the x, y, and z offset corrections for hard iron calibration
void setScale (float x, float y, float z)
 sets the scale factor for the x, y, and z axes
void read (vector &a, vector &m)
 read the raw accelerometer and compass values
float heading (void)
 returns the magnetic heading with respect to the y axis
float heading (vector from)
 returns the heading with respect to the specified vector
void frequency (int hz)
 sets the I2C bus frequency

Detailed Description

Tilt-compensated compass interface Library for the STMicro LSM303DLH 3-axis magnetometer, 3-axis acceleromter.

Michael Shimniok http://bot-thoughts.com

Based on test program by and

Pololu sample library for LSM303DLH breakout by ryantm:

Copyright (c) 2011 Pololu Corporation. For more information, see

http://www.pololu.com/ http://forum.pololu.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 #include "mbed.h"
 #include "LSM303DLH.h"

 Serial debug(USBTX,USBRX);
 LSM303DLH compass(p28, p27);

 int main() {
   float hdg;
   debug.format(8,Serial::None,1);
   debug.baud(115200);
   debug.printf("LSM303DLH Test\x0d\x0a");
   compass.setOffset(29.50, -0.50, 4.00); // example calibration
   compass.setScale(1.00, 1.03, 1.21);    // example calibration
   while(1) {
     hdg = compass.heading();
     debug.printf("Heading: %.2f\n", hdg);
     wait(0.1);
   }
 }

Definition at line 64 of file LSM303DLH.h.


Constructor & Destructor Documentation

LSM303DLH ( PinName  sda,
PinName  scl 
)

Create a new interface for an LSM303DLH.

Parameters:
sdais the pin for the I2C SDA line
sclis the pin for the I2C SCL line

Definition at line 94 of file LSM303DLH.cpp.


Member Function Documentation

void frequency ( int  hz )

sets the I2C bus frequency

Parameters:
frequencyis the I2C bus/clock frequency, either standard (100000) or fast (400000)

Definition at line 216 of file LSM303DLH.cpp.

float heading ( vector  from )

returns the heading with respect to the specified vector

Definition at line 187 of file LSM303DLH.cpp.

float heading ( void   )

returns the magnetic heading with respect to the y axis

Definition at line 182 of file LSM303DLH.cpp.

void read ( vector &  a,
vector &  m 
)

read the raw accelerometer and compass values

Parameters:
ais the accelerometer 3d vector, written by the function
mis the magnetometer 3d vector, written by the function

Definition at line 141 of file LSM303DLH.cpp.

void setOffset ( float  x,
float  y,
float  z 
)

sets the x, y, and z offset corrections for hard iron calibration

Calibration details here: http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/

If you gather raw magnetometer data and find, for example, x is offset by hard iron by -20 then pass +20 to this member function to correct for hard iron.

Parameters:
xis the offset correction for the x axis
yis the offset correction for the y axis
zis the offset correction for the z axis

Definition at line 127 of file LSM303DLH.cpp.

void setScale ( float  x,
float  y,
float  z 
)

sets the scale factor for the x, y, and z axes

Calibratio details here: http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/

Sensitivity of the three axes is never perfectly identical and this function can help to correct differences in sensitivity. You're supplying a multipler such that x, y and z will be normalized to the same max/min values

Definition at line 134 of file LSM303DLH.cpp.