Go Down

Topic: MPU-6050 and jrowberg/i2cdevlib Library Problem (Read 28 times) previous topic - next topic

Babycakes

Hi All

I am coding a simple robot which turns left and right in response to the angle reading from the MPU-6050.

There is an excellent resource available on https://github.com/jrowberg/i2cdevlib, which has been very helpful.

I have managed to get raw values from the MPU-6050 and I have got the angles too, using the example code.

I have modified the example code as follows and this works:

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

  // wait for MPU interrupt or extra packet(s) available
  while (!mpuInterrupt && fifoCount < packetSize) {

    do {
        Serial.print("Current Angle: ");
        Serial.print(ypr[0]);
        Serial.print(" Desired Angle: ");
        Serial.println(desiredAngle);
        spinRight(5);
      }
      else if (ypr[0] > desiredAngle) {
        Serial.print("Current Angle: ");
        Serial.print(ypr[0]);
        Serial.print(" Desired Angle: ");
        Serial.println(desiredAngle);
        spinLeft(5);
      }
  }


However, when I change the code, with the addition of a do...while, then the MPU-6050 gives me a static value that no longer changes. 
Code: [Select]
void loop() {
  // if programming failed, don't try to do anything
  if (!dmpReady) return;

  // wait for MPU interrupt or extra packet(s) available
  while (!mpuInterrupt && fifoCount < packetSize) {

    do {
      if (ypr[0] < desiredAngle) {
        Serial.print("Current Angle: ");
        Serial.print(ypr[0]);
        Serial.print(" Desired Angle: ");
        Serial.println(desiredAngle);
        spinRight(5);
      }
      else if (ypr[0] > desiredAngle) {
        Serial.print("Current Angle: ");
        Serial.print(ypr[0]);
        Serial.print(" Desired Angle: ");
        Serial.println(desiredAngle);
        spinLeft(5);
      }
    } while (ypr[0] != desiredAngle);
  }


Any ideas what I am doing wrong?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy