Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.

Dependencies:   mbed Watchdog SDFileSystem DigoleSerialDisp

Committer:
shimniok
Date:
Fri Nov 30 16:11:53 2018 +0000
Revision:
25:bb5356402687
Parent:
0:a6a169de725f
Initial publish of revised version.

Who changed what in which revision?

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