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