My attempt to made a more useful lib. You can get the accelerator and magnetometer.

Fork of LSM303DLH by Michael Shimniok

Committer:
salco
Date:
Sun Aug 06 19:11:23 2017 +0000
Revision:
5:48722ae56546
Parent:
4:4f2ed3f8726c
Child:
6:86cf2afe3e52
a new morning;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 1:48d83c63d1d9 1 #include "mbed.h"
shimniok 1:48d83c63d1d9 2 #include "vector.h"
salco 3:9b4ff901b5c9 3 #include "AnsiGraphicalConsole.h"
shimniok 1:48d83c63d1d9 4
salco 5:48722ae56546 5 #include "LSM303DLH_RegisterDef.h"
salco 5:48722ae56546 6
shimniok 1:48d83c63d1d9 7 #ifndef M_PI
shimniok 1:48d83c63d1d9 8 #define M_PI 3.14159265358979323846
shimniok 1:48d83c63d1d9 9 #endif
shimniok 1:48d83c63d1d9 10
shimniok 1:48d83c63d1d9 11 /** Tilt-compensated compass interface Library for the STMicro LSM303DLH 3-axis magnetometer, 3-axis acceleromter
shimniok 0:de767f4959ef 12 *
salco 5:48722ae56546 13 * @author Salco <JeSuisSalco@gmail.com>
shimniok 0:de767f4959ef 14 *
shimniok 0:de767f4959ef 15 * @code
shimniok 0:de767f4959ef 16 * #include "mbed.h"
shimniok 0:de767f4959ef 17 * #include "LSM303DLH.h"
shimniok 0:de767f4959ef 18 *
shimniok 0:de767f4959ef 19 * Serial debug(USBTX,USBRX);
shimniok 0:de767f4959ef 20 * LSM303DLH compass(p28, p27);
shimniok 0:de767f4959ef 21 *
shimniok 0:de767f4959ef 22 * int main() {
shimniok 0:de767f4959ef 23 * float hdg;
shimniok 0:de767f4959ef 24 * debug.format(8,Serial::None,1);
shimniok 0:de767f4959ef 25 * debug.baud(115200);
shimniok 0:de767f4959ef 26 * debug.printf("LSM303DLH Test\x0d\x0a");
shimniok 0:de767f4959ef 27 * compass.setOffset(29.50, -0.50, 4.00); // example calibration
shimniok 0:de767f4959ef 28 * compass.setScale(1.00, 1.03, 1.21); // example calibration
shimniok 0:de767f4959ef 29 * while(1) {
shimniok 0:de767f4959ef 30 * hdg = compass.heading();
shimniok 0:de767f4959ef 31 * debug.printf("Heading: %.2f\n", hdg);
shimniok 0:de767f4959ef 32 * wait(0.1);
shimniok 0:de767f4959ef 33 * }
shimniok 0:de767f4959ef 34 * }
shimniok 1:48d83c63d1d9 35 * @endcode
shimniok 0:de767f4959ef 36 */
salco 4:4f2ed3f8726c 37
shimniok 0:de767f4959ef 38 class LSM303DLH {
shimniok 0:de767f4959ef 39 public:
shimniok 0:de767f4959ef 40 /** Create a new interface for an LSM303DLH
shimniok 0:de767f4959ef 41 *
shimniok 0:de767f4959ef 42 * @param sda is the pin for the I2C SDA line
shimniok 0:de767f4959ef 43 * @param scl is the pin for the I2C SCL line
shimniok 0:de767f4959ef 44 */
shimniok 0:de767f4959ef 45 LSM303DLH(PinName sda, PinName scl);
salco 5:48722ae56546 46 /** Create a new interface for an LSM303DLH
salco 5:48722ae56546 47 *
salco 5:48722ae56546 48 * @param ptrI2C is a pointer from existing I2C
salco 5:48722ae56546 49 */
salco 3:9b4ff901b5c9 50 LSM303DLH(I2C* ptrI2C);
shimniok 0:de767f4959ef 51 /** sets the x, y, and z offset corrections for hard iron calibration
shimniok 0:de767f4959ef 52 *
shimniok 0:de767f4959ef 53 * Calibration details here:
shimniok 0:de767f4959ef 54 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
shimniok 0:de767f4959ef 55 *
shimniok 0:de767f4959ef 56 * If you gather raw magnetometer data and find, for example, x is offset
shimniok 0:de767f4959ef 57 * by hard iron by -20 then pass +20 to this member function to correct
shimniok 0:de767f4959ef 58 * for hard iron.
shimniok 0:de767f4959ef 59 *
shimniok 0:de767f4959ef 60 * @param x is the offset correction for the x axis
shimniok 0:de767f4959ef 61 * @param y is the offset correction for the y axis
shimniok 0:de767f4959ef 62 * @param z is the offset correction for the z axis
shimniok 0:de767f4959ef 63 */
shimniok 0:de767f4959ef 64 void setOffset(float x, float y, float z);
shimniok 0:de767f4959ef 65
shimniok 0:de767f4959ef 66 /** sets the scale factor for the x, y, and z axes
shimniok 0:de767f4959ef 67 *
shimniok 0:de767f4959ef 68 * Calibratio details here:
shimniok 0:de767f4959ef 69 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
shimniok 0:de767f4959ef 70 *
shimniok 0:de767f4959ef 71 * Sensitivity of the three axes is never perfectly identical and this
shimniok 0:de767f4959ef 72 * function can help to correct differences in sensitivity. You're
shimniok 0:de767f4959ef 73 * supplying a multipler such that x, y and z will be normalized to the
shimniok 0:de767f4959ef 74 * same max/min values
shimniok 0:de767f4959ef 75 */
shimniok 0:de767f4959ef 76 void setScale(float x, float y, float z);
shimniok 0:de767f4959ef 77
shimniok 0:de767f4959ef 78 /** read the raw accelerometer and compass values
shimniok 0:de767f4959ef 79 *
shimniok 0:de767f4959ef 80 * @param a is the accelerometer 3d vector, written by the function
shimniok 0:de767f4959ef 81 * @param m is the magnetometer 3d vector, written by the function
shimniok 0:de767f4959ef 82 */
salco 3:9b4ff901b5c9 83 bool read(vector &a, vector &m);
shimniok 0:de767f4959ef 84
shimniok 0:de767f4959ef 85 /** returns the magnetic heading with respect to the y axis
shimniok 0:de767f4959ef 86 *
shimniok 0:de767f4959ef 87 */
shimniok 0:de767f4959ef 88 float heading(void);
shimniok 0:de767f4959ef 89
shimniok 0:de767f4959ef 90 /** returns the heading with respect to the specified vector
shimniok 0:de767f4959ef 91 *
shimniok 0:de767f4959ef 92 */
shimniok 0:de767f4959ef 93 float heading(vector from);
shimniok 0:de767f4959ef 94
shimniok 2:aea5caec809c 95 /** sets the I2C bus frequency
shimniok 2:aea5caec809c 96 *
shimniok 2:aea5caec809c 97 * @param frequency is the I2C bus/clock frequency, either standard (100000) or fast (400000)
shimniok 2:aea5caec809c 98 */
shimniok 2:aea5caec809c 99 void frequency(int hz);
salco 5:48722ae56546 100
salco 5:48722ae56546 101 private:
salco 5:48722ae56546 102 enum REG_ADDRS {
salco 5:48722ae56546 103 /* --- Mag --- */
salco 5:48722ae56546 104 CRA_REG_M = 0x00,
salco 5:48722ae56546 105 CRB_REG_M = 0x01,
salco 5:48722ae56546 106 MR_REG_M = 0x02,
salco 5:48722ae56546 107 OUT_X_M = 0x03,
salco 5:48722ae56546 108 OUT_Y_M = 0x05,
salco 5:48722ae56546 109 OUT_Z_M = 0x07,
salco 5:48722ae56546 110 SR_REG_M = 0x09,
salco 5:48722ae56546 111 /* --- Acc --- */
salco 5:48722ae56546 112 CTRL_REG1_A = 0x20,
salco 5:48722ae56546 113 CTRL_REG4_A = 0x23,
salco 5:48722ae56546 114 STATUS_REG_A= 0x27,
salco 5:48722ae56546 115 OUT_X_A = 0x28,
salco 5:48722ae56546 116 OUT_Y_A = 0x2A,
salco 5:48722ae56546 117 OUT_Z_A = 0x2C,
salco 4:4f2ed3f8726c 118 };
salco 4:4f2ed3f8726c 119
salco 3:9b4ff901b5c9 120 I2C* m_ptr_I2C;//_compass;
shimniok 0:de767f4959ef 121 float _offset_x;
shimniok 0:de767f4959ef 122 float _offset_y;
shimniok 0:de767f4959ef 123 float _offset_z;
shimniok 0:de767f4959ef 124 float _scale_x;
shimniok 0:de767f4959ef 125 float _scale_y;
shimniok 0:de767f4959ef 126 float _scale_z;
shimniok 1:48d83c63d1d9 127 long _filt_ax;
shimniok 1:48d83c63d1d9 128 long _filt_ay;
shimniok 1:48d83c63d1d9 129 long _filt_az;
shimniok 1:48d83c63d1d9 130
salco 3:9b4ff901b5c9 131 void init(void) ;
salco 5:48722ae56546 132
salco 5:48722ae56546 133 bool write_reg(int addr_i2c,int addr_reg, uint8_t v);
shimniok 0:de767f4959ef 134 bool write_reg(int addr_i2c,int addr_reg, char v);
salco 5:48722ae56546 135
salco 5:48722ae56546 136 bool read_reg(int addr_i2c,int addr_reg, uint8_t *v);
shimniok 0:de767f4959ef 137 bool read_reg(int addr_i2c,int addr_reg, char *v);
salco 5:48722ae56546 138
shimniok 0:de767f4959ef 139 bool read_reg_short(int addr_i2c,int addr_reg, short *v);
salco 5:48722ae56546 140
salco 5:48722ae56546 141 int8_t get_FullScall_selection(void);
shimniok 0:de767f4959ef 142 };