View unanswered posts | View active topics It is currently Tue Sep 16, 2014 7:25 am






Reply to topic  [ 6 posts ] 
Going in and out of an autonoumous program. 
Author Message
Rookie

Joined: Tue Oct 09, 2012 8:38 pm
Posts: 4
Post Going in and out of an autonoumous program.
Could I use this code to go into a linetracking program when a button is pressed, then leave the linetracking program when the same button is pressed? Or will it infinitely loop? If I can't, what can I do differently?

Code:
while (true) {
 motor[port2] = motor[port4] = vexRT[Ch2]; // Teleoperated
 motor[port3] = motor[port5] = vexRT[Ch3]; // Teleoperated
 if (vexRT[Btn8D] == 1) {                  // Run the loop whn button is pressed
  while (vexRT[Btn8D] == 0) {              // Run the program when the button is depressed, when it is pressed it leave the loop
   //Do linetracking
  }
 }
}


Thanks!


Tue Oct 09, 2012 8:48 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 574
Post Re: Going in and out of an autonoumous program.
You are very close with the code that you have, and are definitely on the right track. The only issue I am seeing; what happens when you press the 8D button? The program will check to see if it's pressed in the 'if' statement, and if it is will immediately check the button status again to see if it is released (if it is, then it does the linetracking segment of the code). I'm not sure how fast your thumbs are, but I'm pretty sure the microcontroller will switch from the if statement to the while statement before you can pull your finger off the button :)

Alternatively, you can put the linetracking code in the while loop, change the while loop's condition to a true, and remove the if statement entirely:

Code:
while (true)
{
while (vexRT[Btn8D] == 1)  // Run the program when the button is depressed, when it is pressed it leave the loop
  {             
   //Do linetracking
  }

 motor[port2] = motor[port4] = vexRT[Ch2]; // Teleoperated
 motor[port3] = motor[port5] = vexRT[Ch3]; // Teleoperated
}


The modified program above will start into an infinite loop. Inside the infinite loop, it will check for button 8D being pressed and if so will start the linetracking segment (and continue to run it as long as the button is held down). If button 8D is not pressed or is released, the program will skip the linetracking segment and go to the teleop part of the program. It will then go back to the beginning of the infinite loop and keep running indefinitely.

Code:
while (true)
{
if (vexRT[Btn8D] == 1)  // Run the program when the button is depressed, when it is pressed it leave the loop
  {             
   //Do linetracking
  }
else
{
 motor[port2] = motor[port4] = vexRT[Ch2]; // Teleoperated
 motor[port3] = motor[port5] = vexRT[Ch3]; // Teleoperated
}
}


You can also use an If-Else statement to check if the button is pressed or not and have the program act accordingly. This is generally a preferred method (for ease of scalability in larger programs) but in this case will act the same way as the double while loops (the program just executes a little bit differently).

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Last edited by JohnWatson on Wed Oct 10, 2012 12:05 pm, edited 1 time in total.



Wed Oct 10, 2012 10:03 am
Profile
Rookie

Joined: Tue Oct 09, 2012 8:38 pm
Posts: 4
Post Re: Going in and out of an autonoumous program.
JohnWatson wrote:
The modified program above will start into an infinite loop. Inside the infinite loop, it will check for button 8D being pressed and if so will start the linetracking segment (and continue to run it as long as the button is held down). If button 8D is not pressed or is released, the program will skill the linetracking segment and go to the teleop part of the program. It will then go back to the beginning of the infinite loop and keep running indefinitely.


Yea, I was affraid it would do that, but I didn't have my robot available to try it.

Thanks for explaining!


Wed Oct 10, 2012 11:32 am
Profile
Rookie

Joined: Tue Oct 09, 2012 8:38 pm
Posts: 4
Post Re: Going in and out of an autonoumous program.
At closer look, those programs would enter the linetracking when button 8D is pressed, but would not exit it when 8D is pressed (maybe I misunderstood)? How would I accomplish that task?


Wed Oct 10, 2012 5:56 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 574
Post Re: Going in and out of an autonoumous program.
Having it toggle will be a bit trickier. Using the code behind the 'untilBump' Natural Language command, I was able to throw together this little doozy;

Code:
task main()
{
   
   int delayTimeMS = 50;
   
   while (true)
      {
      while(vexRT[btn8D] != 1) //Switch is not pressed
         {
         motor[port2] = motor[port4] = vexRT[Ch2]; // Teleoperated
           motor[port3] = motor[port5] = vexRT[Ch3]; // Teleoperated
         }
         
      wait1Msec(delayTimeMS); //Debounce pause

     while(vexRT[btn8D] == 1) //Wait for release
        {
           wait1Msec(1);
        }
        
        wait1Msec(delayTimeMS); //Debounce pause
        
     while(vexRT[btn8D] != 1) //Switch is not pressed
         {
         //Linetracking code
         }
         
      wait1Msec(delayTimeMS); //Debounce Pause

     while(vexRT[btn8D] == 1) //Wait for release
        {
           wait1Msec(1);
        }
        
     wait1Msec(delayTimeMS); //Debounce pause
      }
}


The first thing I did was declare an integer variable called 'delayTimeMS'. This variable is used to compensate for a property of switches called 'bouncing', which is when a switch (physically nearing the other 'prong' internally) rapidly bounces between an open and closed state. You can't see it normally (it happens very quickly) but when you push a switch its output is actually similar to this:

000000000000000000001010101010101111111111111111111111111111101010101010000000000000000000000000

Where 0's are the switch being open or 'not pressed' and 1's are the switch being closed or 'pressed'. We'll get back to how this used in the code soon.

The first while loop puts the robot in tele-op mode while the 8D button is not pressed. Once the Cortex sees the button first being pressed (or the first '1' value if using the example above), it kicks out of the while loop and enters a 50 ms 'debounce' state, which allows it to ignore the rapid 010101 switching. When the debounce period is over it waits for the button to be released, which kicks it out of the second while loop.

At this point you have physically pressed and released the switch. The robot now enters the third while loop (which contains the line tracking code) and stays inside of it until the button is pressed again. It follows the same 'debounce period -wait for release -debounce period' when the button is pressed, and then the whole program loops back up to the start of the infinite loop, starting the process over again.

If we didn't have the debounce wait command, the Cortex would rapidly switch between the tele-op and line-tracking segments of code because the amount of open and closed states (0's and 1's) is completely random when the switch is bouncing. The debounce period is switch dependant as well, so you may have to tweak that value to match your physical robots. Ideally, you would want it as low a value as possible without having any bouncing issues, but it's much better to be safe than sorry.

I highly suggest taking the time to go through our Cortex Video Trainer as it covers a great many of these topics. The knowledge you can gain from them is invaluable as a ROBOTC coder.

Wikipedia also has a great segment on switch bouncing you might want to check out.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Thu Oct 11, 2012 4:23 pm
Profile
Rookie

Joined: Tue Oct 09, 2012 8:38 pm
Posts: 4
Post Re: Going in and out of an autonoumous program.
Ah, ok. That was clever going to the untilBump function :-) .

Thanks!


Thu Oct 11, 2012 8:18 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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.