Interface library for ST LSM303DLM 3-axis magnetometer/accelerometer

Dependents:   AVC_2012 m3pi_Kompass Fish_2014Fall Fish_2014Fall ... more

Committer:
shimniok
Date:
Tue Jan 24 16:56:35 2012 +0000
Revision:
1:fc5c9258ec45
Parent:
0:faef9e4c8bea
Minor revisions to interfaces. Functional but still some functionality to add.

Who changed what in which revision?

UserRevisionLine numberNew 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 1:fc5c9258ec45 59 *
shimniok 1:fc5c9258ec45 60 * This is an early revision; I've not yet implemented heading calculation and the interface differs from my
shimniok 1:fc5c9258ec45 61 * earlier LSM303DLH; I hope to make this library drop in compatible at some point in the future.
shimniok 1:fc5c9258ec45 62 * setScale() and setOffset() have no effect at this time.
shimniok 1:fc5c9258ec45 63 *
shimniok 1:fc5c9258ec45 64 * @code
shimniok 1:fc5c9258ec45 65 * #include "mbed.h"
shimniok 1:fc5c9258ec45 66 * #include "LSM303DLM.h"
shimniok 1:fc5c9258ec45 67 *
shimniok 1:fc5c9258ec45 68 * LSM303DLM compass(p28, p27);
shimniok 1:fc5c9258ec45 69 * ...
shimniok 1:fc5c9258ec45 70 * int a[3], m[3];
shimniok 1:fc5c9258ec45 71 * ...
shimniok 1:fc5c9258ec45 72 * compass.readAcc(a);
shimniok 1:fc5c9258ec45 73 * compass.readMag(m);
shimniok 1:fc5c9258ec45 74 *
shimniok 1:fc5c9258ec45 75 * @endcode
shimniok 0:faef9e4c8bea 76 */
shimniok 0:faef9e4c8bea 77 class LSM303DLM {
shimniok 0:faef9e4c8bea 78
shimniok 0:faef9e4c8bea 79 public:
shimniok 0:faef9e4c8bea 80 /** Create a new interface for an LSM303DLM
shimniok 0:faef9e4c8bea 81 *
shimniok 0:faef9e4c8bea 82 * @param sda is the pin for the I2C SDA line
shimniok 0:faef9e4c8bea 83 * @param scl is the pin for the I2C SCL line
shimniok 0:faef9e4c8bea 84 */
shimniok 0:faef9e4c8bea 85 LSM303DLM(PinName sda, PinName scl);
shimniok 0:faef9e4c8bea 86
shimniok 0:faef9e4c8bea 87 /** sets the x, y, and z offset corrections for hard iron calibration
shimniok 0:faef9e4c8bea 88 *
shimniok 0:faef9e4c8bea 89 * Calibration details here:
shimniok 0:faef9e4c8bea 90 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
shimniok 0:faef9e4c8bea 91 *
shimniok 0:faef9e4c8bea 92 * If you gather raw magnetometer data and find, for example, x is offset
shimniok 0:faef9e4c8bea 93 * by hard iron by -20 then pass +20 to this member function to correct
shimniok 0:faef9e4c8bea 94 * for hard iron.
shimniok 0:faef9e4c8bea 95 *
shimniok 0:faef9e4c8bea 96 * @param x is the offset correction for the x axis
shimniok 0:faef9e4c8bea 97 * @param y is the offset correction for the y axis
shimniok 0:faef9e4c8bea 98 * @param z is the offset correction for the z axis
shimniok 0:faef9e4c8bea 99 */
shimniok 0:faef9e4c8bea 100 void setOffset(float x, float y, float z);
shimniok 0:faef9e4c8bea 101
shimniok 0:faef9e4c8bea 102 /** sets the scale factor for the x, y, and z axes
shimniok 0:faef9e4c8bea 103 *
shimniok 0:faef9e4c8bea 104 * Calibratio details here:
shimniok 0:faef9e4c8bea 105 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
shimniok 0:faef9e4c8bea 106 *
shimniok 0:faef9e4c8bea 107 * Sensitivity of the three axes is never perfectly identical and this
shimniok 0:faef9e4c8bea 108 * function can help to correct differences in sensitivity. You're
shimniok 0:faef9e4c8bea 109 * supplying a multipler such that x, y and z will be normalized to the
shimniok 0:faef9e4c8bea 110 * same max/min values
shimniok 0:faef9e4c8bea 111 */
shimniok 0:faef9e4c8bea 112 void setScale(float x, float y, float z);
shimniok 0:faef9e4c8bea 113
shimniok 0:faef9e4c8bea 114 /** read the calibrated accelerometer and magnetometer values
shimniok 0:faef9e4c8bea 115 *
shimniok 0:faef9e4c8bea 116 * @param a is the accelerometer 3d vector, written by the function
shimniok 0:faef9e4c8bea 117 * @param m is the magnetometer 3d vector, written by the function
shimniok 0:faef9e4c8bea 118 */
shimniok 0:faef9e4c8bea 119 void read(int a[3], int m[3]);
shimniok 0:faef9e4c8bea 120
shimniok 0:faef9e4c8bea 121 /** read the calibrated accelerometer values
shimniok 0:faef9e4c8bea 122 *
shimniok 0:faef9e4c8bea 123 * @param a is the accelerometer 3d vector, written by the function
shimniok 0:faef9e4c8bea 124 */
shimniok 0:faef9e4c8bea 125 void readAcc(int a[3]);
shimniok 0:faef9e4c8bea 126
shimniok 0:faef9e4c8bea 127 /** read the calibrated magnetometer values
shimniok 0:faef9e4c8bea 128 *
shimniok 0:faef9e4c8bea 129 * @param m is the magnetometer 3d vector, written by the function
shimniok 0:faef9e4c8bea 130 */
shimniok 0:faef9e4c8bea 131 void readMag(int m[3]);
shimniok 0:faef9e4c8bea 132
shimniok 0:faef9e4c8bea 133 /** sets the I2C bus frequency
shimniok 0:faef9e4c8bea 134 *
shimniok 0:faef9e4c8bea 135 * @param frequency is the I2C bus/clock frequency, either standard (100000) or fast (400000)
shimniok 0:faef9e4c8bea 136 */
shimniok 0:faef9e4c8bea 137 void frequency(int hz);
shimniok 0:faef9e4c8bea 138
shimniok 0:faef9e4c8bea 139 private:
shimniok 0:faef9e4c8bea 140 I2C _device;
shimniok 0:faef9e4c8bea 141 char _data[6];
shimniok 1:fc5c9258ec45 142 int offset[3];
shimniok 1:fc5c9258ec45 143 int scale[3];
shimniok 0:faef9e4c8bea 144 void init();
shimniok 0:faef9e4c8bea 145 };
shimniok 0:faef9e4c8bea 146
shimniok 0:faef9e4c8bea 147 #endif