I’m trying to get basic pid control working on my robot. I followed the guide here: Guide

In order to keep calculating error, I have the robot calculating the average of the revolutions of the left and right motors and subtract that number from the initial error. The issue that pops up is that both motors continously print 0 for the total rotations no matter what I try.

At first I tried creating a constant variable to add up all of the rotations but that stayed 0

I then printed out the rotation at every loop and it was also always 0.

The drivetrain moves the specified distance but the loop never ends because the error stays what it initially was. This is because the motor.rotation(rev) command always prints 0. In driverControl, the command works perfectly fine but fails to in auton. Why is this?

```
//Constants
double kP = 120;
double kI = 0;
double kD = 0;
double inToRev = 4*M_PI; //Wheel has a diameter of 4in. so C=pi*D=pi*4 ; 1 rev = 4*pi inches
void pid(double dist, string direction) {
double error = 0;
double integral = 0;
double derivative;
double previous_error;
double speed;
int time = 0;
rightdt.resetRotation();
leftdt.resetRotation();
while(abs(error) > 0.005 || time < 500) {
error = (dist/inToRev) - (rightdt.rotation(rev) + leftdt.rotation(rev))/2;
integral = integral + error;
if(error == 0) {
integral = 0;
}
if(abs(error) > 40) {
integral = 0;
}
if(abs(error) < 0.005) {
time++;
}
else {
time = 0;
}
derivative = error - previous_error;
previous_error = error;
speed = kP*error + kI*integral + kD*derivative;
rightdt.spin(directionType::fwd, speed, velocityUnits::pct);
leftdt.spin(directionType::fwd, speed, velocityUnits::pct);
cout << rightdt.rotation(rev) << endl;
cout << error << "---" << time << endl;
vex::task::sleep(20);
}
cout << "Done" << endl;
}
void autonomous( void ) {
pid(-48, "straight"); //Drive forward 40in.
//pid((18*M_PI)/4, "right"); //90 degrees = (1/4)(Circumference) = (1/4)(18*pi)
//pid((18*M_PI)/4, "left"); //90 degrees = (1/4)(Circumference) = (1/4)(18*pi)
}
```