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

Fork of LSM303DLH by Michael Shimniok

Committer:
paulcox
Date:
Wed Oct 15 15:01:31 2014 +0000
Revision:
3:9c3f240d14bf
Parent:
2:aea5caec809c
Program tested with all Nucleo boards updated to 2j23m6 firmware and confirmed as working (led blinks and magnetometer info received via I2C)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 1:48d83c63d1d9 1 #include "mbed.h"
shimniok 1:48d83c63d1d9 2 #include "vector.h"
shimniok 1:48d83c63d1d9 3
shimniok 1:48d83c63d1d9 4 #ifndef M_PI
shimniok 1:48d83c63d1d9 5 #define M_PI 3.14159265358979323846
shimniok 1:48d83c63d1d9 6 #endif
shimniok 1:48d83c63d1d9 7
shimniok 1:48d83c63d1d9 8 /** Tilt-compensated compass interface Library for the STMicro LSM303DLH 3-axis magnetometer, 3-axis acceleromter
shimniok 0:de767f4959ef 9 *
shimniok 0:de767f4959ef 10 * Michael Shimniok http://bot-thoughts.com
shimniok 0:de767f4959ef 11 *
shimniok 0:de767f4959ef 12 * Based on test program by @tosihisa and
shimniok 0:de767f4959ef 13 *
shimniok 0:de767f4959ef 14 * Pololu sample library for LSM303DLH breakout by ryantm:
shimniok 0:de767f4959ef 15 *
shimniok 0:de767f4959ef 16 * Copyright (c) 2011 Pololu Corporation. For more information, see
shimniok 0:de767f4959ef 17 *
shimniok 0:de767f4959ef 18 * http://www.pololu.com/
shimniok 0:de767f4959ef 19 * http://forum.pololu.com/
shimniok 0:de767f4959ef 20 *
shimniok 0:de767f4959ef 21 * Permission is hereby granted, free of charge, to any person
shimniok 0:de767f4959ef 22 * obtaining a copy of this software and associated documentation
shimniok 0:de767f4959ef 23 * files (the "Software"), to deal in the Software without
shimniok 0:de767f4959ef 24 * restriction, including without limitation the rights to use,
shimniok 0:de767f4959ef 25 * copy, modify, merge, publish, distribute, sublicense, and/or sell
shimniok 0:de767f4959ef 26 * copies of the Software, and to permit persons to whom the
shimniok 0:de767f4959ef 27 * Software is furnished to do so, subject to the following
shimniok 0:de767f4959ef 28 * conditions:
shimniok 0:de767f4959ef 29 *
shimniok 0:de767f4959ef 30 * The above copyright notice and this permission notice shall be
shimniok 0:de767f4959ef 31 * included in all copies or substantial portions of the Software.
shimniok 0:de767f4959ef 32 *
shimniok 0:de767f4959ef 33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
shimniok 0:de767f4959ef 34 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
shimniok 0:de767f4959ef 35 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shimniok 0:de767f4959ef 36 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
shimniok 0:de767f4959ef 37 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
shimniok 0:de767f4959ef 38 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
shimniok 0:de767f4959ef 39 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
shimniok 0:de767f4959ef 40 * OTHER DEALINGS IN THE SOFTWARE.
shimniok 0:de767f4959ef 41 *
shimniok 0:de767f4959ef 42 * @code
shimniok 0:de767f4959ef 43 * #include "mbed.h"
shimniok 0:de767f4959ef 44 * #include "LSM303DLH.h"
shimniok 0:de767f4959ef 45 *
shimniok 0:de767f4959ef 46 * Serial debug(USBTX,USBRX);
shimniok 0:de767f4959ef 47 * LSM303DLH compass(p28, p27);
shimniok 0:de767f4959ef 48 *
shimniok 0:de767f4959ef 49 * int main() {
shimniok 0:de767f4959ef 50 * float hdg;
shimniok 0:de767f4959ef 51 * debug.format(8,Serial::None,1);
shimniok 0:de767f4959ef 52 * debug.baud(115200);
shimniok 0:de767f4959ef 53 * debug.printf("LSM303DLH Test\x0d\x0a");
shimniok 0:de767f4959ef 54 * compass.setOffset(29.50, -0.50, 4.00); // example calibration
shimniok 0:de767f4959ef 55 * compass.setScale(1.00, 1.03, 1.21); // example calibration
shimniok 0:de767f4959ef 56 * while(1) {
shimniok 0:de767f4959ef 57 * hdg = compass.heading();
shimniok 0:de767f4959ef 58 * debug.printf("Heading: %.2f\n", hdg);
shimniok 0:de767f4959ef 59 * wait(0.1);
shimniok 0:de767f4959ef 60 * }
shimniok 0:de767f4959ef 61 * }
shimniok 1:48d83c63d1d9 62 * @endcode
shimniok 0:de767f4959ef 63 */
shimniok 0:de767f4959ef 64 class LSM303DLH {
shimniok 0:de767f4959ef 65 public:
shimniok 0:de767f4959ef 66 /** Create a new interface for an LSM303DLH
shimniok 0:de767f4959ef 67 *
shimniok 0:de767f4959ef 68 * @param sda is the pin for the I2C SDA line
shimniok 0:de767f4959ef 69 * @param scl is the pin for the I2C SCL line
shimniok 0:de767f4959ef 70 */
shimniok 0:de767f4959ef 71 LSM303DLH(PinName sda, PinName scl);
shimniok 0:de767f4959ef 72
shimniok 0:de767f4959ef 73 /** sets the x, y, and z offset corrections for hard iron calibration
shimniok 0:de767f4959ef 74 *
shimniok 0:de767f4959ef 75 * Calibration details here:
shimniok 0:de767f4959ef 76 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
shimniok 0:de767f4959ef 77 *
shimniok 0:de767f4959ef 78 * If you gather raw magnetometer data and find, for example, x is offset
shimniok 0:de767f4959ef 79 * by hard iron by -20 then pass +20 to this member function to correct
shimniok 0:de767f4959ef 80 * for hard iron.
shimniok 0:de767f4959ef 81 *
shimniok 0:de767f4959ef 82 * @param x is the offset correction for the x axis
shimniok 0:de767f4959ef 83 * @param y is the offset correction for the y axis
shimniok 0:de767f4959ef 84 * @param z is the offset correction for the z axis
shimniok 0:de767f4959ef 85 */
shimniok 0:de767f4959ef 86 void setOffset(float x, float y, float z);
shimniok 0:de767f4959ef 87
shimniok 0:de767f4959ef 88 /** sets the scale factor for the x, y, and z axes
shimniok 0:de767f4959ef 89 *
shimniok 0:de767f4959ef 90 * Calibratio details here:
shimniok 0:de767f4959ef 91 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
shimniok 0:de767f4959ef 92 *
shimniok 0:de767f4959ef 93 * Sensitivity of the three axes is never perfectly identical and this
shimniok 0:de767f4959ef 94 * function can help to correct differences in sensitivity. You're
shimniok 0:de767f4959ef 95 * supplying a multipler such that x, y and z will be normalized to the
shimniok 0:de767f4959ef 96 * same max/min values
shimniok 0:de767f4959ef 97 */
shimniok 0:de767f4959ef 98 void setScale(float x, float y, float z);
shimniok 0:de767f4959ef 99
shimniok 0:de767f4959ef 100 /** read the raw accelerometer and compass values
shimniok 0:de767f4959ef 101 *
shimniok 0:de767f4959ef 102 * @param a is the accelerometer 3d vector, written by the function
shimniok 0:de767f4959ef 103 * @param m is the magnetometer 3d vector, written by the function
shimniok 0:de767f4959ef 104 */
shimniok 0:de767f4959ef 105 void read(vector &a, vector &m);
shimniok 0:de767f4959ef 106
shimniok 0:de767f4959ef 107 /** returns the magnetic heading with respect to the y axis
shimniok 0:de767f4959ef 108 *
shimniok 0:de767f4959ef 109 */
shimniok 0:de767f4959ef 110 float heading(void);
shimniok 0:de767f4959ef 111
shimniok 0:de767f4959ef 112 /** returns the heading with respect to the specified vector
shimniok 0:de767f4959ef 113 *
shimniok 0:de767f4959ef 114 */
shimniok 0:de767f4959ef 115 float heading(vector from);
shimniok 0:de767f4959ef 116
shimniok 2:aea5caec809c 117 /** sets the I2C bus frequency
shimniok 2:aea5caec809c 118 *
shimniok 2:aea5caec809c 119 * @param frequency is the I2C bus/clock frequency, either standard (100000) or fast (400000)
shimniok 2:aea5caec809c 120 */
shimniok 2:aea5caec809c 121 void frequency(int hz);
shimniok 2:aea5caec809c 122
shimniok 0:de767f4959ef 123 private:
shimniok 0:de767f4959ef 124 I2C _compass;
shimniok 0:de767f4959ef 125 float _offset_x;
shimniok 0:de767f4959ef 126 float _offset_y;
shimniok 0:de767f4959ef 127 float _offset_z;
shimniok 0:de767f4959ef 128 float _scale_x;
shimniok 0:de767f4959ef 129 float _scale_y;
shimniok 0:de767f4959ef 130 float _scale_z;
shimniok 1:48d83c63d1d9 131 long _filt_ax;
shimniok 1:48d83c63d1d9 132 long _filt_ay;
shimniok 1:48d83c63d1d9 133 long _filt_az;
shimniok 1:48d83c63d1d9 134
shimniok 0:de767f4959ef 135 bool write_reg(int addr_i2c,int addr_reg, char v);
shimniok 0:de767f4959ef 136 bool read_reg(int addr_i2c,int addr_reg, char *v);
shimniok 0:de767f4959ef 137 bool read_reg_short(int addr_i2c,int addr_reg, short *v);
shimniok 0:de767f4959ef 138 };