Pid programing help

  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
}

  //settings
  double kP = 0.000001;
  double kI = 0.000001;
  double kD = 0.000001;
  double turnkP = 0.0;
  double turnkI = 0.0;
  double turnkD = 0.0;

  // autonomous settings
  int desiredvalue = 200;
  int desiredturnvalue = 0;

  int error;//sensor value - desired value = positional value
  int preverror = 0;//position 20 millisecounds ago
  int derivitive;// error - preverror = spped
  int totalerror =  0;//totalerror = totalerror + error

  int turnerror;//sensor value - desired value = positional value
  int turnpreverror = 0;//position 20 millisecounds ago
  int turnderivitive;// error - preverror = spped
  int turntotalerror =  0;//totalerror = totalerror + error

  bool resetdrivesensors = false;

  //varibles modified for use 
  bool enabledrivePID=true;

  int drivePID(){

    while(enabledrivePID){

      if (resetdrivesensors){
          resetdrivesensors = false;

          frontleft.setPosition(0,degrees);
          backleft.setPosition(0,degrees);
          frontright.setPosition(0,degrees);
          backright.setPosition(0,degrees);
      }

      
      //get the positions of all four motors
      int backleftmotorposition = backleft.position(degrees);
      int backrightmotorposition = backright.position(degrees);
      int frontleftmotorposition = frontleft.position(degrees);
      int frontrightmotorposition = frontright.position(degrees);

      ///// lateral movement pid
   
      // get the average of the motors
      int averageposition = (backleftmotorposition + backrightmotorposition + frontleftmotorposition + frontrightmotorposition)/2;

      //potential
      error = averageposition - desiredvalue;

      //derivitive
      derivitive = error - preverror;

      //integral
      totalerror += error;

      int lateralmotorpower = error * kP + derivitive * kD + totalerror * kI;

      //turning movement pid

 // get the average of the motors
      int turndifference = backleftmotorposition + frontleftmotorposition - backrightmotorposition + frontrightmotorposition;

      //potential
      turnerror = turndifference - desiredturnvalue;

      //derivitive
      turnderivitive = turnerror - turnpreverror;

      //integral
      turntotalerror += turnerror;

      int turnmotorpower = turnerror * turnkP + turnderivitive * turnkD + turntotalerror * turnkI;      

      frontleft.spin(forward, turnmotorpower,  velocityUnits::pct);
      backleft.spin(forward, turnmotorpower,  velocityUnits::pct);
      frontright.spin(forward, turnmotorpower,  velocityUnits::pct); 
      backright.spin(forward, turnmotorpower,  velocityUnits::pct);

      frontleft.spin(forward, lateralmotorpower,  velocityUnits::pct);
      backleft.spin(forward, lateralmotorpower,  velocityUnits::pct);
      frontright.spin(forward, lateralmotorpower,  velocityUnits::pct); 
      backright.spin(forward, lateralmotorpower,  velocityUnits::pct);

      preverror = error;
      turnpreverror = turnerror;
      
      vex::task::sleep(20);
    }

  return 1;

}

ok so i am really new to vex coding and i was trying to get pids for my suton and i found a guide but it doesnt seem to work im just looking for some help or some explanation so i can understand this code more and make it work when it did work the robot would just drive forward and would not stop even tho it was givin a value to stop at

I’m not great at coding but your kP, kI, and kD all look way too small. I’d start by looking up PID tuning threads on the forum to learn how you’re supposed to adjust them. But also

You have 4 motors, so if you want their average position then divide by 4, not 2.
Lastly,

I’m pretty sure your desiredvalue should go before averageposition, since averageposition starts at 0 and your desiredvalue is 200

0 - 200 = -200

Hopefully some part of this fixes it

2 Likes

i have changed what you mentioned in my code and the robot does not move at all do you have any other sugestions