PID not oscillating

I recently made this PID code, but it is not oscillating, instead it is overshooting and exiting the loop. Any solutions

double error, integral, derivative, power, lastError;
  double Kp = 0.9;
  double Ki = 0.12;
  double Kd = 1.5;
  
  error = target - inertial.get_heading();

  while (abs(error) > 1) {
    error = target - inertial.get_heading();
    if(error < 30){
      integral += error;
    }
    

    

    if (integral > 1000) {
      integral = 1000;
    }    
    if (integral < -1000) {
      integral = -1000;
    }  

    derivative = error - lastError;
    lastError = error;  
    power = Kp*error + Ki*integral + Kd*derivative;


    pros::lcd::print(2, "%f", power);

    drivetrainR.move(power);
    drivetrainRB.move(power*-1);
    drivetrainRF.move(power);
    drivetrainL.move(power);
    drivetrainLB.move(power*-1);
    drivetrainLF.move(power);
    pros::lcd::print(1, "%f", inertial.get_heading());

    pros::delay(20);        
  }
  drivetrainR.brake();
  drivetrainRB.brake();
  drivetrainRF.brake();
  drivetrainL.brake();
  drivetrainLB.brake();
  drivetrainLF.brake();

you tell the code to exit the loop if the error is less than or equal to 1, so change that condition.

7 Likes