# Motor.rotation(rev) consistently printing 0

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;
}
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)
}``````

The problem is that the compiler is looking for “rotationUnits” type, and rev is a directionType. Just switch rev with deg and it should work fine.

Place [ code ] before your code, and [ /code ] after the code. (Without the spaces)

But revolutions printed fine when in driver control. Also, after trying that nothing happened, still printed 0.

Not sure why it isn’t printing (deg and rev should work) . You might try using rotationUnits::rev - I have found some cases where the natural language or brief version did not work.

Couple things about your PID. You have no wait in the while loop, so it will count to 500 very fast and do almost nothing. You are trying to get to 0 error in a calculation that will have a large decimal place precision, so 0 has very little chance of ever being true. There is no filter to cap speed at a reasonable number, so the first values will be max velocity and likely will make your wheels slip at the start of your motion. GL.

1 Like

Oh, looking again… you set error to 0 then check for !0 in your while loop. Loop will never run. Need to calculate the first error prior to the loop.

@Omerktosi, please, be so kind to include your full code wrapped in the `[code]...[/code]` tags next time instead of the screenshot.

Your kD and kI are zero and fast cycle should not affect result but, please, follow @DougMoyers advice and include `vex::task::sleep(10)` into your loop. Without timeout you overflow cout buffers even before robot starts to move.

After you add timeout , please, check first that your motors are not returning same size but opposite sign rotation values that always add up to zero.