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

Dependents:   FCFS

Fork of LSM303DLH by Michael Shimniok

Committer:
shimniok
Date:
Wed Apr 06 05:05:10 2011 +0000
Revision:
0:de767f4959ef
Child:
1:48d83c63d1d9
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 0:de767f4959ef 1 /** LSM303DLH Interface Library
shimniok 0:de767f4959ef 2 *
shimniok 0:de767f4959ef 3 * Michael Shimniok http://bot-thoughts.com
shimniok 0:de767f4959ef 4 *
shimniok 0:de767f4959ef 5 * Based on test program by @tosihisa and
shimniok 0:de767f4959ef 6 *
shimniok 0:de767f4959ef 7 * Pololu sample library for LSM303DLH breakout by ryantm:
shimniok 0:de767f4959ef 8 *
shimniok 0:de767f4959ef 9 * Copyright (c) 2011 Pololu Corporation. For more information, see
shimniok 0:de767f4959ef 10 *
shimniok 0:de767f4959ef 11 * http://www.pololu.com/
shimniok 0:de767f4959ef 12 * http://forum.pololu.com/
shimniok 0:de767f4959ef 13 *
shimniok 0:de767f4959ef 14 * Permission is hereby granted, free of charge, to any person
shimniok 0:de767f4959ef 15 * obtaining a copy of this software and associated documentation
shimniok 0:de767f4959ef 16 * files (the "Software"), to deal in the Software without
shimniok 0:de767f4959ef 17 * restriction, including without limitation the rights to use,
shimniok 0:de767f4959ef 18 * copy, modify, merge, publish, distribute, sublicense, and/or sell
shimniok 0:de767f4959ef 19 * copies of the Software, and to permit persons to whom the
shimniok 0:de767f4959ef 20 * Software is furnished to do so, subject to the following
shimniok 0:de767f4959ef 21 * conditions:
shimniok 0:de767f4959ef 22 *
shimniok 0:de767f4959ef 23 * The above copyright notice and this permission notice shall be
shimniok 0:de767f4959ef 24 * included in all copies or substantial portions of the Software.
shimniok 0:de767f4959ef 25 *
shimniok 0:de767f4959ef 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
shimniok 0:de767f4959ef 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
shimniok 0:de767f4959ef 28 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shimniok 0:de767f4959ef 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
shimniok 0:de767f4959ef 30 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
shimniok 0:de767f4959ef 31 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
shimniok 0:de767f4959ef 32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
shimniok 0:de767f4959ef 33 * OTHER DEALINGS IN THE SOFTWARE.
shimniok 0:de767f4959ef 34 *
shimniok 0:de767f4959ef 35 * @code
shimniok 0:de767f4959ef 36 * #include "mbed.h"
shimniok 0:de767f4959ef 37 * #include "LSM303DLH.h"
shimniok 0:de767f4959ef 38 *
shimniok 0:de767f4959ef 39 * Serial debug(USBTX,USBRX);
shimniok 0:de767f4959ef 40 * LSM303DLH compass(p28, p27);
shimniok 0:de767f4959ef 41 *
shimniok 0:de767f4959ef 42 * int main() {
shimniok 0:de767f4959ef 43 * float hdg;
shimniok 0:de767f4959ef 44 * debug.format(8,Serial::None,1);
shimniok 0:de767f4959ef 45 * debug.baud(115200);
shimniok 0:de767f4959ef 46 * debug.printf("LSM303DLH Test\x0d\x0a");
shimniok 0:de767f4959ef 47 * compass.setOffset(29.50, -0.50, 4.00); // example calibration
shimniok 0:de767f4959ef 48 * compass.setScale(1.00, 1.03, 1.21); // example calibration
shimniok 0:de767f4959ef 49 * while(1) {
shimniok 0:de767f4959ef 50 * hdg = compass.heading();
shimniok 0:de767f4959ef 51 * debug.printf("Heading: %.2f\n", hdg);
shimniok 0:de767f4959ef 52 * wait(0.1);
shimniok 0:de767f4959ef 53 * }
shimniok 0:de767f4959ef 54 * }
shimniok 0:de767f4959ef 55 */
shimniok 0:de767f4959ef 56 #include "mbed.h"
shimniok 0:de767f4959ef 57 #include "vector.h"
shimniok 0:de767f4959ef 58
shimniok 0:de767f4959ef 59 #ifndef M_PI
shimniok 0:de767f4959ef 60 #define M_PI 3.14159265358979323846
shimniok 0:de767f4959ef 61 #endif
shimniok 0:de767f4959ef 62
shimniok 0:de767f4959ef 63 class LSM303DLH {
shimniok 0:de767f4959ef 64 public:
shimniok 0:de767f4959ef 65
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 0:de767f4959ef 117 private:
shimniok 0:de767f4959ef 118 I2C _compass;
shimniok 0:de767f4959ef 119 float _offset_x;
shimniok 0:de767f4959ef 120 float _offset_y;
shimniok 0:de767f4959ef 121 float _offset_z;
shimniok 0:de767f4959ef 122 float _scale_x;
shimniok 0:de767f4959ef 123 float _scale_y;
shimniok 0:de767f4959ef 124 float _scale_z;
shimniok 0:de767f4959ef 125
shimniok 0:de767f4959ef 126 bool write_reg(int addr_i2c,int addr_reg, char v);
shimniok 0:de767f4959ef 127 bool read_reg(int addr_i2c,int addr_reg, char *v);
shimniok 0:de767f4959ef 128 bool read_reg_short(int addr_i2c,int addr_reg, short *v);
shimniok 0:de767f4959ef 129 };