Adafruit 9DOF - LSM303DLHC
Fork of LSM303DLHC by
LSM303DLHC.h@1:cd027bdb4892, 2016-07-14 (annotated)
- Committer:
- afmiee
- Date:
- Thu Jul 14 20:23:10 2016 +0000
- Revision:
- 1:cd027bdb4892
- Parent:
- 0:ffed7ef0b248
First Revision of 9DOF with nRF51, Adafruit 9DOF and sparkfun microSD shield.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
julioefajardo | 0:ffed7ef0b248 | 1 | #ifndef __LSM303DLHC_H |
julioefajardo | 0:ffed7ef0b248 | 2 | #define __LSM303DLHC_H |
julioefajardo | 0:ffed7ef0b248 | 3 | |
julioefajardo | 0:ffed7ef0b248 | 4 | #include "mbed.h" |
julioefajardo | 0:ffed7ef0b248 | 5 | |
julioefajardo | 0:ffed7ef0b248 | 6 | // register addresses |
julioefajardo | 0:ffed7ef0b248 | 7 | |
julioefajardo | 0:ffed7ef0b248 | 8 | #define CTRL_REG1_A 0x20 |
julioefajardo | 0:ffed7ef0b248 | 9 | #define CTRL_REG2_A 0x21 |
julioefajardo | 0:ffed7ef0b248 | 10 | #define CTRL_REG3_A 0x22 |
julioefajardo | 0:ffed7ef0b248 | 11 | #define CTRL_REG4_A 0x23 |
julioefajardo | 0:ffed7ef0b248 | 12 | #define CTRL_REG5_A 0x24 |
julioefajardo | 0:ffed7ef0b248 | 13 | #define CTRL_REG6_A 0x25 // DLHC only |
julioefajardo | 0:ffed7ef0b248 | 14 | #define REFERENCE_A 0x26 |
julioefajardo | 0:ffed7ef0b248 | 15 | #define STATUS_REG_A 0x27 |
julioefajardo | 0:ffed7ef0b248 | 16 | |
julioefajardo | 0:ffed7ef0b248 | 17 | #define OUT_X_L_A 0x28 |
julioefajardo | 0:ffed7ef0b248 | 18 | #define OUT_X_H_A 0x29 |
julioefajardo | 0:ffed7ef0b248 | 19 | #define OUT_Y_L_A 0x2A |
julioefajardo | 0:ffed7ef0b248 | 20 | #define OUT_Y_H_A 0x2B |
julioefajardo | 0:ffed7ef0b248 | 21 | #define OUT_Z_L_A 0x2C |
julioefajardo | 0:ffed7ef0b248 | 22 | #define OUT_Z_H_A 0x2D |
julioefajardo | 0:ffed7ef0b248 | 23 | |
julioefajardo | 0:ffed7ef0b248 | 24 | #define INT1_CFG_A 0x30 |
julioefajardo | 0:ffed7ef0b248 | 25 | #define INT1_SRC_A 0x31 |
julioefajardo | 0:ffed7ef0b248 | 26 | #define INT1_THS_A 0x32 |
julioefajardo | 0:ffed7ef0b248 | 27 | #define INT1_DURATION_A 0x33 |
julioefajardo | 0:ffed7ef0b248 | 28 | #define INT2_CFG_A 0x34 |
julioefajardo | 0:ffed7ef0b248 | 29 | #define INT2_SRC_A 0x35 |
julioefajardo | 0:ffed7ef0b248 | 30 | #define INT2_THS_A 0x36 |
julioefajardo | 0:ffed7ef0b248 | 31 | #define INT2_DURATION_A 0x37 |
julioefajardo | 0:ffed7ef0b248 | 32 | |
julioefajardo | 0:ffed7ef0b248 | 33 | #define CRA_REG_M 0x00 |
julioefajardo | 0:ffed7ef0b248 | 34 | #define CRB_REG_M 0x01 |
julioefajardo | 0:ffed7ef0b248 | 35 | #define MR_REG_M 0x02 |
julioefajardo | 0:ffed7ef0b248 | 36 | |
julioefajardo | 0:ffed7ef0b248 | 37 | #define OUT_X_H_M 0x03 |
julioefajardo | 0:ffed7ef0b248 | 38 | #define OUT_X_L_M 0x04 |
julioefajardo | 0:ffed7ef0b248 | 39 | #define OUT_Y_H_M 0x07 |
julioefajardo | 0:ffed7ef0b248 | 40 | #define OUT_Y_L_M 0x08 |
julioefajardo | 0:ffed7ef0b248 | 41 | #define OUT_Z_H_M 0x05 |
julioefajardo | 0:ffed7ef0b248 | 42 | #define OUT_Z_L_M 0x06 |
julioefajardo | 0:ffed7ef0b248 | 43 | |
julioefajardo | 0:ffed7ef0b248 | 44 | #define SR_REG_M 0x09 |
julioefajardo | 0:ffed7ef0b248 | 45 | #define IRA_REG_M 0x0A |
julioefajardo | 0:ffed7ef0b248 | 46 | #define IRB_REG_M 0x0B |
julioefajardo | 0:ffed7ef0b248 | 47 | #define IRC_REG_M 0x0C |
julioefajardo | 0:ffed7ef0b248 | 48 | |
julioefajardo | 0:ffed7ef0b248 | 49 | /** Tilt-compensated compass interface Library for the STMicro LSM303DLm 3-axis magnetometer, 3-axis acceleromter |
julioefajardo | 0:ffed7ef0b248 | 50 | * @author Michael Shimniok http://www.bot-thoughts.com/ |
julioefajardo | 0:ffed7ef0b248 | 51 | * |
julioefajardo | 0:ffed7ef0b248 | 52 | * This is an early revision; I've not yet implemented heading calculation and the interface differs from my |
julioefajardo | 0:ffed7ef0b248 | 53 | * earlier LSM303DLH; I hope to make this library drop in compatible at some point in the future. |
julioefajardo | 0:ffed7ef0b248 | 54 | * setScale() and setOffset() have no effect at this time. |
julioefajardo | 0:ffed7ef0b248 | 55 | * |
julioefajardo | 0:ffed7ef0b248 | 56 | * @code |
julioefajardo | 0:ffed7ef0b248 | 57 | * #include "mbed.h" |
julioefajardo | 0:ffed7ef0b248 | 58 | * #include "LSM303DLM.h" |
julioefajardo | 0:ffed7ef0b248 | 59 | * |
julioefajardo | 0:ffed7ef0b248 | 60 | * LSM303DLM compass(p28, p27); |
julioefajardo | 0:ffed7ef0b248 | 61 | * ... |
julioefajardo | 0:ffed7ef0b248 | 62 | * int a[3], m[3]; |
julioefajardo | 0:ffed7ef0b248 | 63 | * ... |
julioefajardo | 0:ffed7ef0b248 | 64 | * compass.readAcc(a); |
julioefajardo | 0:ffed7ef0b248 | 65 | * compass.readMag(m); |
julioefajardo | 0:ffed7ef0b248 | 66 | * |
julioefajardo | 0:ffed7ef0b248 | 67 | * @endcode |
julioefajardo | 0:ffed7ef0b248 | 68 | */ |
julioefajardo | 0:ffed7ef0b248 | 69 | class LSM303DLHC { |
julioefajardo | 0:ffed7ef0b248 | 70 | |
julioefajardo | 0:ffed7ef0b248 | 71 | public: |
julioefajardo | 0:ffed7ef0b248 | 72 | /** Create a new interface for an LSM303DLM |
julioefajardo | 0:ffed7ef0b248 | 73 | * |
julioefajardo | 0:ffed7ef0b248 | 74 | * @param sda is the pin for the I2C SDA line |
julioefajardo | 0:ffed7ef0b248 | 75 | * @param scl is the pin for the I2C SCL line |
julioefajardo | 0:ffed7ef0b248 | 76 | */ |
julioefajardo | 0:ffed7ef0b248 | 77 | LSM303DLHC(PinName sda, PinName scl); |
julioefajardo | 0:ffed7ef0b248 | 78 | |
julioefajardo | 0:ffed7ef0b248 | 79 | /** sets the x, y, and z offset corrections for hard iron calibration |
julioefajardo | 0:ffed7ef0b248 | 80 | * |
julioefajardo | 0:ffed7ef0b248 | 81 | * Calibration details here: |
julioefajardo | 0:ffed7ef0b248 | 82 | * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/ |
julioefajardo | 0:ffed7ef0b248 | 83 | * |
julioefajardo | 0:ffed7ef0b248 | 84 | * If you gather raw magnetometer data and find, for example, x is offset |
julioefajardo | 0:ffed7ef0b248 | 85 | * by hard iron by -20 then pass +20 to this member function to correct |
julioefajardo | 0:ffed7ef0b248 | 86 | * for hard iron. |
julioefajardo | 0:ffed7ef0b248 | 87 | * |
julioefajardo | 0:ffed7ef0b248 | 88 | * @param x is the offset correction for the x axis |
julioefajardo | 0:ffed7ef0b248 | 89 | * @param y is the offset correction for the y axis |
julioefajardo | 0:ffed7ef0b248 | 90 | * @param z is the offset correction for the z axis |
julioefajardo | 0:ffed7ef0b248 | 91 | */ |
julioefajardo | 0:ffed7ef0b248 | 92 | void init(); |
julioefajardo | 0:ffed7ef0b248 | 93 | void setOffset(float x, float y, float z); |
julioefajardo | 0:ffed7ef0b248 | 94 | |
julioefajardo | 0:ffed7ef0b248 | 95 | /** sets the scale factor for the x, y, and z axes |
julioefajardo | 0:ffed7ef0b248 | 96 | * |
julioefajardo | 0:ffed7ef0b248 | 97 | * Calibratio details here: |
julioefajardo | 0:ffed7ef0b248 | 98 | * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/ |
julioefajardo | 0:ffed7ef0b248 | 99 | * |
julioefajardo | 0:ffed7ef0b248 | 100 | * Sensitivity of the three axes is never perfectly identical and this |
julioefajardo | 0:ffed7ef0b248 | 101 | * function can help to correct differences in sensitivity. You're |
julioefajardo | 0:ffed7ef0b248 | 102 | * supplying a multipler such that x, y and z will be normalized to the |
julioefajardo | 0:ffed7ef0b248 | 103 | * same max/min values |
julioefajardo | 0:ffed7ef0b248 | 104 | */ |
julioefajardo | 0:ffed7ef0b248 | 105 | void setScale(float x, float y, float z); |
julioefajardo | 0:ffed7ef0b248 | 106 | |
julioefajardo | 0:ffed7ef0b248 | 107 | /** read the calibrated accelerometer and magnetometer values |
julioefajardo | 0:ffed7ef0b248 | 108 | * |
julioefajardo | 0:ffed7ef0b248 | 109 | * @param a is the accelerometer 3d vector, written by the function |
julioefajardo | 0:ffed7ef0b248 | 110 | * @param m is the magnetometer 3d vector, written by the function |
julioefajardo | 0:ffed7ef0b248 | 111 | */ |
julioefajardo | 0:ffed7ef0b248 | 112 | void read(int a[3], int m[3]); |
julioefajardo | 0:ffed7ef0b248 | 113 | |
julioefajardo | 0:ffed7ef0b248 | 114 | /** read the calibrated accelerometer values |
julioefajardo | 0:ffed7ef0b248 | 115 | * |
julioefajardo | 0:ffed7ef0b248 | 116 | * @param a is the accelerometer 3d vector, written by the function |
julioefajardo | 0:ffed7ef0b248 | 117 | */ |
julioefajardo | 0:ffed7ef0b248 | 118 | void readAcc(int a[3]); |
julioefajardo | 0:ffed7ef0b248 | 119 | |
julioefajardo | 0:ffed7ef0b248 | 120 | /** read the calibrated magnetometer values |
julioefajardo | 0:ffed7ef0b248 | 121 | * |
julioefajardo | 0:ffed7ef0b248 | 122 | * @param m is the magnetometer 3d vector, written by the function |
julioefajardo | 0:ffed7ef0b248 | 123 | */ |
julioefajardo | 0:ffed7ef0b248 | 124 | void readMag(int m[3]); |
julioefajardo | 0:ffed7ef0b248 | 125 | |
julioefajardo | 0:ffed7ef0b248 | 126 | /** sets the I2C bus frequency |
julioefajardo | 0:ffed7ef0b248 | 127 | * |
julioefajardo | 0:ffed7ef0b248 | 128 | * @param frequency is the I2C bus/clock frequency, either standard (100000) or fast (400000) |
julioefajardo | 0:ffed7ef0b248 | 129 | */ |
julioefajardo | 0:ffed7ef0b248 | 130 | void frequency(int hz); |
julioefajardo | 0:ffed7ef0b248 | 131 | |
julioefajardo | 0:ffed7ef0b248 | 132 | private: |
julioefajardo | 0:ffed7ef0b248 | 133 | I2C _device; |
julioefajardo | 0:ffed7ef0b248 | 134 | char _data[6]; |
julioefajardo | 0:ffed7ef0b248 | 135 | int offset[3]; |
julioefajardo | 0:ffed7ef0b248 | 136 | int scale[3]; |
julioefajardo | 0:ffed7ef0b248 | 137 | }; |
julioefajardo | 0:ffed7ef0b248 | 138 | |
julioefajardo | 0:ffed7ef0b248 | 139 | #endif |