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