Pros autonomous waiting until previous command is over

So in pros i have been working on my autonomous functions recently, and I need to implement some sort of wait in my functions so the next one will not start in a routine until the previous one is finished. I need this mainly for my drive code. It currently looks like this.
Also this is my first post here so sorry for any issues in the post or anything

void resetPositions()
{
left_drive.tarePosition();
right_drive.tarePosition();

}
void autonDrive(double distance, double targetVelocity)
{

  if(left_front.getPosition() < distance || right_front.getPosition() < distance)
  {
    left_drive.moveVelocity(targetVelocity);
    right_drive.moveVelocity(targetVelocity);
  }
  else if(left_front.getPosition() >= distance && right_front.getPosition() >= distance)

  {
    left_drive.moveVelocity(0);
    right_drive.moveVelocity(0);
    resetPositions();
  }

}

The trouble with just using if statements is that they only operate once. Fortunately, there’s a way to operate a set of statements over and over again until some condition is satisfied. For example, you could have something like this:

while (left_pos < distance || right_pos < distance) {
    // set velocities
    pros::delay(20); // this is necessary so that you don't hog all the processing time
}
// Loop is done, so set velocities to zero and reset positions

The while here signals that we want to keep looping the set of statements until the condition in the parentheses evaluates to true. In English, you could read this example as “while left position is less than position or right position is less than position, do the following…”

(editing the example so it works and filling in the comments with actual statements is left as an exercise for the reader)

5 Likes

Thanks for the help :smiley: i will test it on my bot when i get the chance later today