fish

Dependencies:   mbed

Committer:
tzxl10000
Date:
Wed Jun 14 20:22:45 2017 +0000
Revision:
0:561f7672eaad
fish;

Who changed what in which revision?

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