Go to Post We want to be a positive force in our community, and maybe to encourage our country’s best and brightest to go to work for the ones who’ve put themselves in harm’s way. - Jack Jones [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
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 20-11-2015, 02:25
xmutantson xmutantson is offline
Registered User
FRC #5683
 
Join Date: Nov 2015
Location: Auburn, WA
Posts: 2
xmutantson is an unknown quantity at this point
How to time an action in teleop without delaying teleop.

So me and my team are programming in java, and as head of programming I kind of have to have it together. (I don't. I am a Sophomore and we are a 2nd year team.) What I'm trying to do is trigger a single solenoid, wait for a short time like 65-100 MS and then close the solenoid again, but I'm not sure how to do that without hitting my teleop routine with a 100 MS lag spike in which the whole teleop loop is delayed when the solenoid is opened up. Thanks!
-5683 Programming Dept.
Reply With Quote
  #2   Spotlight this post!  
Unread 20-11-2015, 09:10
Ben Wolsieffer Ben Wolsieffer is offline
Dartmouth 2020
AKA: lopsided98
FRC #2084 (Robots by the C)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Manchester, MA (Hanover, NH)
Posts: 520
Ben Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud of
Re: How to time an action in teleop without delaying teleop.

Are you using the Command framework, or just SampleRobot?

If you are using commands, I would create a Command with a 100 ms timeout, which would open the solenoid in the initialize() method, and close it in the end() method.

If you are just using a simple teleop loop, then it is a little more complicated. Here is some psuedo code that should give you an idea of how to do it:
Code:
// Class variables (their values must persist between iterations of the loop)
boolean solenoidOpen = false;
long solenoidOpenTime;

// Runs on each iteration of the teleop loop
if (!solenoidOpen) {
    if(solenoidShouldBeTriggered) {
        openSolenoid();
        solenoidOpened = true;
        solenoidOpenTime = System.currentTimeMillis();
    }
} else {
    if (System.currentTimeMillis() - solenoidOpenTime > 100) {
         closeSolenoid();
         solenoidOpen = false;
    }
}
If you want more clarification, just ask.
__________________



2016 North Shore District - Semifinalists and Excellence in Engineering Award
2015 Northeastern University District - Semifinalists and Creativity Award
2014 Granite State District - Semifinalists and Innovation in Control Award
2012 Boston Regional - Finalists

Last edited by Ben Wolsieffer : 20-11-2015 at 11:28. Reason: Clarification
Reply With Quote
  #3   Spotlight this post!  
Unread 20-11-2015, 11:15
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 103
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: How to time an action in teleop without delaying teleop.

You can use java.util.Timer and java.util.TimerTask to schedule something to happen in the future while your program still runs.

Code:
// local or class variables
final int solenoidDelay = 65; // milliseconds
final Timer myTimer = new Timer();
final TimerTask closeSolenoidTask = new TimerTask() {

    @Override
    public void run() {
        closeSolenoid();
    }
};

...

mySolenoid.open();
myTimer.schedule(closeSolenoidTask, solenoidDelay);
__________________
WPILib
GRIP, RobotBuilder

Last edited by SamCarlberg : 20-11-2015 at 11:17.
Reply With Quote
  #4   Spotlight this post!  
Unread 20-11-2015, 11:43
Ben Wolsieffer Ben Wolsieffer is offline
Dartmouth 2020
AKA: lopsided98
FRC #2084 (Robots by the C)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Manchester, MA (Hanover, NH)
Posts: 520
Ben Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud of
Re: How to time an action in teleop without delaying teleop.

Quote:
Originally Posted by SamCarlberg View Post
You can use java.util.Timer and java.util.TimerTask to schedule something to happen in the future while your program still runs.
This method would work in this case (assuming the Solenoid methods are thread-safe), but I would be worried about possible race conditions if more complicated logic were to later be added in the TimerTask.

Also: Hi Sam!
__________________



2016 North Shore District - Semifinalists and Excellence in Engineering Award
2015 Northeastern University District - Semifinalists and Creativity Award
2014 Granite State District - Semifinalists and Innovation in Control Award
2012 Boston Regional - Finalists
Reply With Quote
  #5   Spotlight this post!  
Unread 20-11-2015, 12:01
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 103
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: How to time an action in teleop without delaying teleop.

Quote:
Originally Posted by lopsided98 View Post
This method would work in this case (assuming the Solenoid methods are thread-safe), but I would be worried about possible race conditions if more complicated logic were to later be added in the TimerTask.

Also: Hi Sam!
Hey Ben!

Solenoid methods are thread-safe. I'd be more worried about scheduling a close, doing some stuff, then opening the solenoid (and expecting it to stay open) before the scheduled close happens. In this case, the methods for opening/closing the solenoid should have checks to see if it's been claimed by a different call.

Code:
private boolean claimed = false;

public void openSolenoid() {
    if (!claimed) {
        claimed = true;
        // open it
    }
}

public void closeSolenoid() {
    if (claimed) {
        claimed = false;
        // close it
    }
}

But I don't think it's a problem for something this simple.
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #6   Spotlight this post!  
Unread 26-11-2015, 16:50
origamitaco origamitaco is offline
Registered User
FRC #4328
 
Join Date: Nov 2015
Location: Richmond, TX
Posts: 35
origamitaco is an unknown quantity at this point
Re: How to time an action in teleop without delaying teleop.

Definitely need to switch to command-based robot and don't forget to use requires(); with your commands and subsystems, otherwise your whole robot will still pause. WPIlib documentation should be able to take it from there.
Reply With Quote
  #7   Spotlight this post!  
Unread 27-11-2015, 08:42
JesseK's Avatar
JesseK JesseK is online now
Expert Flybot Crasher
FRC #1885 (ILITE)
Team Role: Mentor
 
Join Date: Mar 2007
Rookie Year: 2005
Location: Reston, VA
Posts: 3,684
JesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond repute
Re: How to time an action in teleop without delaying teleop.

Quote:
Originally Posted by SamCarlberg View Post
Hey Ben!

Solenoid methods are thread-safe. I'd be more worried about scheduling a close, doing some stuff, then opening the solenoid (and expecting it to stay open) before the scheduled close happens. In this case, the methods for opening/closing the solenoid should have checks to see if it's been claimed by a different call.


But I don't think it's a problem for something this simple.
It can be solved by limiting the exposure of the solenoid to a class that governs the entire mechanism, then by calling TimerTask.cancel() to eliminate the auto-close when the mechanism is told to do something different.
__________________

Drive Coach, 1885 (2007-present)
CAD Library Updated 5/1/16 - 2016 Curie/Carver Industrial Design Winner
GitHub
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 11:50.

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