|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Re: Multithreading on Java
Quote:
|
|
#2
|
|||||
|
|||||
|
Re: Multithreading on Java
Each command needs to finish its execute command quickly, any waits or long executions will cause everything else to come to a halt until it is finished.
|
|
#3
|
||||
|
||||
|
Re: Multithreading on Java
Quote:
Please note: this is not veiled criticism of the command based model; I'm just trying to untangle things. |
|
#4
|
|||||
|
|||||
|
Re: Multithreading on Java
Quote:
|
|
#5
|
||||
|
||||
|
Re: Multithreading on Java
Quote:
Let me try re-wording it:
I have no idea if the above description is an accurate account of what the Command Based implementation actually does, but is that what you meant? Quote:
Last edited by Ether : 11-02-2014 at 12:29. |
|
#6
|
|||
|
|||
|
Re: Multithreading on Java
Commands can be running "simultaneously" as long as they don't require writing data to the same Subsystems. CommandGroups are used mainly for automation (ex. I might have a ShootAndReload command group that releases my winch, then proceeds to roll it back up again immediately by calling the different Commands in a certain configuration). You can add Commands to a CommandGroup to run in sequence or parallel.
|
|
#7
|
||||
|
||||
|
Re: Multithreading on Java
Quote:
Command A requires only Subsystem B and takes 1ms to complete.My question is, how does the CommandBased model implement this, if not by preemptive time-slicing. Posts 6 and 8, and post 10 if I interpreted it correctly, would answer that question. |
|
#8
|
|||
|
|||
|
Re: Multithreading on Java
They aren't actually running simultaneously. I'm not sure if I'm interpreting "preemptive time slicing" correctly, but any Command-Based program could be written in the IterativeRobot style with a large number of if statements
. They're all running on one thread as I understand it, and I am not sure you have any guarantee to the order they run in. That is why Commands can declare that they require exclusive access to a Subsystem to tell other Commands that they will be writing data to it.Both commands will run the execute() method in their class each robot loop (usually 20ms) until their isFinished() check (also run each loop) returns true. The Scheduler takes care of all the behind-the-scenes work of maintaining command life-cycles. |
|
#9
|
||||
|
||||
|
Re: Multithreading on Java
Quote:
[edit] Just saw Joe's post and am studying it... Last edited by Ether : 11-02-2014 at 13:19. |
|
#10
|
||||
|
||||
|
Re: Multithreading on Java
Quote:
The provided PID controller does start it's own thread to do PID math, and can update the motor speed from this thread too. |
|
#11
|
||||||
|
||||||
|
Re: Multithreading on Java
Quote:
initialize execute end interrupted The user provides the code that goes in those methods. There is a fifth method (isFinished()) that allows the scheduler to know when to transition from execute to end. The normal flow is from initialize to execute to end. The scheduler handles interrupting, if a new command is scheduled that requires the same subsystem. A CommandGroup allows multiple commands to be scheduled sequentially or in parallel (being in parallel requires that they don't require the same subsystem). The only times we have had to add an explicit state machine inside a command, is when there is decision logic that needs to go backwards in states. In our case, that's been relatively rare. The scheduler is entirely cooperative. It depends on the commands methods executing quickly and returning. If the user puts an explicit Timer.wait() in a command, it breaks the execution model. The user can either use a WaitCommand, or check a timer in the isFinished method, or implement their own state machine. We've done all 3. |
|
#12
|
|||||
|
|||||
|
Re: Multithreading on Java
Quote:
The scheduler keeps a list of currently running commands on the system. The execution happens as follows: 1. Gets next command in the list 2. If this is the first time this command is going to run, execute the commands initialize(). 3. Execute the command's execute(). 4. Execute the command's isFinished(), if the method returns true execute the commands end(), otherwise add the command back onto the list at the end. 5. Repeat steps 1-4. For the system to work the commands methods need to execute quickly. This means not putting in any waits in the code or the scheduler will not not move onto the next command. Now, onto the WaitCommand I was talking about before. The CommandGroup allows you to add commands to run in sequence, below is an example that will deploy and arm, wait 5 seconds, then retract the arm. Code:
addSequential(new DeployArmCommand()); addSequential(new WaitCommand(5)); addSequential(new RetractArmCommand()); I hope this makes it clearer than I described earlier. If you need any more clarification on anything let me know. |
|
#13
|
|||
|
|||
|
Re: Multithreading on Java
Quote:
Of course if you have multiple execution cores and are actually assigning tasks to run on those different cores that is a different story. If you are using tasks then you can block (sleep/wait) in those tasks and the operating system will take care of running other tasks that are ready to work. The Command Based model provided by WPILib is really simulating the behavior of the operating system's scheduler. It does so by using a single task to call the execute() method of any command that is scheduled to run. Those execute() methods must be non-blocking (execute quickly) so that the scheduler can all the other commands that are waiting to run in that cycle. Example: You want to run a motor until a limit switch is pressed. The execute() method of the Command should set the motor output to some speed then return immediately. The isFinished() method of the Command should return true when the limit switch is pressed. You may also want to drive the robot during the time this first operation is occuring. In the execute() your drive Command (which requires a different subsystem) should read the joystick input and update the drive motors set points and then return immediately. The isFinished() method in the drive command should always return false. The scheduler will run the execute() on both commands every 20 ms until the first command finishes. Then will continue to run the drive command. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|