View unanswered posts | View active topics It is currently Mon Dec 17, 2018 2:09 pm






Reply to topic  [ 5 posts ] 
Cancel Autonoumous Button (Answered) 
Author Message
Rookie
User avatar

Joined: Fri Jul 24, 2015 2:51 pm
Posts: 3
Location: Comox Valley, BC
Post 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!

_________________
VRC Team 7842D


Last edited by Samuel Crouch on Fri Jul 24, 2015 4:50 pm, edited 1 time in total.



Fri Jul 24, 2015 3:51 pm
Profile
Moderator
Moderator

Joined: Tue May 19, 2015 3:07 pm
Posts: 91
Post 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.


Fri Jul 24, 2015 4:24 pm
Profile
Rookie
User avatar

Joined: Fri Jul 24, 2015 2:51 pm
Posts: 3
Location: Comox Valley, BC
Post Re: cancel autonoumous button
Thank you Tabor473.
That helps a lot!

_________________
VRC Team 7842D


Fri Jul 24, 2015 4:35 pm
Profile
Rookie
User avatar

Joined: Fri Jul 24, 2015 2:51 pm
Posts: 3
Location: Comox Valley, BC
Post 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?

_________________
VRC Team 7842D


Mon Jul 27, 2015 11:31 am
Profile
Moderator
Moderator

Joined: Tue May 19, 2015 3:07 pm
Posts: 91
Post 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.


Mon Jul 27, 2015 2:33 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.