Library containing functions to talk with both the MPU6050 IMUs on the Limbic Controller.

Fork of MPU6050 by Mark Vandermeulen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPU6050.h Source File

MPU6050.h

00001 /*Use #define MPU6050_ES before you include this file if you have an engineering sample (older EVBs will have them), to find out if you have one, check your accelerometer output. 
00002 If it is half of what you expected, and you still are on the correct planet, you got an engineering sample
00003 */
00004 
00005 
00006 #ifndef MPU6050_H
00007 #define MPU6050_H
00008 
00009 /**
00010  * Includes
00011  */
00012 #include "mbed.h"
00013 
00014 
00015 /**
00016  * Defines
00017  */
00018 #ifndef MPU6050_ADDRESS
00019     #define MPU6050_ADDRESS             0x68 // address pin low (GND), default for InvenSense evaluation board
00020 #endif
00021 
00022 #ifdef MPU6050_ES
00023         #define DOUBLE_ACCELERO
00024 #endif  
00025 
00026 /**
00027  * Registers
00028  */
00029  
00030 #define MPU6050_XGOFFS_TC        0x00 // Bit 7 PWR_MODE, bits 6:1 XG_OFFS_TC, bit 0 OTP_BNK_VLD                 
00031 #define MPU6050_YGOFFS_TC        0x01                                                                          
00032 #define MPU6050_ZGOFFS_TC        0x02
00033 #define MPU6050_X_FINE_GAIN      0x03 // [7:0] fine gain
00034 #define MPU6050_Y_FINE_GAIN      0x04
00035 #define MPU6050_Z_FINE_GAIN      0x05
00036 #define MPU6050_XA_OFFSET_H      0x06 // User-defined trim values for accelerometer
00037 #define MPU6050_XA_OFFSET_L_TC   0x07
00038 #define MPU6050_YA_OFFSET_H      0x08
00039 #define MPU6050_YA_OFFSET_L_TC   0x09
00040 #define MPU6050_ZA_OFFSET_H      0x0A
00041 #define MPU6050_ZA_OFFSET_L_TC   0x0B
00042 #define MPU6050_SELF_TEST_X      0x0D
00043 #define MPU6050_SELF_TEST_Y      0x0E    
00044 #define MPU6050_SELF_TEST_Z      0x0F
00045 #define MPU6050_SELF_TEST_A      0x10
00046 #define MPU6050_XG_OFFS_USRH     0x13  // User-defined trim values for gyroscope; supported in MPU-6050?
00047 #define MPU6050_XG_OFFS_USRL     0x14
00048 #define MPU6050_YG_OFFS_USRH     0x15
00049 #define MPU6050_YG_OFFS_USRL     0x16
00050 #define MPU6050_ZG_OFFS_USRH     0x17
00051 #define MPU6050_ZG_OFFS_USRL     0x18
00052 #define MPU6050_SMPLRT_DIV       0x19
00053 #define MPU6050_CONFIG           0x1A
00054 
00055 // Filter bandwidth of accelerometer setting
00056 #define MPU6050_EXT_FSYNC_TEMP   0x08
00057 #define MPU6050_DLPF_CFG_260     0x00
00058 #define MPU6050_DLPF_CFG_184     0x01
00059 #define MPU6050_DLPF_CFG_94      0x02
00060 #define MPU6050_DLPF_CFG_44      0x03
00061 #define MPU6050_DLPF_CFG_21      0x04
00062 #define MPU6050_DLPF_CFG_10      0x05
00063 #define MPU6050_DLPF_CFG_5       0x06
00064 
00065 #define MPU6050_GYRO_CONFIG      0x1B
00066 #define MPU6050_ACCEL_CONFIG     0x1C
00067 #define MPU6050_FF_THR           0x1D  // Free-fall
00068 #define MPU6050_FF_DUR           0x1E  // Free-fall
00069 #define MPU6050_MOT_THR          0x1F  // Motion detection threshold bits [7:0]
00070 #define MPU6050_MOT_DUR          0x20  // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms
00071 #define MPU6050_ZMOT_THR         0x21  // Zero-motion detection threshold bits [7:0]
00072 #define MPU6050_ZRMOT_DUR        0x22  // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms
00073 #define MPU6050_FIFO_EN          0x23
00074 #define MPU6050_I2C_MST_CTRL     0x24   
00075 #define MPU6050_I2C_SLV0_ADDR    0x25
00076 #define MPU6050_I2C_SLV0_REG     0x26
00077 #define MPU6050_I2C_SLV0_CTRL    0x27
00078 #define MPU6050_I2C_SLV1_ADDR    0x28
00079 #define MPU6050_I2C_SLV1_REG     0x29
00080 #define MPU6050_I2C_SLV1_CTRL    0x2A
00081 #define MPU6050_I2C_SLV2_ADDR    0x2B
00082 #define MPU6050_I2C_SLV2_REG     0x2C
00083 #define MPU6050_I2C_SLV2_CTRL    0x2D
00084 #define MPU6050_I2C_SLV3_ADDR    0x2E
00085 #define MPU6050_I2C_SLV3_REG     0x2F
00086 #define MPU6050_I2C_SLV3_CTRL    0x30
00087 #define MPU6050_I2C_SLV4_ADDR    0x31
00088 #define MPU6050_I2C_SLV4_REG     0x32
00089 #define MPU6050_I2C_SLV4_DO      0x33
00090 #define MPU6050_I2C_SLV4_CTRL    0x34
00091 #define MPU6050_I2C_SLV4_DI      0x35
00092 #define MPU6050_I2C_MST_STATUS   0x36
00093 #define MPU6050_INT_PIN_CFG      0x37
00094 #define MPU6050_INT_ENABLE       0x38
00095 #define MPU6050_DMP_INT_STATUS   0x39  // Check DMP interrupt
00096 #define MPU6050_INT_STATUS       0x3A
00097 #define MPU6050_ACCEL_XOUT_H     0x3B
00098 #define MPU6050_ACCEL_XOUT_L     0x3C
00099 #define MPU6050_ACCEL_YOUT_H     0x3D
00100 #define MPU6050_ACCEL_YOUT_L     0x3E
00101 #define MPU6050_ACCEL_ZOUT_H     0x3F
00102 #define MPU6050_ACCEL_ZOUT_L     0x40
00103 #define MPU6050_TEMP_OUT_H       0x41
00104 #define MPU6050_TEMP_OUT_L       0x42
00105 #define MPU6050_GYRO_XOUT_H      0x43
00106 #define MPU6050_GYRO_XOUT_L      0x44
00107 #define MPU6050_GYRO_YOUT_H      0x45
00108 #define MPU6050_GYRO_YOUT_L      0x46
00109 #define MPU6050_GYRO_ZOUT_H      0x47
00110 #define MPU6050_GYRO_ZOUT_L      0x48
00111 #define MPU6050_EXT_SENS_DATA_00 0x49
00112 #define MPU6050_EXT_SENS_DATA_01 0x4A
00113 #define MPU6050_EXT_SENS_DATA_02 0x4B
00114 #define MPU6050_EXT_SENS_DATA_03 0x4C
00115 #define MPU6050_EXT_SENS_DATA_04 0x4D
00116 #define MPU6050_EXT_SENS_DATA_05 0x4E
00117 #define MPU6050_EXT_SENS_DATA_06 0x4F
00118 #define MPU6050_EXT_SENS_DATA_07 0x50
00119 #define MPU6050_EXT_SENS_DATA_08 0x51
00120 #define MPU6050_EXT_SENS_DATA_09 0x52
00121 #define MPU6050_EXT_SENS_DATA_10 0x53
00122 #define MPU6050_EXT_SENS_DATA_11 0x54
00123 #define MPU6050_EXT_SENS_DATA_12 0x55
00124 #define MPU6050_EXT_SENS_DATA_13 0x56
00125 #define MPU6050_EXT_SENS_DATA_14 0x57
00126 #define MPU6050_EXT_SENS_DATA_15 0x58
00127 #define MPU6050_EXT_SENS_DATA_16 0x59
00128 #define MPU6050_EXT_SENS_DATA_17 0x5A
00129 #define MPU6050_EXT_SENS_DATA_18 0x5B
00130 #define MPU6050_EXT_SENS_DATA_19 0x5C
00131 #define MPU6050_EXT_SENS_DATA_20 0x5D
00132 #define MPU6050_EXT_SENS_DATA_21 0x5E
00133 #define MPU6050_EXT_SENS_DATA_22 0x5F
00134 #define MPU6050_EXT_SENS_DATA_23 0x60
00135 #define MPU6050_MOT_DETECT_STATUS 0x61
00136 #define MPU6050_I2C_SLV0_DO      0x63
00137 #define MPU6050_I2C_SLV1_DO      0x64
00138 #define MPU6050_I2C_SLV2_DO      0x65
00139 #define MPU6050_I2C_SLV3_DO      0x66
00140 #define MPU6050_I2C_MST_DELAY_CTRL 0x67
00141 #define MPU6050_SIGNAL_PATH_RESET  0x68
00142 #define MPU6050_MOT_DETECT_CTRL   0x69
00143 #define MPU6050_USER_CTRL        0x6A  // Bit 7 enable DMP, bit 3 reset DMP
00144 #define MPU6050_PWR_MGMT_1       0x6B // Device defaults to the SLEEP mode
00145 #define MPU6050_PWR_MGMT_2       0x6C
00146 #define MPU6050_DMP_BANK         0x6D  // Activates a specific bank in the DMP
00147 #define MPU6050_DMP_RW_PNT       0x6E  // Set read/write pointer to a specific start address in specified DMP bank
00148 #define MPU6050_DMP_REG          0x6F  // Register in DMP from which to read or to which to write
00149 #define MPU6050_DMP_REG_1        0x70
00150 #define MPU6050_DMP_REG_2        0x71
00151 #define MPU6050_FIFO_COUNTH      0x72
00152 #define MPU6050_FIFO_COUNTL      0x73
00153 #define MPU6050_FIFO_R_W         0x74
00154 #define MPU6050_WHO_AM_I 0x75 // Should return 0x68                 
00155  
00156  /**
00157   * Definitions
00158   */
00159 #define MPU6050_SLP_BIT             6
00160 #define MPU6050_BYPASS_BIT         1
00161 
00162 #define MPU6050_BW_256              0
00163 #define MPU6050_BW_188              1
00164 #define MPU6050_BW_98               2
00165 #define MPU6050_BW_42               3
00166 #define MPU6050_BW_20               4
00167 #define MPU6050_BW_10               5
00168 #define MPU6050_BW_5                6
00169 
00170 #define MPU6050_ACCELERO_RANGE_2G   0
00171 #define MPU6050_ACCELERO_RANGE_4G   1
00172 #define MPU6050_ACCELERO_RANGE_8G   2
00173 #define MPU6050_ACCELERO_RANGE_16G  3
00174 
00175 #define MPU6050_GYRO_RANGE_250      0
00176 #define MPU6050_GYRO_RANGE_500      1
00177 #define MPU6050_GYRO_RANGE_1000     2
00178 #define MPU6050_GYRO_RANGE_2000     3
00179 
00180 
00181 /** MPU6050 IMU library.
00182   *
00183   * Example:
00184   * @code
00185   * Later, maybe
00186   * @endcode
00187   */
00188 class MPU6050 {
00189     public:
00190      /**
00191      * Constructor.
00192      *
00193      * Sleep mode of MPU6050 is immediatly disabled
00194      *
00195      * @param sda - mbed pin to use for the SDA I2C line.
00196      * @param scl - mbed pin to use for the SCL I2C line.
00197      */
00198      MPU6050(PinName sda, PinName scl);
00199      
00200 
00201      /**
00202      * Tests the I2C connection by reading the WHO_AM_I register. 
00203      *
00204      * @return True for a working connection, false for an error
00205      */     
00206      char testConnection( void );
00207      
00208      /**
00209      * Sets the bandwidth of the digital low-pass filter 
00210      *
00211      * Macros: MPU6050_BW_256 - MPU6050_BW_188 - MPU6050_BW_98 - MPU6050_BW_42 - MPU6050_BW_20 - MPU6050_BW_10 - MPU6050_BW_5
00212      * Last number is the gyro's BW in Hz (accelero BW is virtually identical)
00213      *
00214      * @param BW - The three bits that set the bandwidth (use the predefined macros)
00215      */     
00216      void setBW( char BW );
00217      
00218      /**
00219      * Sets the auxiliary I2C bus in bypass mode to read the sensors behind the MPU6050 (useful for eval board, otherwise just connect them to primary I2C bus) 
00220      *
00221      * @param state - Enables/disables the I2C bypass mode
00222      */     
00223      void setI2CBypass ( bool state );
00224      
00225      /**
00226      * Sets the Accelero full-scale range
00227      *
00228      * Macros: MPU6050_ACCELERO_RANGE_2G - MPU6050_ACCELERO_RANGE_4G - MPU6050_ACCELERO_RANGE_8G - MPU6050_ACCELERO_RANGE_16G
00229      *
00230      * @param range - The two bits that set the full-scale range (use the predefined macros)
00231      */
00232      void setAcceleroRange(char range);
00233      
00234      
00235      void setAcceleroFilter(char bandwidth);
00236      
00237      
00238      /**
00239      * Reads the accelero x-axis.
00240      *
00241      * @return 16-bit signed integer x-axis accelero data
00242      */   
00243      int getAcceleroRawX( void );
00244      
00245      /**
00246      * Reads the accelero y-axis.
00247      *
00248      * @return 16-bit signed integer y-axis accelero data
00249      */   
00250      int getAcceleroRawY( void );
00251      
00252      /**
00253      * Reads the accelero z-axis.
00254      *
00255      * @return 16-bit signed integer z-axis accelero data
00256      */   
00257      int getAcceleroRawZ( void );
00258      
00259      /**
00260      * Reads all accelero data.
00261      *
00262      * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z
00263      */   
00264      void getAcceleroRaw( int *data );
00265      
00266      /**
00267      * Reads all accelero data, gives the acceleration in m/s2
00268      *
00269      * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work.
00270      *
00271      * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z
00272      */   
00273      void getAccelero( float *data );
00274      
00275      /**
00276      * Sets the Gyro full-scale range
00277      *
00278      * Macros: MPU6050_GYRO_RANGE_250 - MPU6050_GYRO_RANGE_500 - MPU6050_GYRO_RANGE_1000 - MPU6050_GYRO_RANGE_2000
00279      *
00280      * @param range - The two bits that set the full-scale range (use the predefined macros)
00281      */
00282      void setGyroRange(char range);
00283 
00284      /**
00285      * Reads the gyro x-axis.
00286      *
00287      * @return 16-bit signed integer x-axis gyro data
00288      */   
00289      int getGyroRawX( void );
00290      
00291      /**
00292      * Reads the gyro y-axis.
00293      *
00294      * @return 16-bit signed integer y-axis gyro data
00295      */   
00296      int getGyroRawY( void );
00297      
00298      /**
00299      * Reads the gyro z-axis.
00300      *
00301      * @return 16-bit signed integer z-axis gyro data
00302      */   
00303      int getGyroRawZ( void );
00304      
00305      /**
00306      * Reads all gyro data.
00307      *
00308      * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z
00309      */   
00310      void getGyroRaw( int *data );  
00311      
00312      /**
00313      * Reads all gyro data, gives the gyro in rad/s
00314      *
00315      * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work.
00316      *
00317      * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z
00318      */   
00319      void getGyro( float *data);     
00320      
00321      /**
00322      * Reads temperature data.
00323      *
00324      * @return 16 bit signed integer with the raw temperature register value
00325      */  
00326      int getTempRaw( void );
00327      
00328      /**
00329      * Returns current temperature
00330      *
00331      * @returns float with the current temperature
00332      */  
00333      float getTemp( void );
00334 
00335      /**
00336      * Sets the sleep mode of the MPU6050 
00337      *
00338      * @param state - true for sleeping, false for wake up
00339      */     
00340      void setSleepMode( bool state );
00341      
00342      
00343      /**
00344      * Writes data to the device, could be private, but public is handy so you can transmit directly to the MPU. 
00345      *
00346      * @param adress - register address to write to
00347      * @param data - data to write
00348      */
00349      void write( char address, char data);
00350      
00351      /**
00352      * Read data from the device, could be private, but public is handy so you can transmit directly to the MPU. 
00353      *
00354      * @param adress - register address to write to
00355      * @return - data from the register specified by RA
00356      */
00357      char read( char adress);
00358      
00359      /**
00360      * Read multtiple regigsters from the device, more efficient than using multiple normal reads. 
00361      *
00362      * @param adress - register address to write to
00363      * @param length - number of bytes to read
00364      * @param data - pointer where the data needs to be written to 
00365      */
00366      void read( char adress, char *data, int length);
00367      
00368 
00369      
00370         
00371      private:
00372 
00373      I2C connection;
00374      char currentAcceleroRange;
00375      char currentGyroRange;
00376      
00377 
00378 };
00379 
00380 
00381 
00382 #endif