jrowberg / i2cdevlib

I2C device library collection for AVR/Arduino or other C++-based MCUs
http://www.i2cdevlib.com
3.95k stars 7.51k forks source link

No change in sampling rate of the raw data using setRate function #622

Open williamesp2015 opened 3 years ago

williamesp2015 commented 3 years ago

I'm using raw data example in the loop. I first initialize the mpu6050 by NoDMPsetup() without DMP initilizing but always sampling rate is 100 hz. mpu.setRate is not functioning void loop() { while (!mpu.getIntDataReadyStatus()) { delayMicroseconds(100); } mpu.getMotion6(&aaReal.x, &aaReal.y, &aaReal.z, &gy.x, &gy.y, &gy.z); Serial.print(millis()); } void NoDMPsetup(uint8t acc, uint8t gyr,uint8_t DHP,uint8_t DLP__,uint8_t HZ) { mpu.setClockSource(MPU6050_CLOCK_PLL_XGYRO);

  Serial.println(F("Setting gyro sensitivity to +/- 2000 deg/sec..."));
  //setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
  switch (acc_) {
     case  2: mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);  break;
     case  4: mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_4);  break;
     case  8: mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_8);  break;
     case 16: mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_16); break;
     }
     switch (gyr_) {
         case  1: mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_250); break;
         case  2: mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_500); break;
         case  3: mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_1000); break;
         case  4: mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_2000); break;
       }
       Serial.println("Hz:");
       Serial.println(HZ__);
       mpu.setRate(HZ__);/// 1khz / (1 + 4) = 200 Hz  // 0=8kHz, 1=4kHz, 2=2.66Hz, 3=2kHz, 4=1.6kHz, 5=1.33kHz, 6=1.14Hz, 7=1kHz
       mpu.setDLPFMode(DLP__);//setDLPFMode(0x00); //Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
       mpu.setDHPFMode(DHP__);//https://github.com/terragady/LuzmonAccSensor/blob/9c38554506b95d0089494307735396479d60e8e0/ESP32_CPP/src/main.cpp

  mpu.setSleepEnabled(false); // thanks to Jack Elston for pointing this one out!

}

eadf commented 3 years ago

What's the printout from Serial.println(HZ__);?

williamesp2015 commented 3 years ago

I select 1-8 to change setRate but it is not sensitive for raw data.

eadf commented 3 years ago

I'm asking because from your example it's not clear if you ever call NoDMPsetup()

ZHomeSlice commented 3 years ago

@williamesp2015

The sequence of the MPU6050 startup is as follows deviation from this will likely cause difficulties (learned from experience)

int devAddr= [your devices address];
      // reset device:
        mpu.writeBit(devAddr, 0x6B, 7, 1);
        delay(100);
        mpu.writeBits(devAddr, 0x6A, 2, 3, 0b111);
        delay(100); 

      // Load Settings
    mpu.writeBytes(devAddr, 0x6B, 1, 0x00);  //Power Management 1  -- Set to 0b00000000 Internal 8MHz oscillator
    mpu.writeBytes(devAddr, 0x6C, 1, 0x00);  //Power Management 2  -- Low power mode wakeup stuff (all off)
    mpu.writeBytes(devAddr, 0x1A, 1, 0x03);  //Configuration -- external Frame Synchronization (off) and DLPF setting accel Bandwidth 44Hz delay 4.9ms  gyro Bandwidth 42Hz delay 4.8ms  Fs 1kHz
//This sets the gyroscope frequency to 1kHz
//If you set this value to ZERO 0x00 the gyroscope Frequency will be 8kHz

    mpu.writeBytes(devAddr, 0x1B, 1, 0x18);  //Gyroscope Configuration -- set to 0b000 11 000 = 3 ± 2000 °/s
    mpu.writeBytes(devAddr, 0x1C, 1, 0x00);  //Accelerometer Configuration -- set to 0b000 00 000 = 00 ± 2g
    mpu.writeBytes(devAddr, 0x23, 1, 0x00);  //FIFO Enable (Off)
    mpu.writeBytes(devAddr, 0x38, 1, 0x00);  //Interrupt Enable (Off)
    mpu.writeBytes(devAddr, 0x6A, 1, 0x04);  //User Control -- FIFO Reset
    mpu.writeBytes(devAddr, 0x19, 1, 0x04);  //Sample Rate Divider -- for use with DMP I have set it to 4 = 200Hz  as this is what it requires.

// 0x19 Sample Rate Divider Detailed Description:
//This register specifies the divider from the gyroscope output rate used to generate the Sample Rate for the MPU-6050.
//The sensor register output, FIFO output, and DMP sampling 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)
//Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or 7), and 1kHz when the DLPF is enabled (see Register 26 aka 0x1A ).
//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.

// start looking at the raw data now no DMP or FIFO has been started

So for your question, The sensor register output, FIFO output, and DMP sampling are all based on the Sample Rate.

Z

ZHomeSlice commented 3 years ago

@williamesp2015 Does this answer your question? did you come up with any improvements to this library from your work? we would like to know what you've discovered.

Z