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