How to write code to accelerate and decelerate drive train code for autonomous

It’s probably best to make a separate function for the PID that you can call whenever you want. This way you can just pass the targetPos as a parameter to the function.

void drivePID(double targetPos){
  //this is a constant that you will need to tune
  double kP = 0.1; 
  //error is just the difference between target and current pos
  //here I am just getting it from the front left motor, but it doesn't really matter which one you use
  double error = targetPos - FrontLeft.position(deg); 
  //accuracy is how close you want the robot to get to the target; if it is too small it will take too much           time to reach the target, but if it's too large it won't be accurate
  //this is needed because the robot will pretty much never be exactly at the target position
  double accuracy = 5; 
  //run the loop while the robot is not at target position
  while (abs(error) > accuracy){
    //update error
    error = targetPos - FrontLeft.position(deg);
    //calculate output power by multiplying error by kP
    double pow = error * kP;
    //assign the power to the motors
    FrontLeft.setVelocity(pow, pct);
    FrontRight.setVelocity(pow, pct);
    BackLeft.setVelocity(pow, pct);
    BackRight.setVelocity(pow, pct);

    FrontLeft.spin();
    FrontRight.spin();
    BackLeft.spin();
    BackRight.spin();
  }
  //stop when done
  FrontLeft.stop();
  FrontRight.stop();
  BackLeft.stop();
  BackRight.stop();
}

Then, you can just call it in your auton whenever you want to use it.

void autonomous(){
  drivePID(500);
}