My attempt to made a more useful lib. You can get the accelerator and magnetometer.
Fork of LSM303DLH by
LSM303DLH.h@8:cc338ded0b2c, 2017-08-06 (annotated)
- Committer:
- salco
- Date:
- Sun Aug 06 23:57:25 2017 +0000
- Revision:
- 8:cc338ded0b2c
- Parent:
- 7:275a0a69cff5
remove part of my debug comment
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 | |
salco | 5:48722ae56546 | 4 | #include "LSM303DLH_RegisterDef.h" |
salco | 5:48722ae56546 | 5 | |
shimniok | 1:48d83c63d1d9 | 6 | #ifndef M_PI |
shimniok | 1:48d83c63d1d9 | 7 | #define M_PI 3.14159265358979323846 |
shimniok | 1:48d83c63d1d9 | 8 | #endif |
shimniok | 1:48d83c63d1d9 | 9 | |
shimniok | 1:48d83c63d1d9 | 10 | /** Tilt-compensated compass interface Library for the STMicro LSM303DLH 3-axis magnetometer, 3-axis acceleromter |
shimniok | 0:de767f4959ef | 11 | * |
salco | 7:275a0a69cff5 | 12 | * This file is subject to the terms and conditions of the GNU Lesser |
salco | 7:275a0a69cff5 | 13 | * General Public License v2.1. See the file LICENSE in the top level |
salco | 7:275a0a69cff5 | 14 | * directory for more details. |
shimniok | 0:de767f4959ef | 15 | * |
salco | 7:275a0a69cff5 | 16 | * |
salco | 7:275a0a69cff5 | 17 | * |
salco | 7:275a0a69cff5 | 18 | * Base on the @see doccumentation https://cdn-shop.adafruit.com/datasheets/LSM303DLHC.PDF |
shimniok | 0:de767f4959ef | 19 | * @code |
shimniok | 0:de767f4959ef | 20 | * #include "mbed.h" |
shimniok | 0:de767f4959ef | 21 | * #include "LSM303DLH.h" |
shimniok | 0:de767f4959ef | 22 | * |
shimniok | 0:de767f4959ef | 23 | * Serial debug(USBTX,USBRX); |
shimniok | 0:de767f4959ef | 24 | * LSM303DLH compass(p28, p27); |
shimniok | 0:de767f4959ef | 25 | * |
shimniok | 0:de767f4959ef | 26 | * int main() { |
shimniok | 0:de767f4959ef | 27 | * float hdg; |
shimniok | 0:de767f4959ef | 28 | * debug.format(8,Serial::None,1); |
shimniok | 0:de767f4959ef | 29 | * debug.baud(115200); |
shimniok | 0:de767f4959ef | 30 | * debug.printf("LSM303DLH Test\x0d\x0a"); |
shimniok | 0:de767f4959ef | 31 | * compass.setOffset(29.50, -0.50, 4.00); // example calibration |
shimniok | 0:de767f4959ef | 32 | * compass.setScale(1.00, 1.03, 1.21); // example calibration |
shimniok | 0:de767f4959ef | 33 | * while(1) { |
shimniok | 0:de767f4959ef | 34 | * hdg = compass.heading(); |
shimniok | 0:de767f4959ef | 35 | * debug.printf("Heading: %.2f\n", hdg); |
shimniok | 0:de767f4959ef | 36 | * wait(0.1); |
shimniok | 0:de767f4959ef | 37 | * } |
shimniok | 0:de767f4959ef | 38 | * } |
salco | 7:275a0a69cff5 | 39 | * @endcode |
salco | 7:275a0a69cff5 | 40 | * |
salco | 7:275a0a69cff5 | 41 | * @author Salco <JeSuisSalco@gmail.com> |
shimniok | 0:de767f4959ef | 42 | */ |
salco | 4:4f2ed3f8726c | 43 | |
salco | 6:86cf2afe3e52 | 44 | /** enable for MSB @ lower address */ |
salco | 6:86cf2afe3e52 | 45 | //#define LSM303_LITLE_ENDIAN |
salco | 6:86cf2afe3e52 | 46 | |
shimniok | 0:de767f4959ef | 47 | class LSM303DLH { |
shimniok | 0:de767f4959ef | 48 | public: |
shimniok | 0:de767f4959ef | 49 | /** Create a new interface for an LSM303DLH |
shimniok | 0:de767f4959ef | 50 | * |
shimniok | 0:de767f4959ef | 51 | * @param sda is the pin for the I2C SDA line |
shimniok | 0:de767f4959ef | 52 | * @param scl is the pin for the I2C SCL line |
shimniok | 0:de767f4959ef | 53 | */ |
shimniok | 0:de767f4959ef | 54 | LSM303DLH(PinName sda, PinName scl); |
salco | 5:48722ae56546 | 55 | /** Create a new interface for an LSM303DLH |
salco | 5:48722ae56546 | 56 | * |
salco | 5:48722ae56546 | 57 | * @param ptrI2C is a pointer from existing I2C |
salco | 5:48722ae56546 | 58 | */ |
salco | 3:9b4ff901b5c9 | 59 | LSM303DLH(I2C* ptrI2C); |
salco | 6:86cf2afe3e52 | 60 | /** Destructor of the class |
salco | 6:86cf2afe3e52 | 61 | */ |
salco | 6:86cf2afe3e52 | 62 | ~LSM303DLH(); |
shimniok | 0:de767f4959ef | 63 | /** sets the x, y, and z offset corrections for hard iron calibration |
shimniok | 0:de767f4959ef | 64 | * |
shimniok | 0:de767f4959ef | 65 | * Calibration details here: |
shimniok | 0:de767f4959ef | 66 | * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/ |
shimniok | 0:de767f4959ef | 67 | * |
shimniok | 0:de767f4959ef | 68 | * If you gather raw magnetometer data and find, for example, x is offset |
shimniok | 0:de767f4959ef | 69 | * by hard iron by -20 then pass +20 to this member function to correct |
shimniok | 0:de767f4959ef | 70 | * for hard iron. |
shimniok | 0:de767f4959ef | 71 | * |
shimniok | 0:de767f4959ef | 72 | * @param x is the offset correction for the x axis |
shimniok | 0:de767f4959ef | 73 | * @param y is the offset correction for the y axis |
shimniok | 0:de767f4959ef | 74 | * @param z is the offset correction for the z axis |
shimniok | 0:de767f4959ef | 75 | */ |
shimniok | 0:de767f4959ef | 76 | void setOffset(float x, float y, float z); |
shimniok | 0:de767f4959ef | 77 | |
shimniok | 0:de767f4959ef | 78 | /** sets the scale factor for the x, y, and z axes |
shimniok | 0:de767f4959ef | 79 | * |
shimniok | 0:de767f4959ef | 80 | * Calibratio details here: |
shimniok | 0:de767f4959ef | 81 | * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/ |
shimniok | 0:de767f4959ef | 82 | * |
shimniok | 0:de767f4959ef | 83 | * Sensitivity of the three axes is never perfectly identical and this |
shimniok | 0:de767f4959ef | 84 | * function can help to correct differences in sensitivity. You're |
shimniok | 0:de767f4959ef | 85 | * supplying a multipler such that x, y and z will be normalized to the |
shimniok | 0:de767f4959ef | 86 | * same max/min values |
shimniok | 0:de767f4959ef | 87 | */ |
shimniok | 0:de767f4959ef | 88 | void setScale(float x, float y, float z); |
shimniok | 0:de767f4959ef | 89 | |
shimniok | 0:de767f4959ef | 90 | /** read the raw accelerometer and compass values |
shimniok | 0:de767f4959ef | 91 | * |
shimniok | 0:de767f4959ef | 92 | * @param a is the accelerometer 3d vector, written by the function |
shimniok | 0:de767f4959ef | 93 | * @param m is the magnetometer 3d vector, written by the function |
shimniok | 0:de767f4959ef | 94 | */ |
salco | 3:9b4ff901b5c9 | 95 | bool read(vector &a, vector &m); |
salco | 6:86cf2afe3e52 | 96 | /** read the raw accelerometer values |
salco | 6:86cf2afe3e52 | 97 | * |
salco | 6:86cf2afe3e52 | 98 | * @param a is the accelerometer 3d vector, written by the function |
salco | 6:86cf2afe3e52 | 99 | */ |
salco | 6:86cf2afe3e52 | 100 | bool read_acc_raw(vector *a); |
salco | 6:86cf2afe3e52 | 101 | /** read the raw compass values |
salco | 6:86cf2afe3e52 | 102 | * |
salco | 6:86cf2afe3e52 | 103 | * @param m is the magnetometer 3d vector, written by the function |
salco | 6:86cf2afe3e52 | 104 | */ |
salco | 6:86cf2afe3e52 | 105 | bool read_mag_raw(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); |
salco | 5:48722ae56546 | 122 | |
salco | 5:48722ae56546 | 123 | private: |
salco | 6:86cf2afe3e52 | 124 | enum DEV_ADDRS { |
salco | 6:86cf2afe3e52 | 125 | /* --- Mag --- */ |
salco | 6:86cf2afe3e52 | 126 | addr_mag = 0x3c, |
salco | 6:86cf2afe3e52 | 127 | /* --- Acc --- */ |
salco | 6:86cf2afe3e52 | 128 | addr_acc = 0x32,//0x30; |
salco | 6:86cf2afe3e52 | 129 | }; |
salco | 6:86cf2afe3e52 | 130 | |
salco | 5:48722ae56546 | 131 | enum REG_ADDRS { |
salco | 5:48722ae56546 | 132 | /* --- Mag --- */ |
salco | 5:48722ae56546 | 133 | CRA_REG_M = 0x00, |
salco | 5:48722ae56546 | 134 | CRB_REG_M = 0x01, |
salco | 5:48722ae56546 | 135 | MR_REG_M = 0x02, |
salco | 5:48722ae56546 | 136 | OUT_X_M = 0x03, |
salco | 5:48722ae56546 | 137 | OUT_Y_M = 0x05, |
salco | 5:48722ae56546 | 138 | OUT_Z_M = 0x07, |
salco | 5:48722ae56546 | 139 | SR_REG_M = 0x09, |
salco | 5:48722ae56546 | 140 | /* --- Acc --- */ |
salco | 5:48722ae56546 | 141 | CTRL_REG1_A = 0x20, |
salco | 5:48722ae56546 | 142 | CTRL_REG4_A = 0x23, |
salco | 5:48722ae56546 | 143 | STATUS_REG_A= 0x27, |
salco | 5:48722ae56546 | 144 | OUT_X_A = 0x28, |
salco | 5:48722ae56546 | 145 | OUT_Y_A = 0x2A, |
salco | 5:48722ae56546 | 146 | OUT_Z_A = 0x2C, |
salco | 4:4f2ed3f8726c | 147 | }; |
salco | 4:4f2ed3f8726c | 148 | |
salco | 3:9b4ff901b5c9 | 149 | I2C* m_ptr_I2C;//_compass; |
shimniok | 0:de767f4959ef | 150 | float _offset_x; |
shimniok | 0:de767f4959ef | 151 | float _offset_y; |
shimniok | 0:de767f4959ef | 152 | float _offset_z; |
shimniok | 0:de767f4959ef | 153 | float _scale_x; |
shimniok | 0:de767f4959ef | 154 | float _scale_y; |
shimniok | 0:de767f4959ef | 155 | float _scale_z; |
shimniok | 1:48d83c63d1d9 | 156 | long _filt_ax; |
shimniok | 1:48d83c63d1d9 | 157 | long _filt_ay; |
shimniok | 1:48d83c63d1d9 | 158 | long _filt_az; |
salco | 6:86cf2afe3e52 | 159 | int8_t m_FS; |
salco | 6:86cf2afe3e52 | 160 | int8_t m_GN; |
salco | 6:86cf2afe3e52 | 161 | bool m_have_createdI2C; |
salco | 3:9b4ff901b5c9 | 162 | void init(void) ; |
salco | 5:48722ae56546 | 163 | |
salco | 5:48722ae56546 | 164 | bool write_reg(int addr_i2c,int addr_reg, uint8_t v); |
shimniok | 0:de767f4959ef | 165 | bool write_reg(int addr_i2c,int addr_reg, char v); |
salco | 5:48722ae56546 | 166 | |
salco | 5:48722ae56546 | 167 | bool read_reg(int addr_i2c,int addr_reg, uint8_t *v); |
shimniok | 0:de767f4959ef | 168 | bool read_reg(int addr_i2c,int addr_reg, char *v); |
salco | 5:48722ae56546 | 169 | |
shimniok | 0:de767f4959ef | 170 | bool read_reg_short(int addr_i2c,int addr_reg, short *v); |
salco | 6:86cf2afe3e52 | 171 | bool read_reg_short(DEV_ADDRS addr_i2c,REG_ADDRS addr_reg, OUT_XYZ_t *dataRead); |
salco | 5:48722ae56546 | 172 | |
salco | 5:48722ae56546 | 173 | int8_t get_FullScall_selection(void); |
salco | 6:86cf2afe3e52 | 174 | float get_acc_value_in_g(OUT_XYZ_t* dataOut); |
shimniok | 0:de767f4959ef | 175 | }; |