PROS not waiting for auton functions to finish

After a lot of troubleshooting steps, I’ve narrowed down that PROS is only running the last autonomous command that I give to it. It is reading the previous commands, but not waiting for the motors to actually finish spinning. Here’s a picture of the auton, and the functions that I use. (the strafe and turn functions are set up the same way as the drive function, they just have different + and - for the values)

Screen Shot 2020-05-22 at 11.12.16 AM
Screen Shot 2020-05-22 at 11.11.57 AM

Thanks for any help!

https://pros.cs.purdue.edu/v5/api/cpp/motors.html#move-relative

This function simply sets the target for the motor, it does not block program execution until the movement finishes. The example code shows how to block until a movement is finished.

What you need is something like this:

void Drive(double distance, int speed)
{
  leftFront. move_relative(distance, speed);
  rightFront.move_relative(distance, speed);
  leftBack.  move_relative(distance, speed);
  rightBack. move_relative(distance, speed);
  while(1)
  {
     double avgPos = (
             leftFront. get_position() +
             rightFront.get_position() +
             leftBack.  get_position() +
             rightBack. get_position() ) / 4.0;

     if( fabs(avgPos-distance)<5.0 ) break;

     pros::delay(2);
  }
}

For additional info, please, look here, here, and here.

1 Like

What is the 1 that goes in the while loop? Like the while(1)

Ok I tried that code. Now what’s happening I think is it’s not exiting the first loop to move onto the turn command. It’ll just run whatever the first command is.

The PROS documentation for the move_relative function includes a method to fix this problem:

Screen Shot 2020-05-22 at 3.36.38 PM
For your code, I would do something like this:

void drive(int inches, int speed){
	leftFront.tare_position();
	rightFront.tare_position();
	leftBack.tare_position();
	rightBack.tare_position();
	leftFront.set_brake_mode(pros::E_MOTOR_BRAKE_COAST);
	rightFront.set_brake_mode(pros::E_MOTOR_BRAKE_COAST);
	leftBack.set_brake_mode(pros::E_MOTOR_BRAKE_COAST);
	rightBack.set_brake_mode(pros::E_MOTOR_BRAKE_COAST);
	leftFront.move_relative(inches * 50, speed);
	rightFront.move_relative(inches * 50, speed);
	leftBack.move_relative(inches * 50, speed);
	rightBack.move_relative(inches * 50, speed);
	double avgPos = (
             leftFront.get_position() +
             rightFront.get_position() +
             leftBack.get_position() +
             rightBack.get_position() ) / 4.0;
/**
 * I'm using your inches to motor encoder unit distance conversion, along 
 * with a threshold of 20 motor encoder units for the loop to terminate
 */
	while(abs(avgPos - inches * 50) > 20){
		pros::delay(2);
		avgPos = (
      		     leftFront.get_position() +
       	 	     rightFront.get_position() +
        		     leftBack.get_position() +
    		     rightBack.get_position() ) / 4.0;
	}
}

In theory, this should delay the function from returning until the robot has moved to within the given threshold. I’m not sure if this is necessarily the best practice in this situation, but the PROS team presents this solution, so it should work fine for your needs.

3 Likes