Autonomous hard stop

I’ve been thinking about how to do this and I’m not quite sure how to go about it. What I’m wanting to do is make a sort of function that can automatically shut off our autonomous program if certain shaft encoders have not changed in a set amount of time. I would start out with code like this:

void forward(int speed, int rotations)
{
sensorValue[shaftR] = 0;
sensorValue[shaftL] = 0;

   while(sensorValue[shaftR] < rotations)
   {
     motor[drive1] = speed;
     motor[drive2] = speed;
     motor[drive3] = speed;
     motor[drive4] = speed;
   }
     motor[drive1] = 0;
     motor[drive2] = 0;
     motor[drive3] = 0;
     motor[drive4] = 0;
}

This isn’t my actual code but I needed something for reference. I’m wanting some function that I can put in the while loop so that the “hard stop” function will only be working while I’m running an autonomous routine. So, could I maybe do something like:


sensorValue[shaftEncoder] = 0;

if(sensorValue[shaftEncoder] < rotations && timer[T1] > however long my function is)
{
motorStop();
}

Looking at this I should be able to just integrate this into each function. What’s everyone’s thought on this? Any suggestions? The reason I’m wanting to make this is in case we or our partner run the wrong autonomous routine and we catch on one another then the autonomous routine wouldn’t burn out our motors for the rest of the match.

If you want to stop the whole autonomous routine completely, then just execute


StopTask(autonomous);

inside the autonomous program

@NattyMan0007’s answer is probably best.

However, you can also put the entirety of the autonomous routine in a function, in the actual autonomous block/task/whatever call into your autonomous routine, then write a while loop that signals all motors to stop repeatedly.

The while loop is just to ensure the autonomous task doesn’t end before its suppose to.

Now inside your new fancy autonomous function you can return whenever you please, instantly ending the routine.

void actualAuton() {

	// Stuff...

	// Oops I did it again...
	if(playedWithYourHeart)
		return;

}

task autonomous() {

	actualAuton();

	while(true)
		stopAllMotors();
	
}

This way works better if you want to try to do something else in that time as you maintain control of the program execution.

Last year at worlds we had to add this functionality to our auton right before a match, so we did it quickly by just adding


if(Timer[T1] > 2500) wait1ms(9999999);

to all of our while loops so it would look like this


ClearTimer(T1);
motor[drive] = 127;
while(sensorvalue<desiredvalue)
{
    if(Timer[T1] > 2500) wait1ms(9999999);
}
motor[drive] = 0;

This might not be the best way to go about it, but it was the fastest