1

I'm having a problem with my MPU6050. I'm using PID to speed up a motor with the MPU values. What could the problem be?

Here is a link to a previous question that I asked concerning a problem with pid: PID not working correctly

These are the values that I'm getting, which repeat forever. The values are:

  • motor speed;
  • the value that changes the motor speed;
  • the MPU value;
  • the change that PID made.

0.00
0
10900
4800

0.00
0
10900
4800

Here is the code itself.

int16_t ax, ay, az;

double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp,Ki,Kd, DIRECT);

void setup() {
 Input = analogRead(ax);
  Setpoint = 100;
  //turn the PID on
   myPID.SetMode(AUTOMATIC);
}

void loop() {
 accelgyro.getAcceleration(&ax, &ay, &az);
  int a=ax+10900;
//Here I set the used variable to around 0. ax is actually around -10900
//This is always true, this won't be in the final code. This is just making        it work.

if(1) {
 int val;
  val = map(a, -1300, 1300, 0, 1023);
   Input = val;
    myPID.Compute();
     Serial.println(Output);

digitalWrite(motorPin2, HIGH);
Serial.print("\t");
Serial.println(ax);
Serial.print("\t");
Serial.println(a);
Serial.print("\t");
Serial.println(val);
     }
}
  • Please post a Minimal, Complete, and Verifiable example - I don't see, for example, where you are printing the other numbers. And why do you do an analogRead in setup? – Nick Gammon Mar 3 '16 at 22:12
  • I added the prints. I think my teacher did the analogread. Not sure if its supposed to be there. This code has worked before. I think its the pins thats wrong or something. – Marc Mar 3 '16 at 22:30
2

Let's deconstruct this down to a simple example which we can test without the MPU6050:

#include <PID_v1.h>

double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp = 2, Ki = 5, Kd = 1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

void setup() {
  Setpoint = 100;
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  Serial.begin (115200);
  Serial.println (F("\n\nStarting\n\n"));
}

void loop() {
  int a = 0;
  int val = map(a, -1300, 1300, 0, 1023);
  Input = val;
  myPID.Compute();
  Serial.print (F("Input = "));
  Serial.println(Input);
  Serial.print (F("Setpoint = "));
  Serial.println(Setpoint);
  Serial.print (F("Output = "));
  Serial.println(Output);
  Serial.println();
  delay (300);
}

Output:

Starting


Input = 511.00
Setpoint = 100.00
Output = 0.00

Input = 511.00
Setpoint = 100.00
Output = 0.00

Input = 511.00
Setpoint = 100.00
Output = 0.00

Input = 511.00
Setpoint = 100.00
Output = 0.00

Indeed it isn't changing. Let's analyze why:

  • The input is 511
  • The setpoint is 100
  • Therefore the error is 100 - 511 = -411
  • Disregarding (for the moment) the integral and differential terms, you have a proportional term of 2 (Kp) so the algorithm tries to output 2 * Error
  • Output would be 2 * -411 = -822
  • However the maximum and minimum limits default to 0 to 255, and thus the output is clamped to 0.
  • The algorithm outputs 0.
  • And the next time it does the same thing.

There is a function SetOutputLimits which can be used to change the default minimum and maximum output values.

The Ki and Kd values might eventually have an impact, but you won't notice anything with the output clamped to zero.

For example, adding this line in setup gives different results:

  myPID.SetOutputLimits (-5000, 5000);

Result:

Starting


Input = 511.00
Setpoint = 100.00
Output = -5000.00

Input = 511.00
Setpoint = 100.00
Output = -1233.00

Input = 511.00
Setpoint = 100.00
Output = -1438.50

Input = 511.00
Setpoint = 100.00
Output = -1644.00

Input = 511.00
Setpoint = 100.00
Output = -1849.50

Input = 511.00
Setpoint = 100.00
Output = -2055.00

Input = 511.00
Setpoint = 100.00
Output = -2260.50

Input = 511.00
Setpoint = 100.00
Output = -2466.00

Whether they are valid limits depend on what you are attempting to do exactly.

  • Im still having the problem that the mpu's value doesnt change – Marc Mar 3 '16 at 23:08
1

The problem was that I had the SCL and SDA pins connected to the wrong Arduino pins.

Sorry for bothering your time. Thanks for the tips.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.