![]() |
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.
|
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.
|
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. |
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:
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. ;) |
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 |
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. |
Re: How to make timed sequences in java?
Quote:
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 |
Re: How to make timed sequences in java?
Quote:
|
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:
HTH |
Re: How to make timed sequences in java?
Quote:
|
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)
|
Re: How to make timed sequences in java?
Quote:
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. |
Re: How to make timed sequences in java?
Quote:
If CPU usage becomes a problem using this approach, there's probably something seriously wrong elsewhere in the code. |
Re: How to make timed sequences in java?
Quote:
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. |
Re: How to make timed sequences in java?
Quote:
|
| All times are GMT -5. The time now is 03:07. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi