setting a timer for an autonomous operation

How would you set a timer for an autonomous operation? The goal is to have the program move on if the operation takes too long, even if the motors haven’t traveled as far as they need to.

Would something like this work? Is there a better way?

void autonomous( void ) {
    Brain.resetTimer();
    Brain.setTimer(15000,timeUnits::msec);
    Motor1.resetRotation();
    Motor2.resetRotation();
    while(Brain.timer(timeUnits::msec)> 12000 && Motor1.rotation(rotationUnits::deg) < 360 )
    {
        Motor1.rotateFor(360,rotationUnits::deg,50,velocityUnits::pct, false);
        Motor2.rotateFor(360,rotationUnits::deg,50,velocityUnits::pct);
    }
        Motor1.rotateFor(-360,rotationUnits::deg,50,velocityUnits::pct, false);
        Motor2.rotateFor(-360,rotationUnits::deg,50,velocityUnits::pct);
}

The simplest way may just be to set the timeout for the motor.


Motor1.setTimeout(5, vex::timeUnits::sec);

That will cause any rotateFor calls (or other blocking calls) to abort after the timeout period.

cool cool cool! Thanks!

Would you have to reset the value for each rotateFor call?
I guess the timeout just carries through the rest of the program and affects the rest of the calls?
or is it a 1 time use setting?

void autonomous( void ) {
    
        Motor1.setTimeout(5000, vex::timeUnits::msec);
        Motor2.setTimeout(5000, vex::timeUnits::msec);
        Motor1.rotateFor(360,rotationUnits::deg,50,velocityUnits::pct, false);
        Motor2.rotateFor(360,rotationUnits::deg,50,velocityUnits::pct);
    
        Motor1.rotateFor(-2500,rotationUnits::deg,50,velocityUnits::pct, false);
        Motor2.rotateFor(-2500,rotationUnits::deg,50,velocityUnits::pct);
}

timeout would be valid for that instance of the motor until you change it again. We initially set to a very long time.

There are a bunch of other options, you could use startRotateFor and then monitor isSpinning to detect completion yourself. something like.


      int timeout = 5000;

      m1.startRotateTo( 360, rotationUnits::deg, 50, velocityUnits::pct );
      m2.startRotateTo( 360, rotationUnits::deg, 50, velocityUnits::pct );

      while( (m1.isSpinning() || m2.isSpinning()) && timeout > 0 ) {
        this_thread::sleep_for(10);
        timeout -= 10;
      }

1 Like