LSM9DS1 IMU sensor driver i2c

Dependents:   read_Pmod optWingforHAPS_Eigen hexaTest_Eigen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LSM9DS1.h Source File

LSM9DS1.h

00001 // Most of the Credit goes to jimblom
00002 // Modifications by Allen Wild
00003 #ifndef _LSM9DS1_H__
00004 #define _LSM9DS1_H__
00005 
00006 #include "mbed.h"
00007 
00008 /////////////////////////////////////////
00009 // LSM9DS1 Accel/Gyro (XL/G) Registers //
00010 /////////////////////////////////////////
00011 #define ACT_THS             0x04
00012 #define ACT_DUR             0x05
00013 #define INT_GEN_CFG_XL      0x06
00014 #define INT_GEN_THS_X_XL    0x07
00015 #define INT_GEN_THS_Y_XL    0x08
00016 #define INT_GEN_THS_Z_XL    0x09
00017 #define INT_GEN_DUR_XL      0x0A
00018 #define REFERENCE_G         0x0B
00019 #define INT1_CTRL           0x0C
00020 #define INT2_CTRL           0x0D
00021 #define WHO_AM_I_XG         0x0F
00022 #define CTRL_REG1_G         0x10
00023 #define CTRL_REG2_G         0x11
00024 #define CTRL_REG3_G         0x12
00025 #define ORIENT_CFG_G        0x13
00026 #define INT_GEN_SRC_G       0x14
00027 #define OUT_TEMP_L          0x15
00028 #define OUT_TEMP_H          0x16
00029 #define STATUS_REG_0        0x17
00030 #define OUT_X_L_G           0x18
00031 #define OUT_X_H_G           0x19
00032 #define OUT_Y_L_G           0x1A
00033 #define OUT_Y_H_G           0x1B
00034 #define OUT_Z_L_G           0x1C
00035 #define OUT_Z_H_G           0x1D
00036 #define CTRL_REG4           0x1E
00037 #define CTRL_REG5_XL        0x1F
00038 #define CTRL_REG6_XL        0x20
00039 #define CTRL_REG7_XL        0x21
00040 #define CTRL_REG8           0x22
00041 #define CTRL_REG9           0x23
00042 #define CTRL_REG10          0x24
00043 #define INT_GEN_SRC_XL      0x26
00044 #define STATUS_REG_1        0x27
00045 #define OUT_X_L_XL          0x28
00046 #define OUT_X_H_XL          0x29
00047 #define OUT_Y_L_XL          0x2A
00048 #define OUT_Y_H_XL          0x2B
00049 #define OUT_Z_L_XL          0x2C
00050 #define OUT_Z_H_XL          0x2D
00051 #define FIFO_CTRL           0x2E
00052 #define FIFO_SRC            0x2F
00053 #define INT_GEN_CFG_G       0x30
00054 #define INT_GEN_THS_XH_G    0x31
00055 #define INT_GEN_THS_XL_G    0x32
00056 #define INT_GEN_THS_YH_G    0x33
00057 #define INT_GEN_THS_YL_G    0x34
00058 #define INT_GEN_THS_ZH_G    0x35
00059 #define INT_GEN_THS_ZL_G    0x36
00060 #define INT_GEN_DUR_G       0x37
00061 
00062 ///////////////////////////////
00063 // LSM9DS1 Magneto Registers //
00064 ///////////////////////////////
00065 #define OFFSET_X_REG_L_M    0x05
00066 #define OFFSET_X_REG_H_M    0x06
00067 #define OFFSET_Y_REG_L_M    0x07
00068 #define OFFSET_Y_REG_H_M    0x08
00069 #define OFFSET_Z_REG_L_M    0x09
00070 #define OFFSET_Z_REG_H_M    0x0A
00071 #define WHO_AM_I_M          0x0F
00072 #define CTRL_REG1_M         0x20
00073 #define CTRL_REG2_M         0x21
00074 #define CTRL_REG3_M         0x22
00075 #define CTRL_REG4_M         0x23
00076 #define CTRL_REG5_M         0x24
00077 #define STATUS_REG_M        0x27
00078 #define OUT_X_L_M           0x28
00079 #define OUT_X_H_M           0x29
00080 #define OUT_Y_L_M           0x2A
00081 #define OUT_Y_H_M           0x2B
00082 #define OUT_Z_L_M           0x2C
00083 #define OUT_Z_H_M           0x2D
00084 #define INT_CFG_M           0x30
00085 #define INT_SRC_M           0x30
00086 #define INT_THS_L_M         0x32
00087 #define INT_THS_H_M         0x33
00088 
00089 ////////////////////////////////
00090 // LSM9DS1 WHO_AM_I Responses //
00091 ////////////////////////////////
00092 #define WHO_AM_I_AG_RSP     0x68
00093 #define WHO_AM_I_M_RSP      0x3D
00094 
00095 // Possible I2C addresses for the accel/gyro and mag
00096 #define LSM9DS1_AG_I2C_ADDR(sa0) ((sa0) ? 0xD6 : 0xD4)
00097 #define LSM9DS1_M_I2C_ADDR(sa1) ((sa1) ? 0x3C : 0x38)
00098 
00099 /**
00100  * LSM9DS1 Class - driver for the 9 DoF IMU
00101  */
00102 class LSM9DS1
00103 {
00104 public:
00105 
00106     /// gyro_scale defines the possible full-scale ranges of the gyroscope:
00107     enum gyro_scale
00108     {
00109         G_SCALE_245DPS = 0x0 << 3,     // 00 << 3: +/- 245 degrees per second
00110         G_SCALE_500DPS = 0x1 << 3,     // 01 << 3: +/- 500 dps
00111         G_SCALE_2000DPS = 0x3 << 3     // 11 << 3: +/- 2000 dps
00112     };
00113 
00114     /// gyro_odr defines all possible data rate/bandwidth combos of the gyro:
00115     enum gyro_odr
00116     {                               // ODR (Hz) --- Cutoff
00117         G_POWER_DOWN     = 0x00,    //  0           0
00118         G_ODR_15_BW_0    = 0x20,    //  14.9        0
00119         G_ODR_60_BW_16   = 0x40,    //  59.5        16
00120         G_ODR_119_BW_14  = 0x60,    //  119         14
00121         G_ODR_119_BW_31  = 0x61,    //  119         31
00122         G_ODR_238_BW_14  = 0x80,    //  238         14
00123         G_ODR_238_BW_29  = 0x81,    //  238         29
00124         G_ODR_238_BW_63  = 0x82,    //  238         63
00125         G_ODR_238_BW_78  = 0x83,    //  238         78
00126         G_ODR_476_BW_21  = 0xA0,    //  476         21
00127         G_ODR_476_BW_28  = 0xA1,    //  476         28
00128         G_ODR_476_BW_57  = 0xA2,    //  476         57
00129         G_ODR_476_BW_100 = 0xA3,    //  476         100
00130         G_ODR_952_BW_33  = 0xC0,    //  952         33
00131         G_ODR_952_BW_40  = 0xC1,    //  952         40
00132         G_ODR_952_BW_58  = 0xC2,    //  952         58
00133         G_ODR_952_BW_100 = 0xC3     //  952         100
00134     };
00135 
00136     /// accel_scale defines all possible FSR's of the accelerometer:
00137     enum accel_scale
00138     {
00139         A_SCALE_2G, // 00: +/- 2g
00140         A_SCALE_16G,// 01: +/- 16g
00141         A_SCALE_4G, // 10: +/- 4g
00142         A_SCALE_8G  // 11: +/- 8g
00143     };
00144 
00145     /// accel_oder defines all possible output data rates of the accelerometer:
00146     enum accel_odr
00147     {
00148         A_POWER_DOWN,   // Power-down mode (0x0)
00149         A_ODR_10,       // 10 Hz (0x1)
00150         A_ODR_50,       // 50 Hz (0x2)
00151         A_ODR_119,      // 119 Hz (0x3)
00152         A_ODR_238,      // 238 Hz (0x4)
00153         A_ODR_476,      // 476 Hz (0x5)
00154         A_ODR_952       // 952 Hz (0x6)
00155     };
00156 
00157     // accel_bw defines all possible bandwiths for low-pass filter of the accelerometer:
00158     enum accel_bw
00159     {
00160         A_BW_AUTO_SCALE = 0x0,  // Automatic BW scaling (0x0)
00161         A_BW_408 = 0x4,         // 408 Hz (0x4)
00162         A_BW_211 = 0x5,         // 211 Hz (0x5)
00163         A_BW_105 = 0x6,         // 105 Hz (0x6)
00164         A_BW_50 = 0x7           // 50 Hz (0x7)
00165     };
00166 
00167     /// mag_scale defines all possible FSR's of the magnetometer:
00168     enum mag_scale
00169     {
00170         M_SCALE_4GS,    // 00: +/- 4Gs
00171         M_SCALE_8GS,    // 01: +/- 8Gs
00172         M_SCALE_12GS,   // 10: +/- 12Gs
00173         M_SCALE_16GS,   // 11: +/- 16Gs
00174     };
00175 
00176     /// mag_odr defines all possible output data rates of the magnetometer:
00177     enum mag_odr
00178     {
00179         M_ODR_0625, // 0.625 Hz (0x00)
00180         M_ODR_125,  // 1.25 Hz  (0x01)
00181         M_ODR_25,   // 2.5 Hz   (0x02)
00182         M_ODR_5,    // 5 Hz     (0x03)
00183         M_ODR_10,   // 10       (0x04)
00184         M_ODR_20,   // 20 Hz    (0x05)
00185         M_ODR_40,   // 40 Hz    (0x06)
00186         M_ODR_80    // 80 Hz    (0x07)
00187     };
00188 
00189     // We'll store the gyro, accel, and magnetometer readings in a series of
00190     // public class variables. Each sensor gets three variables -- one for each
00191     // axis. Call readGyro(), readAccel(), and readMag() first, before using
00192     // these variables!
00193     // These values are the RAW signed 16-bit readings from the sensors.
00194     int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope
00195     int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer
00196     int16_t mx_raw, my_raw, mz_raw; // x, y, and z axis readings of the magnetometer
00197     int16_t temperature_raw;
00198 
00199     // floating-point values of scaled data in real-world units
00200     float gx, gy, gz;
00201     float ax, ay, az;
00202     float mx, my, mz;
00203     float temperature_c, temperature_f; // temperature in celcius and fahrenheit
00204 
00205     
00206     /**  LSM9DS1 -- LSM9DS1 class constructor
00207     *  The constructor will set up a handful of private variables, and set the
00208     *  communication mode as well.
00209     *  Input:
00210     *   - interface = Either MODE_SPI or MODE_I2C, whichever you're using
00211     *               to talk to the IC.
00212     *   - xgAddr = If MODE_I2C, this is the I2C address of the accel/gyro.
00213     *               If MODE_SPI, this is the chip select pin of the accel/gyro (CS_A/G)
00214     *   - mAddr = If MODE_I2C, this is the I2C address of the mag.
00215     *               If MODE_SPI, this is the cs pin of the mag (CS_M)
00216     */
00217     LSM9DS1(I2C& p_i2c, uint8_t xgAddr = LSM9DS1_AG_I2C_ADDR(1), uint8_t mAddr = LSM9DS1_M_I2C_ADDR(1));
00218     
00219     /**  begin() -- Initialize the gyro, accelerometer, and magnetometer.
00220     *  This will set up the scale and output rate of each sensor. It'll also
00221     *  "turn on" every sensor and every axis of every sensor.
00222     *  Input:
00223     *   - gScl = The scale of the gyroscope. This should be a gyro_scale value.
00224     *   - aScl = The scale of the accelerometer. Should be a accel_scale value.
00225     *   - mScl = The scale of the magnetometer. Should be a mag_scale value.
00226     *   - gODR = Output data rate of the gyroscope. gyro_odr value.
00227     *   - aODR = Output data rate of the accelerometer. accel_odr value.
00228     *   - mODR = Output data rate of the magnetometer. mag_odr value.
00229     *  Output: The function will return an unsigned 16-bit value. The most-sig
00230     *       bytes of the output are the WHO_AM_I reading of the accel/gyro. The
00231     *       least significant two bytes are the WHO_AM_I reading of the mag.
00232     *  All parameters have a defaulted value, so you can call just "begin()".
00233     *  Default values are FSR's of: +/- 245DPS, 4g, 2Gs; ODRs of 119 Hz for 
00234     *  gyro, 119 Hz for accelerometer, 80 Hz for magnetometer.
00235     *  Use the return value of this function to verify communication.
00236     */
00237     uint16_t begin(gyro_scale gScl = G_SCALE_245DPS, 
00238                 accel_scale aScl = A_SCALE_2G, mag_scale mScl = M_SCALE_4GS,
00239                 gyro_odr gODR = G_ODR_119_BW_14, accel_odr aODR = A_ODR_119, 
00240                 mag_odr mODR = M_ODR_80);
00241     
00242     /**  readGyro() -- Read the gyroscope output registers.
00243     *  This function will read all six gyroscope output registers.
00244     *  The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read
00245     *  those _after_ calling readGyro().
00246     */
00247     void readGyro();
00248     
00249     /**  readAccel() -- Read the accelerometer output registers.
00250     *  This function will read all six accelerometer output registers.
00251     *  The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read
00252     *  those _after_ calling readAccel().
00253     */
00254     void readAccel();
00255     
00256     /**  readMag() -- Read the magnetometer output registers.
00257     *  This function will read all six magnetometer output registers.
00258     *  The readings are stored in the class' mx_raw, my_raw, and mz_raw variables. Read
00259     *  those _after_ calling readMag().
00260     */
00261     void readMag();
00262     
00263     /**  readTemp() -- Read the temperature output register.
00264     *  This function will read two temperature output registers.
00265     *  The combined readings are stored in the class' temperature variables. Read
00266     *  those _after_ calling readTemp().
00267     */
00268     void readTemp();
00269     
00270     /**  setGyroScale() -- Set the full-scale range of the gyroscope.
00271     *  This function can be called to set the scale of the gyroscope to 
00272     *  245, 500, or 2000 degrees per second.
00273     *  Input:
00274     *   - gScl = The desired gyroscope scale. Must be one of three possible
00275     *       values from the gyro_scale enum.
00276     */
00277     void setGyroScale(gyro_scale gScl);
00278     
00279     /**  setAccelScale() -- Set the full-scale range of the accelerometer.
00280     *  This function can be called to set the scale of the accelerometer to
00281     *  2, 4, 8, or 16 g's.
00282     *  Input:
00283     *   - aScl = The desired accelerometer scale. Must be one of five possible
00284     *       values from the accel_scale enum.
00285     */
00286     void setAccelScale(accel_scale aScl);
00287     
00288     /**  setMagScale() -- Set the full-scale range of the magnetometer.
00289     *  This function can be called to set the scale of the magnetometer to
00290     *  4, 8, 12, or 16 Gs.
00291     *  Input:
00292     *   - mScl = The desired magnetometer scale. Must be one of four possible
00293     *       values from the mag_scale enum.
00294     */
00295     void setMagScale(mag_scale mScl);
00296     
00297     /**  setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
00298     *  Input:
00299     *   - gRate = The desired output rate and cutoff frequency of the gyro.
00300     *       Must be a value from the gyro_odr enum (check above).
00301     */
00302     void setGyroODR(gyro_odr gRate);
00303     
00304     /**  setAccelODR() -- Set the output data rate of the accelerometer
00305     *  Input:
00306     *   - aRate = The desired output rate of the accel.
00307     *       Must be a value from the accel_odr enum (check above).
00308     */
00309     void setAccelODR(accel_odr aRate);
00310     
00311     /**  setMagODR() -- Set the output data rate of the magnetometer
00312     *  Input:
00313     *   - mRate = The desired output rate of the mag.
00314     *       Must be a value from the mag_odr enum (check above).
00315     */
00316     void setMagODR(mag_odr mRate);
00317 
00318 
00319 private:    
00320     /**  xgAddress and mAddress store the I2C address
00321     *  for each sensor.
00322     */
00323     uint8_t xgAddress, mAddress;
00324     
00325     // I2C bus
00326     I2C &i2c;
00327 
00328     /**  gScale, aScale, and mScale store the current scale range for each 
00329     *  sensor. Should be updated whenever that value changes.
00330     */
00331     gyro_scale gScale;
00332     accel_scale aScale;
00333     mag_scale mScale;
00334     
00335     /**  gRes, aRes, and mRes store the current resolution for each sensor. 
00336     *  Units of these values would be DPS (or g's or Gs's) per ADC tick.
00337     *  This value is calculated as (sensor scale) / (2^15).
00338     */
00339     float gRes, aRes, mRes;
00340     
00341     /**  initGyro() -- Sets up the gyroscope to begin reading.
00342     *  This function steps through all three gyroscope control registers.
00343     */
00344     void initGyro();
00345     
00346     /**  initAccel() -- Sets up the accelerometer to begin reading.
00347     *  This function steps through all accelerometer related control registers.
00348     */
00349     void initAccel();
00350     
00351     /**  initMag() -- Sets up the magnetometer to begin reading.
00352     *  This function steps through all magnetometer-related control registers.
00353     */
00354     void initMag();
00355     
00356     /**  calcgRes() -- Calculate the resolution of the gyroscope.
00357     *  This function will set the value of the gRes variable. gScale must
00358     *  be set prior to calling this function.
00359     */
00360     void calcgRes();
00361     
00362     /**  calcmRes() -- Calculate the resolution of the magnetometer.
00363     *  This function will set the value of the mRes variable. mScale must
00364     *  be set prior to calling this function.
00365     */
00366     void calcmRes();
00367     
00368     /**  calcaRes() -- Calculate the resolution of the accelerometer.
00369     *  This function will set the value of the aRes variable. aScale must
00370     *  be set prior to calling this function.
00371     */
00372     void calcaRes();
00373 };
00374 
00375 #endif // _LSM9DS1_H //