Go to Post I bet Andy Baker would totally own JVN in a 1v1 dodgeball fight. - Greg Marra [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 07-01-2014, 15:36
katsauga's Avatar
katsauga katsauga is offline
Lead Programmer
AKA: Marshall Baskin
no team
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Mora, MN
Posts: 86
katsauga has a little shameless behaviour in the past
Question How to make timed sequences in java?

I was slighty curious on how to make a timed sequence in Java. (Ex. Push out piston, wait two second, pull back piston, stop) any help would be great! could be a sub-class? or just do it right on the robot main.
__________________
If life gives you lemons,Make grape juice and sit back and watch the world wonder how you did it
  #2   Spotlight this post!  
Unread 07-01-2014, 15:46
Jared's Avatar
Jared Jared is offline
Registered User
no team
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2012
Location: Connecticut
Posts: 602
Jared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond reputeJared has a reputation beyond repute
Re: How to make timed sequences in java?

If you're interesting in doing stuff like this, I'd recommend using the command based template. You could make a command ExtendPiston and RetractPiston, then make a command group that had ExtendPiston, WaitCommand(2.0), RetractPiston, and it would do what you wanted.
  #3   Spotlight this post!  
Unread 07-01-2014, 16:09
NotInControl NotInControl is offline
Controls Engineer
AKA: Kevin
FRC #2168 (Aluminum Falcons)
Team Role: Engineer
 
Join Date: Oct 2011
Rookie Year: 2004
Location: Groton, CT
Posts: 261
NotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond repute
Re: How to make timed sequences in java?

You should not do it in main. As a general rule, you should not sleep or wait in any main function.

Any sleep() command you provide will cause the current thread to sleep. If this is done in main() the main function will sleep. The SimpleRobot Project and IterativeRobot project are single threaded by default, so if you use either of those constructs, and call the sleep command the entire robot will sleep. You should run your code in a separate thread so you are not sleeping the main thread. Failure to do that will cause your entire robot to wait 2 seconds. You can create threads in Java by implementing the Runnable Interface or Extending one of the many Thread classes in the java.util.concurrancy package. In either case you will overide the "run" method which opens a solenoid, sleeps, and closes the solenoid but executes in its own thread.

(i.e Public MyClass extends Thread{

do stuff

}

As Jarad mentioned, using the CommandBase construct is helpful in this sense because CommandBase is multithreaded by default. So if this double solenoid was apart of an Arm subsystem. You could simply create a CommandGroup which calls the command to open the solenoid, waits 2 seconds, then calls the command which closes the solenoid. Each command/CommandGroup executes in its own thread, therefore any waits or sleeps will not hault the entire program.
__________________
Controls Engineer, Team 2168 - The Aluminum Falcons
[2016 Season] - World Championship Controls Award, District Controls Award, 3rd BlueBanner
-World Championship- #45 seed in Quals, World Championship Innovation in Controls Award - Curie
-NE Championship- #26 seed in Quals, winner(195,125,2168)
[2015 Season] - NE Championship Controls Award, 2nd Blue Banner
-NE Championship- #26 seed in Quals, NE Championship Innovation in Controls Award
-MA District Event- #17 seed in Quals, Winner(2168,3718,3146)
[2014 Season] - NE Championship Controls Award & Semi-finalists, District Controls Award, Creativity Award, & Finalists
-NE Championship- #36 seed in Quals, SemiFinalist(228,2168,3525), NE Championship Innovation in Controls Award
-RI District Event- #7 seed in Quals, Finalist(1519,2168,5163), Innovation in Controls Award
-Groton District Event- #9 seed in Quals, QuarterFinalist(2168, 125, 5112), Creativity Award
[2013 Season] - WPI Regional Winner - 1st Blue Banner
  #4   Spotlight this post!  
Unread 07-01-2014, 16:50
brennonbrimhall brennonbrimhall is offline
Free Agent
AKA: Brennon Brimhall
no team
Team Role: Alumni
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Clifton Park, NY
Posts: 222
brennonbrimhall is a name known to allbrennonbrimhall is a name known to allbrennonbrimhall is a name known to allbrennonbrimhall is a name known to allbrennonbrimhall is a name known to allbrennonbrimhall is a name known to all
Re: How to make timed sequences in java?

Disclaimer: We use Java, and this might change depending the language you use.

It's not the best method when considered computationally, but we just increment an integer variable, and use a typical if statement when using the IterativeRobot template.

Here's an example:

Code:
DoubleSolenoid solenoid = ... ;
int cycles = 0;

public void teleopPeriodic(){
    cycles++;
    if(cycles<40){
        solenoid.set(DoubleSolenoid.Value.kForward);
    }else if(cycles<60){
         //Do nothing
    }else if (cycles<80){
        solenoid.set(DoubleSolenoid.Value.kReverse);
    }
}

public void disabledInit(){
    //Resetting to zero on disable.
    cycles = 0;
}
Of course, you'd need to figure out how many times the cRIO enters teleopPeriodic per second to figure out the correct numbers to compare to above.

Don't worry about running out of int space in Java. An int's maximum value is 2^32-1, and at 50Hz (around what we observed), the cRIO's JVM won't run into errors for just over 994 days, assuming it's still running by then.
__________________
Team 20, 2012-2014: 4 blue banners, 5 medals, and 9 team awards.
Church of Jesus Christ of Latter-day Saints, 2014-2016: Missionary, Colorado Denver South Mission.
  #5   Spotlight this post!  
Unread 07-01-2014, 17:28
gluxon's Avatar
gluxon gluxon is offline
\n
AKA: Brandon Cheng
FRC #0178 (The 2nd Law Enforcers)
Team Role: Leadership
 
Join Date: Apr 2012
Rookie Year: 2011
Location: Connecticut
Posts: 65
gluxon has a spectacular aura aboutgluxon has a spectacular aura aboutgluxon has a spectacular aura about
Re: How to make timed sequences in java?

If you're using the iterative robot template, you could always just use timers.

http://www.wbrobotics.com/javadoc/ed...ibj/Timer.html
  #6   Spotlight this post!  
Unread 07-01-2014, 17:28
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,729
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: How to make timed sequences in java?

I would also highly suggest using Command Based programming with Java, we have been using it for the past couple of years and are very happy with it. Be careful though, it is not multithreaded as NotInControl has stated. It uses a scheduler to run the commands one execution at a time. But, it does use something called a CommandGroup that will allow you to set up a command exactly like you need as Jared stated earlier.

If you'd like a beginners presentation on how to do these things I can post one. Our team gave a workshop presentation on Command Based Java earlier this fall.
  #7   Spotlight this post!  
Unread 08-01-2014, 04:20
NotInControl NotInControl is offline
Controls Engineer
AKA: Kevin
FRC #2168 (Aluminum Falcons)
Team Role: Engineer
 
Join Date: Oct 2011
Rookie Year: 2004
Location: Groton, CT
Posts: 261
NotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond repute
Re: How to make timed sequences in java?

Quote:
Originally Posted by notmattlythgoe View Post
Be careful though, it is not multithreaded as NotInControl has stated. It uses a scheduler to run the commands one execution at a time.
I don't want to de-rail from the OP, and get off topic with this, but I'll offer this:

I believe you are limited your definition of "multi-threading" to spawning multiple threads for the operating system to schedule which is known as "kernel-level" threads but you are forgetting about "user-level" threads.

The CommandBased Robot Structure doesn't spwan multiple operating system threads for commands, instead it opts a "user-level" thread model where the scheduler adds each new command to a linked-list and executes each Command's "run" method sequentially in a round-robin fashion. (To be complete, other operating system threads are spawned for certain items like PID controllers, compressor, some sensors etc. and are managed by the operating system, not the WPI scheduler)

The WPI scheduler apart of the CommandBased construct will automatically remove commands which have finished execution and prevent commands from being added if the requirements for the command are not met or conflict with another commands.

All of these items are done at the application-level instead of the using the operating system to manage threads which can be classified as "user-level" threads.

In both models, time-division multiplexing is being done to share the resources of the single processor on board. Which is the basic definition of concurrency.

If there was only a single thread and the program only ran sequentially without time sharing then you would never need a scheduler.

So whether you want to call it multi-threading or not is your opinion, to the end user concurrency on a single processor is being done, I didn't state all of this before because I didn't want to muddy/confuse my original post.

Hope this helps,
Kevin
__________________
Controls Engineer, Team 2168 - The Aluminum Falcons
[2016 Season] - World Championship Controls Award, District Controls Award, 3rd BlueBanner
-World Championship- #45 seed in Quals, World Championship Innovation in Controls Award - Curie
-NE Championship- #26 seed in Quals, winner(195,125,2168)
[2015 Season] - NE Championship Controls Award, 2nd Blue Banner
-NE Championship- #26 seed in Quals, NE Championship Innovation in Controls Award
-MA District Event- #17 seed in Quals, Winner(2168,3718,3146)
[2014 Season] - NE Championship Controls Award & Semi-finalists, District Controls Award, Creativity Award, & Finalists
-NE Championship- #36 seed in Quals, SemiFinalist(228,2168,3525), NE Championship Innovation in Controls Award
-RI District Event- #7 seed in Quals, Finalist(1519,2168,5163), Innovation in Controls Award
-Groton District Event- #9 seed in Quals, QuarterFinalist(2168, 125, 5112), Creativity Award
[2013 Season] - WPI Regional Winner - 1st Blue Banner
  #8   Spotlight this post!  
Unread 08-01-2014, 06:00
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,729
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: How to make timed sequences in java?

Quote:
Originally Posted by NotInControl View Post
I don't want to de-rail from the OP, and get off topic with this, but I'll offer this:

I believe you are limited your definition of "multi-threading" to spawning multiple threads for the operating system to schedule which is known as "kernel-level" threads but you are forgetting about "user-level" threads.

The CommandBased Robot Structure doesn't spwan multiple operating system threads for commands, instead it opts a "user-level" thread model where the scheduler adds each new command to a linked-list and executes each Command's "run" method sequentially in a round-robin fashion. (To be complete, other operating system threads are spawned for certain items like PID controllers, compressor, some sensors etc. and are managed by the operating system, not the WPI scheduler)

The WPI scheduler apart of the CommandBased construct will automatically remove commands which have finished execution and prevent commands from being added if the requirements for the command are not met or conflict with another commands.

All of these items are done at the application-level instead of the using the operating system to manage threads which can be classified as "user-level" threads.

In both models, time-division multiplexing is being done to share the resources of the single processor on board. Which is the basic definition of concurrency.

If there was only a single thread and the program only ran sequentially without time sharing then you would never need a scheduler.

So whether you want to call it multi-threading or not is your opinion, to the end user concurrency on a single processor is being done, I didn't state all of this before because I didn't want to muddy/confuse my original post.

Hope this helps,
Kevin
You are right, it is user-threaded. I made the point because the user threads will not force a thread out that is taking too long to execute. So if you create a command that will take a long time to finish one execution it will lock all of the other commands that are waiting on the scheduler to tell them to run. Just something to watch out for.

Last edited by notmattlythgoe : 08-01-2014 at 07:46.
  #9   Spotlight this post!  
Unread 08-01-2014, 10:32
omalleyj omalleyj is offline
Registered User
AKA: Jim O'Malley
FRC #1279 (Cold Fusion)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2008
Location: New Jersey
Posts: 132
omalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to behold
Re: How to make timed sequences in java?

Because many embedded programming environments do not use operating systems I generally avoid threads for dealing with this sort of situation. (threads are a perfectly valid and useful approach in those environments)

In your main loop (either explicit in Simple or implicit in Iterative) just check the time you start and the current time, e.g.: (all from memory, and I usualy code C, so treat this as pseudocode)

Code:
double pistonTimer = 0.0;

while (isTeleop() && isEnabled()){//implicit in Iterative

	//check for the condition that starts the piston
	if (joystick1.RawButton(5)){
		//...stuff that turns on/off the right solenoids...
		pistonTimer = Timer.GetTime();//I believe this is now a double, in seconds
	}

//lots of other things like driving code mixed in

        //check if its done yet
	if (pistonTimer > 0.0 //important so you know you've started timing
	 && Timer.GetTime() - pistonTimer > 2.0){
		//...stuff that turns on/off the other right solenoids...
		pistonTimer = 0.0; //don't forget to reset it
	}

}
While polling like this isn't super efficient it is easy to implement and follow.
HTH
  #10   Spotlight this post!  
Unread 08-01-2014, 11:06
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,125
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: How to make timed sequences in java?

Quote:
Originally Posted by omalleyj View Post
While polling like this isn't super efficient...
Do you think it's less efficient than time-slicing a separate thread?


  #11   Spotlight this post!  
Unread 08-01-2014, 11:09
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,188
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: How to make timed sequences in java?

The CommandRobot way will give you your desired outcome with the least amount of swimming upstream. I recommend giving that a look. (I'd also highly recommend at least looking at the source code you would be building on top of https://github.com/eshsrobotics/wpil...Scheduler.java)
  #12   Spotlight this post!  
Unread 08-01-2014, 11:31
omalleyj omalleyj is offline
Registered User
AKA: Jim O'Malley
FRC #1279 (Cold Fusion)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2008
Location: New Jersey
Posts: 132
omalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to behold
Re: How to make timed sequences in java?

Quote:
Originally Posted by Ether View Post
Do you think it's less efficient than time-slicing a separate thread?


Hard to say without looking in detail. You have the constant tests for setting and checking vs. the context switch to handle the thread. If the piston is used exactly once and never again the thread could be deleted. If its used many times the context switching could become onerous.
There are also many opportunuties to do threads badly. For instance if it was coded to check the time constantly rather than sleeping the whole duration it would be enormously worse. For teams that need to ask how to wait for something polling probably has the fewer pitfalls.
  #13   Spotlight this post!  
Unread 08-01-2014, 12:10
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,125
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: How to make timed sequences in java?

Quote:
Originally Posted by omalleyj View Post
Hard to say without looking in detail. You have the constant tests for setting and checking vs. the context switch to handle the thread.
Your "polling" code (a form of state machine as shown) runs once every 20ms yes? While you're waiting for the 2 seconds to elapse, you're doing one simple conditional jump every 20ms.

If CPU usage becomes a problem using this approach, there's probably something seriously wrong elsewhere in the code.


  #14   Spotlight this post!  
Unread 08-01-2014, 12:35
omalleyj omalleyj is offline
Registered User
AKA: Jim O'Malley
FRC #1279 (Cold Fusion)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2008
Location: New Jersey
Posts: 132
omalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to behold
Re: How to make timed sequences in java?

Quote:
Originally Posted by Ether View Post
Your "polling" code (a form of state machine as shown) runs once every 20ms yes? While you're waiting for the 2 seconds to elapse, you're doing one simple conditional jump every 20ms.

If CPU usage becomes a problem using this approach, there's probably something seriously wrong elsewhere in the code.


Agreed as stated. The Simple robot code is called once and your own loop runs as often as it can, the Iterative I believe is triggered by packet arrival at 20ms intervals (not in a position to verify at the moment).
I was addressing the more general case. I program microcontrollers where the loop is all yours. In FIRST need there are constraints. I try to teach the generic case (as a statemachine) and mention the FIRSTisms that impact it.
If programming a PIC or similar I would use a timer interupt and be more efficient than either by miles.
Didn't mean to start a debate, just to acknowledge that polling can sometimes be unnecessarily expensive, (and might be compared to a thread whose only instruction was sleep(2000), but I've never profiled either situation), but this was a case where simplicity was probably the best.

Last edited by omalleyj : 08-01-2014 at 12:37. Reason: can't spell
  #15   Spotlight this post!  
Unread 08-01-2014, 13:05
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,125
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: How to make timed sequences in java?

Quote:
Originally Posted by omalleyj View Post
The Simple robot code is called once and your own loop runs as often as it can...
You might want to throttle it with a sleep to run only as fast as it needs to. There are other processes running.


Closed Thread


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 03:29.

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