ROBOTC.net forums
http://www.robotc.net/forums/

Cancel Autonoumous Button (Answered)
http://www.robotc.net/forums/viewtopic.php?f=11&t=11762
Page 1 of 1

Author:  Samuel Crouch [ Fri Jul 24, 2015 3:51 pm ]
Post subject:  Cancel Autonoumous Button (Answered)

I'm relatively new to RobotC, and would like some help writing a program that does what I want it to.
What I want to do is to have an autonomous program running, and have a button that I can press at any point to cancel the program / switch it over to driver control.
Here is the current program (I don't have access to a robot to test it right now, but I know that it doesn't work properly):

task main( ) {

void checkbtn( ) // Function that checks if the button is pressed
{
if(SensorValue[button] == 1) // Skips the rest of the if the button is pressed
break;
}


do {
// first part of code
checkbtn;
// second part of code
checkbtn;
// Etcetera...
} while(false); // Goes through the code once and doesn't repeat. Do-while is just here so that the break command works

// Driver control / End of program
}


I want the program to be constantly checking if the button is pressed, not just when in between sections of the autonomous.
Help would be much appreciated!

Author:  Tabor473 [ Fri Jul 24, 2015 4:24 pm ]
Post subject:  Re: cancel autonoumous button

So the simplest way to do what you want is to utilize multitasking. Remember to read this thread on how to use them correctly. http://www.robotc.net/forums/viewtopic.php?t=3341

Code:
task autonomous(){
   //autonomous code
   forward(); //etc
}
task driverControl(){
   //driver code
   stopAllMotors();
   while(1){//always drive
      tankControl();//etc
   }
}

task main()
{
   startTask (autonomous);
   while(SensorValue[button]!=1){wait1Msec(50);}//waits until button pressed
   stopTask(autonomous);//ends autonomous
   startTask(driverControl);//begins driver control
   while(1){}//stops the program from ending

}

This is a simple program showing how to use tasks in this way. Driver control could very easily have been inside of the while(1) in the main task but I preferred the symmetry between autonomous and driver both being tasks. This allows you to have something else easily kill the driver control task to go back to autonomous. This program will switch out of autonomous if the button is pressed no matter what autonomous is being run at the time.

Author:  Samuel Crouch [ Fri Jul 24, 2015 4:35 pm ]
Post subject:  Re: cancel autonoumous button

Thank you Tabor473.
That helps a lot!

Author:  Samuel Crouch [ Mon Jul 27, 2015 11:31 am ]
Post subject:  Re: Cancel Autonoumous Button (Answered)

That's great, but I just realized that I need it to transfer into the driver control mode automatically when the autonomous mode ends as well. Is there any way to check if the autonomous task has been completed?

Author:  Tabor473 [ Mon Jul 27, 2015 2:33 pm ]
Post subject:  Re: Cancel Autonoumous Button (Answered)

So a couple simple ways. If you know exactly how much time autonomous should take then you can use the built in RobotC timers.

Code:
task autonomous(){
   //autonomous code
   forward(); //etc
}
task driverControl(){
   //driver code
   stopAllMotors();
   while(1){//always drive
      tankControl();//etc
   }
}

task main()
{
   int autonTime=15000;//miliseconds
   startTask (autonomous);
   clearTimer(T1);
   while(SensorValue[button]!=1&&timer1[T1]<autonTime){wait1Msec(50);}//waits until button pressed or time runs out
   stopTask(autonomous);//ends autonomous
   startTask(driverControl);//begins driver control
   while(1){}//stops the program from ending

}

This ends autonomous if the time runs out or if the button is pressed.


The other way is to have a variable finished. This variable is always false and at the end of autonomous you change it to be true. Changing it to true would end the while loop that is idly waiting.

Code:
bool finished=false;
task autonomous(){
   //autonomous code
   forward(); //etc
   
   
   
   
   finished=true;
}
task driverControl(){
   //driver code
   stopAllMotors();
   while(1){//always drive
      tankControl();//etc
   }
}

task main()
{
   startTask (autonomous);
   while(SensorValue[button]!=1&&!finished){wait1Msec(50);}//waits until button pressed or autonomous says it is done
   stopTask(autonomous);//ends autonomous
   startTask(driverControl);//begins driver control
   while(1){}//stops the program from ending

}



EDIT Just thought I would add that the && symbol means that the while loop requires both operations to be true for it to repeat.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/