|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#16
|
||||
|
||||
|
Re: Multithreading on Java
Quote:
[edit] Just saw Joe's post and am studying it... Last edited by Ether : 02-11-2014 at 01:19 PM. |
|
#17
|
|||||
|
|||||
|
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. |
|
#18
|
|||
|
|||
|
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. |
|
#19
|
||||
|
||||
|
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. |
|
#20
|
||||
|
||||
|
Re: Multithreading on Java
Thank you everyone for the responses. I've been carefully reviewing them all and I think I now have a reasonably coherent idea of how command-based works. There are however a few seeming inconsistencies (or perhaps ambiguities) that I'd like to resolve. Here are two: Firstly, it is necessary but not sufficient that each individual command in the scheduler's queue complete within 20ms. Rather, the sum total of the individual execution times must be less than 20ms. Correct? Secondly, the scheduler maintains not only a queue of all the commands to be run, but also the state of each command... and only the method corresponding to each command's state is executed. Correct? (Compare post 15 to post 17 for example of apparent inconsistency). |
|
#21
|
||||||
|
||||||
|
Re: Multithreading on Java
Quote:
Quote:
|
|
#22
|
|||||
|
|||||
|
Re: Multithreading on Java
Quote:
The second point is also correct. The only real state that the scheduler needs to keep track of is which commands are running for their first time. The only thing I'd like to clarify further is that once a command is finished it is removed from the scheduler. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|