Jump to content
I2Cdevlib Forums

A question to change the sample rate in MPU6050.h file


Recommended Posts

Dear all,
 
I am working on a small project and need to have the sample rate up to 50Hz. I ran to a thread in Arduino Forum community and someone said that he was able to change a setting from 0x0A to 0x03 in MPU6050.h to have the sample rate moved up from 18Hz to 50Hz. I am not able to locate that line of code in mpu6050.h file. I am currently using your MPU6050 raw code. It works great for me. The thread is here.Thank you very much.
 

 

 

Ji

Link to comment
Share on other sites

By the way, I would not use the MPU6050_DMP6 code because I dont want to set the interrupt for MPU6050. I just want to do simply data polling from MPU6050.  The MPU6050 is connected to Arduino Pro Mini 328 3.3v/8MHz. The arduino's interrupts are designated to my encoder. I kind of know how to change the sample rate for MPU 6050 in MPU6050_DMP6 codes. Anyway, I just try to see if i am not able to change the sample rate in MPU6050.h file for MPU6050 raw code, Can I remove the interrupts in MP6050_DMP6.ino code, along with changing FIFO rate in MPU6050_6Axis_MotionApps20.h file? At last, I want to say I am sorry for my not-very-good discription of my project. I am a really code rookie, haven't done any real coding in my life. Thanks again.

 

Ji

Link to comment
Share on other sites

Hi Ji,

I think it's a function called setRate. It's defined in mpu6050.h and incorporated in mpu6050.cpp. But it seems it is not called for in the MPU6050 Raw example. I would put it in initialize,

I'm using the DMPexample, also without interrupt. What I understood is that MPU6050 sample frequency should be higher as the polling frequency. I,m testing on MPUint bit from the interrupt status register, and that seems to work.

Like this:

mpuIntStatus = mpuL.getIntStatus(); // get int status

fifoCount = mpuL.getFIFOCount();

if (mpuIntStatus & 0x02) {

while (fifoCount < packetSize) fifoCount = mpuL.getFIFOCount();

mpuL.getFIFOBytes(fifoBuffer, packetSize);

fifoCount -= packetSize;

mpuL.dmpGetQuaternion(&q, fifoBuffer);

mpuL.dmpGetYawPitchRoll(ypr, &q, &gravity);

mpuL.dmpGetAccel(&aa, fifoBuffer);

mpuL.dmpGetGravity(&gravity, &q);

mpuL.dmpGetLinearAccel(&aaReal, &aa, &gravity);

mpuL.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);

Buffer[0] = ypr[0] * 180/M_PI;

Buffer[1] = ypr[1] * 180/M_PI;

Buffer[2] = ypr[2] * 180/M_PI;

Buffer[3] = aaWorld.x;

Buffer[4] = aaWorld.y;

Buffer[5] = aaWorld.z;

mpuL.resetFIFO();

// export it to pure data stuff

}

}

my sample rate is 200 Hz, polling frequency about 10 Hz.

But I'm not sure or the DMP uses all information in the FIFO. Maybe Jeff knows??

Greetings, Pam

Link to comment
Share on other sites

Pam, 

 

Thank you so much for your kind help. I am very new to programming and actually haven't done any coding from scratch. I hope you don't mind I shoot some questions about the code you posted. It seems to me that this excerpt of code is similar to DMP6 running code. As you mentioned, you don't use the interrupt part of this code too, I am trying to remove the interrupt from  the code MPU6050_DMP6.ino. Would you kindly tell a look at it and let me know if this is correct. Thank you again.

#define OUTPUT_TEAPOT



#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
bool blinkState = false;

// MPU control/status vars
//bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector

// packet structure for InvenSense teapot demo
uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };



//// ================================================================
//// ===               INTERRUPT DETECTION ROUTINE                ===
//// ================================================================
//
//volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
//void dmpDataReady() {
//    mpuInterrupt = true;
//}



// ================================================================
// ===                      INITIAL SETUP                       ===
// ================================================================

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    // initialize serial communication
    // (115200 chosen because it is required for Teapot Demo output, but it's
    // really up to you depending on your project)
    Serial.begin(9600);
    while (!Serial); // wait for Leonardo enumeration, others continue immediately

    // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3v or Ardunio
    // Pro Mini running at 3.3v, cannot handle this baud rate reliably due to
    // the baud timing being too misaligned with processor ticks. You must use
    // 38400 or slower in these cases, or use some kind of external separate
    // crystal solution for the UART timer.

    // initialize device
    Serial.println(F("Initializing I2C devices..."));
    mpu.initialize();

    // verify connection
    Serial.println(F("Testing device connections..."));
    Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

    // wait for ready
    Serial.println(F("\nSend any character to begin DMP programming and demo: "));
    while (Serial.available() && Serial.read()); // empty buffer
    while (!Serial.available());                 // wait for data
    while (Serial.available() && Serial.read()); // empty buffer again

    // load and configure the DMP
    Serial.println(F("Initializing DMP..."));
    devStatus = mpu.dmpInitialize();
    
    // make sure it worked (returns 0 if so)
    if (devStatus == 0) {
        // turn on the DMP, now that it's ready
        Serial.println(F("Enabling DMP..."));
        mpu.setDMPEnabled(true);

//        // enable Arduino interrupt detection
//        Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
//        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();

        // set our DMP Ready flag so the main loop() function knows it's okay to use it
        Serial.println(F("DMP ready! Waiting for first interrupt..."));
        dmpReady = true;

        // get expected DMP packet size for later comparison
        packetSize = mpu.dmpGetFIFOPacketSize();
    } else {
        // ERROR!
        // 1 = initial memory load failed
        // 2 = DMP configuration updates failed
        // (if it's going to break, usually the code will be 1)
        Serial.print(F("DMP Initialization failed (code "));
        Serial.print(devStatus);
        Serial.println(F(")"));
    }

    // configure LED for output
    pinMode(LED_PIN, OUTPUT);
}



// ================================================================
// ===                    MAIN PROGRAM LOOP                     ===
// ================================================================

void loop() {
    // if programming failed, don't try to do anything
    if (!dmpReady) return;

    // wait for MPU interrupt or extra packet(s) available
    while (fifoCount < packetSize) {
    //while (!mpuInterrupt && fifoCount < packetSize) {
        // other program behavior stuff here
        // .
        // .
        // .
        // if you are really paranoid you can frequently test in between other
        // stuff to see if mpuInterrupt is true, and if so, "break;" from the
        // while() loop to immediately process the MPU data
        // .
        // .
        // .
    }

    // reset interrupt flag and get INT_STATUS byte
   // mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();

    // get current FIFO count
    fifoCount = mpu.getFIFOCount();

    // check for overflow (this should never happen unless our code is too inefficient)
    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
        // reset so we can continue cleanly
        mpu.resetFIFO();
        Serial.println(F("FIFO overflow!"));

    // otherwise, check for DMP data ready interrupt (this should happen frequently)
    } else if (mpuIntStatus & 0x02) {
        // wait for correct available data length, should be a VERY short wait
        while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

        // read a packet from FIFO
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        
        // track FIFO count here in case there is > 1 packet available
        // (this lets us immediately read more without waiting for an interrupt)
        fifoCount -= packetSize;

        #ifdef OUTPUT_READABLE_QUATERNION
            // display quaternion values in easy matrix form: w x y z
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            Serial.print("quat\t");
            Serial.print(q.w,DEC);
            Serial.print("\t");
            Serial.print(q.x,DEC);
            Serial.print("\t");
            Serial.print(q.y,DEC);
            Serial.print("\t");
            Serial.println(q.z,DEC);
        #endif

        #ifdef OUTPUT_READABLE_EULER
            // display Euler angles in degrees
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetEuler(euler, &q);
            Serial.print("euler\t");
            Serial.print(euler[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(euler[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(euler[2] * 180/M_PI);
        #endif

        #ifdef OUTPUT_READABLE_YAWPITCHROLL
            // display Euler angles in degrees
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
            Serial.print("ypr\t");
            Serial.print(ypr[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(ypr[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(ypr[2] * 180/M_PI);
        #endif

        #ifdef OUTPUT_READABLE_REALACCEL
            // display real acceleration, adjusted to remove gravity
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetAccel(&aa, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
            Serial.print("areal\t");
            Serial.print(aaReal.x);
            Serial.print("\t");
            Serial.print(aaReal.y);
            Serial.print("\t");
            Serial.println(aaReal.z);
        #endif

        #ifdef OUTPUT_READABLE_WORLDACCEL
            // display initial world-frame acceleration, adjusted to remove gravity
            // and rotated based on known orientation from quaternion
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetAccel(&aa, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
            mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
            Serial.print("aworld\t");
            Serial.print(aaWorld.x);
            Serial.print("\t");
            Serial.print(aaWorld.y);
            Serial.print("\t");
            Serial.println(aaWorld.z);
        #endif
    
        #ifdef OUTPUT_TEAPOT
            // display quaternion values in InvenSense Teapot demo format:
            teapotPacket[2] = fifoBuffer[0];
            teapotPacket[3] = fifoBuffer[1];
            teapotPacket[4] = fifoBuffer[4];
            teapotPacket[5] = fifoBuffer[5];
            teapotPacket[6] = fifoBuffer[8];
            teapotPacket[7] = fifoBuffer[9];
            teapotPacket[8] = fifoBuffer[12];
            teapotPacket[9] = fifoBuffer[13];
            Serial.write(teapotPacket, 14);
            teapotPacket[11]++; // packetCount, loops at 0xFF on purpose
        #endif

        // blink LED to indicate activity
        blinkState = !blinkState;
        digitalWrite(LED_PIN, blinkState);
    }
}
Link to comment
Share on other sites

Hi,

 

well.... I'm not even sure about my own code. But it seems we have the same problem so we can figure it out.

 

The documentation says: motion processing algorithms should be run at a high rate, often around 200Hz, in order to provide accurate results with low latency. This is required even if the application updates at a much lower rate; for example, a low power user interface may update as slowly as 5Hz, but the motion processing should still run at 200Hz.

 

As I understand it this means setting the rate divider at 4 to obtain a sample frequency of 200 Hz (as in the DMP example code). The dmp code block specifies a fifo rate of 100 Hz by setting the divider to 1 200 Hz/(1 + 1). I suppose the dmp interrupt is triggered at this frequency as the fifocounter increases with 1 or 2 42 byte blocks??

Does this mean the DMP averages the data in between??? What if I sample at 200 Hz and read the FIFO at 20 Hz?? Do I have 10 packets in the FIFO?? Which can be averaged?? Or does the FIFO contain just 1 averaged package???

 

Well something to try out I think (tomorrow)

Link to comment
Share on other sites

Hi guys,

 

The setRate() method only applies to raw measurements, but not to the DMP which has its own rate. This is defined on line 261 (currently) of MPU6050_6Axis_MotionApps_20.h. The default rate is 100Hz (the last byte is 0x01), but you can increase it to 50Hz by changing that byte to 0x03 instead. To the best of my knowledge, the internal sensors are still read at a very fast rate, but the DMP algorithms compensate such that the output data is appropriate for 50Hz intervals.

 

If you don't want to use the interrupt pin (which is a shame since it's much more efficient), you can simply poll regularly for a FIFO count that is >= 42 (which is the size of a typical MotionApps 2.0 DMP packet), and read those 42 bytes anytime they are available until there are less than 42 bytes available. It's kind of an ugly way to do it, but it does work as long as you read fast enough that the FIFO doesn't overflow (>= 1024 bytes).

Link to comment
Share on other sites

Hi Jeff,

 

thank you very much. It's amazing that you know an answer to all these questions :).

 

By the way:  100 Hz is for a 16 Mhz Arduino?? That is 50Hz  for a 8 Mhz (I'm using Arduino Fio)???

 

Regarding the decision about interrupts: I would like to discuss about that, but I will make a different thread for it.

Link to comment
Share on other sites

Hi Pam and Jeff,

 

Thank you so much for your reply, Sorry to get back a little late. It does and will still take me quite time to understand everything u put up there. But it is great.

 

I will close this thread temporarily and will get back to this topic again sometime later on when the project takes me back to this area.

Link to comment
Share on other sites

  • 3 weeks later...

You can gather data at 100Hz on an 8MHz Arduino, but you have very little time left for processing it, especially if your I2C bus is running at 100KHz instead of 400KHz. Many people have run into a problem where they use Serial.println() a lot to output data, and that takes a whole lot of time; generally, you have to make sure you have very tight code if you want 100Hz motion processing on an 8MHz host.

Link to comment
Share on other sites

  • 5 months later...

Jeff! First I want to thank you for your libraries, they are great. 

 

I has been working with the MPU6050 for a project, and i want to measure the acceleration of a vibrator motor. This motor vibrates at a frequency of 175 Hz, so i need a sample rate bigger than 350 Hz. Is it possible to do that with the MPU6050, specially using your library.

 

Thank for your help.

 

Gus

Link to comment
Share on other sites

 

 

350 Hz. Is it possible to do that with the MPU6050, specially using your library

 

Yes, it should be possible.

 

I think you will have to change your I2C frequency to 400KHz (max is 500KHz), only read accelerometers, configure the MPU sample rate to 350 Hz or higher, configure the MPU low pass filter to 175Hz or higher and check the accelerometer range so that it does not saturate.

 

Besides you need an Arduino or similar capable or doing all this and having extra time to process what you want to do with those measures. I guess you should be fine with 16MHz Arduino, but can't confirm because I usually use Arduino DUE (84 MHz).

 

There are 3 ways of reading this info from sensor, but that's other matter.

 

Let me know if you achieve it!

Link to comment
Share on other sites

  • 3 years later...

Hi Jeff,

Great Work doing the library. Have a question one of your posts on Changing the Sampling Rate of the DMP -  one do change the setDMPConfig1 to the value 0x03 or 0x01 in MPU6050_6Axis_MotionApps_20.h whatever you have said earlier depending on the sampling rate needed ??

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...