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