Interface library for STMicro LSM303DLH 3-axis magnetometer w/ 3-axis acceleromter. Computes magnetic heading.
LSM303DLH.h@1:48d83c63d1d9, 2011-04-08 (annotated)
- Committer:
- shimniok
- Date:
- Fri Apr 08 07:29:51 2011 +0000
- Revision:
- 1:48d83c63d1d9
- Parent:
- 0:de767f4959ef
- Child:
- 2:aea5caec809c
Added accelerometer filtering in an attempt to reduce vibration effects on heading.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
shimniok | 0:de767f4959ef | 67 | /** Create a new interface for an LSM303DLH |
shimniok | 0:de767f4959ef | 68 | * |
shimniok | 0:de767f4959ef | 69 | * @param sda is the pin for the I2C SDA line |
shimniok | 0:de767f4959ef | 70 | * @param scl is the pin for the I2C SCL line |
shimniok | 0:de767f4959ef | 71 | */ |
shimniok | 0:de767f4959ef | 72 | LSM303DLH(PinName sda, PinName scl); |
shimniok | 0:de767f4959ef | 73 | |
shimniok | 0:de767f4959ef | 74 | /** sets the x, y, and z offset corrections for hard iron calibration |
shimniok | 0:de767f4959ef | 75 | * |
shimniok | 0:de767f4959ef | 76 | * Calibration details here: |
shimniok | 0:de767f4959ef | 77 | * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/ |
shimniok | 0:de767f4959ef | 78 | * |
shimniok | 0:de767f4959ef | 79 | * If you gather raw magnetometer data and find, for example, x is offset |
shimniok | 0:de767f4959ef | 80 | * by hard iron by -20 then pass +20 to this member function to correct |
shimniok | 0:de767f4959ef | 81 | * for hard iron. |
shimniok | 0:de767f4959ef | 82 | * |
shimniok | 0:de767f4959ef | 83 | * @param x is the offset correction for the x axis |
shimniok | 0:de767f4959ef | 84 | * @param y is the offset correction for the y axis |
shimniok | 0:de767f4959ef | 85 | * @param z is the offset correction for the z axis |
shimniok | 0:de767f4959ef | 86 | */ |
shimniok | 0:de767f4959ef | 87 | void setOffset(float x, float y, float z); |
shimniok | 0:de767f4959ef | 88 | |
shimniok | 0:de767f4959ef | 89 | /** sets the scale factor for the x, y, and z axes |
shimniok | 0:de767f4959ef | 90 | * |
shimniok | 0:de767f4959ef | 91 | * Calibratio details here: |
shimniok | 0:de767f4959ef | 92 | * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/ |
shimniok | 0:de767f4959ef | 93 | * |
shimniok | 0:de767f4959ef | 94 | * Sensitivity of the three axes is never perfectly identical and this |
shimniok | 0:de767f4959ef | 95 | * function can help to correct differences in sensitivity. You're |
shimniok | 0:de767f4959ef | 96 | * supplying a multipler such that x, y and z will be normalized to the |
shimniok | 0:de767f4959ef | 97 | * same max/min values |
shimniok | 0:de767f4959ef | 98 | */ |
shimniok | 0:de767f4959ef | 99 | void setScale(float x, float y, float z); |
shimniok | 0:de767f4959ef | 100 | |
shimniok | 0:de767f4959ef | 101 | /** read the raw accelerometer and compass values |
shimniok | 0:de767f4959ef | 102 | * |
shimniok | 0:de767f4959ef | 103 | * @param a is the accelerometer 3d vector, written by the function |
shimniok | 0:de767f4959ef | 104 | * @param m is the magnetometer 3d vector, written by the function |
shimniok | 0:de767f4959ef | 105 | */ |
shimniok | 0:de767f4959ef | 106 | void read(vector &a, vector &m); |
shimniok | 0:de767f4959ef | 107 | |
shimniok | 0:de767f4959ef | 108 | /** returns the magnetic heading with respect to the y axis |
shimniok | 0:de767f4959ef | 109 | * |
shimniok | 0:de767f4959ef | 110 | */ |
shimniok | 0:de767f4959ef | 111 | float heading(void); |
shimniok | 0:de767f4959ef | 112 | |
shimniok | 0:de767f4959ef | 113 | /** returns the heading with respect to the specified vector |
shimniok | 0:de767f4959ef | 114 | * |
shimniok | 0:de767f4959ef | 115 | */ |
shimniok | 0:de767f4959ef | 116 | float heading(vector from); |
shimniok | 0:de767f4959ef | 117 | |
shimniok | 0:de767f4959ef | 118 | private: |
shimniok | 0:de767f4959ef | 119 | I2C _compass; |
shimniok | 0:de767f4959ef | 120 | float _offset_x; |
shimniok | 0:de767f4959ef | 121 | float _offset_y; |
shimniok | 0:de767f4959ef | 122 | float _offset_z; |
shimniok | 0:de767f4959ef | 123 | float _scale_x; |
shimniok | 0:de767f4959ef | 124 | float _scale_y; |
shimniok | 0:de767f4959ef | 125 | float _scale_z; |
shimniok | 1:48d83c63d1d9 | 126 | long _filt_ax; |
shimniok | 1:48d83c63d1d9 | 127 | long _filt_ay; |
shimniok | 1:48d83c63d1d9 | 128 | long _filt_az; |
shimniok | 1:48d83c63d1d9 | 129 | |
shimniok | 0:de767f4959ef | 130 | bool write_reg(int addr_i2c,int addr_reg, char v); |
shimniok | 0:de767f4959ef | 131 | bool read_reg(int addr_i2c,int addr_reg, char *v); |
shimniok | 0:de767f4959ef | 132 | bool read_reg_short(int addr_i2c,int addr_reg, short *v); |
shimniok | 0:de767f4959ef | 133 | }; |