VEX P loop not working

My team coded this p loop to help stop our robot from jerking when it stoped, we don’t know whats wrong. Our drive moves backward and clamp just does random things
Here is our code

float currentPosition = ((back_L.rotation(deg) + back_R.rotation(deg) + front_L.rotation(deg) + front_R.rotation(deg)) / 4);
  float Target = 1320;
  float error = Target - currentPosition;

 float k = 0.008;
  while (error < Target) {
    currentPosition = ((back_L.rotation(deg) + back_R.rotation(deg) + front_L.rotation(deg) + front_R.rotation(deg)) / 4);
    error = Target - currentPosition;

    rightside.spin(reverse, (error * k), volt);
    leftside.spin(fwd, (error * k), volt);
    wait(20, msec); 

edit: code tags added by mods

Sorry, my teammate did not expain what is going wrong. Basically it reverts the auton and the robot just runs into the wall going backwards. We tried changing the direction, but it still did not work. It makes the clamp go reverse too even if we change the direction of that.

When debugging issues like this you really need to monitor the values of your variables are as the loop goes around. Usually, the easiest way to do this is to print the values to the terminal/console using printf() statements.

When you do, you’ll probably see something unexpected. Perhaps the .rotation() values being returned are getting smaller or are negative, rather than increasing as you are expecting.

Also, typically you would want to stop (exit the while loop) when the current >= target, or error <= 0.

1 Like