My attempt to made a more useful lib. You can get the accelerator and magnetometer.
Fork of LSM303DLH by
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. | |
LSM303DLH (I2C *ptrI2C) | |
Create a new interface for an LSM303DLH. | |
~LSM303DLH () | |
Destructor of the class. | |
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 | |
bool | read (vector &a, vector &m) |
read the raw accelerometer and compass values | |
bool | read_acc_raw (vector *a) |
read the raw accelerometer values | |
bool | read_mag_raw (vector *m) |
read the raw 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.
This file is subject to the terms and conditions of the GNU Lesser General Public License v2.1. See the file LICENSE in the top level directory for more details.
Base on the
- See also:
- doccumentation https://cdn-shop.adafruit.com/datasheets/LSM303DLHC.PDF
#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 47 of file LSM303DLH.h.
Constructor & Destructor Documentation
LSM303DLH | ( | PinName | sda, |
PinName | scl | ||
) |
Create a new interface for an LSM303DLH.
- Parameters:
-
sda is the pin for the I2C SDA line scl is the pin for the I2C SCL line
Definition at line 119 of file LSM303DLH.cpp.
LSM303DLH | ( | I2C * | ptrI2C ) |
Create a new interface for an LSM303DLH.
- Parameters:
-
ptrI2C is a pointer from existing I2C
Definition at line 125 of file LSM303DLH.cpp.
~LSM303DLH | ( | ) |
Destructor of the class.
Definition at line 112 of file LSM303DLH.cpp.
Member Function Documentation
void frequency | ( | int | hz ) |
sets the I2C bus frequency
- Parameters:
-
frequency is the I2C bus/clock frequency, either standard (100000) or fast (400000)
Definition at line 306 of file LSM303DLH.cpp.
float heading | ( | void | ) |
returns the magnetic heading with respect to the y axis
Definition at line 272 of file LSM303DLH.cpp.
float heading | ( | vector | from ) |
returns the heading with respect to the specified vector
Definition at line 277 of file LSM303DLH.cpp.
bool read | ( | vector & | a, |
vector & | m | ||
) |
read the raw accelerometer and compass values
- Parameters:
-
a is the accelerometer 3d vector, written by the function m is the magnetometer 3d vector, written by the function
Definition at line 204 of file LSM303DLH.cpp.
bool read_acc_raw | ( | vector * | a ) |
read the raw accelerometer values
- Parameters:
-
a is the accelerometer 3d vector, written by the function
Definition at line 324 of file LSM303DLH.cpp.
bool read_mag_raw | ( | vector * | m ) |
read the raw compass values
- Parameters:
-
m is the magnetometer 3d vector, written by the function
Definition at line 374 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:
-
x is the offset correction for the x axis y is the offset correction for the y axis z is the offset correction for the z axis
Definition at line 190 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 197 of file LSM303DLH.cpp.
Generated on Fri Jul 15 2022 14:50:37 by 1.7.2