戊辰寒 发表于 2012-12-27 23:18:13

MPU6050.cpp知多少

今天调试dmp各种头大于是跑去libraries看.h和.cpp的文件…………结果…………(真是骑驴找驴)

/** Default constructor, uses default I2C address.默认的函数结构,使用默认的I2C地址。
* @see MPU6050_DEFAULT_ADDRESS
*/
MPU6050::MPU6050() {
    devAddr = MPU6050_DEFAULT_ADDRESS;
}
直接使用MPU6050 mpu;的格式,mpu是设备名可以改的。
/** Specific address constructor.特殊地址的函数结构。
* @param address I2C address
* @see MPU6050_DEFAULT_ADDRESS
* @see MPU6050_ADDRESS_AD0_LOW
* @see MPU6050_ADDRESS_AD0_HIGH
*/
MPU6050::MPU6050(uint8_t address) {
    devAddr = address;
}
这里是MPU6050 mpu(0x68);默认地址是68,可以改。
/** Power on and prepare for general usage.上电,调试
* This will activate the device and take it out of sleep mode (which must be done
* after start-up). This function also sets both the accelerometer and the gyroscope
* to their most sensitive settings, namely +/- 2g 加速度计设为+/- 2gand +/- 250 degrees/sec陀螺仪设为+/- 250度/秒, and sets
* the clock source to use the X Gyro for reference, which is slightly better than
* the default internal clock source.
*/
void MPU6050::initialize() {
    setClockSource(MPU6050_CLOCK_PLL_XGYRO);
    setFullScaleGyroRange(MPU6050_GYRO_FS_250);
    setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
    setSleepEnabled(false); // thanks to Jack Elston for pointing this one out!
}
这里是mpu.initialize();/** Verify the I2C connection.验证I2C接口。
* Make sure the device is connected and responds as expected.
* @return True if connection is valid, false otherwise
*/
bool MPU6050::testConnection() {
    return getDeviceID() == 0x34;
}mpu.testConnection();// AUX_VDDIO register (InvenSense demo code calls this RA_*G_OFFS_TC)

/** Get the auxiliary I2C supply voltage level.获取I2C辅助电源电压。
* When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
* 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
* the MPU-6000, which does not have a VLOGIC pin.
* @return I2C supply voltage level (0=VLOGIC, 1=VDD)
*/
uint8_t MPU6050::getAuxVDDIOLevel() {
    I2Cdev::readBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, buffer);
    return buffer;
}mpu.getAuxVDDIOLevel();/** Set the auxiliary I2C supply voltage level.设定I2C辅助电源电压。
* When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
* 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
* the MPU-6000, which does not have a VLOGIC pin.
* @param level I2C supply voltage level (0=VLOGIC, 1=VDD)
*/
void MPU6050::setAuxVDDIOLevel(uint8_t level) {
    I2Cdev::writeBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, level);
}mpu.setAuxVDDIOLevel(uint8_t level) ;// SMPLRT_DIV register

/** Get gyroscope output rate divider.获取陀螺仪输出频率间隔。
* The sensor register output, FIFO output, DMP sampling, Motion detection, Zero
* Motion detection, and Free Fall detection are all based on the Sample Rate.
* The Sample Rate is generated by dividing the gyroscope output rate by
* SMPLRT_DIV:
*
* Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
*
* where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or
* 7), and 1kHz when the DLPF is enabled (see Register 26).
*
* Note: The accelerometer output rate is 1kHz. This means that for a Sample
* Rate greater than 1kHz, the same accelerometer sample may be output to the
* FIFO, DMP, and sensor registers more than once.
*
* For a diagram of the gyroscope and accelerometer signal paths, see Section 8
* of the MPU-6000/MPU-6050 Product Specification document.
*
* @return Current sample rate
* @see MPU6050_RA_SMPLRT_DIV
*/
uint8_t MPU6050::getRate() {
    I2Cdev::readByte(devAddr, MPU6050_RA_SMPLRT_DIV, buffer);
    return buffer;
}mpu.getRate();/** Set gyroscope sample rate divider.设定陀螺仪的采样频率间隔。
* @param rate New sample rate divider
* @see getRate()
* @see MPU6050_RA_SMPLRT_DIV
*/
void MPU6050::setRate(uint8_t rate) {
    I2Cdev::writeByte(devAddr, MPU6050_RA_SMPLRT_DIV, rate);
}mpu.setRate(uint8_t rate);// CONFIG register

/** Get external FSYNC configuration.获取FSYNC函数外接配置。
* Configures the external Frame Synchronization (FSYNC) pin sampling. An
* external signal connected to the FSYNC pin can be sampled by configuring
* EXT_SYNC_SET. Signal changes to the FSYNC pin are latched so that short
* strobes may be captured. The latched FSYNC signal will be sampled at the
* Sampling Rate, as defined in register 25. After sampling, the latch will
* reset to the current FSYNC signal state.
*
* The sampled value will be reported in place of the least significant bit in
* a sensor data register determined by the value of EXT_SYNC_SET according to
* the following table.
*
* <pre>
* EXT_SYNC_SET | FSYNC Bit Location
* -------------+-------------------
* 0            | Input disabled
* 1            | TEMP_OUT_L
* 2            | GYRO_XOUT_L
* 3            | GYRO_YOUT_L
* 4            | GYRO_ZOUT_L
* 5            | ACCEL_XOUT_L
* 6            | ACCEL_YOUT_L
* 7            | ACCEL_ZOUT_L
* </pre>
*
* @return FSYNC configuration value
*/
uint8_t MPU6050::getExternalFrameSync() {
    I2Cdev::readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, buffer);
    return buffer;
}mpu.getExternalFrameSync();/** Set external FSYNC configuration.设定FSYNC函数外接配置。
* @see getExternalFrameSync()
* @see MPU6050_RA_CONFIG
* @param sync New FSYNC configuration value
*/
void MPU6050::setExternalFrameSync(uint8_t sync) {
    I2Cdev::writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, sync);
}mpu.setExternalFrameSync(uint8_t sync);!!!好东西来了!

/** Get digital low-pass filter configuration.获取数字低通滤波器的配置。
* The DLPF_CFG parameter sets the digital low pass filter configuration. It
* also determines the internal sampling rate used by the device as shown in
* the table below.
*
* Note: The accelerometer output rate is 1kHz. This means that for a Sample
* Rate greater than 1kHz, the same accelerometer sample may be output to the
* FIFO, DMP, and sensor registers more than once.
*
* <pre>
*          |   ACCELEROMETER    |         GYROSCOPE
* DLPF_CFG | Bandwidth | Delay| Bandwidth | Delay| Sample Rate
* ---------+-----------+--------+-----------+--------+-------------
* 0      | 260Hz   | 0ms    | 256Hz   | 0.98ms | 8kHz
* 1      | 184Hz   | 2.0ms| 188Hz   | 1.9ms| 1kHz
* 2      | 94Hz      | 3.0ms| 98Hz      | 2.8ms| 1kHz
* 3      | 44Hz      | 4.9ms| 42Hz      | 4.8ms| 1kHz
* 4      | 21Hz      | 8.5ms| 20Hz      | 8.3ms| 1kHz
* 5      | 10Hz      | 13.8ms | 10Hz      | 13.4ms | 1kHz
* 6      | 5Hz       | 19.0ms | 5Hz       | 18.6ms | 1kHz
* 7      |   -- Reserved --   |   -- Reserved --   | Reserved
* </pre>
*
* @return DLFP configuration
* @see MPU6050_RA_CONFIG
* @see MPU6050_CFG_DLPF_CFG_BIT
* @see MPU6050_CFG_DLPF_CFG_LENGTH
*/
uint8_t MPU6050::getDLPFMode() {
    I2Cdev::readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, buffer);
    return buffer;
}mpu.getDLPFMode();/** Set digital low-pass filter configuration.设定数字低通滤波器的配置。
* @param mode New DLFP configuration setting
* @see getDLPFBandwidth()
* @see MPU6050_DLPF_BW_256
* @see MPU6050_RA_CONFIG
* @see MPU6050_CFG_DLPF_CFG_BIT
* @see MPU6050_CFG_DLPF_CFG_LENGTH
*/
void MPU6050::setDLPFMode(uint8_t mode) {
    I2Cdev::writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, mode);
}mpu.setDLPFMode(uint8_t mode);// GYRO_CONFIG register

/** Get full-scale gyroscope range.获取全量程的陀螺仪范围。
* The FS_SEL parameter allows setting the full-scale range of the gyro sensors,
* as described in the table below.
*
* <pre>
* 0 = +/- 250 degrees/sec
* 1 = +/- 500 degrees/sec
* 2 = +/- 1000 degrees/sec
* 3 = +/- 2000 degrees/sec
* </pre>
*
* @return Current full-scale gyroscope range setting
* @see MPU6050_GYRO_FS_250
* @see MPU6050_RA_GYRO_CONFIG
* @see MPU6050_GCONFIG_FS_SEL_BIT
* @see MPU6050_GCONFIG_FS_SEL_LENGTH
*/
uint8_t MPU6050::getFullScaleGyroRange() {
    I2Cdev::readBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, buffer);
    return buffer;
}mpu.getFullScaleGyroRange();/** Set full-scale gyroscope range.设定全量程的陀螺仪范围。
* @param range New full-scale gyroscope range value
* @see getFullScaleRange()
* @see MPU6050_GYRO_FS_250
* @see MPU6050_RA_GYRO_CONFIG
* @see MPU6050_GCONFIG_FS_SEL_BIT
* @see MPU6050_GCONFIG_FS_SEL_LENGTH
*/
void MPU6050::setFullScaleGyroRange(uint8_t range) {
    I2Cdev::writeBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, range);
}mpu.setFullScaleGyroRange(uint8_t range);这里我跳过了加速度计的安全测试。

/** Get full-scale accelerometer range.获取全量程的加速度计范围。
* The FS_SEL parameter allows setting the full-scale range of the accelerometer
* sensors, as described in the table below.
*
* <pre>
* 0 = +/- 2g
* 1 = +/- 4g
* 2 = +/- 8g
* 3 = +/- 16g
* </pre>
*
* @return Current full-scale accelerometer range setting
* @see MPU6050_ACCEL_FS_2
* @see MPU6050_RA_ACCEL_CONFIG
* @see MPU6050_ACONFIG_AFS_SEL_BIT
* @see MPU6050_ACONFIG_AFS_SEL_LENGTH
*/
uint8_t MPU6050::getFullScaleAccelRange() {
    I2Cdev::readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, buffer);
    return buffer;
}mpu.getFullScaleAccelRange();/** Set full-scale accelerometer range.设定全量程的加速度计范围。
* @param range New full-scale accelerometer range setting
* @see getFullScaleAccelRange()
*/
void MPU6050::setFullScaleAccelRange(uint8_t range) {
    I2Cdev::writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, range);
}mpu.setFullScaleAccelRange(uint8_t range);高通滤波器!
/** Get the high-pass filter configuration.获取高通滤波器的配置。
* The DHPF is a filter module in the path leading to motion detectors (Free
* Fall, Motion threshold, and Zero Motion). The high pass filter output is not
* available to the data registers (see Figure in Section 8 of the MPU-6000/
* MPU-6050 Product Specification document).
*
* The high pass filter has three modes:
*
* <pre>
*    Reset: The filter output settles to zero within one sample. This
*         effectively disables the high pass filter. This mode may be toggled
*         to quickly settle the filter.
*
*    On:    The high pass filter will pass signals above the cut off frequency.
*
*    Hold:When triggered, the filter holds the present sample. The filter
*         output will be the difference between the input sample and the held
*         sample.
* </pre>
*
* <pre>
* ACCEL_HPF | Filter Mode | Cut-off Frequency
* ----------+-------------+------------------
* 0         | Reset       | None
* 1         | On          | 5Hz
* 2         | On          | 2.5Hz
* 3         | On          | 1.25Hz
* 4         | On          | 0.63Hz
* 7         | Hold      | None
* </pre>
*
* @return Current high-pass filter configuration
* @see MPU6050_DHPF_RESET
* @see MPU6050_RA_ACCEL_CONFIG
*/
uint8_t MPU6050::getDHPFMode() {
    I2Cdev::readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, buffer);
    return buffer;
}mpu.getDHPFMode();/** Set the high-pass filter configuration.设定高通滤波器的配置。
* @param bandwidth New high-pass filter configuration
* @see setDHPFMode()
* @see MPU6050_DHPF_RESET
* @see MPU6050_RA_ACCEL_CONFIG
*/
void MPU6050::setDHPFMode(uint8_t bandwidth) {
    I2Cdev::writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, bandwidth);
}mpu.setDHPFMode(uint8_t bandwidth);后面还有很多说明,我在这里抛砖引玉,希望大家能一起来交流,翻译~

戊辰寒 发表于 2012-12-28 13:55:52

本帖最后由 戊辰寒 于 2012-12-28 20:35 编辑

                                                            注意!!
陀螺仪精度
* <pre>
* 0 = +/- 250 degreesc
* 1 = +/- 500 degreesc
* 2 = +/- 1000 degreesc
* 3 = +/- 2000 degreesc
* </pre>
加速度计精度
* <pre>
* 0 = +/- 2g
* 1 = +/- 4g
* 2 = +/- 8g
* 3 = +/- 16g
* </pre>

这2处有bug,编码跟精度是反的。应该是:

* <pre>
* 3 = +/- 250 degreesc
* 2 = +/- 500 degreesc
* 1 = +/- 1000 degreesc
* 0 = +/- 2000 degreesc
* </pre>

* <pre>
* 3 = +/- 2g
* 2 = +/- 4g
* 1 = +/- 8g
* 0 = +/- 16g
* </pre>

调用setFullScaleGyroRange(uint8_t range)和setFullScaleAccelRange(uint8_t range)
函数的时候请一定注意!

戊辰寒 发表于 2012-12-28 13:56:52

占楼,后续

荒野无涯 发表于 2012-12-29 00:17:14

:):):):):):):)学习中

Randy 发表于 2012-12-29 11:25:01

这个非常详细,赞一个!

utuu 发表于 2013-1-25 20:24:12

非常好 加油

Limius 发表于 2013-1-25 21:09:49

谢谢! 源代码才是核心啊!

yangjx 发表于 2013-1-28 22:18:23

好,期待。。。

jack4904 发表于 2013-1-31 22:07:19

佔位等詳細, 頂啊!

迷你强 发表于 2013-1-31 22:46:38

:o貌似很高端。。。。准备弄个加速度传感器+sd卡套装,发个快递。。。记录下盒子在路上如何惨遭蹂躏的。

Big_fish 发表于 2013-3-6 23:08:35

学习啊学习啊~

路遥~倒转回忆 发表于 2013-4-13 23:30:04


//配置MPU6000bypass模式
accelgyro.setI2CMasterModeEnabled(0);
   accelgyro.setI2CBypassEnabled(1);
   if((!accelgyro.getI2CMasterModeEnabled()) && accelgyro.getI2CBypassEnabled())
   printf("Set MPU6000 Bypass Mode success!\n");

我想把mpu和hmc5883一起用,但是把它们分别和arduino nano的A4A5连接后,把代码复制到一起,然后串口就只能得到mpu的数据?

参考这个帖子http://www.geek-workshop.com/forum.php?mod=viewthread&tid=1793&page=1#pid24528,说要把mup设置成bypass模式 把这个放到代码前面,但是程序不能识别,
请问楼主这短代码该放到哪儿呢?

firewise 发表于 2013-4-20 14:25:03

好贴,非常详细,谢谢

firewise 发表于 2013-4-23 19:48:04

非常好,谢谢翻译。
继续努力

Ansifa 发表于 2013-4-24 01:07:52

同在研究6050头痛中!我也是骑驴找驴的人啊.:dizzy:
页: [1] 2
查看完整版本: MPU6050.cpp知多少