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