publish

Dependencies:   mbed

Dependents:   RoboCup_2015

Fork of LSM9DS0 by Taylor Andrews

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