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);复制代码后面还有很多说明,我在这里抛砖引玉,希望大家能一起来交流,翻译~
    via - 极客工坊

标签: Arduino教程