sa
Embed:
(wiki syntax)
Show/hide line numbers
MPU6050.cpp
00001 //ported from arduino library: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050 00002 //written by szymon gaertig (email: szymon@gaertig.com.pl) 00003 // 00004 //Changelog: 00005 //2013-01-08 - first beta release 00006 00007 // I2Cdev library collection - MPU6050 I2C device class 00008 // Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00) 00009 // 8/24/2011 by Jeff Rowberg <jeff@rowberg.net> 00010 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib 00011 // 00012 // Changelog: 00013 // ... - ongoing debug release 00014 00015 // NOTE: THIS IS ONLY A PARIAL RELEASE. THIS DEVICE CLASS IS CURRENTLY UNDERGOING ACTIVE 00016 // DEVELOPMENT AND IS STILL MISSING SOME IMPORTANT FEATURES. PLEASE KEEP THIS IN MIND IF 00017 // YOU DECIDE TO USE THIS PARTICULAR CODE FOR ANYTHING. 00018 00019 /* ============================================ 00020 I2Cdev device library code is placed under the MIT license 00021 Copyright (c) 2012 Jeff Rowberg 00022 00023 Permission is hereby granted, free of charge, to any person obtaining a copy 00024 of this software and associated documentation files (the "Software"), to deal 00025 in the Software without restriction, including without limitation the rights 00026 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00027 copies of the Software, and to permit persons to whom the Software is 00028 furnished to do so, subject to the following conditions: 00029 00030 The above copyright notice and this permission notice shall be included in 00031 all copies or substantial portions of the Software. 00032 00033 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00034 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00035 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00036 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00037 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00038 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00039 THE SOFTWARE. 00040 =============================================== 00041 */ 00042 00043 #include "MPU6050.h" 00044 int16_t accelData[3],gyroData[3],tempData; 00045 float accelBias[3] = {0, 0, 0}; // Bias corrections for acc 00046 float gyroBias[3] = {0, 0, 0}; // Bias corrections for gyro 00047 enum Ascale 00048 { 00049 AFS_2G=0, 00050 AFS_4G, 00051 AFS_8G, 00052 AFS_16G 00053 }; 00054 enum Gscale 00055 { 00056 GFS_250DPS=0, 00057 GFS_500DPS, 00058 GFS_1000DPS, 00059 GFS_2000DPS 00060 }; 00061 int Gscale = GFS_250DPS; 00062 float aRes; 00063 float gRes; 00064 int Ascale = AFS_2G; 00065 00066 //int16_t ax,ay,az; 00067 //int16_t gx,gy,gz; 00068 00069 float ax,ay,az; 00070 float gx,gy,gz; 00071 #define useDebugSerial 0 00072 00073 //instead of using pgmspace.h 00074 typedef const unsigned char prog_uchar; 00075 #define pgm_read_byte_near(x) (*(prog_uchar*)x) 00076 #define pgm_read_byte(x) (*(prog_uchar*)x) 00077 00078 /** Default constructor, uses default I2C address. 00079 * @see MPU6050_DEFAULT_ADDRESS 00080 */ 00081 MPU6050::MPU6050() 00082 { 00083 devAddr = MPU6050_DEFAULT_ADDRESS; 00084 } 00085 00086 /** Specific address constructor. 00087 * @param address I2C address 00088 * @see MPU6050_DEFAULT_ADDRESS 00089 * @see MPU6050_ADDRESS_AD0_LOW 00090 * @see MPU6050_ADDRESS_AD0_HIGH 00091 */ 00092 MPU6050::MPU6050(uint8_t address) 00093 { 00094 devAddr = address; 00095 } 00096 00097 /** Power on and prepare for general usage. 00098 * This will activate the device and take it out of sleep mode (which must be done 00099 * after start-up). This function also sets both the accelerometer and the gyroscope 00100 * to their most sensitive settings, namely +/- 2g and +/- 250 degrees/sec, and sets 00101 * the clock source to use the X Gyro for reference, which is slightly better than 00102 * the default internal clock source. 00103 */ 00104 void MPU6050::initialize() 00105 { 00106 00107 #ifdef useDebugSerial 00108 // debugSerial.printf("MPU6050::initialize start\n"); 00109 #endif 00110 //i2Cdev.frequency(400000); // fast i2c: 400 kHz 00111 00112 /* Wake up the device */ 00113 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, 0x00); // wake up the device by clearing the sleep bit (bit6) 00114 //wait_ms(100); // wait 100 ms to stabilize 00115 00116 /* Get stable time source */ 00117 // PLL with X axis gyroscope reference is used to improve stability 00118 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, 0x01); 00119 00120 /* Configure Gyroscope and Accelerometer */ 00121 // Disable FSYNC, acc bandwidth: 44 Hz, gyro bandwidth: 42 Hz 00122 // Sample rates: 1kHz, maximum delay: 4.9ms (which is pretty good for a 200 Hz maximum rate) 00123 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_CONFIG, 0x03); 00124 00125 /* Set sample rate = gyroscope output rate/(1+SMPLRT_DIV) */ 00126 // SMPLRT_DIV=4 and sample rate=200 Hz (compatible with config above) 00127 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_SMPLRT_DIV, 0x04); 00128 00129 /* Accelerometer configuration */ 00130 //uint8_t temp; 00131 //i2Cdev.readByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_CONFIG,&temp); 00132 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_CONFIG, temp & ~0xE0); // Clear self-test bits [7:5] 00133 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_CONFIG, temp & ~0x18); // Clear AFS bits [4:3] 00134 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_CONFIG, temp | Ascale<<3); // Set full scale range 00135 00136 /* Gyroscope configuration */ 00137 //i2Cdev.readByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_CONFIG, &temp); 00138 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_CONFIG, temp & ~0xE0); // Clear self-test bits [7:5] 00139 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_CONFIG, temp & ~0x18); // Clear FS bits [4:3] 00140 //i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_CONFIG, temp | Gscale<<3); // Set full scale range 00141 setClockSource(MPU6050_CLOCK_PLL_XGYRO); 00142 setFullScaleGyroRange(MPU6050_GYRO_FS_250); 00143 setFullScaleAccelRange(MPU6050_ACCEL_FS_2); 00144 setSleepEnabled(false); // thanks to Jack Elston for pointing this one out! 00145 00146 00147 #ifdef useDebugSerial 00148 //debugSerial.printf("MPU6050::initialize end\n"); 00149 #endif 00150 } 00151 00152 /** Verify the I2C connection. 00153 * Make sure the device is connected and responds as expected. 00154 * @return True if connection is valid, false otherwise 00155 */ 00156 bool MPU6050::testConnection() 00157 { 00158 #ifdef useDebugSerial 00159 //debugSerial.printf("MPU6050::testConnection start\n"); 00160 #endif 00161 uint8_t deviceId = getDeviceID(); 00162 //debugSerial.printf("\n\n____%d___\n",deviceId); 00163 #ifdef useDebugSerial 00164 //debugSerial.printf("DeviceId = %d\n",deviceId); 00165 #endif 00166 return deviceId == 0x34; 00167 } 00168 00169 void MPU6050::calibrate(float* dest1, float* dest2){ 00170 uint8_t data[12]; // data array to hold acc and gyro x,y,z data 00171 uint16_t fifo_count, packet_count, count; 00172 int32_t accel_bias[3] = {0,0,0}; 00173 int32_t gyro_bias[3] = {0,0,0}; 00174 float aRes = 2.0/32768.0; 00175 float gRes = 250.0/32768.0; 00176 uint16_t accelsensitivity = 16384; // = 1/aRes = 16384 LSB/g 00177 //uint16_t gyrosensitivity = 131; // = 1/gRes = 131 LSB/dps 00178 00179 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, 0x80); // set bit7 to reset the device 00180 wait_ms(100); 00181 00182 /*Get stable time source */ 00183 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, 0x01); // PLL with X axis gyroscope reference is used to improve stability 00184 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_2, 0x00); // Disable accel only low power mode 00185 wait(0.2); 00186 00187 /* Configure device for bias calculation */ 00188 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_INT_ENABLE, 0x00); // Disable all interrupts 00189 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_FIFO_EN, 0x00); // Disable FIFO 00190 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, 0x00); // Turn on internal clock source 00191 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_I2C_MST_CTRL, 0x00); // Disable I2C master 00192 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_USER_CTRL, 0x00); // Disable FIFO and I2C master modes 00193 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_USER_CTRL, 0x04); // Reset FIFO 00194 wait(0.015); 00195 00196 00197 /* Configure accel and gyro for bias calculation */ 00198 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_CONFIG, 0x01); // Set low-pass filter to 188 Hz 00199 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_SMPLRT_DIV, 0x00); // Set sample rate to 1 kHz 00200 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_CONFIG, 0x00); // Set accelerometer full-scale to 2 g, maximum sensitivity 00201 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_CONFIG, 0x00); // Set gyro full-scale to 250 degrees per second, maximum sensitivity 00202 00203 /* Configure FIFO to capture accelerometer and gyro data for bias calculation */ 00204 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_USER_CTRL, 0x40); // Enable FIFO 00205 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_FIFO_EN, 0x78); // Enable accelerometer and gyro for FIFO (max size 1024 bytes in MPU-6050) 00206 wait(0.08); // Sample rate is 1 kHz, accumulates 80 samples in 80 milliseconds. 00207 // accX: 2 byte, accY: 2 byte, accZ: 2 byte. gyroX: 2 byte, gyroY: 2 byte, gyroZ: 2 byte. 12*80=960 byte < 1024 byte 00208 00209 00210 /* At end of sample accumulation, turn off FIFO sensor read */ 00211 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_FIFO_EN, 0x00); // Disable FIFO 00212 i2Cdev.readBytes(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_FIFO_COUNTH, 2, &data[0]); // Read FIFO sample count 00213 fifo_count = ((uint16_t)data[0] << 8) | data[1]; 00214 packet_count = fifo_count/12; // The number of sets of full acc and gyro data for averaging. packet_count = 80 in this case 00215 00216 for(count=0; count<packet_count; count++) 00217 { 00218 int16_t accel_temp[3]={0,0,0}; 00219 int16_t gyro_temp[3]={0,0,0}; 00220 i2Cdev.readBytes(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_FIFO_R_W, 12, &data[0]); // read data for averaging 00221 00222 /* Form signed 16-bit integer for each sample in FIFO */ 00223 accel_temp[0] = (int16_t) (((int16_t)data[0] << 8) | data[1] ) ; 00224 accel_temp[1] = (int16_t) (((int16_t)data[2] << 8) | data[3] ) ; 00225 accel_temp[2] = (int16_t) (((int16_t)data[4] << 8) | data[5] ) ; 00226 gyro_temp[0] = (int16_t) (((int16_t)data[6] << 8) | data[7] ) ; 00227 gyro_temp[1] = (int16_t) (((int16_t)data[8] << 8) | data[9] ) ; 00228 gyro_temp[2] = (int16_t) (((int16_t)data[10] << 8) | data[11]) ; 00229 00230 /* Sum individual signed 16-bit biases to get accumulated signed 32-bit biases */ 00231 accel_bias[0] += (int32_t) accel_temp[0]; 00232 accel_bias[1] += (int32_t) accel_temp[1]; 00233 accel_bias[2] += (int32_t) accel_temp[2]; 00234 gyro_bias[0] += (int32_t) gyro_temp[0]; 00235 gyro_bias[1] += (int32_t) gyro_temp[1]; 00236 gyro_bias[2] += (int32_t) gyro_temp[2]; 00237 } 00238 /* Normalize sums to get average count biases */ 00239 accel_bias[0] /= (int32_t) packet_count; 00240 accel_bias[1] /= (int32_t) packet_count; 00241 accel_bias[2] /= (int32_t) packet_count; 00242 gyro_bias[0] /= (int32_t) packet_count; 00243 gyro_bias[1] /= (int32_t) packet_count; 00244 gyro_bias[2] /= (int32_t) packet_count; 00245 00246 /* Remove gravity from the z-axis accelerometer bias calculation */ 00247 if(accel_bias[2] > 0) {accel_bias[2] -= (int32_t) accelsensitivity;} 00248 else {accel_bias[2] += (int32_t) accelsensitivity;} 00249 00250 /* Output scaled accelerometer biases for manual subtraction in the main program */ 00251 dest1[0] = accel_bias[0]*aRes; 00252 dest1[1] = accel_bias[1]*aRes; 00253 dest1[2] = accel_bias[2]*aRes; 00254 00255 /* Construct the gyro biases for push to the hardware gyro bias registers, which are reset to zero upon device startup */ 00256 data[0] = (-gyro_bias[0]/4 >> 8) & 0xFF; // Divide by 4 to get 32.9 LSB per deg/s to conform to expected bias input format 00257 data[1] = (-gyro_bias[0]/4) & 0xFF; // Biases are additive, so change sign on calculated average gyro biases 00258 data[2] = (-gyro_bias[1]/4 >> 8) & 0xFF; 00259 data[3] = (-gyro_bias[1]/4) & 0xFF; 00260 data[4] = (-gyro_bias[2]/4 >> 8) & 0xFF; 00261 data[5] = (-gyro_bias[2]/4) & 0xFF; 00262 00263 /* Push gyro biases to hardware registers */ 00264 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_XG_OFFS_USRH, data[0]); 00265 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_XG_OFFS_USRL, data[1]); 00266 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_YG_OFFS_USRH, data[2]); 00267 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_YG_OFFS_USRL, data[3]); 00268 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ZG_OFFS_USRH, data[4]); 00269 i2Cdev.writeByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ZG_OFFS_USRL, data[5]); 00270 00271 /* Construct gyro bias in deg/s for later manual subtraction */ 00272 dest2[0] = gyro_bias[0]*gRes; 00273 dest2[1] = gyro_bias[1]*gRes; 00274 dest2[2] = gyro_bias[2]*gRes; 00275 } 00276 void MPU6050::readAccelData(int16_t* dest) 00277 { 00278 uint8_t rawData[6]; // x,y,z acc data 00279 i2Cdev.readBytes(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_XOUT_H, 6, &rawData[0]); // read six raw data registers sequentially and write them into data array 00280 00281 /* Turn the MSB LSB into signed 16-bit value */ 00282 dest[0] = (int16_t)(((int16_t)rawData[0]<<8) | rawData[1]); // ACCEL_XOUT 00283 dest[1] = (int16_t)(((int16_t)rawData[2]<<8) | rawData[3]); // ACCEL_YOUT 00284 dest[2] = (int16_t)(((int16_t)rawData[4]<<8) | rawData[5]); // ACCEL_ZOUT 00285 } 00286 void MPU6050::readGyroData(int16_t* dest) 00287 { 00288 uint8_t rawData[6]; // x,y,z gyro data 00289 i2Cdev.readBytes(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_XOUT_H, 6, &rawData[0]); // read the six raw data registers sequentially and write them into data array 00290 00291 /* Turn the MSB LSB into signed 16-bit value */ 00292 dest[0] = (int16_t)(((int16_t)rawData[0]<<8) | rawData[1]); // GYRO_XOUT 00293 dest[1] = (int16_t)(((int16_t)rawData[2]<<8) | rawData[3]); // GYRO_YOUT 00294 dest[2] = (int16_t)(((int16_t)rawData[4]<<8) | rawData[5]); // GYRO_ZOUT 00295 } 00296 void MPU6050::getGres() 00297 { 00298 switch(Gscale) 00299 { 00300 case GFS_250DPS: 00301 gRes = 250.0/32768.0; 00302 break; 00303 case GFS_500DPS: 00304 gRes = 500.0/32768.0; 00305 break; 00306 case GFS_1000DPS: 00307 gRes = 1000.0/32768.0; 00308 break; 00309 case GFS_2000DPS: 00310 gRes = 2000.0/32768.0; 00311 break; 00312 } 00313 } 00314 void MPU6050::getAres() 00315 { 00316 switch(Ascale) 00317 { 00318 case AFS_2G: 00319 aRes = 2.0/32768.0; 00320 break; 00321 case AFS_4G: 00322 aRes = 4.0/32768.0; 00323 break; 00324 case AFS_8G: 00325 aRes = 8.0/32768.0; 00326 break; 00327 case AFS_16G: 00328 aRes = 16.0/32768.0; 00329 break; 00330 } 00331 } 00332 // AUX_VDDIO register (InvenSense demo code calls this RA_*G_OFFS_TC) 00333 00334 /** Get the auxiliary I2C supply voltage level. 00335 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to 00336 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to 00337 * the MPU-6000, which does not have a VLOGIC pin. 00338 * @return I2C supply voltage level (0=VLOGIC, 1=VDD) 00339 */ 00340 uint8_t MPU6050::getAuxVDDIOLevel() 00341 { 00342 i2Cdev.readBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, buffer); 00343 return buffer[0]; 00344 } 00345 /** Set the auxiliary I2C supply voltage level. 00346 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to 00347 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to 00348 * the MPU-6000, which does not have a VLOGIC pin. 00349 * @param level I2C supply voltage level (0=VLOGIC, 1=VDD) 00350 */ 00351 void MPU6050::setAuxVDDIOLevel(uint8_t level) 00352 { 00353 i2Cdev.writeBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, level); 00354 } 00355 00356 // SMPLRT_DIV register 00357 00358 /** Get gyroscope output rate divider. 00359 * The sensor register output, FIFO output, DMP sampling, Motion detection, Zero 00360 * Motion detection, and Free Fall detection are all based on the Sample Rate. 00361 * The Sample Rate is generated by dividing the gyroscope output rate by 00362 * SMPLRT_DIV: 00363 * 00364 * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) 00365 * 00366 * where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or 00367 * 7), and 1kHz when the DLPF is enabled (see Register 26). 00368 * 00369 * Note: The accelerometer output rate is 1kHz. This means that for a Sample 00370 * Rate greater than 1kHz, the same accelerometer sample may be output to the 00371 * FIFO, DMP, and sensor registers more than once. 00372 * 00373 * For a diagram of the gyroscope and accelerometer signal paths, see Section 8 00374 * of the MPU-6000/MPU-6050 Product Specification document. 00375 * 00376 * @return Current sample rate 00377 * @see MPU6050_RA_SMPLRT_DIV 00378 */ 00379 uint8_t MPU6050::getRate() 00380 { 00381 i2Cdev.readByte(devAddr, MPU6050_RA_SMPLRT_DIV, buffer); 00382 return buffer[0]; 00383 } 00384 /** Set gyroscope sample rate divider. 00385 * @param rate New sample rate divider 00386 * @see getRate() 00387 * @see MPU6050_RA_SMPLRT_DIV 00388 */ 00389 void MPU6050::setRate(uint8_t rate) 00390 { 00391 i2Cdev.writeByte(devAddr, MPU6050_RA_SMPLRT_DIV, rate); 00392 } 00393 00394 // CONFIG register 00395 00396 /** Get external FSYNC configuration. 00397 * Configures the external Frame Synchronization (FSYNC) pin sampling. An 00398 * external signal connected to the FSYNC pin can be sampled by configuring 00399 * EXT_SYNC_SET. Signal changes to the FSYNC pin are latched so that short 00400 * strobes may be captured. The latched FSYNC signal will be sampled at the 00401 * Sampling Rate, as defined in register 25. After sampling, the latch will 00402 * reset to the current FSYNC signal state. 00403 * 00404 * The sampled value will be reported in place of the least significant bit in 00405 * a sensor data register determined by the value of EXT_SYNC_SET according to 00406 * the following table. 00407 * 00408 * <pre> 00409 * EXT_SYNC_SET | FSYNC Bit Location 00410 * -------------+------------------- 00411 * 0 | Input disabled 00412 * 1 | TEMP_OUT_L[0] 00413 * 2 | GYRO_XOUT_L[0] 00414 * 3 | GYRO_YOUT_L[0] 00415 * 4 | GYRO_ZOUT_L[0] 00416 * 5 | ACCEL_XOUT_L[0] 00417 * 6 | ACCEL_YOUT_L[0] 00418 * 7 | ACCEL_ZOUT_L[0] 00419 * </pre> 00420 * 00421 * @return FSYNC configuration value 00422 */ 00423 uint8_t MPU6050::getExternalFrameSync() 00424 { 00425 i2Cdev.readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, buffer); 00426 return buffer[0]; 00427 } 00428 /** Set external FSYNC configuration. 00429 * @see getExternalFrameSync() 00430 * @see MPU6050_RA_CONFIG 00431 * @param sync New FSYNC configuration value 00432 */ 00433 void MPU6050::setExternalFrameSync(uint8_t sync) 00434 { 00435 i2Cdev.writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, sync); 00436 } 00437 /** Get digital low-pass filter configuration. 00438 * The DLPF_CFG parameter sets the digital low pass filter configuration. It 00439 * also determines the internal sampling rate used by the device as shown in 00440 * the table below. 00441 * 00442 * Note: The accelerometer output rate is 1kHz. This means that for a Sample 00443 * Rate greater than 1kHz, the same accelerometer sample may be output to the 00444 * FIFO, DMP, and sensor registers more than once. 00445 * 00446 * <pre> 00447 * | ACCELEROMETER | GYROSCOPE 00448 * DLPF_CFG | Bandwidth | Delay | Bandwidth | Delay | Sample Rate 00449 * ---------+-----------+--------+-----------+--------+------------- 00450 * 0 | 260Hz | 0ms | 256Hz | 0.98ms | 8kHz 00451 * 1 | 184Hz | 2.0ms | 188Hz | 1.9ms | 1kHz 00452 * 2 | 94Hz | 3.0ms | 98Hz | 2.8ms | 1kHz 00453 * 3 | 44Hz | 4.9ms | 42Hz | 4.8ms | 1kHz 00454 * 4 | 21Hz | 8.5ms | 20Hz | 8.3ms | 1kHz 00455 * 5 | 10Hz | 13.8ms | 10Hz | 13.4ms | 1kHz 00456 * 6 | 5Hz | 19.0ms | 5Hz | 18.6ms | 1kHz 00457 * 7 | -- Reserved -- | -- Reserved -- | Reserved 00458 * </pre> 00459 * 00460 * @return DLFP configuration 00461 * @see MPU6050_RA_CONFIG 00462 * @see MPU6050_CFG_DLPF_CFG_BIT 00463 * @see MPU6050_CFG_DLPF_CFG_LENGTH 00464 */ 00465 uint8_t MPU6050::getDLPFMode() 00466 { 00467 i2Cdev.readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, buffer); 00468 return buffer[0]; 00469 } 00470 /** Set digital low-pass filter configuration. 00471 * @param mode New DLFP configuration setting 00472 * @see getDLPFBandwidth() 00473 * @see MPU6050_DLPF_BW_256 00474 * @see MPU6050_RA_CONFIG 00475 * @see MPU6050_CFG_DLPF_CFG_BIT 00476 * @see MPU6050_CFG_DLPF_CFG_LENGTH 00477 */ 00478 void MPU6050::setDLPFMode(uint8_t mode) 00479 { 00480 i2Cdev.writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, mode); 00481 } 00482 00483 // GYRO_CONFIG register 00484 00485 /** Get full-scale gyroscope range. 00486 * The FS_SEL parameter allows setting the full-scale range of the gyro sensors, 00487 * as described in the table below. 00488 * 00489 * <pre> 00490 * 0 = +/- 250 degrees/sec 00491 * 1 = +/- 500 degrees/sec 00492 * 2 = +/- 1000 degrees/sec 00493 * 3 = +/- 2000 degrees/sec 00494 * </pre> 00495 * 00496 * @return Current full-scale gyroscope range setting 00497 * @see MPU6050_GYRO_FS_250 00498 * @see MPU6050_RA_GYRO_CONFIG 00499 * @see MPU6050_GCONFIG_FS_SEL_BIT 00500 * @see MPU6050_GCONFIG_FS_SEL_LENGTH 00501 */ 00502 uint8_t MPU6050::getFullScaleGyroRange() 00503 { 00504 i2Cdev.readBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, buffer); 00505 return buffer[0]; 00506 } 00507 /** Set full-scale gyroscope range. 00508 * @param range New full-scale gyroscope range value 00509 * @see getFullScaleRange() 00510 * @see MPU6050_GYRO_FS_250 00511 * @see MPU6050_RA_GYRO_CONFIG 00512 * @see MPU6050_GCONFIG_FS_SEL_BIT 00513 * @see MPU6050_GCONFIG_FS_SEL_LENGTH 00514 */ 00515 void MPU6050::setFullScaleGyroRange(uint8_t range) 00516 { 00517 i2Cdev.writeBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, range); 00518 } 00519 00520 // ACCEL_CONFIG register 00521 00522 /** Get self-test enabled setting for accelerometer X axis. 00523 * @return Self-test enabled value 00524 * @see MPU6050_RA_ACCEL_CONFIG 00525 */ 00526 bool MPU6050::getAccelXSelfTest() 00527 { 00528 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, buffer); 00529 return buffer[0]; 00530 } 00531 /** Get self-test enabled setting for accelerometer X axis. 00532 * @param enabled Self-test enabled value 00533 * @see MPU6050_RA_ACCEL_CONFIG 00534 */ 00535 void MPU6050::setAccelXSelfTest(bool enabled) 00536 { 00537 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, enabled); 00538 } 00539 /** Get self-test enabled value for accelerometer Y axis. 00540 * @return Self-test enabled value 00541 * @see MPU6050_RA_ACCEL_CONFIG 00542 */ 00543 bool MPU6050::getAccelYSelfTest() 00544 { 00545 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, buffer); 00546 return buffer[0]; 00547 } 00548 /** Get self-test enabled value for accelerometer Y axis. 00549 * @param enabled Self-test enabled value 00550 * @see MPU6050_RA_ACCEL_CONFIG 00551 */ 00552 void MPU6050::setAccelYSelfTest(bool enabled) 00553 { 00554 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, enabled); 00555 } 00556 /** Get self-test enabled value for accelerometer Z axis. 00557 * @return Self-test enabled value 00558 * @see MPU6050_RA_ACCEL_CONFIG 00559 */ 00560 bool MPU6050::getAccelZSelfTest() 00561 { 00562 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, buffer); 00563 return buffer[0]; 00564 } 00565 /** Set self-test enabled value for accelerometer Z axis. 00566 * @param enabled Self-test enabled value 00567 * @see MPU6050_RA_ACCEL_CONFIG 00568 */ 00569 void MPU6050::setAccelZSelfTest(bool enabled) 00570 { 00571 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, enabled); 00572 } 00573 /** Get full-scale accelerometer range. 00574 * The FS_SEL parameter allows setting the full-scale range of the accelerometer 00575 * sensors, as described in the table below. 00576 * 00577 * <pre> 00578 * 0 = +/- 2g 00579 * 1 = +/- 4g 00580 * 2 = +/- 8g 00581 * 3 = +/- 16g 00582 * </pre> 00583 * 00584 * @return Current full-scale accelerometer range setting 00585 * @see MPU6050_ACCEL_FS_2 00586 * @see MPU6050_RA_ACCEL_CONFIG 00587 * @see MPU6050_ACONFIG_AFS_SEL_BIT 00588 * @see MPU6050_ACONFIG_AFS_SEL_LENGTH 00589 */ 00590 uint8_t MPU6050::getFullScaleAccelRange() 00591 { 00592 i2Cdev.readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, buffer); 00593 return buffer[0]; 00594 } 00595 /** Set full-scale accelerometer range. 00596 * @param range New full-scale accelerometer range setting 00597 * @see getFullScaleAccelRange() 00598 */ 00599 void MPU6050::setFullScaleAccelRange(uint8_t range) 00600 { 00601 i2Cdev.writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, range); 00602 } 00603 /** Get the high-pass filter configuration. 00604 * The DHPF is a filter module in the path leading to motion detectors (Free 00605 * Fall, Motion threshold, and Zero Motion). The high pass filter output is not 00606 * available to the data registers (see Figure in Section 8 of the MPU-6000/ 00607 * MPU-6050 Product Specification document). 00608 * 00609 * The high pass filter has three modes: 00610 * 00611 * <pre> 00612 * Reset: The filter output settles to zero within one sample. This 00613 * effectively disables the high pass filter. This mode may be toggled 00614 * to quickly settle the filter. 00615 * 00616 * On: The high pass filter will pass signals above the cut off frequency. 00617 * 00618 * Hold: When triggered, the filter holds the present sample. The filter 00619 * output will be the difference between the input sample and the held 00620 * sample. 00621 * </pre> 00622 * 00623 * <pre> 00624 * ACCEL_HPF | Filter Mode | Cut-off Frequency 00625 * ----------+-------------+------------------ 00626 * 0 | Reset | None 00627 * 1 | On | 5Hz 00628 * 2 | On | 2.5Hz 00629 * 3 | On | 1.25Hz 00630 * 4 | On | 0.63Hz 00631 * 7 | Hold | None 00632 * </pre> 00633 * 00634 * @return Current high-pass filter configuration 00635 * @see MPU6050_DHPF_RESET 00636 * @see MPU6050_RA_ACCEL_CONFIG 00637 */ 00638 uint8_t MPU6050::getDHPFMode() 00639 { 00640 i2Cdev.readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, buffer); 00641 return buffer[0]; 00642 } 00643 /** Set the high-pass filter configuration. 00644 * @param bandwidth New high-pass filter configuration 00645 * @see setDHPFMode() 00646 * @see MPU6050_DHPF_RESET 00647 * @see MPU6050_RA_ACCEL_CONFIG 00648 */ 00649 void MPU6050::setDHPFMode(uint8_t bandwidth) 00650 { 00651 i2Cdev.writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, bandwidth); 00652 } 00653 00654 // FF_THR register 00655 00656 /** Get free-fall event acceleration threshold. 00657 * This register configures the detection threshold for Free Fall event 00658 * detection. The unit of FF_THR is 1LSB = 2mg. Free Fall is detected when the 00659 * absolute value of the accelerometer measurements for the three axes are each 00660 * less than the detection threshold. This condition increments the Free Fall 00661 * duration counter (Register 30). The Free Fall interrupt is triggered when the 00662 * Free Fall duration counter reaches the time specified in FF_DUR. 00663 * 00664 * For more details on the Free Fall detection interrupt, see Section 8.2 of the 00665 * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and 00666 * 58 of this document. 00667 * 00668 * @return Current free-fall acceleration threshold value (LSB = 2mg) 00669 * @see MPU6050_RA_FF_THR 00670 */ 00671 uint8_t MPU6050::getFreefallDetectionThreshold() 00672 { 00673 i2Cdev.readByte(devAddr, MPU6050_RA_FF_THR, buffer); 00674 return buffer[0]; 00675 } 00676 /** Get free-fall event acceleration threshold. 00677 * @param threshold New free-fall acceleration threshold value (LSB = 2mg) 00678 * @see getFreefallDetectionThreshold() 00679 * @see MPU6050_RA_FF_THR 00680 */ 00681 void MPU6050::setFreefallDetectionThreshold(uint8_t threshold) 00682 { 00683 i2Cdev.writeByte(devAddr, MPU6050_RA_FF_THR, threshold); 00684 } 00685 00686 // FF_DUR register 00687 00688 /** Get free-fall event duration threshold. 00689 * This register configures the duration counter threshold for Free Fall event 00690 * detection. The duration counter ticks at 1kHz, therefore FF_DUR has a unit 00691 * of 1 LSB = 1 ms. 00692 * 00693 * The Free Fall duration counter increments while the absolute value of the 00694 * accelerometer measurements are each less than the detection threshold 00695 * (Register 29). The Free Fall interrupt is triggered when the Free Fall 00696 * duration counter reaches the time specified in this register. 00697 * 00698 * For more details on the Free Fall detection interrupt, see Section 8.2 of 00699 * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56 00700 * and 58 of this document. 00701 * 00702 * @return Current free-fall duration threshold value (LSB = 1ms) 00703 * @see MPU6050_RA_FF_DUR 00704 */ 00705 uint8_t MPU6050::getFreefallDetectionDuration() 00706 { 00707 i2Cdev.readByte(devAddr, MPU6050_RA_FF_DUR, buffer); 00708 return buffer[0]; 00709 } 00710 /** Get free-fall event duration threshold. 00711 * @param duration New free-fall duration threshold value (LSB = 1ms) 00712 * @see getFreefallDetectionDuration() 00713 * @see MPU6050_RA_FF_DUR 00714 */ 00715 void MPU6050::setFreefallDetectionDuration(uint8_t duration) 00716 { 00717 i2Cdev.writeByte(devAddr, MPU6050_RA_FF_DUR, duration); 00718 } 00719 00720 // MOT_THR register 00721 00722 /** Get motion detection event acceleration threshold. 00723 * This register configures the detection threshold for Motion interrupt 00724 * generation. The unit of MOT_THR is 1LSB = 2mg. Motion is detected when the 00725 * absolute value of any of the accelerometer measurements exceeds this Motion 00726 * detection threshold. This condition increments the Motion detection duration 00727 * counter (Register 32). The Motion detection interrupt is triggered when the 00728 * Motion Detection counter reaches the time count specified in MOT_DUR 00729 * (Register 32). 00730 * 00731 * The Motion interrupt will indicate the axis and polarity of detected motion 00732 * in MOT_DETECT_STATUS (Register 97). 00733 * 00734 * For more details on the Motion detection interrupt, see Section 8.3 of the 00735 * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and 00736 * 58 of this document. 00737 * 00738 * @return Current motion detection acceleration threshold value (LSB = 2mg) 00739 * @see MPU6050_RA_MOT_THR 00740 */ 00741 uint8_t MPU6050::getMotionDetectionThreshold() 00742 { 00743 i2Cdev.readByte(devAddr, MPU6050_RA_MOT_THR, buffer); 00744 return buffer[0]; 00745 } 00746 /** Set free-fall event acceleration threshold. 00747 * @param threshold New motion detection acceleration threshold value (LSB = 2mg) 00748 * @see getMotionDetectionThreshold() 00749 * @see MPU6050_RA_MOT_THR 00750 */ 00751 void MPU6050::setMotionDetectionThreshold(uint8_t threshold) 00752 { 00753 i2Cdev.writeByte(devAddr, MPU6050_RA_MOT_THR, threshold); 00754 } 00755 00756 // register 00757 00758 /** Get motion detection event duration threshold. 00759 * This register configures the duration counter threshold for Motion interrupt 00760 * generation. The duration counter ticks at 1 kHz, therefore MOT_DUR has a unit 00761 * of 1LSB = 1ms. The Motion detection duration counter increments when the 00762 * absolute value of any of the accelerometer measurements exceeds the Motion 00763 * detection threshold (Register 31). The Motion detection interrupt is 00764 * triggered when the Motion detection counter reaches the time count specified 00765 * in this register. 00766 * 00767 * For more details on the Motion detection interrupt, see Section 8.3 of the 00768 * MPU-6000/MPU-6050 Product Specification document. 00769 * 00770 * @return Current motion detection duration threshold value (LSB = 1ms) 00771 * @see MPU6050_RA_MOT_DUR 00772 */ 00773 uint8_t MPU6050::getMotionDetectionDuration() 00774 { 00775 i2Cdev.readByte(devAddr, MPU6050_RA_MOT_DUR, buffer); 00776 return buffer[0]; 00777 } 00778 /** Set motion detection event duration threshold. 00779 * @param duration New motion detection duration threshold value (LSB = 1ms) 00780 * @see getMotionDetectionDuration() 00781 * @see MPU6050_RA_MOT_DUR 00782 */ 00783 void MPU6050::setMotionDetectionDuration(uint8_t duration) 00784 { 00785 i2Cdev.writeByte(devAddr, MPU6050_RA_MOT_DUR, duration); 00786 } 00787 00788 // ZRMOT_THR register 00789 00790 /** Get zero motion detection event acceleration threshold. 00791 * This register configures the detection threshold for Zero Motion interrupt 00792 * generation. The unit of ZRMOT_THR is 1LSB = 2mg. Zero Motion is detected when 00793 * the absolute value of the accelerometer measurements for the 3 axes are each 00794 * less than the detection threshold. This condition increments the Zero Motion 00795 * duration counter (Register 34). The Zero Motion interrupt is triggered when 00796 * the Zero Motion duration counter reaches the time count specified in 00797 * ZRMOT_DUR (Register 34). 00798 * 00799 * Unlike Free Fall or Motion detection, Zero Motion detection triggers an 00800 * interrupt both when Zero Motion is first detected and when Zero Motion is no 00801 * longer detected. 00802 * 00803 * When a zero motion event is detected, a Zero Motion Status will be indicated 00804 * in the MOT_DETECT_STATUS register (Register 97). When a motion-to-zero-motion 00805 * condition is detected, the status bit is set to 1. When a zero-motion-to- 00806 * motion condition is detected, the status bit is set to 0. 00807 * 00808 * For more details on the Zero Motion detection interrupt, see Section 8.4 of 00809 * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56 00810 * and 58 of this document. 00811 * 00812 * @return Current zero motion detection acceleration threshold value (LSB = 2mg) 00813 * @see MPU6050_RA_ZRMOT_THR 00814 */ 00815 uint8_t MPU6050::getZeroMotionDetectionThreshold() 00816 { 00817 i2Cdev.readByte(devAddr, MPU6050_RA_ZRMOT_THR, buffer); 00818 return buffer[0]; 00819 } 00820 /** Set zero motion detection event acceleration threshold. 00821 * @param threshold New zero motion detection acceleration threshold value (LSB = 2mg) 00822 * @see getZeroMotionDetectionThreshold() 00823 * @see MPU6050_RA_ZRMOT_THR 00824 */ 00825 void MPU6050::setZeroMotionDetectionThreshold(uint8_t threshold) 00826 { 00827 i2Cdev.writeByte(devAddr, MPU6050_RA_ZRMOT_THR, threshold); 00828 } 00829 00830 // ZRMOT_DUR register 00831 00832 /** Get zero motion detection event duration threshold. 00833 * This register configures the duration counter threshold for Zero Motion 00834 * interrupt generation. The duration counter ticks at 16 Hz, therefore 00835 * ZRMOT_DUR has a unit of 1 LSB = 64 ms. The Zero Motion duration counter 00836 * increments while the absolute value of the accelerometer measurements are 00837 * each less than the detection threshold (Register 33). The Zero Motion 00838 * interrupt is triggered when the Zero Motion duration counter reaches the time 00839 * count specified in this register. 00840 * 00841 * For more details on the Zero Motion detection interrupt, see Section 8.4 of 00842 * the MPU-6000/MPU-6050 Product Specification document, as well as Registers 56 00843 * and 58 of this document. 00844 * 00845 * @return Current zero motion detection duration threshold value (LSB = 64ms) 00846 * @see MPU6050_RA_ZRMOT_DUR 00847 */ 00848 uint8_t MPU6050::getZeroMotionDetectionDuration() 00849 { 00850 i2Cdev.readByte(devAddr, MPU6050_RA_ZRMOT_DUR, buffer); 00851 return buffer[0]; 00852 } 00853 /** Set zero motion detection event duration threshold. 00854 * @param duration New zero motion detection duration threshold value (LSB = 1ms) 00855 * @see getZeroMotionDetectionDuration() 00856 * @see MPU6050_RA_ZRMOT_DUR 00857 */ 00858 void MPU6050::setZeroMotionDetectionDuration(uint8_t duration) 00859 { 00860 i2Cdev.writeByte(devAddr, MPU6050_RA_ZRMOT_DUR, duration); 00861 } 00862 00863 // FIFO_EN register 00864 00865 /** Get temperature FIFO enabled value. 00866 * When set to 1, this bit enables TEMP_OUT_H and TEMP_OUT_L (Registers 65 and 00867 * 66) to be written into the FIFO buffer. 00868 * @return Current temperature FIFO enabled value 00869 * @see MPU6050_RA_FIFO_EN 00870 */ 00871 bool MPU6050::getTempFIFOEnabled() 00872 { 00873 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, buffer); 00874 return buffer[0]; 00875 } 00876 /** Set temperature FIFO enabled value. 00877 * @param enabled New temperature FIFO enabled value 00878 * @see getTempFIFOEnabled() 00879 * @see MPU6050_RA_FIFO_EN 00880 */ 00881 void MPU6050::setTempFIFOEnabled(bool enabled) 00882 { 00883 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, enabled); 00884 } 00885 /** Get gyroscope X-axis FIFO enabled value. 00886 * When set to 1, this bit enables GYRO_XOUT_H and GYRO_XOUT_L (Registers 67 and 00887 * 68) to be written into the FIFO buffer. 00888 * @return Current gyroscope X-axis FIFO enabled value 00889 * @see MPU6050_RA_FIFO_EN 00890 */ 00891 bool MPU6050::getXGyroFIFOEnabled() 00892 { 00893 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, buffer); 00894 return buffer[0]; 00895 } 00896 /** Set gyroscope X-axis FIFO enabled value. 00897 * @param enabled New gyroscope X-axis FIFO enabled value 00898 * @see getXGyroFIFOEnabled() 00899 * @see MPU6050_RA_FIFO_EN 00900 */ 00901 void MPU6050::setXGyroFIFOEnabled(bool enabled) 00902 { 00903 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, enabled); 00904 } 00905 /** Get gyroscope Y-axis FIFO enabled value. 00906 * When set to 1, this bit enables GYRO_YOUT_H and GYRO_YOUT_L (Registers 69 and 00907 * 70) to be written into the FIFO buffer. 00908 * @return Current gyroscope Y-axis FIFO enabled value 00909 * @see MPU6050_RA_FIFO_EN 00910 */ 00911 bool MPU6050::getYGyroFIFOEnabled() 00912 { 00913 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, buffer); 00914 return buffer[0]; 00915 } 00916 /** Set gyroscope Y-axis FIFO enabled value. 00917 * @param enabled New gyroscope Y-axis FIFO enabled value 00918 * @see getYGyroFIFOEnabled() 00919 * @see MPU6050_RA_FIFO_EN 00920 */ 00921 void MPU6050::setYGyroFIFOEnabled(bool enabled) 00922 { 00923 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, enabled); 00924 } 00925 /** Get gyroscope Z-axis FIFO enabled value. 00926 * When set to 1, this bit enables GYRO_ZOUT_H and GYRO_ZOUT_L (Registers 71 and 00927 * 72) to be written into the FIFO buffer. 00928 * @return Current gyroscope Z-axis FIFO enabled value 00929 * @see MPU6050_RA_FIFO_EN 00930 */ 00931 bool MPU6050::getZGyroFIFOEnabled() 00932 { 00933 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, buffer); 00934 return buffer[0]; 00935 } 00936 /** Set gyroscope Z-axis FIFO enabled value. 00937 * @param enabled New gyroscope Z-axis FIFO enabled value 00938 * @see getZGyroFIFOEnabled() 00939 * @see MPU6050_RA_FIFO_EN 00940 */ 00941 void MPU6050::setZGyroFIFOEnabled(bool enabled) 00942 { 00943 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, enabled); 00944 } 00945 /** Get accelerometer FIFO enabled value. 00946 * When set to 1, this bit enables ACCEL_XOUT_H, ACCEL_XOUT_L, ACCEL_YOUT_H, 00947 * ACCEL_YOUT_L, ACCEL_ZOUT_H, and ACCEL_ZOUT_L (Registers 59 to 64) to be 00948 * written into the FIFO buffer. 00949 * @return Current accelerometer FIFO enabled value 00950 * @see MPU6050_RA_FIFO_EN 00951 */ 00952 bool MPU6050::getAccelFIFOEnabled() 00953 { 00954 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, buffer); 00955 return buffer[0]; 00956 } 00957 /** Set accelerometer FIFO enabled value. 00958 * @param enabled New accelerometer FIFO enabled value 00959 * @see getAccelFIFOEnabled() 00960 * @see MPU6050_RA_FIFO_EN 00961 */ 00962 void MPU6050::setAccelFIFOEnabled(bool enabled) 00963 { 00964 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, enabled); 00965 } 00966 /** Get Slave 2 FIFO enabled value. 00967 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96) 00968 * associated with Slave 2 to be written into the FIFO buffer. 00969 * @return Current Slave 2 FIFO enabled value 00970 * @see MPU6050_RA_FIFO_EN 00971 */ 00972 bool MPU6050::getSlave2FIFOEnabled() 00973 { 00974 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, buffer); 00975 return buffer[0]; 00976 } 00977 /** Set Slave 2 FIFO enabled value. 00978 * @param enabled New Slave 2 FIFO enabled value 00979 * @see getSlave2FIFOEnabled() 00980 * @see MPU6050_RA_FIFO_EN 00981 */ 00982 void MPU6050::setSlave2FIFOEnabled(bool enabled) 00983 { 00984 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, enabled); 00985 } 00986 /** Get Slave 1 FIFO enabled value. 00987 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96) 00988 * associated with Slave 1 to be written into the FIFO buffer. 00989 * @return Current Slave 1 FIFO enabled value 00990 * @see MPU6050_RA_FIFO_EN 00991 */ 00992 bool MPU6050::getSlave1FIFOEnabled() 00993 { 00994 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, buffer); 00995 return buffer[0]; 00996 } 00997 /** Set Slave 1 FIFO enabled value. 00998 * @param enabled New Slave 1 FIFO enabled value 00999 * @see getSlave1FIFOEnabled() 01000 * @see MPU6050_RA_FIFO_EN 01001 */ 01002 void MPU6050::setSlave1FIFOEnabled(bool enabled) 01003 { 01004 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, enabled); 01005 } 01006 /** Get Slave 0 FIFO enabled value. 01007 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96) 01008 * associated with Slave 0 to be written into the FIFO buffer. 01009 * @return Current Slave 0 FIFO enabled value 01010 * @see MPU6050_RA_FIFO_EN 01011 */ 01012 bool MPU6050::getSlave0FIFOEnabled() 01013 { 01014 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, buffer); 01015 return buffer[0]; 01016 } 01017 /** Set Slave 0 FIFO enabled value. 01018 * @param enabled New Slave 0 FIFO enabled value 01019 * @see getSlave0FIFOEnabled() 01020 * @see MPU6050_RA_FIFO_EN 01021 */ 01022 void MPU6050::setSlave0FIFOEnabled(bool enabled) 01023 { 01024 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, enabled); 01025 } 01026 01027 // I2C_MST_CTRL register 01028 01029 /** Get multi-master enabled value. 01030 * Multi-master capability allows multiple I2C masters to operate on the same 01031 * bus. In circuits where multi-master capability is required, set MULT_MST_EN 01032 * to 1. This will increase current drawn by approximately 30uA. 01033 * 01034 * In circuits where multi-master capability is required, the state of the I2C 01035 * bus must always be monitored by each separate I2C Master. Before an I2C 01036 * Master can assume arbitration of the bus, it must first confirm that no other 01037 * I2C Master has arbitration of the bus. When MULT_MST_EN is set to 1, the 01038 * MPU-60X0's bus arbitration detection logic is turned on, enabling it to 01039 * detect when the bus is available. 01040 * 01041 * @return Current multi-master enabled value 01042 * @see MPU6050_RA_I2C_MST_CTRL 01043 */ 01044 bool MPU6050::getMultiMasterEnabled() 01045 { 01046 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, buffer); 01047 return buffer[0]; 01048 } 01049 /** Set multi-master enabled value. 01050 * @param enabled New multi-master enabled value 01051 * @see getMultiMasterEnabled() 01052 * @see MPU6050_RA_I2C_MST_CTRL 01053 */ 01054 void MPU6050::setMultiMasterEnabled(bool enabled) 01055 { 01056 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, enabled); 01057 } 01058 /** Get wait-for-external-sensor-data enabled value. 01059 * When the WAIT_FOR_ES bit is set to 1, the Data Ready interrupt will be 01060 * delayed until External Sensor data from the Slave Devices are loaded into the 01061 * EXT_SENS_DATA registers. This is used to ensure that both the internal sensor 01062 * data (i.e. from gyro and accel) and external sensor data have been loaded to 01063 * their respective data registers (i.e. the data is synced) when the Data Ready 01064 * interrupt is triggered. 01065 * 01066 * @return Current wait-for-external-sensor-data enabled value 01067 * @see MPU6050_RA_I2C_MST_CTRL 01068 */ 01069 bool MPU6050::getWaitForExternalSensorEnabled() 01070 { 01071 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, buffer); 01072 return buffer[0]; 01073 } 01074 /** Set wait-for-external-sensor-data enabled value. 01075 * @param enabled New wait-for-external-sensor-data enabled value 01076 * @see getWaitForExternalSensorEnabled() 01077 * @see MPU6050_RA_I2C_MST_CTRL 01078 */ 01079 void MPU6050::setWaitForExternalSensorEnabled(bool enabled) 01080 { 01081 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, enabled); 01082 } 01083 /** Get Slave 3 FIFO enabled value. 01084 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96) 01085 * associated with Slave 3 to be written into the FIFO buffer. 01086 * @return Current Slave 3 FIFO enabled value 01087 * @see MPU6050_RA_MST_CTRL 01088 */ 01089 bool MPU6050::getSlave3FIFOEnabled() 01090 { 01091 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, buffer); 01092 return buffer[0]; 01093 } 01094 /** Set Slave 3 FIFO enabled value. 01095 * @param enabled New Slave 3 FIFO enabled value 01096 * @see getSlave3FIFOEnabled() 01097 * @see MPU6050_RA_MST_CTRL 01098 */ 01099 void MPU6050::setSlave3FIFOEnabled(bool enabled) 01100 { 01101 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, enabled); 01102 } 01103 /** Get slave read/write transition enabled value. 01104 * The I2C_MST_P_NSR bit configures the I2C Master's transition from one slave 01105 * read to the next slave read. If the bit equals 0, there will be a restart 01106 * between reads. If the bit equals 1, there will be a stop followed by a start 01107 * of the following read. When a write transaction follows a read transaction, 01108 * the stop followed by a start of the successive write will be always used. 01109 * 01110 * @return Current slave read/write transition enabled value 01111 * @see MPU6050_RA_I2C_MST_CTRL 01112 */ 01113 bool MPU6050::getSlaveReadWriteTransitionEnabled() 01114 { 01115 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, buffer); 01116 return buffer[0]; 01117 } 01118 /** Set slave read/write transition enabled value. 01119 * @param enabled New slave read/write transition enabled value 01120 * @see getSlaveReadWriteTransitionEnabled() 01121 * @see MPU6050_RA_I2C_MST_CTRL 01122 */ 01123 void MPU6050::setSlaveReadWriteTransitionEnabled(bool enabled) 01124 { 01125 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, enabled); 01126 } 01127 /** Get I2C master clock speed. 01128 * I2C_MST_CLK is a 4 bit unsigned value which configures a divider on the 01129 * MPU-60X0 internal 8MHz clock. It sets the I2C master clock speed according to 01130 * the following table: 01131 * 01132 * <pre> 01133 * I2C_MST_CLK | I2C Master Clock Speed | 8MHz Clock Divider 01134 * ------------+------------------------+------------------- 01135 * 0 | 348kHz | 23 01136 * 1 | 333kHz | 24 01137 * 2 | 320kHz | 25 01138 * 3 | 308kHz | 26 01139 * 4 | 296kHz | 27 01140 * 5 | 286kHz | 28 01141 * 6 | 276kHz | 29 01142 * 7 | 267kHz | 30 01143 * 8 | 258kHz | 31 01144 * 9 | 500kHz | 16 01145 * 10 | 471kHz | 17 01146 * 11 | 444kHz | 18 01147 * 12 | 421kHz | 19 01148 * 13 | 400kHz | 20 01149 * 14 | 381kHz | 21 01150 * 15 | 364kHz | 22 01151 * </pre> 01152 * 01153 * @return Current I2C master clock speed 01154 * @see MPU6050_RA_I2C_MST_CTRL 01155 */ 01156 uint8_t MPU6050::getMasterClockSpeed() 01157 { 01158 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, buffer); 01159 return buffer[0]; 01160 } 01161 /** Set I2C master clock speed. 01162 * @reparam speed Current I2C master clock speed 01163 * @see MPU6050_RA_I2C_MST_CTRL 01164 */ 01165 void MPU6050::setMasterClockSpeed(uint8_t speed) 01166 { 01167 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, speed); 01168 } 01169 01170 // I2C_SLV* registers (Slave 0-3) 01171 01172 /** Get the I2C address of the specified slave (0-3). 01173 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read 01174 * operation, and if it is cleared, then it's a write operation. The remaining 01175 * bits (6-0) are the 7-bit device address of the slave device. 01176 * 01177 * In read mode, the result of the read is placed in the lowest available 01178 * EXT_SENS_DATA register. For further information regarding the allocation of 01179 * read results, please refer to the EXT_SENS_DATA register description 01180 * (Registers 73 - 96). 01181 * 01182 * The MPU-6050 supports a total of five slaves, but Slave 4 has unique 01183 * characteristics, and so it has its own functions (getSlave4* and setSlave4*). 01184 * 01185 * I2C data transactions are performed at the Sample Rate, as defined in 01186 * Register 25. The user is responsible for ensuring that I2C data transactions 01187 * to and from each enabled Slave can be completed within a single period of the 01188 * Sample Rate. 01189 * 01190 * The I2C slave access rate can be reduced relative to the Sample Rate. This 01191 * reduced access rate is determined by I2C_MST_DLY (Register 52). Whether a 01192 * slave's access rate is reduced relative to the Sample Rate is determined by 01193 * I2C_MST_DELAY_CTRL (Register 103). 01194 * 01195 * The processing order for the slaves is fixed. The sequence followed for 01196 * processing the slaves is Slave 0, Slave 1, Slave 2, Slave 3 and Slave 4. If a 01197 * particular Slave is disabled it will be skipped. 01198 * 01199 * Each slave can either be accessed at the sample rate or at a reduced sample 01200 * rate. In a case where some slaves are accessed at the Sample Rate and some 01201 * slaves are accessed at the reduced rate, the sequence of accessing the slaves 01202 * (Slave 0 to Slave 4) is still followed. However, the reduced rate slaves will 01203 * be skipped if their access rate dictates that they should not be accessed 01204 * during that particular cycle. For further information regarding the reduced 01205 * access rate, please refer to Register 52. Whether a slave is accessed at the 01206 * Sample Rate or at the reduced rate is determined by the Delay Enable bits in 01207 * Register 103. 01208 * 01209 * @param num Slave number (0-3) 01210 * @return Current address for specified slave 01211 * @see MPU6050_RA_I2C_SLV0_ADDR 01212 */ 01213 uint8_t MPU6050::getSlaveAddress(uint8_t num) 01214 { 01215 if (num > 3) return 0; 01216 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, buffer); 01217 return buffer[0]; 01218 } 01219 /** Set the I2C address of the specified slave (0-3). 01220 * @param num Slave number (0-3) 01221 * @param address New address for specified slave 01222 * @see getSlaveAddress() 01223 * @see MPU6050_RA_I2C_SLV0_ADDR 01224 */ 01225 void MPU6050::setSlaveAddress(uint8_t num, uint8_t address) 01226 { 01227 if (num > 3) return; 01228 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, address); 01229 } 01230 /** Get the active internal register for the specified slave (0-3). 01231 * Read/write operations for this slave will be done to whatever internal 01232 * register address is stored in this MPU register. 01233 * 01234 * The MPU-6050 supports a total of five slaves, but Slave 4 has unique 01235 * characteristics, and so it has its own functions. 01236 * 01237 * @param num Slave number (0-3) 01238 * @return Current active register for specified slave 01239 * @see MPU6050_RA_I2C_SLV0_REG 01240 */ 01241 uint8_t MPU6050::getSlaveRegister(uint8_t num) 01242 { 01243 if (num > 3) return 0; 01244 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, buffer); 01245 return buffer[0]; 01246 } 01247 /** Set the active internal register for the specified slave (0-3). 01248 * @param num Slave number (0-3) 01249 * @param reg New active register for specified slave 01250 * @see getSlaveRegister() 01251 * @see MPU6050_RA_I2C_SLV0_REG 01252 */ 01253 void MPU6050::setSlaveRegister(uint8_t num, uint8_t reg) 01254 { 01255 if (num > 3) return; 01256 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, reg); 01257 } 01258 /** Get the enabled value for the specified slave (0-3). 01259 * When set to 1, this bit enables Slave 0 for data transfer operations. When 01260 * cleared to 0, this bit disables Slave 0 from data transfer operations. 01261 * @param num Slave number (0-3) 01262 * @return Current enabled value for specified slave 01263 * @see MPU6050_RA_I2C_SLV0_CTRL 01264 */ 01265 bool MPU6050::getSlaveEnabled(uint8_t num) 01266 { 01267 if (num > 3) return 0; 01268 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, buffer); 01269 return buffer[0]; 01270 } 01271 /** Set the enabled value for the specified slave (0-3). 01272 * @param num Slave number (0-3) 01273 * @param enabled New enabled value for specified slave 01274 * @see getSlaveEnabled() 01275 * @see MPU6050_RA_I2C_SLV0_CTRL 01276 */ 01277 void MPU6050::setSlaveEnabled(uint8_t num, bool enabled) 01278 { 01279 if (num > 3) return; 01280 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, enabled); 01281 } 01282 /** Get word pair byte-swapping enabled for the specified slave (0-3). 01283 * When set to 1, this bit enables byte swapping. When byte swapping is enabled, 01284 * the high and low bytes of a word pair are swapped. Please refer to 01285 * I2C_SLV0_GRP for the pairing convention of the word pairs. When cleared to 0, 01286 * bytes transferred to and from Slave 0 will be written to EXT_SENS_DATA 01287 * registers in the order they were transferred. 01288 * 01289 * @param num Slave number (0-3) 01290 * @return Current word pair byte-swapping enabled value for specified slave 01291 * @see MPU6050_RA_I2C_SLV0_CTRL 01292 */ 01293 bool MPU6050::getSlaveWordByteSwap(uint8_t num) 01294 { 01295 if (num > 3) return 0; 01296 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, buffer); 01297 return buffer[0]; 01298 } 01299 /** Set word pair byte-swapping enabled for the specified slave (0-3). 01300 * @param num Slave number (0-3) 01301 * @param enabled New word pair byte-swapping enabled value for specified slave 01302 * @see getSlaveWordByteSwap() 01303 * @see MPU6050_RA_I2C_SLV0_CTRL 01304 */ 01305 void MPU6050::setSlaveWordByteSwap(uint8_t num, bool enabled) 01306 { 01307 if (num > 3) return; 01308 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, enabled); 01309 } 01310 /** Get write mode for the specified slave (0-3). 01311 * When set to 1, the transaction will read or write data only. When cleared to 01312 * 0, the transaction will write a register address prior to reading or writing 01313 * data. This should equal 0 when specifying the register address within the 01314 * Slave device to/from which the ensuing data transaction will take place. 01315 * 01316 * @param num Slave number (0-3) 01317 * @return Current write mode for specified slave (0 = register address + data, 1 = data only) 01318 * @see MPU6050_RA_I2C_SLV0_CTRL 01319 */ 01320 bool MPU6050::getSlaveWriteMode(uint8_t num) 01321 { 01322 if (num > 3) return 0; 01323 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, buffer); 01324 return buffer[0]; 01325 } 01326 /** Set write mode for the specified slave (0-3). 01327 * @param num Slave number (0-3) 01328 * @param mode New write mode for specified slave (0 = register address + data, 1 = data only) 01329 * @see getSlaveWriteMode() 01330 * @see MPU6050_RA_I2C_SLV0_CTRL 01331 */ 01332 void MPU6050::setSlaveWriteMode(uint8_t num, bool mode) 01333 { 01334 if (num > 3) return; 01335 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, mode); 01336 } 01337 /** Get word pair grouping order offset for the specified slave (0-3). 01338 * This sets specifies the grouping order of word pairs received from registers. 01339 * When cleared to 0, bytes from register addresses 0 and 1, 2 and 3, etc (even, 01340 * then odd register addresses) are paired to form a word. When set to 1, bytes 01341 * from register addresses are paired 1 and 2, 3 and 4, etc. (odd, then even 01342 * register addresses) are paired to form a word. 01343 * 01344 * @param num Slave number (0-3) 01345 * @return Current word pair grouping order offset for specified slave 01346 * @see MPU6050_RA_I2C_SLV0_CTRL 01347 */ 01348 bool MPU6050::getSlaveWordGroupOffset(uint8_t num) 01349 { 01350 if (num > 3) return 0; 01351 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, buffer); 01352 return buffer[0]; 01353 } 01354 /** Set word pair grouping order offset for the specified slave (0-3). 01355 * @param num Slave number (0-3) 01356 * @param enabled New word pair grouping order offset for specified slave 01357 * @see getSlaveWordGroupOffset() 01358 * @see MPU6050_RA_I2C_SLV0_CTRL 01359 */ 01360 void MPU6050::setSlaveWordGroupOffset(uint8_t num, bool enabled) 01361 { 01362 if (num > 3) return; 01363 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, enabled); 01364 } 01365 /** Get number of bytes to read for the specified slave (0-3). 01366 * Specifies the number of bytes transferred to and from Slave 0. Clearing this 01367 * bit to 0 is equivalent to disabling the register by writing 0 to I2C_SLV0_EN. 01368 * @param num Slave number (0-3) 01369 * @return Number of bytes to read for specified slave 01370 * @see MPU6050_RA_I2C_SLV0_CTRL 01371 */ 01372 uint8_t MPU6050::getSlaveDataLength(uint8_t num) 01373 { 01374 if (num > 3) return 0; 01375 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, buffer); 01376 return buffer[0]; 01377 } 01378 /** Set number of bytes to read for the specified slave (0-3). 01379 * @param num Slave number (0-3) 01380 * @param length Number of bytes to read for specified slave 01381 * @see getSlaveDataLength() 01382 * @see MPU6050_RA_I2C_SLV0_CTRL 01383 */ 01384 void MPU6050::setSlaveDataLength(uint8_t num, uint8_t length) 01385 { 01386 if (num > 3) return; 01387 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, length); 01388 } 01389 01390 // I2C_SLV* registers (Slave 4) 01391 01392 /** Get the I2C address of Slave 4. 01393 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read 01394 * operation, and if it is cleared, then it's a write operation. The remaining 01395 * bits (6-0) are the 7-bit device address of the slave device. 01396 * 01397 * @return Current address for Slave 4 01398 * @see getSlaveAddress() 01399 * @see MPU6050_RA_I2C_SLV4_ADDR 01400 */ 01401 uint8_t MPU6050::getSlave4Address() 01402 { 01403 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, buffer); 01404 return buffer[0]; 01405 } 01406 /** Set the I2C address of Slave 4. 01407 * @param address New address for Slave 4 01408 * @see getSlave4Address() 01409 * @see MPU6050_RA_I2C_SLV4_ADDR 01410 */ 01411 void MPU6050::setSlave4Address(uint8_t address) 01412 { 01413 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, address); 01414 } 01415 /** Get the active internal register for the Slave 4. 01416 * Read/write operations for this slave will be done to whatever internal 01417 * register address is stored in this MPU register. 01418 * 01419 * @return Current active register for Slave 4 01420 * @see MPU6050_RA_I2C_SLV4_REG 01421 */ 01422 uint8_t MPU6050::getSlave4Register() 01423 { 01424 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_REG, buffer); 01425 return buffer[0]; 01426 } 01427 /** Set the active internal register for Slave 4. 01428 * @param reg New active register for Slave 4 01429 * @see getSlave4Register() 01430 * @see MPU6050_RA_I2C_SLV4_REG 01431 */ 01432 void MPU6050::setSlave4Register(uint8_t reg) 01433 { 01434 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_REG, reg); 01435 } 01436 /** Set new byte to write to Slave 4. 01437 * This register stores the data to be written into the Slave 4. If I2C_SLV4_RW 01438 * is set 1 (set to read), this register has no effect. 01439 * @param data New byte to write to Slave 4 01440 * @see MPU6050_RA_I2C_SLV4_DO 01441 */ 01442 void MPU6050::setSlave4OutputByte(uint8_t data) 01443 { 01444 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_DO, data); 01445 } 01446 /** Get the enabled value for the Slave 4. 01447 * When set to 1, this bit enables Slave 4 for data transfer operations. When 01448 * cleared to 0, this bit disables Slave 4 from data transfer operations. 01449 * @return Current enabled value for Slave 4 01450 * @see MPU6050_RA_I2C_SLV4_CTRL 01451 */ 01452 bool MPU6050::getSlave4Enabled() 01453 { 01454 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, buffer); 01455 return buffer[0]; 01456 } 01457 /** Set the enabled value for Slave 4. 01458 * @param enabled New enabled value for Slave 4 01459 * @see getSlave4Enabled() 01460 * @see MPU6050_RA_I2C_SLV4_CTRL 01461 */ 01462 void MPU6050::setSlave4Enabled(bool enabled) 01463 { 01464 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, enabled); 01465 } 01466 /** Get the enabled value for Slave 4 transaction interrupts. 01467 * When set to 1, this bit enables the generation of an interrupt signal upon 01468 * completion of a Slave 4 transaction. When cleared to 0, this bit disables the 01469 * generation of an interrupt signal upon completion of a Slave 4 transaction. 01470 * The interrupt status can be observed in Register 54. 01471 * 01472 * @return Current enabled value for Slave 4 transaction interrupts. 01473 * @see MPU6050_RA_I2C_SLV4_CTRL 01474 */ 01475 bool MPU6050::getSlave4InterruptEnabled() 01476 { 01477 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, buffer); 01478 return buffer[0]; 01479 } 01480 /** Set the enabled value for Slave 4 transaction interrupts. 01481 * @param enabled New enabled value for Slave 4 transaction interrupts. 01482 * @see getSlave4InterruptEnabled() 01483 * @see MPU6050_RA_I2C_SLV4_CTRL 01484 */ 01485 void MPU6050::setSlave4InterruptEnabled(bool enabled) 01486 { 01487 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, enabled); 01488 } 01489 /** Get write mode for Slave 4. 01490 * When set to 1, the transaction will read or write data only. When cleared to 01491 * 0, the transaction will write a register address prior to reading or writing 01492 * data. This should equal 0 when specifying the register address within the 01493 * Slave device to/from which the ensuing data transaction will take place. 01494 * 01495 * @return Current write mode for Slave 4 (0 = register address + data, 1 = data only) 01496 * @see MPU6050_RA_I2C_SLV4_CTRL 01497 */ 01498 bool MPU6050::getSlave4WriteMode() 01499 { 01500 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, buffer); 01501 return buffer[0]; 01502 } 01503 /** Set write mode for the Slave 4. 01504 * @param mode New write mode for Slave 4 (0 = register address + data, 1 = data only) 01505 * @see getSlave4WriteMode() 01506 * @see MPU6050_RA_I2C_SLV4_CTRL 01507 */ 01508 void MPU6050::setSlave4WriteMode(bool mode) 01509 { 01510 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, mode); 01511 } 01512 /** Get Slave 4 master delay value. 01513 * This configures the reduced access rate of I2C slaves relative to the Sample 01514 * Rate. When a slave's access rate is decreased relative to the Sample Rate, 01515 * the slave is accessed every: 01516 * 01517 * 1 / (1 + I2C_MST_DLY) samples 01518 * 01519 * This base Sample Rate in turn is determined by SMPLRT_DIV (register 25) and 01520 * DLPF_CFG (register 26). Whether a slave's access rate is reduced relative to 01521 * the Sample Rate is determined by I2C_MST_DELAY_CTRL (register 103). For 01522 * further information regarding the Sample Rate, please refer to register 25. 01523 * 01524 * @return Current Slave 4 master delay value 01525 * @see MPU6050_RA_I2C_SLV4_CTRL 01526 */ 01527 uint8_t MPU6050::getSlave4MasterDelay() 01528 { 01529 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, buffer); 01530 return buffer[0]; 01531 } 01532 /** Set Slave 4 master delay value. 01533 * @param delay New Slave 4 master delay value 01534 * @see getSlave4MasterDelay() 01535 * @see MPU6050_RA_I2C_SLV4_CTRL 01536 */ 01537 void MPU6050::setSlave4MasterDelay(uint8_t delay) 01538 { 01539 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, delay); 01540 } 01541 /** Get last available byte read from Slave 4. 01542 * This register stores the data read from Slave 4. This field is populated 01543 * after a read transaction. 01544 * @return Last available byte read from to Slave 4 01545 * @see MPU6050_RA_I2C_SLV4_DI 01546 */ 01547 uint8_t MPU6050::getSlate4InputByte() 01548 { 01549 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_DI, buffer); 01550 return buffer[0]; 01551 } 01552 01553 // I2C_MST_STATUS register 01554 01555 /** Get FSYNC interrupt status. 01556 * This bit reflects the status of the FSYNC interrupt from an external device 01557 * into the MPU-60X0. This is used as a way to pass an external interrupt 01558 * through the MPU-60X0 to the host application processor. When set to 1, this 01559 * bit will cause an interrupt if FSYNC_INT_EN is asserted in INT_PIN_CFG 01560 * (Register 55). 01561 * @return FSYNC interrupt status 01562 * @see MPU6050_RA_I2C_MST_STATUS 01563 */ 01564 bool MPU6050::getPassthroughStatus() 01565 { 01566 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_PASS_THROUGH_BIT, buffer); 01567 return buffer[0]; 01568 } 01569 /** Get Slave 4 transaction done status. 01570 * Automatically sets to 1 when a Slave 4 transaction has completed. This 01571 * triggers an interrupt if the I2C_MST_INT_EN bit in the INT_ENABLE register 01572 * (Register 56) is asserted and if the SLV_4_DONE_INT bit is asserted in the 01573 * I2C_SLV4_CTRL register (Register 52). 01574 * @return Slave 4 transaction done status 01575 * @see MPU6050_RA_I2C_MST_STATUS 01576 */ 01577 bool MPU6050::getSlave4IsDone() 01578 { 01579 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_DONE_BIT, buffer); 01580 return buffer[0]; 01581 } 01582 /** Get master arbitration lost status. 01583 * This bit automatically sets to 1 when the I2C Master has lost arbitration of 01584 * the auxiliary I2C bus (an error condition). This triggers an interrupt if the 01585 * I2C_MST_INT_EN bit in the INT_ENABLE register (Register 56) is asserted. 01586 * @return Master arbitration lost status 01587 * @see MPU6050_RA_I2C_MST_STATUS 01588 */ 01589 bool MPU6050::getLostArbitration() 01590 { 01591 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_LOST_ARB_BIT, buffer); 01592 return buffer[0]; 01593 } 01594 /** Get Slave 4 NACK status. 01595 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01596 * transaction with Slave 4. This triggers an interrupt if the I2C_MST_INT_EN 01597 * bit in the INT_ENABLE register (Register 56) is asserted. 01598 * @return Slave 4 NACK interrupt status 01599 * @see MPU6050_RA_I2C_MST_STATUS 01600 */ 01601 bool MPU6050::getSlave4Nack() 01602 { 01603 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_NACK_BIT, buffer); 01604 return buffer[0]; 01605 } 01606 /** Get Slave 3 NACK status. 01607 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01608 * transaction with Slave 3. This triggers an interrupt if the I2C_MST_INT_EN 01609 * bit in the INT_ENABLE register (Register 56) is asserted. 01610 * @return Slave 3 NACK interrupt status 01611 * @see MPU6050_RA_I2C_MST_STATUS 01612 */ 01613 bool MPU6050::getSlave3Nack() 01614 { 01615 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV3_NACK_BIT, buffer); 01616 return buffer[0]; 01617 } 01618 /** Get Slave 2 NACK status. 01619 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01620 * transaction with Slave 2. This triggers an interrupt if the I2C_MST_INT_EN 01621 * bit in the INT_ENABLE register (Register 56) is asserted. 01622 * @return Slave 2 NACK interrupt status 01623 * @see MPU6050_RA_I2C_MST_STATUS 01624 */ 01625 bool MPU6050::getSlave2Nack() 01626 { 01627 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV2_NACK_BIT, buffer); 01628 return buffer[0]; 01629 } 01630 /** Get Slave 1 NACK status. 01631 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01632 * transaction with Slave 1. This triggers an interrupt if the I2C_MST_INT_EN 01633 * bit in the INT_ENABLE register (Register 56) is asserted. 01634 * @return Slave 1 NACK interrupt status 01635 * @see MPU6050_RA_I2C_MST_STATUS 01636 */ 01637 bool MPU6050::getSlave1Nack() 01638 { 01639 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV1_NACK_BIT, buffer); 01640 return buffer[0]; 01641 } 01642 /** Get Slave 0 NACK status. 01643 * This bit automatically sets to 1 when the I2C Master receives a NACK in a 01644 * transaction with Slave 0. This triggers an interrupt if the I2C_MST_INT_EN 01645 * bit in the INT_ENABLE register (Register 56) is asserted. 01646 * @return Slave 0 NACK interrupt status 01647 * @see MPU6050_RA_I2C_MST_STATUS 01648 */ 01649 bool MPU6050::getSlave0Nack() 01650 { 01651 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV0_NACK_BIT, buffer); 01652 return buffer[0]; 01653 } 01654 01655 // INT_PIN_CFG register 01656 01657 /** Get interrupt logic level mode. 01658 * Will be set 0 for active-high, 1 for active-low. 01659 * @return Current interrupt mode (0=active-high, 1=active-low) 01660 * @see MPU6050_RA_INT_PIN_CFG 01661 * @see MPU6050_INTCFG_INT_LEVEL_BIT 01662 */ 01663 bool MPU6050::getInterruptMode() 01664 { 01665 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, buffer); 01666 return buffer[0]; 01667 } 01668 /** Set interrupt logic level mode. 01669 * @param mode New interrupt mode (0=active-high, 1=active-low) 01670 * @see getInterruptMode() 01671 * @see MPU6050_RA_INT_PIN_CFG 01672 * @see MPU6050_INTCFG_INT_LEVEL_BIT 01673 */ 01674 void MPU6050::setInterruptMode(bool mode) 01675 { 01676 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, mode); 01677 } 01678 /** Get interrupt drive mode. 01679 * Will be set 0 for push-pull, 1 for open-drain. 01680 * @return Current interrupt drive mode (0=push-pull, 1=open-drain) 01681 * @see MPU6050_RA_INT_PIN_CFG 01682 * @see MPU6050_INTCFG_INT_OPEN_BIT 01683 */ 01684 bool MPU6050::getInterruptDrive() 01685 { 01686 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, buffer); 01687 return buffer[0]; 01688 } 01689 /** Set interrupt drive mode. 01690 * @param drive New interrupt drive mode (0=push-pull, 1=open-drain) 01691 * @see getInterruptDrive() 01692 * @see MPU6050_RA_INT_PIN_CFG 01693 * @see MPU6050_INTCFG_INT_OPEN_BIT 01694 */ 01695 void MPU6050::setInterruptDrive(bool drive) 01696 { 01697 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, drive); 01698 } 01699 /** Get interrupt latch mode. 01700 * Will be set 0 for 50us-pulse, 1 for latch-until-int-cleared. 01701 * @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared) 01702 * @see MPU6050_RA_INT_PIN_CFG 01703 * @see MPU6050_INTCFG_LATCH_INT_EN_BIT 01704 */ 01705 bool MPU6050::getInterruptLatch() 01706 { 01707 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, buffer); 01708 return buffer[0]; 01709 } 01710 /** Set interrupt latch mode. 01711 * @param latch New latch mode (0=50us-pulse, 1=latch-until-int-cleared) 01712 * @see getInterruptLatch() 01713 * @see MPU6050_RA_INT_PIN_CFG 01714 * @see MPU6050_INTCFG_LATCH_INT_EN_BIT 01715 */ 01716 void MPU6050::setInterruptLatch(bool latch) 01717 { 01718 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, latch); 01719 } 01720 /** Get interrupt latch clear mode. 01721 * Will be set 0 for status-read-only, 1 for any-register-read. 01722 * @return Current latch clear mode (0=status-read-only, 1=any-register-read) 01723 * @see MPU6050_RA_INT_PIN_CFG 01724 * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT 01725 */ 01726 bool MPU6050::getInterruptLatchClear() 01727 { 01728 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, buffer); 01729 return buffer[0]; 01730 } 01731 /** Set interrupt latch clear mode. 01732 * @param clear New latch clear mode (0=status-read-only, 1=any-register-read) 01733 * @see getInterruptLatchClear() 01734 * @see MPU6050_RA_INT_PIN_CFG 01735 * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT 01736 */ 01737 void MPU6050::setInterruptLatchClear(bool clear) 01738 { 01739 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, clear); 01740 } 01741 /** Get FSYNC interrupt logic level mode. 01742 * @return Current FSYNC interrupt mode (0=active-high, 1=active-low) 01743 * @see getFSyncInterruptMode() 01744 * @see MPU6050_RA_INT_PIN_CFG 01745 * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT 01746 */ 01747 bool MPU6050::getFSyncInterruptLevel() 01748 { 01749 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, buffer); 01750 return buffer[0]; 01751 } 01752 /** Set FSYNC interrupt logic level mode. 01753 * @param mode New FSYNC interrupt mode (0=active-high, 1=active-low) 01754 * @see getFSyncInterruptMode() 01755 * @see MPU6050_RA_INT_PIN_CFG 01756 * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT 01757 */ 01758 void MPU6050::setFSyncInterruptLevel(bool level) 01759 { 01760 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, level); 01761 } 01762 /** Get FSYNC pin interrupt enabled setting. 01763 * Will be set 0 for disabled, 1 for enabled. 01764 * @return Current interrupt enabled setting 01765 * @see MPU6050_RA_INT_PIN_CFG 01766 * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT 01767 */ 01768 bool MPU6050::getFSyncInterruptEnabled() 01769 { 01770 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, buffer); 01771 return buffer[0]; 01772 } 01773 /** Set FSYNC pin interrupt enabled setting. 01774 * @param enabled New FSYNC pin interrupt enabled setting 01775 * @see getFSyncInterruptEnabled() 01776 * @see MPU6050_RA_INT_PIN_CFG 01777 * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT 01778 */ 01779 void MPU6050::setFSyncInterruptEnabled(bool enabled) 01780 { 01781 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, enabled); 01782 } 01783 /** Get I2C bypass enabled status. 01784 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to 01785 * 0, the host application processor will be able to directly access the 01786 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host 01787 * application processor will not be able to directly access the auxiliary I2C 01788 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106 01789 * bit[5]). 01790 * @return Current I2C bypass enabled status 01791 * @see MPU6050_RA_INT_PIN_CFG 01792 * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT 01793 */ 01794 bool MPU6050::getI2CBypassEnabled() 01795 { 01796 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, buffer); 01797 return buffer[0]; 01798 } 01799 /** Set I2C bypass enabled status. 01800 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to 01801 * 0, the host application processor will be able to directly access the 01802 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host 01803 * application processor will not be able to directly access the auxiliary I2C 01804 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106 01805 * bit[5]). 01806 * @param enabled New I2C bypass enabled status 01807 * @see MPU6050_RA_INT_PIN_CFG 01808 * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT 01809 */ 01810 void MPU6050::setI2CBypassEnabled(bool enabled) 01811 { 01812 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, enabled); 01813 } 01814 /** Get reference clock output enabled status. 01815 * When this bit is equal to 1, a reference clock output is provided at the 01816 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For 01817 * further information regarding CLKOUT, please refer to the MPU-60X0 Product 01818 * Specification document. 01819 * @return Current reference clock output enabled status 01820 * @see MPU6050_RA_INT_PIN_CFG 01821 * @see MPU6050_INTCFG_CLKOUT_EN_BIT 01822 */ 01823 bool MPU6050::getClockOutputEnabled() 01824 { 01825 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, buffer); 01826 return buffer[0]; 01827 } 01828 /** Set reference clock output enabled status. 01829 * When this bit is equal to 1, a reference clock output is provided at the 01830 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For 01831 * further information regarding CLKOUT, please refer to the MPU-60X0 Product 01832 * Specification document. 01833 * @param enabled New reference clock output enabled status 01834 * @see MPU6050_RA_INT_PIN_CFG 01835 * @see MPU6050_INTCFG_CLKOUT_EN_BIT 01836 */ 01837 void MPU6050::setClockOutputEnabled(bool enabled) 01838 { 01839 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, enabled); 01840 } 01841 01842 // INT_ENABLE register 01843 01844 /** Get full interrupt enabled status. 01845 * Full register byte for all interrupts, for quick reading. Each bit will be 01846 * set 0 for disabled, 1 for enabled. 01847 * @return Current interrupt enabled status 01848 * @see MPU6050_RA_INT_ENABLE 01849 * @see MPU6050_INTERRUPT_FF_BIT 01850 **/ 01851 uint8_t MPU6050::getIntEnabled() 01852 { 01853 i2Cdev.readByte(devAddr, MPU6050_RA_INT_ENABLE, buffer); 01854 return buffer[0]; 01855 } 01856 /** Set full interrupt enabled status. 01857 * Full register byte for all interrupts, for quick reading. Each bit should be 01858 * set 0 for disabled, 1 for enabled. 01859 * @param enabled New interrupt enabled status 01860 * @see getIntFreefallEnabled() 01861 * @see MPU6050_RA_INT_ENABLE 01862 * @see MPU6050_INTERRUPT_FF_BIT 01863 **/ 01864 void MPU6050::setIntEnabled(uint8_t enabled) 01865 { 01866 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_ENABLE, enabled); 01867 } 01868 /** Get Free Fall interrupt enabled status. 01869 * Will be set 0 for disabled, 1 for enabled. 01870 * @return Current interrupt enabled status 01871 * @see MPU6050_RA_INT_ENABLE 01872 * @see MPU6050_INTERRUPT_FF_BIT 01873 **/ 01874 bool MPU6050::getIntFreefallEnabled() 01875 { 01876 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, buffer); 01877 return buffer[0]; 01878 } 01879 /** Set Free Fall interrupt enabled status. 01880 * @param enabled New interrupt enabled status 01881 * @see getIntFreefallEnabled() 01882 * @see MPU6050_RA_INT_ENABLE 01883 * @see MPU6050_INTERRUPT_FF_BIT 01884 **/ 01885 void MPU6050::setIntFreefallEnabled(bool enabled) 01886 { 01887 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, enabled); 01888 } 01889 /** Get Motion Detection interrupt enabled status. 01890 * Will be set 0 for disabled, 1 for enabled. 01891 * @return Current interrupt enabled status 01892 * @see MPU6050_RA_INT_ENABLE 01893 * @see MPU6050_INTERRUPT_MOT_BIT 01894 **/ 01895 bool MPU6050::getIntMotionEnabled() 01896 { 01897 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, buffer); 01898 return buffer[0]; 01899 } 01900 /** Set Motion Detection interrupt enabled status. 01901 * @param enabled New interrupt enabled status 01902 * @see getIntMotionEnabled() 01903 * @see MPU6050_RA_INT_ENABLE 01904 * @see MPU6050_INTERRUPT_MOT_BIT 01905 **/ 01906 void MPU6050::setIntMotionEnabled(bool enabled) 01907 { 01908 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, enabled); 01909 } 01910 /** Get Zero Motion Detection interrupt enabled status. 01911 * Will be set 0 for disabled, 1 for enabled. 01912 * @return Current interrupt enabled status 01913 * @see MPU6050_RA_INT_ENABLE 01914 * @see MPU6050_INTERRUPT_ZMOT_BIT 01915 **/ 01916 bool MPU6050::getIntZeroMotionEnabled() 01917 { 01918 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, buffer); 01919 return buffer[0]; 01920 } 01921 /** Set Zero Motion Detection interrupt enabled status. 01922 * @param enabled New interrupt enabled status 01923 * @see getIntZeroMotionEnabled() 01924 * @see MPU6050_RA_INT_ENABLE 01925 * @see MPU6050_INTERRUPT_ZMOT_BIT 01926 **/ 01927 void MPU6050::setIntZeroMotionEnabled(bool enabled) 01928 { 01929 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, enabled); 01930 } 01931 /** Get FIFO Buffer Overflow interrupt enabled status. 01932 * Will be set 0 for disabled, 1 for enabled. 01933 * @return Current interrupt enabled status 01934 * @see MPU6050_RA_INT_ENABLE 01935 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT 01936 **/ 01937 bool MPU6050::getIntFIFOBufferOverflowEnabled() 01938 { 01939 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer); 01940 return buffer[0]; 01941 } 01942 /** Set FIFO Buffer Overflow interrupt enabled status. 01943 * @param enabled New interrupt enabled status 01944 * @see getIntFIFOBufferOverflowEnabled() 01945 * @see MPU6050_RA_INT_ENABLE 01946 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT 01947 **/ 01948 void MPU6050::setIntFIFOBufferOverflowEnabled(bool enabled) 01949 { 01950 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, enabled); 01951 } 01952 /** Get I2C Master interrupt enabled status. 01953 * This enables any of the I2C Master interrupt sources to generate an 01954 * interrupt. Will be set 0 for disabled, 1 for enabled. 01955 * @return Current interrupt enabled status 01956 * @see MPU6050_RA_INT_ENABLE 01957 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT 01958 **/ 01959 bool MPU6050::getIntI2CMasterEnabled() 01960 { 01961 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer); 01962 return buffer[0]; 01963 } 01964 /** Set I2C Master interrupt enabled status. 01965 * @param enabled New interrupt enabled status 01966 * @see getIntI2CMasterEnabled() 01967 * @see MPU6050_RA_INT_ENABLE 01968 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT 01969 **/ 01970 void MPU6050::setIntI2CMasterEnabled(bool enabled) 01971 { 01972 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, enabled); 01973 } 01974 /** Get Data Ready interrupt enabled setting. 01975 * This event occurs each time a write operation to all of the sensor registers 01976 * has been completed. Will be set 0 for disabled, 1 for enabled. 01977 * @return Current interrupt enabled status 01978 * @see MPU6050_RA_INT_ENABLE 01979 * @see MPU6050_INTERRUPT_DATA_RDY_BIT 01980 */ 01981 bool MPU6050::getIntDataReadyEnabled() 01982 { 01983 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer); 01984 return buffer[0]; 01985 } 01986 /** Set Data Ready interrupt enabled status. 01987 * @param enabled New interrupt enabled status 01988 * @see getIntDataReadyEnabled() 01989 * @see MPU6050_RA_INT_CFG 01990 * @see MPU6050_INTERRUPT_DATA_RDY_BIT 01991 */ 01992 void MPU6050::setIntDataReadyEnabled(bool enabled) 01993 { 01994 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, enabled); 01995 } 01996 01997 // INT_STATUS register 01998 01999 /** Get full set of interrupt status bits. 02000 * These bits clear to 0 after the register has been read. Very useful 02001 * for getting multiple INT statuses, since each single bit read clears 02002 * all of them because it has to read the whole byte. 02003 * @return Current interrupt status 02004 * @see MPU6050_RA_INT_STATUS 02005 */ 02006 uint8_t MPU6050::getIntStatus() 02007 { 02008 i2Cdev.readByte(devAddr, MPU6050_RA_INT_STATUS, buffer); 02009 return buffer[0]; 02010 } 02011 /** Get Free Fall interrupt status. 02012 * This bit automatically sets to 1 when a Free Fall interrupt has been 02013 * generated. The bit clears to 0 after the register has been read. 02014 * @return Current interrupt status 02015 * @see MPU6050_RA_INT_STATUS 02016 * @see MPU6050_INTERRUPT_FF_BIT 02017 */ 02018 bool MPU6050::getIntFreefallStatus() 02019 { 02020 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FF_BIT, buffer); 02021 return buffer[0]; 02022 } 02023 /** Get Motion Detection interrupt status. 02024 * This bit automatically sets to 1 when a Motion Detection interrupt has been 02025 * generated. The bit clears to 0 after the register has been read. 02026 * @return Current interrupt status 02027 * @see MPU6050_RA_INT_STATUS 02028 * @see MPU6050_INTERRUPT_MOT_BIT 02029 */ 02030 bool MPU6050::getIntMotionStatus() 02031 { 02032 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_MOT_BIT, buffer); 02033 return buffer[0]; 02034 } 02035 /** Get Zero Motion Detection interrupt status. 02036 * This bit automatically sets to 1 when a Zero Motion Detection interrupt has 02037 * been generated. The bit clears to 0 after the register has been read. 02038 * @return Current interrupt status 02039 * @see MPU6050_RA_INT_STATUS 02040 * @see MPU6050_INTERRUPT_ZMOT_BIT 02041 */ 02042 bool MPU6050::getIntZeroMotionStatus() 02043 { 02044 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_ZMOT_BIT, buffer); 02045 return buffer[0]; 02046 } 02047 /** Get FIFO Buffer Overflow interrupt status. 02048 * This bit automatically sets to 1 when a Free Fall interrupt has been 02049 * generated. The bit clears to 0 after the register has been read. 02050 * @return Current interrupt status 02051 * @see MPU6050_RA_INT_STATUS 02052 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT 02053 */ 02054 bool MPU6050::getIntFIFOBufferOverflowStatus() 02055 { 02056 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer); 02057 return buffer[0]; 02058 } 02059 /** Get I2C Master interrupt status. 02060 * This bit automatically sets to 1 when an I2C Master interrupt has been 02061 * generated. For a list of I2C Master interrupts, please refer to Register 54. 02062 * The bit clears to 0 after the register has been read. 02063 * @return Current interrupt status 02064 * @see MPU6050_RA_INT_STATUS 02065 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT 02066 */ 02067 bool MPU6050::getIntI2CMasterStatus() 02068 { 02069 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer); 02070 return buffer[0]; 02071 } 02072 /** Get Data Ready interrupt status. 02073 * This bit automatically sets to 1 when a Data Ready interrupt has been 02074 * generated. The bit clears to 0 after the register has been read. 02075 * @return Current interrupt status 02076 * @see MPU6050_RA_INT_STATUS 02077 * @see MPU6050_INTERRUPT_DATA_RDY_BIT 02078 */ 02079 bool MPU6050::getIntDataReadyStatus() 02080 { 02081 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer); 02082 return buffer[0]; 02083 } 02084 02085 // ACCEL_*OUT_* registers 02086 02087 /** Get raw 9-axis motion sensor readings (accel/gyro/compass). 02088 * FUNCTION NOT FULLY IMPLEMENTED YET. 02089 * @param ax 16-bit signed integer container for accelerometer X-axis value 02090 * @param ay 16-bit signed integer container for accelerometer Y-axis value 02091 * @param az 16-bit signed integer container for accelerometer Z-axis value 02092 * @param gx 16-bit signed integer container for gyroscope X-axis value 02093 * @param gy 16-bit signed integer container for gyroscope Y-axis value 02094 * @param gz 16-bit signed integer container for gyroscope Z-axis value 02095 * @param mx 16-bit signed integer container for magnetometer X-axis value 02096 * @param my 16-bit signed integer container for magnetometer Y-axis value 02097 * @param mz 16-bit signed integer container for magnetometer Z-axis value 02098 * @see getMotion6() 02099 * @see getAcceleration() 02100 * @see getRotation() 02101 * @see MPU6050_RA_ACCEL_XOUT_H 02102 */ 02103 void MPU6050::getMotion9(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz, int16_t* mx, int16_t* my, int16_t* mz) 02104 { 02105 getMotion6(ax, ay, az, gx, gy, gz); 02106 // TODO: magnetometer integration 02107 } 02108 /** Get raw 6-axis motion sensor readings (accel/gyro). 02109 * Retrieves all currently available motion sensor values. 02110 * @param ax 16-bit signed integer container for accelerometer X-axis value 02111 * @param ay 16-bit signed integer container for accelerometer Y-axis value 02112 * @param az 16-bit signed integer container for accelerometer Z-axis value 02113 * @param gx 16-bit signed integer container for gyroscope X-axis value 02114 * @param gy 16-bit signed integer container for gyroscope Y-axis value 02115 * @param gz 16-bit signed integer container for gyroscope Z-axis value 02116 * @see getAcceleration() 02117 * @see getRotation() 02118 * @see MPU6050_RA_ACCEL_XOUT_H 02119 */ 02120 void MPU6050::getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz) 02121 { 02122 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 14, buffer); 02123 *ax = (((int16_t)buffer[0]) << 8) | buffer[1]; 02124 *ay = (((int16_t)buffer[2]) << 8) | buffer[3]; 02125 *az = (((int16_t)buffer[4]) << 8) | buffer[5]; 02126 *gx = (((int16_t)buffer[8]) << 8) | buffer[9]; 02127 *gy = (((int16_t)buffer[10]) << 8) | buffer[11]; 02128 *gz = (((int16_t)buffer[12]) << 8) | buffer[13]; 02129 } 02130 /** Get 3-axis accelerometer readings. 02131 * These registers store the most recent accelerometer measurements. 02132 * Accelerometer measurements are written to these registers at the Sample Rate 02133 * as defined in Register 25. 02134 * 02135 * The accelerometer measurement registers, along with the temperature 02136 * measurement registers, gyroscope measurement registers, and external sensor 02137 * data registers, are composed of two sets of registers: an internal register 02138 * set and a user-facing read register set. 02139 * 02140 * The data within the accelerometer sensors' internal register set is always 02141 * updated at the Sample Rate. Meanwhile, the user-facing read register set 02142 * duplicates the internal register set's data values whenever the serial 02143 * interface is idle. This guarantees that a burst read of sensor registers will 02144 * read measurements from the same sampling instant. Note that if burst reads 02145 * are not used, the user is responsible for ensuring a set of single byte reads 02146 * correspond to a single sampling instant by checking the Data Ready interrupt. 02147 * 02148 * Each 16-bit accelerometer measurement has a full scale defined in ACCEL_FS 02149 * (Register 28). For each full scale setting, the accelerometers' sensitivity 02150 * per LSB in ACCEL_xOUT is shown in the table below: 02151 * 02152 * <pre> 02153 * AFS_SEL | Full Scale Range | LSB Sensitivity 02154 * --------+------------------+---------------- 02155 * 0 | +/- 2g | 8192 LSB/mg 02156 * 1 | +/- 4g | 4096 LSB/mg 02157 * 2 | +/- 8g | 2048 LSB/mg 02158 * 3 | +/- 16g | 1024 LSB/mg 02159 * </pre> 02160 * 02161 * @param x 16-bit signed integer container for X-axis acceleration 02162 * @param y 16-bit signed integer container for Y-axis acceleration 02163 * @param z 16-bit signed integer container for Z-axis acceleration 02164 * @see MPU6050_RA_GYRO_XOUT_H 02165 */ 02166 void MPU6050::getAcceleration(int16_t* x, int16_t* y, int16_t* z) 02167 { 02168 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 6, buffer); 02169 *x = (((int16_t)buffer[0]) << 8) | buffer[1]; 02170 *y = (((int16_t)buffer[2]) << 8) | buffer[3]; 02171 *z = (((int16_t)buffer[4]) << 8) | buffer[5]; 02172 } 02173 /** Get X-axis accelerometer reading. 02174 * @return X-axis acceleration measurement in 16-bit 2's complement format 02175 * @see getMotion6() 02176 * @see MPU6050_RA_ACCEL_XOUT_H 02177 */ 02178 int16_t MPU6050::getAccelerationX() 02179 { 02180 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 2, buffer); 02181 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02182 } 02183 /** Get Y-axis accelerometer reading. 02184 * @return Y-axis acceleration measurement in 16-bit 2's complement format 02185 * @see getMotion6() 02186 * @see MPU6050_RA_ACCEL_YOUT_H 02187 */ 02188 int16_t MPU6050::getAccelerationY() 02189 { 02190 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_YOUT_H, 2, buffer); 02191 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02192 } 02193 /** Get Z-axis accelerometer reading. 02194 * @return Z-axis acceleration measurement in 16-bit 2's complement format 02195 * @see getMotion6() 02196 * @see MPU6050_RA_ACCEL_ZOUT_H 02197 */ 02198 int16_t MPU6050::getAccelerationZ() 02199 { 02200 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_ZOUT_H, 2, buffer); 02201 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02202 } 02203 02204 // TEMP_OUT_* registers 02205 02206 /** Get current internal temperature. 02207 * @return Temperature reading in 16-bit 2's complement format 02208 * @see MPU6050_RA_TEMP_OUT_H 02209 */ 02210 int16_t MPU6050::getTemperature() 02211 { 02212 i2Cdev.readBytes(devAddr, MPU6050_RA_TEMP_OUT_H, 2, buffer); 02213 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02214 } 02215 02216 // GYRO_*OUT_* registers 02217 02218 /** Get 3-axis gyroscope readings. 02219 * These gyroscope measurement registers, along with the accelerometer 02220 * measurement registers, temperature measurement registers, and external sensor 02221 * data registers, are composed of two sets of registers: an internal register 02222 * set and a user-facing read register set. 02223 * The data within the gyroscope sensors' internal register set is always 02224 * updated at the Sample Rate. Meanwhile, the user-facing read register set 02225 * duplicates the internal register set's data values whenever the serial 02226 * interface is idle. This guarantees that a burst read of sensor registers will 02227 * read measurements from the same sampling instant. Note that if burst reads 02228 * are not used, the user is responsible for ensuring a set of single byte reads 02229 * correspond to a single sampling instant by checking the Data Ready interrupt. 02230 * 02231 * Each 16-bit gyroscope measurement has a full scale defined in FS_SEL 02232 * (Register 27). For each full scale setting, the gyroscopes' sensitivity per 02233 * LSB in GYRO_xOUT is shown in the table below: 02234 * 02235 * <pre> 02236 * FS_SEL | Full Scale Range | LSB Sensitivity 02237 * -------+--------------------+---------------- 02238 * 0 | +/- 250 degrees/s | 131 LSB/deg/s 02239 * 1 | +/- 500 degrees/s | 65.5 LSB/deg/s 02240 * 2 | +/- 1000 degrees/s | 32.8 LSB/deg/s 02241 * 3 | +/- 2000 degrees/s | 16.4 LSB/deg/s 02242 * </pre> 02243 * 02244 * @param x 16-bit signed integer container for X-axis rotation 02245 * @param y 16-bit signed integer container for Y-axis rotation 02246 * @param z 16-bit signed integer container for Z-axis rotation 02247 * @see getMotion6() 02248 * @see MPU6050_RA_GYRO_XOUT_H 02249 */ 02250 void MPU6050::getRotation(int16_t* x, int16_t* y, int16_t* z) 02251 { 02252 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 6, buffer); 02253 *x = (((int16_t)buffer[0]) << 8) | buffer[1]; 02254 *y = (((int16_t)buffer[2]) << 8) | buffer[3]; 02255 *z = (((int16_t)buffer[4]) << 8) | buffer[5]; 02256 } 02257 /** Get X-axis gyroscope reading. 02258 * @return X-axis rotation measurement in 16-bit 2's complement format 02259 * @see getMotion6() 02260 * @see MPU6050_RA_GYRO_XOUT_H 02261 */ 02262 int16_t MPU6050::getRotationX() 02263 { 02264 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 2, buffer); 02265 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02266 } 02267 /** Get Y-axis gyroscope reading. 02268 * @return Y-axis rotation measurement in 16-bit 2's complement format 02269 * @see getMotion6() 02270 * @see MPU6050_RA_GYRO_YOUT_H 02271 */ 02272 int16_t MPU6050::getRotationY() 02273 { 02274 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_YOUT_H, 2, buffer); 02275 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02276 } 02277 /** Get Z-axis gyroscope reading. 02278 * @return Z-axis rotation measurement in 16-bit 2's complement format 02279 * @see getMotion6() 02280 * @see MPU6050_RA_GYRO_ZOUT_H 02281 */ 02282 int16_t MPU6050::getRotationZ() 02283 { 02284 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_ZOUT_H, 2, buffer); 02285 return (((int16_t)buffer[0]) << 8) | buffer[1]; 02286 } 02287 02288 // EXT_SENS_DATA_* registers 02289 02290 /** Read single byte from external sensor data register. 02291 * These registers store data read from external sensors by the Slave 0, 1, 2, 02292 * and 3 on the auxiliary I2C interface. Data read by Slave 4 is stored in 02293 * I2C_SLV4_DI (Register 53). 02294 * 02295 * External sensor data is written to these registers at the Sample Rate as 02296 * defined in Register 25. This access rate can be reduced by using the Slave 02297 * Delay Enable registers (Register 103). 02298 * 02299 * External sensor data registers, along with the gyroscope measurement 02300 * registers, accelerometer measurement registers, and temperature measurement 02301 * registers, are composed of two sets of registers: an internal register set 02302 * and a user-facing read register set. 02303 * 02304 * The data within the external sensors' internal register set is always updated 02305 * at the Sample Rate (or the reduced access rate) whenever the serial interface 02306 * is idle. This guarantees that a burst read of sensor registers will read 02307 * measurements from the same sampling instant. Note that if burst reads are not 02308 * used, the user is responsible for ensuring a set of single byte reads 02309 * correspond to a single sampling instant by checking the Data Ready interrupt. 02310 * 02311 * Data is placed in these external sensor data registers according to 02312 * I2C_SLV0_CTRL, I2C_SLV1_CTRL, I2C_SLV2_CTRL, and I2C_SLV3_CTRL (Registers 39, 02313 * 42, 45, and 48). When more than zero bytes are read (I2C_SLVx_LEN > 0) from 02314 * an enabled slave (I2C_SLVx_EN = 1), the slave is read at the Sample Rate (as 02315 * defined in Register 25) or delayed rate (if specified in Register 52 and 02316 * 103). During each Sample cycle, slave reads are performed in order of Slave 02317 * number. If all slaves are enabled with more than zero bytes to be read, the 02318 * order will be Slave 0, followed by Slave 1, Slave 2, and Slave 3. 02319 * 02320 * Each enabled slave will have EXT_SENS_DATA registers associated with it by 02321 * number of bytes read (I2C_SLVx_LEN) in order of slave number, starting from 02322 * EXT_SENS_DATA_00. Note that this means enabling or disabling a slave may 02323 * change the higher numbered slaves' associated registers. Furthermore, if 02324 * fewer total bytes are being read from the external sensors as a result of 02325 * such a change, then the data remaining in the registers which no longer have 02326 * an associated slave device (i.e. high numbered registers) will remain in 02327 * these previously allocated registers unless reset. 02328 * 02329 * If the sum of the read lengths of all SLVx transactions exceed the number of 02330 * available EXT_SENS_DATA registers, the excess bytes will be dropped. There 02331 * are 24 EXT_SENS_DATA registers and hence the total read lengths between all 02332 * the slaves cannot be greater than 24 or some bytes will be lost. 02333 * 02334 * Note: Slave 4's behavior is distinct from that of Slaves 0-3. For further 02335 * information regarding the characteristics of Slave 4, please refer to 02336 * Registers 49 to 53. 02337 * 02338 * EXAMPLE: 02339 * Suppose that Slave 0 is enabled with 4 bytes to be read (I2C_SLV0_EN = 1 and 02340 * I2C_SLV0_LEN = 4) while Slave 1 is enabled with 2 bytes to be read so that 02341 * I2C_SLV1_EN = 1 and I2C_SLV1_LEN = 2. In such a situation, EXT_SENS_DATA _00 02342 * through _03 will be associated with Slave 0, while EXT_SENS_DATA _04 and 05 02343 * will be associated with Slave 1. If Slave 2 is enabled as well, registers 02344 * starting from EXT_SENS_DATA_06 will be allocated to Slave 2. 02345 * 02346 * If Slave 2 is disabled while Slave 3 is enabled in this same situation, then 02347 * registers starting from EXT_SENS_DATA_06 will be allocated to Slave 3 02348 * instead. 02349 * 02350 * REGISTER ALLOCATION FOR DYNAMIC DISABLE VS. NORMAL DISABLE: 02351 * If a slave is disabled at any time, the space initially allocated to the 02352 * slave in the EXT_SENS_DATA register, will remain associated with that slave. 02353 * This is to avoid dynamic adjustment of the register allocation. 02354 * 02355 * The allocation of the EXT_SENS_DATA registers is recomputed only when (1) all 02356 * slaves are disabled, or (2) the I2C_MST_RST bit is set (Register 106). 02357 * 02358 * This above is also true if one of the slaves gets NACKed and stops 02359 * functioning. 02360 * 02361 * @param position Starting position (0-23) 02362 * @return Byte read from register 02363 */ 02364 uint8_t MPU6050::getExternalSensorByte(int position) 02365 { 02366 i2Cdev.readByte(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, buffer); 02367 return buffer[0]; 02368 } 02369 /** Read word (2 bytes) from external sensor data registers. 02370 * @param position Starting position (0-21) 02371 * @return Word read from register 02372 * @see getExternalSensorByte() 02373 */ 02374 uint16_t MPU6050::getExternalSensorWord(int position) 02375 { 02376 i2Cdev.readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 2, buffer); 02377 return (((uint16_t)buffer[0]) << 8) | buffer[1]; 02378 } 02379 /** Read double word (4 bytes) from external sensor data registers. 02380 * @param position Starting position (0-20) 02381 * @return Double word read from registers 02382 * @see getExternalSensorByte() 02383 */ 02384 uint32_t MPU6050::getExternalSensorDWord(int position) 02385 { 02386 i2Cdev.readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 4, buffer); 02387 return (((uint32_t)buffer[0]) << 24) | (((uint32_t)buffer[1]) << 16) | (((uint16_t)buffer[2]) << 8) | buffer[3]; 02388 } 02389 02390 // MOT_DETECT_STATUS register 02391 02392 /** Get X-axis negative motion detection interrupt status. 02393 * @return Motion detection status 02394 * @see MPU6050_RA_MOT_DETECT_STATUS 02395 * @see MPU6050_MOTION_MOT_XNEG_BIT 02396 */ 02397 bool MPU6050::getXNegMotionDetected() 02398 { 02399 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XNEG_BIT, buffer); 02400 return buffer[0]; 02401 } 02402 /** Get X-axis positive motion detection interrupt status. 02403 * @return Motion detection status 02404 * @see MPU6050_RA_MOT_DETECT_STATUS 02405 * @see MPU6050_MOTION_MOT_XPOS_BIT 02406 */ 02407 bool MPU6050::getXPosMotionDetected() 02408 { 02409 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XPOS_BIT, buffer); 02410 return buffer[0]; 02411 } 02412 /** Get Y-axis negative motion detection interrupt status. 02413 * @return Motion detection status 02414 * @see MPU6050_RA_MOT_DETECT_STATUS 02415 * @see MPU6050_MOTION_MOT_YNEG_BIT 02416 */ 02417 bool MPU6050::getYNegMotionDetected() 02418 { 02419 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YNEG_BIT, buffer); 02420 return buffer[0]; 02421 } 02422 /** Get Y-axis positive motion detection interrupt status. 02423 * @return Motion detection status 02424 * @see MPU6050_RA_MOT_DETECT_STATUS 02425 * @see MPU6050_MOTION_MOT_YPOS_BIT 02426 */ 02427 bool MPU6050::getYPosMotionDetected() 02428 { 02429 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YPOS_BIT, buffer); 02430 return buffer[0]; 02431 } 02432 /** Get Z-axis negative motion detection interrupt status. 02433 * @return Motion detection status 02434 * @see MPU6050_RA_MOT_DETECT_STATUS 02435 * @see MPU6050_MOTION_MOT_ZNEG_BIT 02436 */ 02437 bool MPU6050::getZNegMotionDetected() 02438 { 02439 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZNEG_BIT, buffer); 02440 return buffer[0]; 02441 } 02442 /** Get Z-axis positive motion detection interrupt status. 02443 * @return Motion detection status 02444 * @see MPU6050_RA_MOT_DETECT_STATUS 02445 * @see MPU6050_MOTION_MOT_ZPOS_BIT 02446 */ 02447 bool MPU6050::getZPosMotionDetected() 02448 { 02449 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZPOS_BIT, buffer); 02450 return buffer[0]; 02451 } 02452 /** Get zero motion detection interrupt status. 02453 * @return Motion detection status 02454 * @see MPU6050_RA_MOT_DETECT_STATUS 02455 * @see MPU6050_MOTION_MOT_ZRMOT_BIT 02456 */ 02457 bool MPU6050::getZeroMotionDetected() 02458 { 02459 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZRMOT_BIT, buffer); 02460 return buffer[0]; 02461 } 02462 02463 // I2C_SLV*_DO register 02464 02465 /** Write byte to Data Output container for specified slave. 02466 * This register holds the output data written into Slave when Slave is set to 02467 * write mode. For further information regarding Slave control, please 02468 * refer to Registers 37 to 39 and immediately following. 02469 * @param num Slave number (0-3) 02470 * @param data Byte to write 02471 * @see MPU6050_RA_I2C_SLV0_DO 02472 */ 02473 void MPU6050::setSlaveOutputByte(uint8_t num, uint8_t data) 02474 { 02475 if (num > 3) return; 02476 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_DO + num, data); 02477 } 02478 02479 // I2C_MST_DELAY_CTRL register 02480 02481 /** Get external data shadow delay enabled status. 02482 * This register is used to specify the timing of external sensor data 02483 * shadowing. When DELAY_ES_SHADOW is set to 1, shadowing of external 02484 * sensor data is delayed until all data has been received. 02485 * @return Current external data shadow delay enabled status. 02486 * @see MPU6050_RA_I2C_MST_DELAY_CTRL 02487 * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT 02488 */ 02489 bool MPU6050::getExternalShadowDelayEnabled() 02490 { 02491 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, buffer); 02492 return buffer[0]; 02493 } 02494 /** Set external data shadow delay enabled status. 02495 * @param enabled New external data shadow delay enabled status. 02496 * @see getExternalShadowDelayEnabled() 02497 * @see MPU6050_RA_I2C_MST_DELAY_CTRL 02498 * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT 02499 */ 02500 void MPU6050::setExternalShadowDelayEnabled(bool enabled) 02501 { 02502 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, enabled); 02503 } 02504 /** Get slave delay enabled status. 02505 * When a particular slave delay is enabled, the rate of access for the that 02506 * slave device is reduced. When a slave's access rate is decreased relative to 02507 * the Sample Rate, the slave is accessed every: 02508 * 02509 * 1 / (1 + I2C_MST_DLY) Samples 02510 * 02511 * This base Sample Rate in turn is determined by SMPLRT_DIV (register * 25) 02512 * and DLPF_CFG (register 26). 02513 * 02514 * For further information regarding I2C_MST_DLY, please refer to register 52. 02515 * For further information regarding the Sample Rate, please refer to register 25. 02516 * 02517 * @param num Slave number (0-4) 02518 * @return Current slave delay enabled status. 02519 * @see MPU6050_RA_I2C_MST_DELAY_CTRL 02520 * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT 02521 */ 02522 bool MPU6050::getSlaveDelayEnabled(uint8_t num) 02523 { 02524 // MPU6050_DELAYCTRL_I2C_SLV4_DLY_EN_BIT is 4, SLV3 is 3, etc. 02525 if (num > 4) return 0; 02526 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, buffer); 02527 return buffer[0]; 02528 } 02529 /** Set slave delay enabled status. 02530 * @param num Slave number (0-4) 02531 * @param enabled New slave delay enabled status. 02532 * @see MPU6050_RA_I2C_MST_DELAY_CTRL 02533 * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT 02534 */ 02535 void MPU6050::setSlaveDelayEnabled(uint8_t num, bool enabled) 02536 { 02537 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, enabled); 02538 } 02539 02540 // SIGNAL_PATH_RESET register 02541 02542 /** Reset gyroscope signal path. 02543 * The reset will revert the signal path analog to digital converters and 02544 * filters to their power up configurations. 02545 * @see MPU6050_RA_SIGNAL_PATH_RESET 02546 * @see MPU6050_PATHRESET_GYRO_RESET_BIT 02547 */ 02548 void MPU6050::resetGyroscopePath() 02549 { 02550 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_GYRO_RESET_BIT, true); 02551 } 02552 /** Reset accelerometer signal path. 02553 * The reset will revert the signal path analog to digital converters and 02554 * filters to their power up configurations. 02555 * @see MPU6050_RA_SIGNAL_PATH_RESET 02556 * @see MPU6050_PATHRESET_ACCEL_RESET_BIT 02557 */ 02558 void MPU6050::resetAccelerometerPath() 02559 { 02560 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_ACCEL_RESET_BIT, true); 02561 } 02562 /** Reset temperature sensor signal path. 02563 * The reset will revert the signal path analog to digital converters and 02564 * filters to their power up configurations. 02565 * @see MPU6050_RA_SIGNAL_PATH_RESET 02566 * @see MPU6050_PATHRESET_TEMP_RESET_BIT 02567 */ 02568 void MPU6050::resetTemperaturePath() 02569 { 02570 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_TEMP_RESET_BIT, true); 02571 } 02572 02573 // MOT_DETECT_CTRL register 02574 02575 /** Get accelerometer power-on delay. 02576 * The accelerometer data path provides samples to the sensor registers, Motion 02577 * detection, Zero Motion detection, and Free Fall detection modules. The 02578 * signal path contains filters which must be flushed on wake-up with new 02579 * samples before the detection modules begin operations. The default wake-up 02580 * delay, of 4ms can be lengthened by up to 3ms. This additional delay is 02581 * specified in ACCEL_ON_DELAY in units of 1 LSB = 1 ms. The user may select 02582 * any value above zero unless instructed otherwise by InvenSense. Please refer 02583 * to Section 8 of the MPU-6000/MPU-6050 Product Specification document for 02584 * further information regarding the detection modules. 02585 * @return Current accelerometer power-on delay 02586 * @see MPU6050_RA_MOT_DETECT_CTRL 02587 * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT 02588 */ 02589 uint8_t MPU6050::getAccelerometerPowerOnDelay() 02590 { 02591 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, buffer); 02592 return buffer[0]; 02593 } 02594 /** Set accelerometer power-on delay. 02595 * @param delay New accelerometer power-on delay (0-3) 02596 * @see getAccelerometerPowerOnDelay() 02597 * @see MPU6050_RA_MOT_DETECT_CTRL 02598 * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT 02599 */ 02600 void MPU6050::setAccelerometerPowerOnDelay(uint8_t delay) 02601 { 02602 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, delay); 02603 } 02604 /** Get Free Fall detection counter decrement configuration. 02605 * Detection is registered by the Free Fall detection module after accelerometer 02606 * measurements meet their respective threshold conditions over a specified 02607 * number of samples. When the threshold conditions are met, the corresponding 02608 * detection counter increments by 1. The user may control the rate at which the 02609 * detection counter decrements when the threshold condition is not met by 02610 * configuring FF_COUNT. The decrement rate can be set according to the 02611 * following table: 02612 * 02613 * <pre> 02614 * FF_COUNT | Counter Decrement 02615 * ---------+------------------ 02616 * 0 | Reset 02617 * 1 | 1 02618 * 2 | 2 02619 * 3 | 4 02620 * </pre> 02621 * 02622 * When FF_COUNT is configured to 0 (reset), any non-qualifying sample will 02623 * reset the counter to 0. For further information on Free Fall detection, 02624 * please refer to Registers 29 to 32. 02625 * 02626 * @return Current decrement configuration 02627 * @see MPU6050_RA_MOT_DETECT_CTRL 02628 * @see MPU6050_DETECT_FF_COUNT_BIT 02629 */ 02630 uint8_t MPU6050::getFreefallDetectionCounterDecrement() 02631 { 02632 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, buffer); 02633 return buffer[0]; 02634 } 02635 /** Set Free Fall detection counter decrement configuration. 02636 * @param decrement New decrement configuration value 02637 * @see getFreefallDetectionCounterDecrement() 02638 * @see MPU6050_RA_MOT_DETECT_CTRL 02639 * @see MPU6050_DETECT_FF_COUNT_BIT 02640 */ 02641 void MPU6050::setFreefallDetectionCounterDecrement(uint8_t decrement) 02642 { 02643 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, decrement); 02644 } 02645 /** Get Motion detection counter decrement configuration. 02646 * Detection is registered by the Motion detection module after accelerometer 02647 * measurements meet their respective threshold conditions over a specified 02648 * number of samples. When the threshold conditions are met, the corresponding 02649 * detection counter increments by 1. The user may control the rate at which the 02650 * detection counter decrements when the threshold condition is not met by 02651 * configuring MOT_COUNT. The decrement rate can be set according to the 02652 * following table: 02653 * 02654 * <pre> 02655 * MOT_COUNT | Counter Decrement 02656 * ----------+------------------ 02657 * 0 | Reset 02658 * 1 | 1 02659 * 2 | 2 02660 * 3 | 4 02661 * </pre> 02662 * 02663 * When MOT_COUNT is configured to 0 (reset), any non-qualifying sample will 02664 * reset the counter to 0. For further information on Motion detection, 02665 * please refer to Registers 29 to 32. 02666 * 02667 */ 02668 uint8_t MPU6050::getMotionDetectionCounterDecrement() 02669 { 02670 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, buffer); 02671 return buffer[0]; 02672 } 02673 /** Set Motion detection counter decrement configuration. 02674 * @param decrement New decrement configuration value 02675 * @see getMotionDetectionCounterDecrement() 02676 * @see MPU6050_RA_MOT_DETECT_CTRL 02677 * @see MPU6050_DETECT_MOT_COUNT_BIT 02678 */ 02679 void MPU6050::setMotionDetectionCounterDecrement(uint8_t decrement) 02680 { 02681 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, decrement); 02682 } 02683 02684 // USER_CTRL register 02685 02686 /** Get FIFO enabled status. 02687 * When this bit is set to 0, the FIFO buffer is disabled. The FIFO buffer 02688 * cannot be written to or read from while disabled. The FIFO buffer's state 02689 * does not change unless the MPU-60X0 is power cycled. 02690 * @return Current FIFO enabled status 02691 * @see MPU6050_RA_USER_CTRL 02692 * @see MPU6050_USERCTRL_FIFO_EN_BIT 02693 */ 02694 bool MPU6050::getFIFOEnabled() 02695 { 02696 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, buffer); 02697 return buffer[0]; 02698 } 02699 /** Set FIFO enabled status. 02700 * @param enabled New FIFO enabled status 02701 * @see getFIFOEnabled() 02702 * @see MPU6050_RA_USER_CTRL 02703 * @see MPU6050_USERCTRL_FIFO_EN_BIT 02704 */ 02705 void MPU6050::setFIFOEnabled(bool enabled) 02706 { 02707 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, enabled); 02708 } 02709 /** Get I2C Master Mode enabled status. 02710 * When this mode is enabled, the MPU-60X0 acts as the I2C Master to the 02711 * external sensor slave devices on the auxiliary I2C bus. When this bit is 02712 * cleared to 0, the auxiliary I2C bus lines (AUX_DA and AUX_CL) are logically 02713 * driven by the primary I2C bus (SDA and SCL). This is a precondition to 02714 * enabling Bypass Mode. For further information regarding Bypass Mode, please 02715 * refer to Register 55. 02716 * @return Current I2C Master Mode enabled status 02717 * @see MPU6050_RA_USER_CTRL 02718 * @see MPU6050_USERCTRL_I2C_MST_EN_BIT 02719 */ 02720 bool MPU6050::getI2CMasterModeEnabled() 02721 { 02722 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, buffer); 02723 return buffer[0]; 02724 } 02725 /** Set I2C Master Mode enabled status. 02726 * @param enabled New I2C Master Mode enabled status 02727 * @see getI2CMasterModeEnabled() 02728 * @see MPU6050_RA_USER_CTRL 02729 * @see MPU6050_USERCTRL_I2C_MST_EN_BIT 02730 */ 02731 void MPU6050::setI2CMasterModeEnabled(bool enabled) 02732 { 02733 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, enabled); 02734 } 02735 /** Switch from I2C to SPI mode (MPU-6000 only) 02736 * If this is set, the primary SPI interface will be enabled in place of the 02737 * disabled primary I2C interface. 02738 */ 02739 void MPU6050::switchSPIEnabled(bool enabled) 02740 { 02741 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_IF_DIS_BIT, enabled); 02742 } 02743 /** Reset the FIFO. 02744 * This bit resets the FIFO buffer when set to 1 while FIFO_EN equals 0. This 02745 * bit automatically clears to 0 after the reset has been triggered. 02746 * @see MPU6050_RA_USER_CTRL 02747 * @see MPU6050_USERCTRL_FIFO_RESET_BIT 02748 */ 02749 void MPU6050::resetFIFO() 02750 { 02751 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_RESET_BIT, true); 02752 } 02753 /** Reset the I2C Master. 02754 * This bit resets the I2C Master when set to 1 while I2C_MST_EN equals 0. 02755 * This bit automatically clears to 0 after the reset has been triggered. 02756 * @see MPU6050_RA_USER_CTRL 02757 * @see MPU6050_USERCTRL_I2C_MST_RESET_BIT 02758 */ 02759 void MPU6050::resetI2CMaster() 02760 { 02761 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_RESET_BIT, true); 02762 } 02763 /** Reset all sensor registers and signal paths. 02764 * When set to 1, this bit resets the signal paths for all sensors (gyroscopes, 02765 * accelerometers, and temperature sensor). This operation will also clear the 02766 * sensor registers. This bit automatically clears to 0 after the reset has been 02767 * triggered. 02768 * 02769 * When resetting only the signal path (and not the sensor registers), please 02770 * use Register 104, SIGNAL_PATH_RESET. 02771 * 02772 * @see MPU6050_RA_USER_CTRL 02773 * @see MPU6050_USERCTRL_SIG_COND_RESET_BIT 02774 */ 02775 void MPU6050::resetSensors() 02776 { 02777 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_SIG_COND_RESET_BIT, true); 02778 } 02779 02780 // PWR_MGMT_1 register 02781 02782 /** Trigger a full device reset. 02783 * A small delay of ~50ms may be desirable after triggering a reset. 02784 * @see MPU6050_RA_PWR_MGMT_1 02785 * @see MPU6050_PWR1_DEVICE_RESET_BIT 02786 */ 02787 void MPU6050::reset() 02788 { 02789 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_DEVICE_RESET_BIT, true); 02790 } 02791 /** Get sleep mode status. 02792 * Setting the SLEEP bit in the register puts the device into very low power 02793 * sleep mode. In this mode, only the serial interface and internal registers 02794 * remain active, allowing for a very low standby current. Clearing this bit 02795 * puts the device back into normal mode. To save power, the individual standby 02796 * selections for each of the gyros should be used if any gyro axis is not used 02797 * by the application. 02798 * @return Current sleep mode enabled status 02799 * @see MPU6050_RA_PWR_MGMT_1 02800 * @see MPU6050_PWR1_SLEEP_BIT 02801 */ 02802 bool MPU6050::getSleepEnabled() 02803 { 02804 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, buffer); 02805 return buffer[0]; 02806 } 02807 /** Set sleep mode status. 02808 * @param enabled New sleep mode enabled status 02809 * @see getSleepEnabled() 02810 * @see MPU6050_RA_PWR_MGMT_1 02811 * @see MPU6050_PWR1_SLEEP_BIT 02812 */ 02813 void MPU6050::setSleepEnabled(bool enabled) 02814 { 02815 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, enabled); 02816 } 02817 /** Get wake cycle enabled status. 02818 * When this bit is set to 1 and SLEEP is disabled, the MPU-60X0 will cycle 02819 * between sleep mode and waking up to take a single sample of data from active 02820 * sensors at a rate determined by LP_WAKE_CTRL (register 108). 02821 * @return Current sleep mode enabled status 02822 * @see MPU6050_RA_PWR_MGMT_1 02823 * @see MPU6050_PWR1_CYCLE_BIT 02824 */ 02825 bool MPU6050::getWakeCycleEnabled() 02826 { 02827 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, buffer); 02828 return buffer[0]; 02829 } 02830 /** Set wake cycle enabled status. 02831 * @param enabled New sleep mode enabled status 02832 * @see getWakeCycleEnabled() 02833 * @see MPU6050_RA_PWR_MGMT_1 02834 * @see MPU6050_PWR1_CYCLE_BIT 02835 */ 02836 void MPU6050::setWakeCycleEnabled(bool enabled) 02837 { 02838 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, enabled); 02839 } 02840 /** Get temperature sensor enabled status. 02841 * Control the usage of the internal temperature sensor. 02842 * 02843 * Note: this register stores the *disabled* value, but for consistency with the 02844 * rest of the code, the function is named and used with standard true/false 02845 * values to indicate whether the sensor is enabled or disabled, respectively. 02846 * 02847 * @return Current temperature sensor enabled status 02848 * @see MPU6050_RA_PWR_MGMT_1 02849 * @see MPU6050_PWR1_TEMP_DIS_BIT 02850 */ 02851 bool MPU6050::getTempSensorEnabled() 02852 { 02853 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, buffer); 02854 return buffer[0] == 0; // 1 is actually disabled here 02855 } 02856 /** Set temperature sensor enabled status. 02857 * Note: this register stores the *disabled* value, but for consistency with the 02858 * rest of the code, the function is named and used with standard true/false 02859 * values to indicate whether the sensor is enabled or disabled, respectively. 02860 * 02861 * @param enabled New temperature sensor enabled status 02862 * @see getTempSensorEnabled() 02863 * @see MPU6050_RA_PWR_MGMT_1 02864 * @see MPU6050_PWR1_TEMP_DIS_BIT 02865 */ 02866 void MPU6050::setTempSensorEnabled(bool enabled) 02867 { 02868 // 1 is actually disabled here 02869 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, !enabled); 02870 } 02871 /** Get clock source setting. 02872 * @return Current clock source setting 02873 * @see MPU6050_RA_PWR_MGMT_1 02874 * @see MPU6050_PWR1_CLKSEL_BIT 02875 * @see MPU6050_PWR1_CLKSEL_LENGTH 02876 */ 02877 uint8_t MPU6050::getClockSource() 02878 { 02879 i2Cdev.readBits(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, buffer); 02880 return buffer[0]; 02881 } 02882 /** Set clock source setting. 02883 * An internal 8MHz oscillator, gyroscope based clock, or external sources can 02884 * be selected as the MPU-60X0 clock source. When the internal 8 MHz oscillator 02885 * or an external source is chosen as the clock source, the MPU-60X0 can operate 02886 * in low power modes with the gyroscopes disabled. 02887 * 02888 * Upon power up, the MPU-60X0 clock source defaults to the internal oscillator. 02889 * However, it is highly recommended that the device be configured to use one of 02890 * the gyroscopes (or an external clock source) as the clock reference for 02891 * improved stability. The clock source can be selected according to the following table: 02892 * 02893 * <pre> 02894 * CLK_SEL | Clock Source 02895 * --------+-------------------------------------- 02896 * 0 | Internal oscillator 02897 * 1 | PLL with X Gyro reference 02898 * 2 | PLL with Y Gyro reference 02899 * 3 | PLL with Z Gyro reference 02900 * 4 | PLL with external 32.768kHz reference 02901 * 5 | PLL with external 19.2MHz reference 02902 * 6 | Reserved 02903 * 7 | Stops the clock and keeps the timing generator in reset 02904 * </pre> 02905 * 02906 * @param source New clock source setting 02907 * @see getClockSource() 02908 * @see MPU6050_RA_PWR_MGMT_1 02909 * @see MPU6050_PWR1_CLKSEL_BIT 02910 * @see MPU6050_PWR1_CLKSEL_LENGTH 02911 */ 02912 void MPU6050::setClockSource(uint8_t source) 02913 { 02914 i2Cdev.writeBits(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, source); 02915 } 02916 02917 // PWR_MGMT_2 register 02918 02919 /** Get wake frequency in Accel-Only Low Power Mode. 02920 * The MPU-60X0 can be put into Accerlerometer Only Low Power Mode by setting 02921 * PWRSEL to 1 in the Power Management 1 register (Register 107). In this mode, 02922 * the device will power off all devices except for the primary I2C interface, 02923 * waking only the accelerometer at fixed intervals to take a single 02924 * measurement. The frequency of wake-ups can be configured with LP_WAKE_CTRL 02925 * as shown below: 02926 * 02927 * <pre> 02928 * LP_WAKE_CTRL | Wake-up Frequency 02929 * -------------+------------------ 02930 * 0 | 1.25 Hz 02931 * 1 | 2.5 Hz 02932 * 2 | 5 Hz 02933 * 3 | 10 Hz 02934 * <pre> 02935 * 02936 * For further information regarding the MPU-60X0's power modes, please refer to 02937 * Register 107. 02938 * 02939 * @return Current wake frequency 02940 * @see MPU6050_RA_PWR_MGMT_2 02941 */ 02942 uint8_t MPU6050::getWakeFrequency() 02943 { 02944 i2Cdev.readBits(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, buffer); 02945 return buffer[0]; 02946 } 02947 /** Set wake frequency in Accel-Only Low Power Mode. 02948 * @param frequency New wake frequency 02949 * @see MPU6050_RA_PWR_MGMT_2 02950 */ 02951 void MPU6050::setWakeFrequency(uint8_t frequency) 02952 { 02953 i2Cdev.writeBits(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, frequency); 02954 } 02955 02956 /** Get X-axis accelerometer standby enabled status. 02957 * If enabled, the X-axis will not gather or report data (or use power). 02958 * @return Current X-axis standby enabled status 02959 * @see MPU6050_RA_PWR_MGMT_2 02960 * @see MPU6050_PWR2_STBY_XA_BIT 02961 */ 02962 bool MPU6050::getStandbyXAccelEnabled() 02963 { 02964 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, buffer); 02965 return buffer[0]; 02966 } 02967 /** Set X-axis accelerometer standby enabled status. 02968 * @param New X-axis standby enabled status 02969 * @see getStandbyXAccelEnabled() 02970 * @see MPU6050_RA_PWR_MGMT_2 02971 * @see MPU6050_PWR2_STBY_XA_BIT 02972 */ 02973 void MPU6050::setStandbyXAccelEnabled(bool enabled) 02974 { 02975 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, enabled); 02976 } 02977 /** Get Y-axis accelerometer standby enabled status. 02978 * If enabled, the Y-axis will not gather or report data (or use power). 02979 * @return Current Y-axis standby enabled status 02980 * @see MPU6050_RA_PWR_MGMT_2 02981 * @see MPU6050_PWR2_STBY_YA_BIT 02982 */ 02983 bool MPU6050::getStandbyYAccelEnabled() 02984 { 02985 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, buffer); 02986 return buffer[0]; 02987 } 02988 /** Set Y-axis accelerometer standby enabled status. 02989 * @param New Y-axis standby enabled status 02990 * @see getStandbyYAccelEnabled() 02991 * @see MPU6050_RA_PWR_MGMT_2 02992 * @see MPU6050_PWR2_STBY_YA_BIT 02993 */ 02994 void MPU6050::setStandbyYAccelEnabled(bool enabled) 02995 { 02996 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, enabled); 02997 } 02998 /** Get Z-axis accelerometer standby enabled status. 02999 * If enabled, the Z-axis will not gather or report data (or use power). 03000 * @return Current Z-axis standby enabled status 03001 * @see MPU6050_RA_PWR_MGMT_2 03002 * @see MPU6050_PWR2_STBY_ZA_BIT 03003 */ 03004 bool MPU6050::getStandbyZAccelEnabled() 03005 { 03006 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, buffer); 03007 return buffer[0]; 03008 } 03009 /** Set Z-axis accelerometer standby enabled status. 03010 * @param New Z-axis standby enabled status 03011 * @see getStandbyZAccelEnabled() 03012 * @see MPU6050_RA_PWR_MGMT_2 03013 * @see MPU6050_PWR2_STBY_ZA_BIT 03014 */ 03015 void MPU6050::setStandbyZAccelEnabled(bool enabled) 03016 { 03017 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, enabled); 03018 } 03019 /** Get X-axis gyroscope standby enabled status. 03020 * If enabled, the X-axis will not gather or report data (or use power). 03021 * @return Current X-axis standby enabled status 03022 * @see MPU6050_RA_PWR_MGMT_2 03023 * @see MPU6050_PWR2_STBY_XG_BIT 03024 */ 03025 bool MPU6050::getStandbyXGyroEnabled() 03026 { 03027 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, buffer); 03028 return buffer[0]; 03029 } 03030 /** Set X-axis gyroscope standby enabled status. 03031 * @param New X-axis standby enabled status 03032 * @see getStandbyXGyroEnabled() 03033 * @see MPU6050_RA_PWR_MGMT_2 03034 * @see MPU6050_PWR2_STBY_XG_BIT 03035 */ 03036 void MPU6050::setStandbyXGyroEnabled(bool enabled) 03037 { 03038 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, enabled); 03039 } 03040 /** Get Y-axis gyroscope standby enabled status. 03041 * If enabled, the Y-axis will not gather or report data (or use power). 03042 * @return Current Y-axis standby enabled status 03043 * @see MPU6050_RA_PWR_MGMT_2 03044 * @see MPU6050_PWR2_STBY_YG_BIT 03045 */ 03046 bool MPU6050::getStandbyYGyroEnabled() 03047 { 03048 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, buffer); 03049 return buffer[0]; 03050 } 03051 /** Set Y-axis gyroscope standby enabled status. 03052 * @param New Y-axis standby enabled status 03053 * @see getStandbyYGyroEnabled() 03054 * @see MPU6050_RA_PWR_MGMT_2 03055 * @see MPU6050_PWR2_STBY_YG_BIT 03056 */ 03057 void MPU6050::setStandbyYGyroEnabled(bool enabled) 03058 { 03059 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, enabled); 03060 } 03061 /** Get Z-axis gyroscope standby enabled status. 03062 * If enabled, the Z-axis will not gather or report data (or use power). 03063 * @return Current Z-axis standby enabled status 03064 * @see MPU6050_RA_PWR_MGMT_2 03065 * @see MPU6050_PWR2_STBY_ZG_BIT 03066 */ 03067 bool MPU6050::getStandbyZGyroEnabled() 03068 { 03069 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, buffer); 03070 return buffer[0]; 03071 } 03072 /** Set Z-axis gyroscope standby enabled status. 03073 * @param New Z-axis standby enabled status 03074 * @see getStandbyZGyroEnabled() 03075 * @see MPU6050_RA_PWR_MGMT_2 03076 * @see MPU6050_PWR2_STBY_ZG_BIT 03077 */ 03078 void MPU6050::setStandbyZGyroEnabled(bool enabled) 03079 { 03080 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, enabled); 03081 } 03082 03083 // FIFO_COUNT* registers 03084 03085 /** Get current FIFO buffer size. 03086 * This value indicates the number of bytes stored in the FIFO buffer. This 03087 * number is in turn the number of bytes that can be read from the FIFO buffer 03088 * and it is directly proportional to the number of samples available given the 03089 * set of sensor data bound to be stored in the FIFO (register 35 and 36). 03090 * @return Current FIFO buffer size 03091 */ 03092 uint16_t MPU6050::getFIFOCount() 03093 { 03094 i2Cdev.readBytes(devAddr, MPU6050_RA_FIFO_COUNTH, 2, buffer); 03095 return (((uint16_t)buffer[0]) << 8) | buffer[1]; 03096 } 03097 03098 // FIFO_R_W register 03099 03100 /** Get byte from FIFO buffer. 03101 * This register is used to read and write data from the FIFO buffer. Data is 03102 * written to the FIFO in order of register number (from lowest to highest). If 03103 * all the FIFO enable flags (see below) are enabled and all External Sensor 03104 * Data registers (Registers 73 to 96) are associated with a Slave device, the 03105 * contents of registers 59 through 96 will be written in order at the Sample 03106 * Rate. 03107 * 03108 * The contents of the sensor data registers (Registers 59 to 96) are written 03109 * into the FIFO buffer when their corresponding FIFO enable flags are set to 1 03110 * in FIFO_EN (Register 35). An additional flag for the sensor data registers 03111 * associated with I2C Slave 3 can be found in I2C_MST_CTRL (Register 36). 03112 * 03113 * If the FIFO buffer has overflowed, the status bit FIFO_OFLOW_INT is 03114 * automatically set to 1. This bit is located in INT_STATUS (Register 58). 03115 * When the FIFO buffer has overflowed, the oldest data will be lost and new 03116 * data will be written to the FIFO. 03117 * 03118 * If the FIFO buffer is empty, reading this register will return the last byte 03119 * that was previously read from the FIFO until new data is available. The user 03120 * should check FIFO_COUNT to ensure that the FIFO buffer is not read when 03121 * empty. 03122 * 03123 * @return Byte from FIFO buffer 03124 */ 03125 uint8_t MPU6050::getFIFOByte() 03126 { 03127 i2Cdev.readByte(devAddr, MPU6050_RA_FIFO_R_W, buffer); 03128 return buffer[0]; 03129 } 03130 void MPU6050::getFIFOBytes(uint8_t *data, uint8_t length) 03131 { 03132 i2Cdev.readBytes(devAddr, MPU6050_RA_FIFO_R_W, length, data); 03133 } 03134 /** Write byte to FIFO buffer. 03135 * @see getFIFOByte() 03136 * @see MPU6050_RA_FIFO_R_W 03137 */ 03138 void MPU6050::setFIFOByte(uint8_t data) 03139 { 03140 i2Cdev.writeByte(devAddr, MPU6050_RA_FIFO_R_W, data); 03141 } 03142 03143 // WHO_AM_I register 03144 03145 /** Get Device ID. 03146 * This register is used to verify the identity of the device (0b110100, 0x34). 03147 * @return Device ID (6 bits only! should be 0x34) 03148 * @see MPU6050_RA_WHO_AM_I 03149 * @see MPU6050_WHO_AM_I_BIT 03150 * @see MPU6050_WHO_AM_I_LENGTH 03151 */ 03152 uint8_t MPU6050::getDeviceID() 03153 { 03154 i2Cdev.readBits(devAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, buffer); 03155 return buffer[0]; 03156 } 03157 /** Set Device ID. 03158 * Write a new ID into the WHO_AM_I register (no idea why this should ever be 03159 * necessary though). 03160 * @param id New device ID to set. 03161 * @see getDeviceID() 03162 * @see MPU6050_RA_WHO_AM_I 03163 * @see MPU6050_WHO_AM_I_BIT 03164 * @see MPU6050_WHO_AM_I_LENGTH 03165 */ 03166 void MPU6050::setDeviceID(uint8_t id) 03167 { 03168 i2Cdev.writeBits(devAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, id); 03169 } 03170 03171 // ======== UNDOCUMENTED/DMP REGISTERS/METHODS ======== 03172 03173 // XG_OFFS_TC register 03174 03175 uint8_t MPU6050::getOTPBankValid() 03176 { 03177 i2Cdev.readBit(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, buffer); 03178 return buffer[0]; 03179 } 03180 void MPU6050::setOTPBankValid(bool enabled) 03181 { 03182 i2Cdev.writeBit(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, enabled); 03183 } 03184 int8_t MPU6050::getXGyroOffset() 03185 { 03186 i2Cdev.readBits(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer); 03187 return buffer[0]; 03188 } 03189 void MPU6050::setXGyroOffset(int8_t offset) 03190 { 03191 i2Cdev.writeBits(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset); 03192 } 03193 03194 // YG_OFFS_TC register 03195 03196 int8_t MPU6050::getYGyroOffset() 03197 { 03198 i2Cdev.readBits(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer); 03199 return buffer[0]; 03200 } 03201 void MPU6050::setYGyroOffset(int8_t offset) 03202 { 03203 i2Cdev.writeBits(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset); 03204 } 03205 03206 // ZG_OFFS_TC register 03207 03208 int8_t MPU6050::getZGyroOffset() 03209 { 03210 i2Cdev.readBits(devAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer); 03211 return buffer[0]; 03212 } 03213 void MPU6050::setZGyroOffset(int8_t offset) 03214 { 03215 i2Cdev.writeBits(devAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset); 03216 } 03217 03218 // X_FINE_GAIN register 03219 03220 int8_t MPU6050::getXFineGain() 03221 { 03222 i2Cdev.readByte(devAddr, MPU6050_RA_X_FINE_GAIN, buffer); 03223 return buffer[0]; 03224 } 03225 void MPU6050::setXFineGain(int8_t gain) 03226 { 03227 i2Cdev.writeByte(devAddr, MPU6050_RA_X_FINE_GAIN, gain); 03228 } 03229 03230 // Y_FINE_GAIN register 03231 03232 int8_t MPU6050::getYFineGain() 03233 { 03234 i2Cdev.readByte(devAddr, MPU6050_RA_Y_FINE_GAIN, buffer); 03235 return buffer[0]; 03236 } 03237 void MPU6050::setYFineGain(int8_t gain) 03238 { 03239 i2Cdev.writeByte(devAddr, MPU6050_RA_Y_FINE_GAIN, gain); 03240 } 03241 03242 // Z_FINE_GAIN register 03243 03244 int8_t MPU6050::getZFineGain() 03245 { 03246 i2Cdev.readByte(devAddr, MPU6050_RA_Z_FINE_GAIN, buffer); 03247 return buffer[0]; 03248 } 03249 void MPU6050::setZFineGain(int8_t gain) 03250 { 03251 i2Cdev.writeByte(devAddr, MPU6050_RA_Z_FINE_GAIN, gain); 03252 } 03253 03254 // XA_OFFS_* registers 03255 03256 int16_t MPU6050::getXAccelOffset() 03257 { 03258 i2Cdev.readBytes(devAddr, MPU6050_RA_XA_OFFS_H, 2, buffer); 03259 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03260 } 03261 void MPU6050::setXAccelOffset(int16_t offset) 03262 { 03263 i2Cdev.writeWord(devAddr, MPU6050_RA_XA_OFFS_H, offset); 03264 } 03265 03266 // YA_OFFS_* register 03267 03268 int16_t MPU6050::getYAccelOffset() 03269 { 03270 i2Cdev.readBytes(devAddr, MPU6050_RA_YA_OFFS_H, 2, buffer); 03271 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03272 } 03273 void MPU6050::setYAccelOffset(int16_t offset) 03274 { 03275 i2Cdev.writeWord(devAddr, MPU6050_RA_YA_OFFS_H, offset); 03276 } 03277 03278 // ZA_OFFS_* register 03279 03280 int16_t MPU6050::getZAccelOffset() 03281 { 03282 i2Cdev.readBytes(devAddr, MPU6050_RA_ZA_OFFS_H, 2, buffer); 03283 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03284 } 03285 void MPU6050::setZAccelOffset(int16_t offset) 03286 { 03287 i2Cdev.writeWord(devAddr, MPU6050_RA_ZA_OFFS_H, offset); 03288 } 03289 03290 // XG_OFFS_USR* registers 03291 03292 int16_t MPU6050::getXGyroOffsetUser() 03293 { 03294 i2Cdev.readBytes(devAddr, MPU6050_RA_XG_OFFS_USRH, 2, buffer); 03295 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03296 } 03297 void MPU6050::setXGyroOffsetUser(int16_t offset) 03298 { 03299 i2Cdev.writeWord(devAddr, MPU6050_RA_XG_OFFS_USRH, offset); 03300 } 03301 03302 // YG_OFFS_USR* register 03303 03304 int16_t MPU6050::getYGyroOffsetUser() 03305 { 03306 i2Cdev.readBytes(devAddr, MPU6050_RA_YG_OFFS_USRH, 2, buffer); 03307 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03308 } 03309 void MPU6050::setYGyroOffsetUser(int16_t offset) 03310 { 03311 i2Cdev.writeWord(devAddr, MPU6050_RA_YG_OFFS_USRH, offset); 03312 } 03313 03314 // ZG_OFFS_USR* register 03315 03316 int16_t MPU6050::getZGyroOffsetUser() 03317 { 03318 i2Cdev.readBytes(devAddr, MPU6050_RA_ZG_OFFS_USRH, 2, buffer); 03319 return (((int16_t)buffer[0]) << 8) | buffer[1]; 03320 } 03321 void MPU6050::setZGyroOffsetUser(int16_t offset) 03322 { 03323 i2Cdev.writeWord(devAddr, MPU6050_RA_ZG_OFFS_USRH, offset); 03324 } 03325 03326 // INT_ENABLE register (DMP functions) 03327 03328 bool MPU6050::getIntPLLReadyEnabled() 03329 { 03330 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, buffer); 03331 return buffer[0]; 03332 } 03333 void MPU6050::setIntPLLReadyEnabled(bool enabled) 03334 { 03335 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, enabled); 03336 } 03337 bool MPU6050::getIntDMPEnabled() 03338 { 03339 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, buffer); 03340 return buffer[0]; 03341 } 03342 void MPU6050::setIntDMPEnabled(bool enabled) 03343 { 03344 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, enabled); 03345 } 03346 03347 // DMP_INT_STATUS 03348 03349 bool MPU6050::getDMPInt5Status() 03350 { 03351 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_5_BIT, buffer); 03352 return buffer[0]; 03353 } 03354 bool MPU6050::getDMPInt4Status() 03355 { 03356 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_4_BIT, buffer); 03357 return buffer[0]; 03358 } 03359 bool MPU6050::getDMPInt3Status() 03360 { 03361 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_3_BIT, buffer); 03362 return buffer[0]; 03363 } 03364 bool MPU6050::getDMPInt2Status() 03365 { 03366 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_2_BIT, buffer); 03367 return buffer[0]; 03368 } 03369 bool MPU6050::getDMPInt1Status() 03370 { 03371 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_1_BIT, buffer); 03372 return buffer[0]; 03373 } 03374 bool MPU6050::getDMPInt0Status() 03375 { 03376 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_0_BIT, buffer); 03377 return buffer[0]; 03378 } 03379 03380 // INT_STATUS register (DMP functions) 03381 03382 bool MPU6050::getIntPLLReadyStatus() 03383 { 03384 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, buffer); 03385 return buffer[0]; 03386 } 03387 bool MPU6050::getIntDMPStatus() 03388 { 03389 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DMP_INT_BIT, buffer); 03390 return buffer[0]; 03391 } 03392 03393 // USER_CTRL register (DMP functions) 03394 03395 bool MPU6050::getDMPEnabled() 03396 { 03397 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, buffer); 03398 return buffer[0]; 03399 } 03400 void MPU6050::setDMPEnabled(bool enabled) 03401 { 03402 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, enabled); 03403 } 03404 void MPU6050::resetDMP() 03405 { 03406 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_RESET_BIT, true); 03407 } 03408 03409 // BANK_SEL register 03410 03411 void MPU6050::setMemoryBank(uint8_t bank, bool prefetchEnabled, bool userBank) 03412 { 03413 bank &= 0x1F; 03414 if (userBank) bank |= 0x20; 03415 if (prefetchEnabled) bank |= 0x40; 03416 i2Cdev.writeByte(devAddr, MPU6050_RA_BANK_SEL, bank); 03417 } 03418 //Read certain register 03419 uint8_t MPU6050::readThisByte(uint8_t address) 03420 { 03421 i2Cdev.readByte(devAddr, address, buffer); 03422 return buffer[0]; 03423 } 03424 // MEM_START_ADDR register 03425 03426 void MPU6050::setMemoryStartAddress(uint8_t address) 03427 { 03428 i2Cdev.writeByte(devAddr, MPU6050_RA_MEM_START_ADDR, address); 03429 } 03430 03431 // MEM_R_W register 03432 03433 uint8_t MPU6050::readMemoryByte() 03434 { 03435 i2Cdev.readByte(devAddr, MPU6050_RA_MEM_R_W, buffer); 03436 return buffer[0]; 03437 } 03438 void MPU6050::writeMemoryByte(uint8_t data) 03439 { 03440 i2Cdev.writeByte(devAddr, MPU6050_RA_MEM_R_W, data); 03441 } 03442 void MPU6050::readMemoryBlock(uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address) 03443 { 03444 setMemoryBank(bank); 03445 setMemoryStartAddress(address); 03446 uint8_t chunkSize; 03447 for (uint16_t i = 0; i < dataSize;) { 03448 // determine correct chunk size according to bank position and data size 03449 chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE; 03450 03451 // make sure we don't go past the data size 03452 if (i + chunkSize > dataSize) chunkSize = dataSize - i; 03453 03454 // make sure this chunk doesn't go past the bank boundary (256 bytes) 03455 if (chunkSize > 256 - address) chunkSize = 256 - address; 03456 03457 // read the chunk of data as specified 03458 i2Cdev.readBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, data + i); 03459 03460 // increase byte index by [chunkSize] 03461 i += chunkSize; 03462 03463 // uint8_t automatically wraps to 0 at 256 03464 address += chunkSize; 03465 03466 // if we aren't done, update bank (if necessary) and address 03467 if (i < dataSize) { 03468 if (address == 0) bank++; 03469 setMemoryBank(bank); 03470 setMemoryStartAddress(address); 03471 } 03472 } 03473 } 03474 bool MPU6050::writeMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify, bool useProgMem) 03475 { 03476 setMemoryBank(bank); 03477 setMemoryStartAddress(address); 03478 uint8_t chunkSize; 03479 uint8_t *verifyBuffer; 03480 uint8_t *progBuffer; 03481 uint16_t i; 03482 uint8_t j; 03483 if (verify) verifyBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE); 03484 if (useProgMem) progBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE); 03485 for (i = 0; i < dataSize;) { 03486 // determine correct chunk size according to bank position and data size 03487 chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE; 03488 03489 // make sure we don't go past the data size 03490 if (i + chunkSize > dataSize) chunkSize = dataSize - i; 03491 03492 // make sure this chunk doesn't go past the bank boundary (256 bytes) 03493 if (chunkSize > 256 - address) chunkSize = 256 - address; 03494 03495 if (useProgMem) { 03496 // write the chunk of data as specified 03497 for (j = 0; j < chunkSize; j++) progBuffer[j] = pgm_read_byte(data + i + j); 03498 } else { 03499 // write the chunk of data as specified 03500 progBuffer = (uint8_t *)data + i; 03501 } 03502 03503 i2Cdev.writeBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, progBuffer); 03504 03505 // verify data if needed 03506 if (verify && verifyBuffer) { 03507 setMemoryBank(bank); 03508 setMemoryStartAddress(address); 03509 i2Cdev.readBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, verifyBuffer); 03510 if (memcmp(progBuffer, verifyBuffer, chunkSize) != 0) { 03511 /*Serial.print("Block write verification error, bank "); 03512 Serial.print(bank, DEC); 03513 Serial.print(", address "); 03514 Serial.print(address, DEC); 03515 Serial.print("!\nExpected:"); 03516 for (j = 0; j < chunkSize; j++) { 03517 Serial.print(" 0x"); 03518 if (progBuffer[j] < 16) Serial.print("0"); 03519 Serial.print(progBuffer[j], HEX); 03520 } 03521 Serial.print("\nReceived:"); 03522 for (uint8_t j = 0; j < chunkSize; j++) { 03523 Serial.print(" 0x"); 03524 if (verifyBuffer[i + j] < 16) Serial.print("0"); 03525 Serial.print(verifyBuffer[i + j], HEX); 03526 } 03527 Serial.print("\n");*/ 03528 free(verifyBuffer); 03529 if (useProgMem) free(progBuffer); 03530 return false; // uh oh. 03531 } 03532 } 03533 03534 // increase byte index by [chunkSize] 03535 i += chunkSize; 03536 03537 // uint8_t automatically wraps to 0 at 256 03538 address += chunkSize; 03539 03540 // if we aren't done, update bank (if necessary) and address 03541 if (i < dataSize) { 03542 if (address == 0) bank++; 03543 setMemoryBank(bank); 03544 setMemoryStartAddress(address); 03545 } 03546 } 03547 if (verify) free(verifyBuffer); 03548 if (useProgMem) free(progBuffer); 03549 return true; 03550 } 03551 bool MPU6050::writeProgMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify) 03552 { 03553 return writeMemoryBlock(data, dataSize, bank, address, verify, true); 03554 } 03555 bool MPU6050::writeDMPConfigurationSet(const uint8_t *data, uint16_t dataSize, bool useProgMem) 03556 { 03557 uint8_t *progBuffer, success, special; 03558 uint16_t i, j; 03559 if (useProgMem) { 03560 progBuffer = (uint8_t *)malloc(8); // assume 8-byte blocks, realloc later if necessary 03561 } 03562 03563 // config set data is a long string of blocks with the following structure: 03564 // [bank] [offset] [length] [byte[0], byte[1], ..., byte[length]] 03565 uint8_t bank, offset, length; 03566 for (i = 0; i < dataSize;) { 03567 if (useProgMem) { 03568 bank = pgm_read_byte(data + i++); 03569 offset = pgm_read_byte(data + i++); 03570 length = pgm_read_byte(data + i++); 03571 } else { 03572 bank = data[i++]; 03573 offset = data[i++]; 03574 length = data[i++]; 03575 } 03576 03577 // write data or perform special action 03578 if (length > 0) { 03579 // regular block of data to write 03580 /*Serial.print("Writing config block to bank "); 03581 Serial.print(bank); 03582 Serial.print(", offset "); 03583 Serial.print(offset); 03584 Serial.print(", length="); 03585 Serial.println(length);*/ 03586 if (useProgMem) { 03587 if (sizeof(progBuffer) < length) progBuffer = (uint8_t *)realloc(progBuffer, length); 03588 for (j = 0; j < length; j++) progBuffer[j] = pgm_read_byte(data + i + j); 03589 } else { 03590 progBuffer = (uint8_t *)data + i; 03591 } 03592 success = writeMemoryBlock(progBuffer, length, bank, offset, true); 03593 i += length; 03594 } else { 03595 // special instruction 03596 // NOTE: this kind of behavior (what and when to do certain things) 03597 // is totally undocumented. This code is in here based on observed 03598 // behavior only, and exactly why (or even whether) it has to be here 03599 // is anybody's guess for now. 03600 if (useProgMem) { 03601 special = pgm_read_byte(data + i++); 03602 } else { 03603 special = data[i++]; 03604 } 03605 /*Serial.print("Special command code "); 03606 Serial.print(special, HEX); 03607 Serial.println(" found...");*/ 03608 if (special == 0x01) { 03609 // enable DMP-related interrupts 03610 03611 //setIntZeroMotionEnabled(true); 03612 //setIntFIFOBufferOverflowEnabled(true); 03613 //setIntDMPEnabled(true); 03614 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_ENABLE, 0x32); // single operation 03615 03616 success = true; 03617 } else { 03618 // unknown special command 03619 success = false; 03620 } 03621 } 03622 03623 if (!success) { 03624 if (useProgMem) free(progBuffer); 03625 return false; // uh oh 03626 } 03627 } 03628 if (useProgMem) free(progBuffer); 03629 return true; 03630 } 03631 bool MPU6050::writeProgDMPConfigurationSet(const uint8_t *data, uint16_t dataSize) 03632 { 03633 return writeDMPConfigurationSet(data, dataSize, false); 03634 } 03635 03636 // DMP_CFG_1 register 03637 03638 uint8_t MPU6050::getDMPConfig1() 03639 { 03640 i2Cdev.readByte(devAddr, MPU6050_RA_DMP_CFG_1, buffer); 03641 return buffer[0]; 03642 } 03643 void MPU6050::setDMPConfig1(uint8_t config) 03644 { 03645 i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_1, config); 03646 } 03647 03648 // DMP_CFG_2 register 03649 03650 uint8_t MPU6050::getDMPConfig2() 03651 { 03652 i2Cdev.readByte(devAddr, MPU6050_RA_DMP_CFG_2, buffer); 03653 return buffer[0]; 03654 } 03655 void MPU6050::setDMPConfig2(uint8_t config) 03656 { 03657 i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_2, config); 03658 }
Generated on Fri Jul 15 2022 19:49:43 by 1.7.2