MPU6050 library that is kinda beta and will probably never leave beta but it might help some people.

Dependents:   upverter_fitbit_clone sensor accelero_giro mbed_sence_test ... more

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             0x69 // 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  #define MPU6050_CONFIG_REG         0x1A
00030  #define MPU6050_GYRO_CONFIG_REG    0x1B
00031  #define MPU6050_ACCELERO_CONFIG_REG    0x1C
00032   
00033  #define MPU6050_INT_PIN_CFG        0x37
00034  
00035  #define MPU6050_ACCEL_XOUT_H_REG   0x3B
00036  #define MPU6050_ACCEL_YOUT_H_REG   0x3D
00037  #define MPU6050_ACCEL_ZOUT_H_REG   0x3F
00038  
00039  #define MPU6050_TEMP_H_REG         0x41
00040  
00041  #define MPU6050_GYRO_XOUT_H_REG    0x43
00042  #define MPU6050_GYRO_YOUT_H_REG    0x45
00043  #define MPU6050_GYRO_ZOUT_H_REG    0x47
00044  
00045  
00046  
00047  #define MPU6050_PWR_MGMT_1_REG     0x6B
00048  #define MPU6050_WHO_AM_I_REG       0x75
00049  
00050                  
00051  
00052  /**
00053   * Definitions
00054   */
00055 #define MPU6050_SLP_BIT             6
00056 #define MPU6050_BYPASS_BIT         1
00057 
00058 #define MPU6050_BW_256              0
00059 #define MPU6050_BW_188              1
00060 #define MPU6050_BW_98               2
00061 #define MPU6050_BW_42               3
00062 #define MPU6050_BW_20               4
00063 #define MPU6050_BW_10               5
00064 #define MPU6050_BW_5                6
00065 
00066 #define MPU6050_ACCELERO_RANGE_2G   0
00067 #define MPU6050_ACCELERO_RANGE_4G   1
00068 #define MPU6050_ACCELERO_RANGE_8G   2
00069 #define MPU6050_ACCELERO_RANGE_16G  3
00070 
00071 #define MPU6050_GYRO_RANGE_250      0
00072 #define MPU6050_GYRO_RANGE_500      1
00073 #define MPU6050_GYRO_RANGE_1000     2
00074 #define MPU6050_GYRO_RANGE_2000     3
00075 
00076 
00077 /** MPU6050 IMU library.
00078   *
00079   * Example:
00080   * @code
00081   * Later, maybe
00082   * @endcode
00083   */
00084 class MPU6050 {
00085     public:
00086      /**
00087      * Constructor.
00088      *
00089      * Sleep mode of MPU6050 is immediatly disabled
00090      *
00091      * @param sda - mbed pin to use for the SDA I2C line.
00092      * @param scl - mbed pin to use for the SCL I2C line.
00093      */
00094      MPU6050(PinName sda, PinName scl);
00095      
00096 
00097      /**
00098      * Tests the I2C connection by reading the WHO_AM_I register. 
00099      *
00100      * @return True for a working connection, false for an error
00101      */     
00102      bool testConnection( void );
00103      
00104      /**
00105      * Sets the bandwidth of the digital low-pass filter 
00106      *
00107      * Macros: MPU6050_BW_256 - MPU6050_BW_188 - MPU6050_BW_98 - MPU6050_BW_42 - MPU6050_BW_20 - MPU6050_BW_10 - MPU6050_BW_5
00108      * Last number is the gyro's BW in Hz (accelero BW is virtually identical)
00109      *
00110      * @param BW - The three bits that set the bandwidth (use the predefined macros)
00111      */     
00112      void setBW( char BW );
00113      
00114      /**
00115      * 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) 
00116      *
00117      * @param state - Enables/disables the I2C bypass mode
00118      */     
00119      void setI2CBypass ( bool state );
00120      
00121      /**
00122      * Sets the Accelero full-scale range
00123      *
00124      * Macros: MPU6050_ACCELERO_RANGE_2G - MPU6050_ACCELERO_RANGE_4G - MPU6050_ACCELERO_RANGE_8G - MPU6050_ACCELERO_RANGE_16G
00125      *
00126      * @param range - The two bits that set the full-scale range (use the predefined macros)
00127      */
00128      void setAcceleroRange(char range);
00129      
00130      /**
00131      * Reads the accelero x-axis.
00132      *
00133      * @return 16-bit signed integer x-axis accelero data
00134      */   
00135      int getAcceleroRawX( void );
00136      
00137      /**
00138      * Reads the accelero y-axis.
00139      *
00140      * @return 16-bit signed integer y-axis accelero data
00141      */   
00142      int getAcceleroRawY( void );
00143      
00144      /**
00145      * Reads the accelero z-axis.
00146      *
00147      * @return 16-bit signed integer z-axis accelero data
00148      */   
00149      int getAcceleroRawZ( void );
00150      
00151      /**
00152      * Reads all accelero data.
00153      *
00154      * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z
00155      */   
00156      void getAcceleroRaw( int *data );
00157      
00158      /**
00159      * Reads all accelero data, gives the acceleration in m/s2
00160      *
00161      * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work.
00162      *
00163      * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z
00164      */   
00165      void getAccelero( float *data );
00166      
00167      /**
00168      * Sets the Gyro full-scale range
00169      *
00170      * Macros: MPU6050_GYRO_RANGE_250 - MPU6050_GYRO_RANGE_500 - MPU6050_GYRO_RANGE_1000 - MPU6050_GYRO_RANGE_2000
00171      *
00172      * @param range - The two bits that set the full-scale range (use the predefined macros)
00173      */
00174      void setGyroRange(char range);
00175 
00176      /**
00177      * Reads the gyro x-axis.
00178      *
00179      * @return 16-bit signed integer x-axis gyro data
00180      */   
00181      int getGyroRawX( void );
00182      
00183      /**
00184      * Reads the gyro y-axis.
00185      *
00186      * @return 16-bit signed integer y-axis gyro data
00187      */   
00188      int getGyroRawY( void );
00189      
00190      /**
00191      * Reads the gyro z-axis.
00192      *
00193      * @return 16-bit signed integer z-axis gyro data
00194      */   
00195      int getGyroRawZ( void );
00196      
00197      /**
00198      * Reads all gyro data.
00199      *
00200      * @param data - pointer to signed integer array with length three: data[0] = X, data[1] = Y, data[2] = Z
00201      */   
00202      void getGyroRaw( int *data );  
00203      
00204      /**
00205      * Reads all gyro data, gives the gyro in rad/s
00206      *
00207      * Function uses the last setup value of the full scale range, if you manually set in another range, this won't work.
00208      *
00209      * @param data - pointer to float array with length three: data[0] = X, data[1] = Y, data[2] = Z
00210      */   
00211      void getGyro( float *data);     
00212      
00213      /**
00214      * Reads temperature data.
00215      *
00216      * @return 16 bit signed integer with the raw temperature register value
00217      */  
00218      int getTempRaw( void );
00219      
00220      /**
00221      * Returns current temperature
00222      *
00223      * @returns float with the current temperature
00224      */  
00225      float getTemp( void );
00226 
00227      /**
00228      * Sets the sleep mode of the MPU6050 
00229      *
00230      * @param state - true for sleeping, false for wake up
00231      */     
00232      void setSleepMode( bool state );
00233      
00234      
00235      /**
00236      * Writes data to the device, could be private, but public is handy so you can transmit directly to the MPU. 
00237      *
00238      * @param adress - register address to write to
00239      * @param data - data to write
00240      */
00241      void write( char address, char data);
00242      
00243      /**
00244      * Read data from the device, could be private, but public is handy so you can transmit directly to the MPU. 
00245      *
00246      * @param adress - register address to write to
00247      * @return - data from the register specified by RA
00248      */
00249      char read( char adress);
00250      
00251      /**
00252      * Read multtiple regigsters from the device, more efficient than using multiple normal reads. 
00253      *
00254      * @param adress - register address to write to
00255      * @param length - number of bytes to read
00256      * @param data - pointer where the data needs to be written to 
00257      */
00258      void read( char adress, char *data, int length);
00259      
00260 
00261      
00262         
00263      private:
00264 
00265      I2C connection;
00266      char currentAcceleroRange;
00267      char currentGyroRange;
00268      
00269 
00270 };
00271 
00272 
00273 
00274 #endif