PID help- PID does not work

Hello, I am a programmer looking to implement pid into my robot. However, when I try to run the code, it does nothing.

const double PI = 3.14159255359;

double getDifferce(double a, double b) {
  a *= PI / 180;
  b *= PI / 180;
  return (180 / PI * atan2(cos(b - a), sin(b - a))) - 90;
}
double getAngleError(double goal) { return getDifferce(ponty.heading(), goal); }

void pidloopturn (double angle) {
  float kP = 1;
  float kI=0.00001;
  float kD =0.001;
  float voltsN;
  float error;
  float preverror;
  float drev=0;
  float integral;
  int final=0;
  int timeout=0;
 
  error = getAngleError(angle);
  voltsN=0;
 while(final !=12){
  error = getAngleError(angle);
  integral=integral+error;
  if ((error=0)){
    kI = 0;
  }
  if(abs(error)>70) {
    kI= 0;
  }
  if (error>1 or error<-1) {final=0;}
  if (error<0.5 && error>-0.5) {final=final++;}
 if (timeout=150) {final=12;};
 timeout=timeout++;

  drev= error-preverror;
  preverror=error;
 voltsN=(kP*error+kI*integral+kD*drev);
 

LeftFront.spin(reverse, voltsN, volt);
leftmiddle.spin(reverse, voltsN, volt);
    LeftBack.spin(reverse, voltsN, volt);
    Rightfront.spin(fwd, voltsN, volt);
    RightMiddle.spin(fwd, voltsN, volt);
    Rightback.spin(fwd, voltsN, volt);


  
 }
 LeftFront.setBrake(coast);
  leftmiddle.setBrake(coast);
  LeftBack.setBrake(coast);
  Rightfront.setBrake(coast);
  RightMiddle.setBrake(coast);
  Rightback.setBrake(coast);
}

Can anyone please help me with this.
PS- for some reason ++ does not work for me, so I just use +1

When you check if error equals zero and if timeout equals 150 you use a single equal sign, which is the assignment operator, instead of a double equal sign (==) for checking for equality. Also, adding ++ to the end of a variable already assigns the new value to the variable, so you can replace lines like final = final++ with just final++. Lastly, you never set an initial value to integral, and because whenever you set a new value to it it uses the old value it will never actually be a real number.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.