Allen Wild / LSM9DS0

Dependents:   4180_LSM9DS0_lab HW2_P2 HW2_P3 HW2_P4 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LSM9DS0.h Source File

LSM9DS0.h

00001 // Most of the Credit goes to jimblom
00002 // Modifications by Allen Wild
00003 #ifndef _LSM9DS0_H__
00004 #define _LSM9DS0_H__
00005 
00006 #include "mbed.h"
00007 
00008 ////////////////////////////
00009 // LSM9DS0 Gyro Registers //
00010 ////////////////////////////
00011 #define WHO_AM_I_G          0x0F
00012 #define CTRL_REG1_G         0x20
00013 #define CTRL_REG2_G         0x21
00014 #define CTRL_REG3_G         0x22
00015 #define CTRL_REG4_G         0x23
00016 #define CTRL_REG5_G         0x24
00017 #define REFERENCE_G         0x25
00018 #define STATUS_REG_G        0x27
00019 #define OUT_X_L_G           0x28
00020 #define OUT_X_H_G           0x29
00021 #define OUT_Y_L_G           0x2A
00022 #define OUT_Y_H_G           0x2B
00023 #define OUT_Z_L_G           0x2C
00024 #define OUT_Z_H_G           0x2D
00025 #define FIFO_CTRL_REG_G     0x2E
00026 #define FIFO_SRC_REG_G      0x2F
00027 #define INT1_CFG_G          0x30
00028 #define INT1_SRC_G          0x31
00029 #define INT1_THS_XH_G       0x32
00030 #define INT1_THS_XL_G       0x33
00031 #define INT1_THS_YH_G       0x34
00032 #define INT1_THS_YL_G       0x35
00033 #define INT1_THS_ZH_G       0x36
00034 #define INT1_THS_ZL_G       0x37
00035 #define INT1_DURATION_G     0x38
00036 
00037 //////////////////////////////////////////
00038 // LSM9DS0 Accel/Magneto (XM) Registers //
00039 //////////////////////////////////////////
00040 #define OUT_TEMP_L_XM       0x05
00041 #define OUT_TEMP_H_XM       0x06
00042 #define STATUS_REG_M        0x07
00043 #define OUT_X_L_M           0x08
00044 #define OUT_X_H_M           0x09
00045 #define OUT_Y_L_M           0x0A
00046 #define OUT_Y_H_M           0x0B
00047 #define OUT_Z_L_M           0x0C
00048 #define OUT_Z_H_M           0x0D
00049 #define WHO_AM_I_XM         0x0F
00050 #define INT_CTRL_REG_M      0x12
00051 #define INT_SRC_REG_M       0x13
00052 #define INT_THS_L_M         0x14
00053 #define INT_THS_H_M         0x15
00054 #define OFFSET_X_L_M        0x16
00055 #define OFFSET_X_H_M        0x17
00056 #define OFFSET_Y_L_M        0x18
00057 #define OFFSET_Y_H_M        0x19
00058 #define OFFSET_Z_L_M        0x1A
00059 #define OFFSET_Z_H_M        0x1B
00060 #define REFERENCE_X         0x1C
00061 #define REFERENCE_Y         0x1D
00062 #define REFERENCE_Z         0x1E
00063 #define CTRL_REG0_XM        0x1F
00064 #define CTRL_REG1_XM        0x20
00065 #define CTRL_REG2_XM        0x21
00066 #define CTRL_REG3_XM        0x22
00067 #define CTRL_REG4_XM        0x23
00068 #define CTRL_REG5_XM        0x24
00069 #define CTRL_REG6_XM        0x25
00070 #define CTRL_REG7_XM        0x26
00071 #define STATUS_REG_A        0x27
00072 #define OUT_X_L_A           0x28
00073 #define OUT_X_H_A           0x29
00074 #define OUT_Y_L_A           0x2A
00075 #define OUT_Y_H_A           0x2B
00076 #define OUT_Z_L_A           0x2C
00077 #define OUT_Z_H_A           0x2D
00078 #define FIFO_CTRL_REG       0x2E
00079 #define FIFO_SRC_REG        0x2F
00080 #define INT_GEN_1_REG       0x30
00081 #define INT_GEN_1_SRC       0x31
00082 #define INT_GEN_1_THS       0x32
00083 #define INT_GEN_1_DURATION  0x33
00084 #define INT_GEN_2_REG       0x34
00085 #define INT_GEN_2_SRC       0x35
00086 #define INT_GEN_2_THS       0x36
00087 #define INT_GEN_2_DURATION  0x37
00088 #define CLICK_CFG           0x38
00089 #define CLICK_SRC           0x39
00090 #define CLICK_THS           0x3A
00091 #define TIME_LIMIT          0x3B
00092 #define TIME_LATENCY        0x3C
00093 #define TIME_WINDOW         0x3D
00094 #define ACT_THS             0x3E
00095 #define ACT_DUR             0x3F
00096 
00097 /**
00098  * LSM9DS0 Class - driver for the 9 DoF IMU
00099  */
00100 class LSM9DS0
00101 {
00102 public:
00103 
00104     /// gyro_scale defines the possible full-scale ranges of the gyroscope:
00105     enum gyro_scale
00106     {
00107         G_SCALE_245DPS,     // 00: +/- 245 degrees per second
00108         G_SCALE_500DPS,     // 01: +/- 500 dps
00109         G_SCALE_2000DPS,    // 10: +/- 2000 dps
00110     };
00111 
00112     /// accel_scale defines all possible FSR's of the accelerometer:
00113     enum accel_scale
00114     {
00115         A_SCALE_2G, // 000: +/- 2g
00116         A_SCALE_4G, // 001: +/- 4g
00117         A_SCALE_6G, // 010: +/- 6g
00118         A_SCALE_8G, // 011: +/- 8g
00119         A_SCALE_16G // 100: +/- 16g
00120     };
00121 
00122     /// mag_scale defines all possible FSR's of the magnetometer:
00123     enum mag_scale
00124     {
00125         M_SCALE_2GS,    // 00: +/- 2Gs
00126         M_SCALE_4GS,    // 01: +/- 4Gs
00127         M_SCALE_8GS,    // 10: +/- 8Gs
00128         M_SCALE_12GS,   // 11: +/- 12Gs
00129     };
00130 
00131     /// gyro_odr defines all possible data rate/bandwidth combos of the gyro:
00132     enum gyro_odr
00133     {                           // ODR (Hz) --- Cutoff
00134         G_ODR_95_BW_125  = 0x0, //   95         12.5
00135         G_ODR_95_BW_25   = 0x1, //   95          25
00136         // 0x2 and 0x3 define the same data rate and bandwidth
00137         G_ODR_190_BW_125 = 0x4, //   190        12.5
00138         G_ODR_190_BW_25  = 0x5, //   190         25
00139         G_ODR_190_BW_50  = 0x6, //   190         50
00140         G_ODR_190_BW_70  = 0x7, //   190         70
00141         G_ODR_380_BW_20  = 0x8, //   380         20
00142         G_ODR_380_BW_25  = 0x9, //   380         25
00143         G_ODR_380_BW_50  = 0xA, //   380         50
00144         G_ODR_380_BW_100 = 0xB, //   380         100
00145         G_ODR_760_BW_30  = 0xC, //   760         30
00146         G_ODR_760_BW_35  = 0xD, //   760         35
00147         G_ODR_760_BW_50  = 0xE, //   760         50
00148         G_ODR_760_BW_100 = 0xF, //   760         100
00149     };
00150 
00151     /// accel_oder defines all possible output data rates of the accelerometer:
00152     enum accel_odr
00153     {
00154         A_POWER_DOWN,   // Power-down mode (0x0)
00155         A_ODR_3125,     // 3.125 Hz (0x1)
00156         A_ODR_625,      // 6.25 Hz (0x2)
00157         A_ODR_125,      // 12.5 Hz (0x3)
00158         A_ODR_25,       // 25 Hz (0x4)
00159         A_ODR_50,       // 50 Hz (0x5)
00160         A_ODR_100,      // 100 Hz (0x6)
00161         A_ODR_200,      // 200 Hz (0x7)
00162         A_ODR_400,      // 400 Hz (0x8)
00163         A_ODR_800,      // 800 Hz (9)
00164         A_ODR_1600      // 1600 Hz (0xA)
00165     };
00166     /// accel_oder defines all possible output data rates of the magnetometer:
00167     enum mag_odr
00168     {
00169         M_ODR_3125, // 3.125 Hz (0x00)
00170         M_ODR_625,  // 6.25 Hz (0x01)
00171         M_ODR_125,  // 12.5 Hz (0x02)
00172         M_ODR_25,   // 25 Hz (0x03)
00173         M_ODR_50,   // 50 (0x04)
00174         M_ODR_100,  // 100 Hz (0x05)
00175     };
00176 
00177     // We'll store the gyro, accel, and magnetometer readings in a series of
00178     // public class variables. Each sensor gets three variables -- one for each
00179     // axis. Call readGyro(), readAccel(), and readMag() first, before using
00180     // these variables!
00181     // These values are the RAW signed 16-bit readings from the sensors.
00182     int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope
00183     int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer
00184     int16_t mx_raw, my_raw, mz_raw; // x, y, and z axis readings of the magnetometer
00185     int16_t temperature_raw;
00186 
00187     // floating-point values of scaled data in real-world units
00188     float gx, gy, gz;
00189     float ax, ay, az;
00190     float mx, my, mz;
00191     float temperature_c, temperature_f; // temperature in celcius and fahrenheit
00192 
00193     float abias[3];
00194     float gbias[3];
00195 
00196     
00197     /**  LSM9DS0 -- LSM9DS0 class constructor
00198     *  The constructor will set up a handful of private variables, and set the
00199     *  communication mode as well.
00200     *  Input:
00201     *   - interface = Either MODE_SPI or MODE_I2C, whichever you're using
00202     *               to talk to the IC.
00203     *   - gAddr = If MODE_I2C, this is the I2C address of the gyroscope.
00204     *               If MODE_SPI, this is the chip select pin of the gyro (CSG)
00205     *   - xmAddr = If MODE_I2C, this is the I2C address of the accel/mag.
00206     *               If MODE_SPI, this is the cs pin of the accel/mag (CSXM)
00207     */
00208     LSM9DS0(PinName sda, PinName scl, uint8_t gAddr, uint8_t xmAddr);
00209     
00210     /**  begin() -- Initialize the gyro, accelerometer, and magnetometer.
00211     *  This will set up the scale and output rate of each sensor. It'll also
00212     *  "turn on" every sensor and every axis of every sensor.
00213     *  Input:
00214     *   - gScl = The scale of the gyroscope. This should be a gyro_scale value.
00215     *   - aScl = The scale of the accelerometer. Should be a accel_scale value.
00216     *   - mScl = The scale of the magnetometer. Should be a mag_scale value.
00217     *   - gODR = Output data rate of the gyroscope. gyro_odr value.
00218     *   - aODR = Output data rate of the accelerometer. accel_odr value.
00219     *   - mODR = Output data rate of the magnetometer. mag_odr value.
00220     *  Output: The function will return an unsigned 16-bit value. The most-sig
00221     *       bytes of the output are the WHO_AM_I reading of the accel. The
00222     *       least significant two bytes are the WHO_AM_I reading of the gyro.
00223     *  All parameters have a defaulted value, so you can call just "begin()".
00224     *  Default values are FSR's of: +/- 245DPS, 2g, 2Gs; ODRs of 95 Hz for 
00225     *  gyro, 100 Hz for accelerometer, 100 Hz for magnetometer.
00226     *  Use the return value of this function to verify communication.
00227     */
00228     uint16_t begin(gyro_scale gScl = G_SCALE_245DPS, 
00229                 accel_scale aScl = A_SCALE_2G, mag_scale mScl = M_SCALE_2GS,
00230                 gyro_odr gODR = G_ODR_95_BW_125, accel_odr aODR = A_ODR_50, 
00231                 mag_odr mODR = M_ODR_50);
00232     
00233     /**  readGyro() -- Read the gyroscope output registers.
00234     *  This function will read all six gyroscope output registers.
00235     *  The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read
00236     *  those _after_ calling readGyro().
00237     */
00238     void readGyro();
00239     
00240     /**  readAccel() -- Read the accelerometer output registers.
00241     *  This function will read all six accelerometer output registers.
00242     *  The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read
00243     *  those _after_ calling readAccel().
00244     */
00245     void readAccel();
00246     
00247     /**  readMag() -- Read the magnetometer output registers.
00248     *  This function will read all six magnetometer output registers.
00249     *  The readings are stored in the class' mx_raw, my_raw, and mz_raw variables. Read
00250     *  those _after_ calling readMag().
00251     */
00252     void readMag();
00253     
00254     /**  readTemp() -- Read the temperature output register.
00255     *  This function will read two temperature output registers.
00256     *  The combined readings are stored in the class' temperature variables. Read
00257     *  those _after_ calling readTemp().
00258     */
00259     void readTemp();
00260     
00261     /**  setGyroScale() -- Set the full-scale range of the gyroscope.
00262     *  This function can be called to set the scale of the gyroscope to 
00263     *  245, 500, or 200 degrees per second.
00264     *  Input:
00265     *   - gScl = The desired gyroscope scale. Must be one of three possible
00266     *       values from the gyro_scale enum.
00267     */
00268     void setGyroScale(gyro_scale gScl);
00269     
00270     /**  setAccelScale() -- Set the full-scale range of the accelerometer.
00271     *  This function can be called to set the scale of the accelerometer to
00272     *  2, 4, 6, 8, or 16 g's.
00273     *  Input:
00274     *   - aScl = The desired accelerometer scale. Must be one of five possible
00275     *       values from the accel_scale enum.
00276     */
00277     void setAccelScale(accel_scale aScl);
00278     
00279     /**  setMagScale() -- Set the full-scale range of the magnetometer.
00280     *  This function can be called to set the scale of the magnetometer to
00281     *  2, 4, 8, or 12 Gs.
00282     *  Input:
00283     *   - mScl = The desired magnetometer scale. Must be one of four possible
00284     *       values from the mag_scale enum.
00285     */
00286     void setMagScale(mag_scale mScl);
00287     
00288     /**  setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
00289     *  Input:
00290     *   - gRate = The desired output rate and cutoff frequency of the gyro.
00291     *       Must be a value from the gyro_odr enum (check above, there're 14).
00292     */
00293     void setGyroODR(gyro_odr gRate);
00294     
00295     /**  setAccelODR() -- Set the output data rate of the accelerometer
00296     *  Input:
00297     *   - aRate = The desired output rate of the accel.
00298     *       Must be a value from the accel_odr enum (check above, there're 11).
00299     */
00300     void setAccelODR(accel_odr aRate);
00301     
00302     /**  setMagODR() -- Set the output data rate of the magnetometer
00303     *  Input:
00304     *   - mRate = The desired output rate of the mag.
00305     *       Must be a value from the mag_odr enum (check above, there're 6).
00306     */
00307     void setMagODR(mag_odr mRate);
00308     
00309     /**  configGyroInt() -- Configure the gyro interrupt output.
00310     *  Triggers can be set to either rising above or falling below a specified
00311     *  threshold. This function helps setup the interrupt configuration and 
00312     *  threshold values for all axes.
00313     *  Input:
00314     *   - int1Cfg = A 8-bit value that is sent directly to the INT1_CFG_G
00315     *       register. This sets AND/OR and high/low interrupt gen for each axis
00316     *   - int1ThsX = 16-bit interrupt threshold value for x-axis
00317     *   - int1ThsY = 16-bit interrupt threshold value for y-axis
00318     *   - int1ThsZ = 16-bit interrupt threshold value for z-axis
00319     *   - duration = Duration an interrupt holds after triggered. This value
00320     *       is copied directly into the INT1_DURATION_G register.
00321     *  Before using this function, read about the INT1_CFG_G register and
00322     *  the related INT1* registers in the LMS9DS0 datasheet.
00323     */
00324     void configGyroInt(uint8_t int1Cfg, uint16_t int1ThsX = 0,
00325                           uint16_t int1ThsY = 0, uint16_t int1ThsZ = 0, 
00326                           uint8_t duration = 0);
00327                           
00328     void calcBias();
00329 
00330     /**  return a comass heading (in degrees) using X/Y magnetometer data */
00331     float calcHeading();
00332 
00333 
00334 private:    
00335     /**  xmAddress and gAddress store the I2C address
00336     *  for each sensor.
00337     */
00338     uint8_t xmAddress, gAddress;
00339     
00340     /**  gScale, aScale, and mScale store the current scale range for each 
00341     *  sensor. Should be updated whenever that value changes.
00342     */
00343     gyro_scale gScale;
00344     accel_scale aScale;
00345     mag_scale mScale;
00346     
00347     /**  gRes, aRes, and mRes store the current resolution for each sensor. 
00348     *  Units of these values would be DPS (or g's or Gs's) per ADC tick.
00349     *  This value is calculated as (sensor scale) / (2^15).
00350     */
00351     float gRes, aRes, mRes;
00352     
00353     /**  initGyro() -- Sets up the gyroscope to begin reading.
00354     *  This function steps through all five gyroscope control registers.
00355     *  Upon exit, the following parameters will be set:
00356     *   - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled. 
00357     *       95 Hz ODR, 12.5 Hz cutoff frequency.
00358     *   - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency
00359     *       set to 7.2 Hz (depends on ODR).
00360     *   - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and
00361     *       active high). Data-ready output enabled on DRDY_G.
00362     *   - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower
00363     *       address. Scale set to 245 DPS. SPI mode set to 4-wire.
00364     *   - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled.
00365     */
00366     void initGyro();
00367     
00368     /**  initAccel() -- Sets up the accelerometer to begin reading.
00369     *  This function steps through all accelerometer related control registers.
00370     *  Upon exit these registers will be set as:
00371     *   - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode.
00372     *   - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update.
00373     *       all axes enabled.
00374     *   - CTRL_REG2_XM = 0x00: +/- 2g scale. 773 Hz anti-alias filter BW.
00375     *   - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin.
00376     */
00377     void initAccel();
00378     
00379     /**  initMag() -- Sets up the magnetometer to begin reading.
00380     *  This function steps through all magnetometer-related control registers.
00381     *  Upon exit these registers will be set as:
00382     *   - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin.
00383     *   - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt
00384     *       requests don't latch. Temperature sensor disabled.
00385     *   - CTRL_REG6_XM = 0x00: +/- 2 Gs scale.
00386     *   - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode.
00387     *   - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts.
00388     */
00389     void initMag();
00390     
00391     /**  gReadByte() -- Reads a byte from a specified gyroscope register.
00392     *  Input:
00393     *   - subAddress = Register to be read from.
00394     *  Output:
00395     *   - An 8-bit value read from the requested address.
00396     */
00397     uint8_t gReadByte(uint8_t subAddress);
00398     
00399     /**  gWriteByte() -- Write a byte to a register in the gyroscope.
00400     *  Input:
00401     *   - subAddress = Register to be written to.
00402     *   - data = data to be written to the register.
00403     */
00404     void gWriteByte(uint8_t subAddress, uint8_t data);
00405     
00406     /**  xmReadByte() -- Read a byte from a register in the accel/mag sensor
00407     *  Input:
00408     *   - subAddress = Register to be read from.
00409     *  Output:
00410     *   - An 8-bit value read from the requested register.
00411     */
00412     uint8_t xmReadByte(uint8_t subAddress);
00413     
00414     /**  xmWriteByte() -- Write a byte to a register in the accel/mag sensor.
00415     *  Input:
00416     *   - subAddress = Register to be written to.
00417     *   - data = data to be written to the register.
00418     */
00419     void xmWriteByte(uint8_t subAddress, uint8_t data);
00420     
00421     /**  calcgRes() -- Calculate the resolution of the gyroscope.
00422     *  This function will set the value of the gRes variable. gScale must
00423     *  be set prior to calling this function.
00424     */
00425     void calcgRes();
00426     
00427     /**  calcmRes() -- Calculate the resolution of the magnetometer.
00428     *  This function will set the value of the mRes variable. mScale must
00429     *  be set prior to calling this function.
00430     */
00431     void calcmRes();
00432     
00433     /**  calcaRes() -- Calculate the resolution of the accelerometer.
00434     *  This function will set the value of the aRes variable. aScale must
00435     *  be set prior to calling this function.
00436     */
00437     void calcaRes();
00438     
00439     
00440     ///////////////////
00441     // I2C Functions //
00442     ///////////////////
00443     I2C i2c;
00444 
00445     
00446     /**  I2CwriteByte() -- Write a byte out of I2C to a register in the device
00447     *  Input:
00448     *   - address = The 7-bit I2C address of the slave device.
00449     *   - subAddress = The register to be written to.
00450     *   - data = Byte to be written to the register.
00451     */
00452     void I2CwriteByte(char address, char subAddress, char data);
00453     
00454     /**  I2CreadByte() -- Read a single byte from a register over I2C.
00455     *  Input:
00456     *   - address = The 7-bit I2C address of the slave device.
00457     *   - subAddress = The register to be read from.
00458     *  Output:
00459     *   - The byte read from the requested address.
00460     */
00461     uint8_t I2CreadByte(char address, char subAddress);
00462 };
00463 
00464 #endif // _LSM9DS0_H //