Just the sight of you makes my vision system go haywire.
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 12-02-2017, 21:22
Skammerman0 Skammerman0 is offline
Registered User
FRC #5199
 
Join Date: Feb 2017
Location: Laguna Niguel, California
Posts: 3
Skammerman0 is an unknown quantity at this point
Breaking loops when switching to tele-op?

Our drivers are worried about the robot getting stuck in a while loop during autonomous, rendering the robot completely unusable for the rest of the match.
Do loops automatically break when the robot is automatically switched to tele-op or is this a legitimate concern? Thanks!
Reply With Quote
  #2   Spotlight this post!  
Unread 12-02-2017, 21:31
virtuald's Avatar
virtuald virtuald is online now
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,117
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Breaking loops when switching to tele-op?

Quote:
Originally Posted by Skammerman0 View Post
Our drivers are worried about the robot getting stuck in a while loop during autonomous, rendering the robot completely unusable for the rest of the match.
Do loops automatically break when the robot is automatically switched to tele-op or is this a legitimate concern? Thanks!
Your drivers have a legitimate concern. Never use while loops in autonomous, there are better approaches (search the forums, this comes up a lot).
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #3   Spotlight this post!  
Unread 12-02-2017, 21:51
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,782
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Breaking loops when switching to tele-op?

You don't mention what language you are using. LabVIEW aborts the autonomous when the period ends. It will break out of loops.

I don't believe the other language frameworks do that.

Greg McKaskle
Reply With Quote
  #4   Spotlight this post!  
Unread 12-02-2017, 21:53
elijah1111's Avatar
elijah1111 elijah1111 is online now
Registered User
FRC #4601 (Circuit Birds)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Ohio
Posts: 39
elijah1111 is an unknown quantity at this point
Re: Breaking loops when switching to tele-op?

What you could do is look for the Tele start signal in your loop and if it sees it break.
Reply With Quote
  #5   Spotlight this post!  
Unread 12-02-2017, 21:56
ollien ollien is offline
Registered User
FRC #5202
 
Join Date: Feb 2015
Location: United States
Posts: 358
ollien is just really niceollien is just really niceollien is just really niceollien is just really nice
Re: Breaking loops when switching to tele-op?

Quote:
Originally Posted by Greg McKaskle View Post
You don't mention what language you are using. LabVIEW aborts the autonomous when the period ends. It will break out of loops.

I don't believe the other language frameworks do that.

Greg McKaskle
Java does not. The "SampleRobot" base class has the following while loop in its autonomous function, which is only called once.

Code:
while(isOperatorControl() && isEnabled()) {
Once teleop starts, this while loop breaks because of the condition of the loop. I HIGHLY recommend using an iterative robot instead of SampleRobot. It handles this nonsense for you.

You can test out if your code breaks by using the "practice" mode in driver station.


As a rule of thumb: while trues are a sin 'round these parts.
Reply With Quote
  #6   Spotlight this post!  
Unread 12-02-2017, 22:48
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,609
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Breaking loops when switching to tele-op?

Quote:
Originally Posted by ollien View Post
I HIGHLY recommend using an iterative robot instead of SampleRobot. It handles this nonsense for you.
Iterative robot does not break out of loops for you. Rather it moves the loop into the framework. If you have your own loop in iterative, you'll have the same problem as sample.
Reply With Quote
  #7   Spotlight this post!  
Unread 12-02-2017, 23:51
Jaci's Avatar
Jaci Jaci is offline
Registered User
AKA: Jaci R Brunning
FRC #5333 (Can't C# | OpenRIO)
Team Role: Mentor
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Perth, Western Australia
Posts: 268
Jaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond reputeJaci has a reputation beyond repute
Re: Breaking loops when switching to tele-op?

I'm going to assume you're using IterativeRobot, since SampleRobot (or implementations of RobotBase) should really only be used if you're 100% certain you know what you're doing.

You'll notice IterativeRobot has both Init and Periodic functions for Robot, Autonomous, Teleop, Test and Disabled (e.g. AutonomousPeriodic, TeleopPeriodic, RobotInit, etc). The Init functions are called when you enter that mode, and the Periodic functions are called on a regular basis. You can think of the Periodic functions as being inside of the loop, and the Init functions above the loop.

The robot main loop follows a process to ensure you don't end up 'locking up' the program in the loop. It goes like this:

Check state (from driverstation) -> If different from the last, call <state>Init -> Call <state>Periodic -> Repeat

During each iteration, the state is checked, and the appropriate periodic function is called. Because of this, it's important not to lock up the Periodic functions (e.g. don't put in your own loops unless they have a set number of iterations, such as iterating over motors).

"But how can I do motor.set(val); sleep(3); motor.set(0)?"
This is fairly simple. Instead of putting a sleep in the periodic function, check if that time has elapsed. Something like this:
Code:
void AutonomousInit() {
   start_time = now();
}
void AutonomousPeriodic() {
   if (now() - start_time < 3) {
      motor.Set(val);
   } else {
      motor.Set(0);
   }
}
This allows you to do time-based functions using the periodic functions. I wouldn't recommend coding these time checks yourself, though. WPILib provides the Command-Based programming system that abstracts this for you, and allows you to code Commands for a discrete action (e.g. DriveForward, LiftArm, etc). I suggest doing some reading on that if you're looking to do autonomous.
__________________
Jacinta R

Curtin FRC (5333+5663) : Mentor
5333 : Former [Captain | Programmer | Driver], Now Mentor
OpenRIO : Owner

Website | Twitter | Github
jaci.brunning@gmail.com
Reply With Quote
  #8   Spotlight this post!  
Unread Yesterday, 00:00
Oblarg Oblarg is offline
Registered User
AKA: Eli Barnett
FRC #0449 (The Blair Robot Project)
Team Role: Mentor
 
Join Date: Mar 2009
Rookie Year: 2008
Location: Philadelphia, PA
Posts: 1,133
Oblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond repute
Re: Breaking loops when switching to tele-op?

Quote:
Originally Posted by Jaci View Post
I'm going to assume you're using IterativeRobot, since SampleRobot (or implementations of RobotBase) should really only be used if you're 100% certain you know what you're doing.

You'll notice IterativeRobot has both Init and Periodic functions for Robot, Autonomous, Teleop, Test and Disabled (e.g. AutonomousPeriodic, TeleopPeriodic, RobotInit, etc). The Init functions are called when you enter that mode, and the Periodic functions are called on a regular basis. You can think of the Periodic functions as being inside of the loop, and the Init functions above the loop.

The robot main loop follows a process to ensure you don't end up 'locking up' the program in the loop. It goes like this:

Check state (from driverstation) -> If different from the last, call <state>Init -> Call <state>Periodic -> Repeat

During each iteration, the state is checked, and the appropriate periodic function is called. Because of this, it's important not to lock up the Periodic functions (e.g. don't put in your own loops unless they have a set number of iterations, such as iterating over motors).

"But how can I do motor.set(val); sleep(3); motor.set(0)?"
This is fairly simple. Instead of putting a sleep in the periodic function, check if that time has elapsed. Something like this:
Code:
void AutonomousInit() {
   start_time = now();
}
void AutonomousPeriodic() {
   if (now() - start_time < 3) {
      motor.Set(val);
   } else {
      motor.Set(0);
   }
}
This allows you to do time-based functions using the periodic functions. I wouldn't recommend coding these time checks yourself, though. WPILib provides the Command-Based programming system that abstracts this for you, and allows you to code Commands for a discrete action (e.g. DriveForward, LiftArm, etc). I suggest doing some reading on that if you're looking to do autonomous.
Note that if you do decide to do the time checks yourself, always compare a difference of time stamps against a value (as is done in the above example). Never directly compare two timestamps (e.g. "now < start_time + 3"). The former is robust to clock overflow. The latter is not.
__________________
"Mmmmm, chain grease and aluminum shavings..."
"The breakfast of champions!"

Member, FRC Team 449: 2007-2010
Drive Mechanics Lead, FRC Team 449: 2009-2010
Alumnus/Technical Mentor, FRC Team 449: 2010-Present
Lead Technical Mentor, FRC Team 4464: 2012-2015
Technical Mentor, FRC Team 5830: 2015-2016
Reply With Quote
  #9   Spotlight this post!  
Unread Yesterday, 00:21
ollien ollien is offline
Registered User
FRC #5202
 
Join Date: Feb 2015
Location: United States
Posts: 358
ollien is just really niceollien is just really niceollien is just really niceollien is just really nice
Re: Breaking loops when switching to tele-op?

Quote:
Originally Posted by Joe Ross View Post
Iterative robot does not break out of loops for you. Rather it moves the loop into the framework. If you have your own loop in iterative, you'll have the same problem as sample.
I know. I'm stating that the problem that any fear of a problem that SampleRobot presents with their while loops can be solved with IterativeRobot.
Reply With Quote
  #10   Spotlight this post!  
Unread Yesterday, 00:48
Skammerman0 Skammerman0 is offline
Registered User
FRC #5199
 
Join Date: Feb 2017
Location: Laguna Niguel, California
Posts: 3
Skammerman0 is an unknown quantity at this point
Re: Breaking loops when switching to tele-op?

Just tested with practice mode (thanks for the idea). Can confirm that it gets stuck for the rest of teleop.
Reply With Quote
  #11   Spotlight this post!  
Unread Yesterday, 22:36
BitTwiddler's Avatar
BitTwiddler BitTwiddler is offline
electronics/programming mentor
AKA: Mr Tanguay
FRC #1726 (N.E.R.D.S.)
Team Role: Mentor
 
Join Date: Oct 2008
Rookie Year: 2006
Location: Sierra Vista, AZ
Posts: 259
BitTwiddler is on a distinguished road
Re: Breaking loops when switching to tele-op?

Quote:
Originally Posted by Greg McKaskle View Post
You don't mention what language you are using. LabVIEW aborts the autonomous when the period ends. It will break out of loops.

I don't believe the other language frameworks do that.

Greg McKaskle
We use LabView. Last year we got stuck in a control loop for Autonomous during an entire match. The problem code was a while loop running in Timed Tasks that never completed its task. If the while loops are executing within the Autonomous VI you should be safe but beware of loops running in Timed Tasks because that VI is running in Teleop as well as Autonomous.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 15:30.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi