Long lags / pauses / delays before and after 90-degree turns

New programmer here – using RobotC on a VEX IQ kit. We’re just trying to program the robot to complete a basic (what we thought would be easy) autonomous square. It runs OK, but we experience long pauses / lags before and after the turns. Can someone offer guidance on what we’re doing wrong? TIA

#pragma config(Motor, motor1, left, tmotorVexIQ, PIDControl, encoder)
#pragma config(Motor, motor2, right, tmotorVexIQ, PIDControl, reversed, encoder)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{

resetMotorEncoder(left);
resetMotorEncoder(right);

setMotorTarget(left, 1180, 50);
setMotorTarget(right, 1180, 50);

repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));



setMotorTarget(left, -500, 50);
setMotorTarget(right, 500, 50);

repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));



resetMotorEncoder(left);
resetMotorEncoder(right);

setMotorTarget(left, 1180, 50);
setMotorTarget(right, 1180, 50);

repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));


resetMotorEncoder(motor2);

setMotorTarget(motor2, 550, 50);

repeatUntil(getMotorEncoder(motor2) == getMotorTarget(motor2));



resetMotorEncoder(left);
resetMotorEncoder(right);

setMotorTarget(left, 1180, 50);
setMotorTarget(right, 1180, 50);

repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));


resetMotorEncoder(motor2);

setMotorTarget(motor2, 500, 50);

repeatUntil(getMotorEncoder(motor2) == getMotorTarget(motor2));



resetMotorEncoder(left);
resetMotorEncoder(right);

setMotorTarget(left, 1180, 50);
setMotorTarget(right, 1180, 50);

repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));

Possibly because it is waiting for the error to be exactly 0 for both motors, and that takes a lot of time. Sometimes you need to put a threshold to make it exit faster. Consider making a function to simplify the process.

void waitForSettle() {
  int leftError = 0;
  int rightError = 0;
  int threshold = 2; //tolerance
  bool completed = false; //the loop will exit once this is true
  //this is a do-while loop, it will run at least once to do calculations before exiting
  do { 
    leftError = getMotorEncoder(left) - getMotorTarget(left); //if they are equal, error will be 0
    rightError = getMotorEncoder(right) - getMotorTarget(right);
    //completed will be true if both motors are threshold away from target, regardless of direction
    completed = abs(leftError) < threshold && abs(rightError) < threshold; 
  } while(!completed); //loop while not completed
}
1 Like

Couple things. Good start…
Lotsa ways to do most things, but have to pick one or the other.

1st, no reset before the 1st turn, maybe cut/paste error when posting?
2nd, later turns only use one motor.
3rd, testing for == is too narrow, try >= and <=. This causes the lag…

The post above Is a perfect solution for controllable accuracy.

4th (minor) helpful to name consistently (right vs motor2) - and to use comments.
Finally, how could you use this?:

repeat(numberOfTimes)
{
  body
}

Rewrote your version for clarity, below.

task main()
{
// 1st straight
resetMotorEncoder(left);
resetMotorEncoder(right);
setMotorTarget(left, 1180, 50);
setMotorTarget(right, 1180, 50);
repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));

// turn left
setMotorTarget(left, -500, 50);
setMotorTarget(right, 500, 50);
repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));

// 2nd straight
resetMotorEncoder(left);
resetMotorEncoder(right);
setMotorTarget(left, 1180, 50);
setMotorTarget(right, 1180, 50);
repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));

// 2nd turn left
resetMotorEncoder(right);
setMotorTarget(right, 550, 50);
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));

// 3rd straight
resetMotorEncoder(left);
resetMotorEncoder(right);
setMotorTarget(left, 1180, 50);
setMotorTarget(right, 1180, 50);
repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));

// 3rd turn left
resetMotorEncoder(right);
setMotorTarget(motor1, 500, 50);
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));

// 4th straight
resetMotorEncoder(left);
resetMotorEncoder(right);
setMotorTarget(left, 1180, 50);
setMotorTarget(right, 1180, 50);
repeatUntil(getMotorEncoder(left) == getMotorTarget(left));
repeatUntil(getMotorEncoder(right) == getMotorTarget(right));
}

What’s your bot look like (width, wheelbase)? 500 turn not near 90 degrees on my standardbot.
Let us know how it goes.

1 Like

Thank you to both of you for your quick responses to our inquiry. We were able to incorporate code from the second suggestion and it is greatly improved. Thank you – what a supportive community!

1 Like

All good, thanks for feedback. Go for loops and functions, too.

Curious, what +/- motor degrees worked to get you 90 degree bot turn?