Pid loop tuning clearly not working at all

I tried to tune my pid loop… i tried everything… the Ziegler–Nichols method doing it myself EVERYTHING but absolutely nothing works to i think its the code so here my code:

double desiredValue = 3000 * 0.8; // in percent
double currSpeed;
int flywheelPID(){
  double kU = 0.1;
  double pU = 0.5;
	double kP = 0.2; // 0.04 0.045 0.07
	double kI = 0.0; // 0.01 0.01 //0.018
	double kD = 19; // 0.45 0.8 1.5 14.5
  double kF =0; //0.0;
	double totalError = 0;
	double lastError = Rotation20.velocity(rpm);
  vex::task testing(SeeIfPID);
	while (PIDenable == true) {
    double currValue = Rotation20.velocity(rpm);
		double currError = desiredValue - currValue;
    double target = desiredValue * kF;
	  totalError += currError;
    if (currError == 0 or currError >= desiredValue){
      //totalError = 0;
    }
    double outputFlywheel=FlywheelPID.getOutput(currValue,desiredValue);
		double diffError = currError - lastError;
		currSpeed = (currError * kP + totalError * kI + diffError * kD + target)/12;
    if (currValue / 3000 * 100 >= 1){
      printf("Flywheel is going at: %f\n",outputFlywheel);

    }
    if (currSpeed <= 0){
     // currSpeed = 0;
    }
    //outputFlywheel = 0;
    LauncherMotorA.spin(forward,currSpeed,volt);
    LauncherMotorB.spin(forward,currSpeed,volt);
    //LauncherMotorA.setVelocity(-currSpeed, percent);
    //LauncherMotorB.setVelocity(-currSpeed, percent);
    lastError = currError;
		task::sleep(20);
  }
  return 1;
}

is something wrong in it?

Firstly, for a flywheel I highly recommend adding kf. Secondly your kd is way way way too high. I recommend just getting rid of it or making it smaller (it should be less than kp). Also your thread structure might be off
What you have

while(PID==true){
PID code
}

What’s probably better

while(true){
if(PID==true){
PID code
}
}
1 Like

I also do not know why your kP is so tiny. I would usually have mine around 3 but i do not know what you are using it for. You must have a massive setpoint. Also, a question for @EcstaticPilot , what is kU, pU, and kF? I have never used these before, although my PID is perfect. I have only used kP, kI, and kD.

kU and pU are both apart of the Ziegler Nichols method. kU is the kp value that causes a period of constant oscillation and pU is the period of that oscillation. These values are used to create a mathematically perfect set of kp, ki, and kd values. kF is the feedforward value. Feedforward multiplies the target. Feedforward is most useful in flywheels due to the fact it prevents the speed from being negative if the error is negative.

1 Like

i tried kf making it lower higher kP higher lower everything but i think im making it badly? Right now I kinda gave up on it but if i have time ill retry to make one for world

Firstly can you explain what your robot is doing. Does it move at all?

yea it does move, my flywheel spins and everything just the pid is literally dying inside