Raynaud Gilles / LSM6DS33_GR1-1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LSM6DS33_GR1.h Source File

LSM6DS33_GR1.h

00001 // Based on Eugene Gonzalez's version of LSM9DS1_Demo
00002 // Modified by Sherry Yang for LSM6DS3 sensor
00003 // Modified by Brian Claus for LSM6DS33 sensor
00004 // Modified by Gilles Raynaud
00005 #ifndef _LSM6DS33_H__
00006 #define _LSM6DS33_H__
00007 
00008 #include "mbed.h"
00009 
00010 /////////////////////////////////////////
00011 // LSM6DS33 Accel/Gyro (XL/G) Registers //
00012 /////////////////////////////////////////
00013 #define RAM_ACCESS            0x01
00014 #define FIFO_CTRL1            0x06
00015 #define FIFO_CTRL2            0x07
00016 #define FIFO_CTRL3            0x08
00017 #define FIFO_CTRL4            0x09
00018 #define FIFO_CTRL5            0x0A
00019 #define ORIENT_CFG_G          0x0B
00020 #define INT1_CTRL             0x0D
00021 #define INT2_CTRL             0x0E
00022 #define WHO_AM_I_REG          0X0F
00023 #define CTRL1_XL              0x10
00024 #define CTRL2_G               0x11
00025 #define CTRL3_C               0x12
00026 #define CTRL4_C               0x13
00027 #define CTRL5_C               0x14
00028 #define CTRL6_C               0x15
00029 #define CTRL7_G               0x16
00030 #define CTRL8_XL              0x17
00031 #define CTRL9_XL              0x18
00032 #define CTRL10_C              0x19
00033 #define WAKE_UP_SRC           0x1B
00034 #define TAP_SRC               0x1C
00035 #define D6D_SRC               0x1D
00036 #define STATUS_REG            0x1E
00037 #define OUT_TEMP_L            0x20
00038 #define OUT_TEMP_H            0x21
00039 #define OUTX_L_G              0x22
00040 #define OUTX_H_G              0x23
00041 #define OUTY_L_G              0x24
00042 #define OUTY_H_G              0x25
00043 #define OUTZ_L_G              0x26
00044 #define OUTZ_H_G              0x27
00045 #define OUTX_L_XL             0x28
00046 #define OUTX_H_XL             0x29
00047 #define OUTY_L_XL             0x2A
00048 #define OUTY_H_XL             0x2B
00049 #define OUTZ_L_XL             0x2C
00050 #define OUTZ_H_XL             0x2D
00051 #define FIFO_STATUS1          0x3A
00052 #define FIFO_STATUS2          0x3B
00053 #define FIFO_STATUS3          0x3C
00054 #define FIFO_STATUS4          0x3D
00055 #define FIFO_DATA_OUT_L       0x3E
00056 #define FIFO_DATA_OUT_H       0x3F
00057 #define TIMESTAMP0_REG        0x40
00058 #define TIMESTAMP1_REG        0x41
00059 #define TIMESTAMP2_REG        0x42
00060 #define STEP_TIMESTAMP_L      0x49
00061 #define STEP_TIMESTAMP_H      0x4A
00062 #define STEP_COUNTER_L        0x4B
00063 #define STEP_COUNTER_H        0x4C
00064 #define FUNC_SR               0x53
00065 #define TAP_CFG               0x58
00066 #define TAP_THS_6D            0x59
00067 #define INT_DUR2              0x5A
00068 #define WAKE_UP_THS           0x5B
00069 #define WAKE_UP_DUR           0x5C
00070 #define FREE_FALL             0x5D
00071 #define MD1_CFG               0x5E
00072 #define MD2_CFG               0x5F
00073 
00074 // Possible I2C addresses for the accel/gyro
00075 #define LSM6DS33_AG_I2C_ADDR(sa0) ((sa0) ? 0xD6 : 0xD4)
00076 
00077 /**
00078  * LSM6DS33 Class - driver for the 6 DoF IMU
00079  */
00080 class LSM6DS33
00081 {
00082 public:
00083 
00084     /// gyro_scale defines the possible full-scale ranges of the gyroscope:
00085     enum gyro_scale
00086     {
00087         G_SCALE_125DPS = 0x1 ,     // 00 << 3: +/- 125 degrees per second
00088         G_SCALE_250DPS = 0x0 ,     // 00 << 3: +/- 250 degrees per second
00089         G_SCALE_500DPS = 0x2 ,     // 01 << 3: +/- 500 dps
00090         G_SCALE_1000DPS = 0x4 ,    // 10 << 3: +/- 1000 dps
00091         G_SCALE_2000DPS = 0x6      // 11 << 3: +/- 2000 dps  ////GR  <<2 au lieu de 3
00092     };
00093 
00094     /// gyro_odr defines all possible data rate/bandwidth combos of the gyro:
00095     enum gyro_odr
00096     {                               // ODR (Hz) --- Cutoff
00097         G_POWER_DOWN     = 0x00,    //  0           0
00098         G_ODR_13_BW_0    = 0x10,    //  12.5        0.0081      low power
00099         G_ODR_26_BW_2    = 0x20,    //  26          2.07        low power
00100         G_ODR_52_BW_16   = 0x30,    //  52          16.32       low power
00101         G_ODR_104        = 0x40,    //  104         
00102         G_ODR_208        = 0x50,    //  208         
00103         G_ODR_416        = 0x60,    //  416         
00104         G_ODR_833        = 0x70,    //  833         
00105         G_ODR_1660       = 0x80     //  1660
00106     };
00107 
00108     /// accel_scale defines all possible FSR's of the accelerometer:
00109     enum accel_scale
00110     {
00111         A_SCALE_2G, // 00: +/- 2g
00112         A_SCALE_16G,// 01: +/- 16g
00113         A_SCALE_4G, // 10: +/- 4g
00114         A_SCALE_8G  // 11: +/- 8g
00115     };
00116 
00117     /// accel_oder defines all possible output data rates of the accelerometer:
00118     enum accel_odr
00119     {
00120         A_POWER_DOWN,   // Power-down mode (0x0)
00121         A_ODR_13,       // 12.5 Hz (0x1)        low power
00122         A_ODR_26,       // 26 Hz (0x2)          low power
00123         A_ODR_52,       // 52 Hz (0x3)          low power
00124         A_ODR_104,      // 104 Hz (0x4)         normal mode
00125         A_ODR_208,      // 208 Hz (0x5)         normal mode
00126         A_ODR_416,      // 416 Hz (0x6)         high performance
00127         A_ODR_833,      // 833 Hz (0x7)         high performance
00128         A_ODR_1660,     // 1.66 kHz (0x8)       high performance
00129         A_ODR_3330,     // 3.33 kHz (0x9)       high performance
00130         A_ODR_6660,     // 6.66 kHz (0xA)       high performance
00131     };
00132 
00133     // accel_bw defines all possible bandwiths for low-pass filter of the accelerometer:
00134     enum accel_bw
00135     {
00136         
00137         A_BW_400 = 0x0,         // 400 Hz 
00138         A_BW_200 = 0x1,         // 200 Hz 
00139         A_BW_100 = 0x2,         // 100 Hz 
00140         A_BW_50 = 0x3           // 50 Hz 
00141     };
00142     
00143     
00144 
00145     // We'll store the gyro, and accel, readings in a series of
00146     // public class variables. Each sensor gets three variables -- one for each
00147     // axis. Call readGyro(), and readAccel() first, before using
00148     // these variables!
00149     // These values are the RAW signed 16-bit readings from the sensors.
00150     int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope
00151     int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer
00152     int16_t temperature_raw;
00153     int32_t time_raw;
00154     // modif GR1 valeurs brutes en octet
00155     char gxh,gxl, gyh, gyl, gzh, gzl; // x, y, and z axis readings of the gyroscope
00156     char axh,axl, ayh, ayl, azh, azl; // x, y, and z axis readings of the accelerometer
00157     char gxoh,gxol, gyoh, gyol, gzoh, gzol;// offset
00158 
00159     // floating-point values of scaled data in real-world units
00160     double gx, gy, gz;
00161     double gx_off,gy_off,gz_off;
00162     double ax, ay, az;
00163     double temperature_c, temperature_f; // temperature in celcius and fahrenheit
00164     double intr;
00165     double time;
00166 
00167     
00168     /**  LSM6DS33 -- LSM6DS33 class constructor
00169     *  The constructor will set up a handful of private variables, and set the
00170     *  communication mode as well.
00171     *  Input:
00172     *   - interface = Either MODE_SPI or MODE_I2C, whichever you're using
00173     *               to talk to the IC.
00174     *   - xgAddr = If MODE_I2C, this is the I2C address of the accel/gyro.
00175     *               If MODE_SPI, this is the chip select pin of the accel/gyro (CS_A/G)
00176     */
00177     LSM6DS33(PinName sda, PinName scl, uint8_t xgAddr = LSM6DS33_AG_I2C_ADDR(1));
00178     
00179     /**  begin() -- Initialize the gyro, and accelerometer.
00180     *  This will set up the scale and output rate of each sensor. It'll also
00181     *  "turn on" every sensor and every axis of every sensor.
00182     *  Input:
00183     *   - gScl = The scale of the gyroscope. This should be a gyro_scale value.
00184     *   - aScl = The scale of the accelerometer. Should be a accel_scale value.
00185     *   - gODR = Output data rate of the gyroscope. gyro_odr value.
00186     *   - aODR = Output data rate of the accelerometer. accel_odr value.
00187     *  Output: The function will return an unsigned 16-bit value. The most-sig
00188     *       bytes of the output are the WHO_AM_I reading of the accel/gyro.
00189     *  All parameters have a defaulted value, so you can call just "begin()".
00190     *  Default values are FSR's of: +/- 245DPS, 4g, 2Gs; ODRs of 119 Hz for 
00191     *  gyro, 119 Hz for accelerometer.
00192     *  Use the return value of this function to verify communication.
00193     */
00194     uint16_t begin(gyro_scale gScl = G_SCALE_250DPS, 
00195                 accel_scale aScl = A_SCALE_2G, gyro_odr gODR = G_ODR_104, 
00196                 accel_odr aODR = A_ODR_104);
00197     // modif GR1 readAllraw
00198     void readAllraw();
00199     
00200     void readAll();
00201     
00202     /**  readGyro() -- Read the gyroscope output registers.
00203     *  This function will read all six gyroscope output registers.
00204     *  The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read
00205     *  those _after_ calling readGyro().
00206     */
00207     void readGyro();
00208     
00209     /**  readAccel() -- Read the accelerometer output registers.
00210     *  This function will read all six accelerometer output registers.
00211     *  The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read
00212     *  those _after_ calling readAccel().
00213     */
00214     void readAccel();
00215     
00216     /**  readTemp() -- Read the temperature output register.
00217     *  This function will read two temperature output registers.
00218     *  The combined readings are stored in the class' temperature variables. Read
00219     *  those _after_ calling readTemp().
00220     */
00221     void readTemp();
00222     
00223     /** Read Interrupt **/
00224     void readIntr();
00225     
00226     /**  setGyroScale() -- Set the full-scale range of the gyroscope.
00227     *  This function can be called to set the scale of the gyroscope to 
00228     *  245, 500, or 2000 degrees per second.
00229     *  Input:
00230     *   - gScl = The desired gyroscope scale. Must be one of three possible
00231     *       values from the gyro_scale enum.
00232     */
00233     void setGyroScale(gyro_scale gScl);
00234     
00235     /**  setAccelScale() -- Set the full-scale range of the accelerometer.
00236     *  This function can be called to set the scale of the accelerometer to
00237     *  2, 4, 8, or 16 g's.
00238     *  Input:
00239     *   - aScl = The desired accelerometer scale. Must be one of five possible
00240     *       values from the accel_scale enum.
00241     */
00242     void setAccelScale(accel_scale aScl);
00243     
00244     /**  setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
00245     *  Input:
00246     *   - gRate = The desired output rate and cutoff frequency of the gyro.
00247     *       Must be a value from the gyro_odr enum (check above).
00248     */
00249     void setGyroODR(gyro_odr gRate);
00250     
00251     /**  setAccelODR() -- Set the output data rate of the accelerometer
00252     *  Input:
00253     *   - aRate = The desired output rate of the accel.
00254     *       Must be a value from the accel_odr enum (check above).
00255     */
00256     void setAccelODR(accel_odr aRate);
00257     /**  calibration() -- Calculate the offset of the accel and the gyro
00258     */
00259     void calibration( int16_t iter);
00260 
00261 private:    
00262     /**  xgAddress store the I2C address
00263     *  for each sensor.
00264     */
00265     uint8_t xgAddress;
00266     
00267     // I2C bus
00268     I2C i2c;
00269 
00270     /**  gScale, and aScale store the current scale range for each 
00271     *  sensor. Should be updated whenever that value changes.
00272     */
00273     gyro_scale gScale;
00274     accel_scale aScale;
00275     
00276     /**  gRes, and aRes store the current resolution for each sensor. 
00277     *  Units of these values would be DPS (or g's or Gs's) per ADC tick.
00278     *  This value is calculated as (sensor scale) / (2^15).
00279     */
00280     float gRes, aRes;
00281     
00282     /**  initGyro() -- Sets up the gyroscope to begin reading.
00283     *  This function steps through all three gyroscope control registers.
00284     */
00285     void initGyro();
00286     
00287     /**  initAccel() -- Sets up the accelerometer to begin reading.
00288     *  This function steps through all accelerometer related control registers.
00289     */
00290     void initAccel();
00291     
00292     /** Setup Interrupt **/
00293     void initIntr();
00294     
00295     /**  calcgRes() -- Calculate the resolution of the gyroscope.
00296     *  This function will set the value of the gRes variable. gScale must
00297     *  be set prior to calling this function.
00298     */
00299     void calcgRes();
00300     
00301     /**  calcaRes() -- Calculate the resolution of the accelerometer.
00302     *  This function will set the value of the aRes variable. aScale must
00303     *  be set prior to calling this function.
00304     */
00305     void calcaRes();
00306     
00307 };
00308 
00309 #endif // _LSM6DS33_H //