![]() |
Command Based Programming (Threads?)
I was under the assumption that when you used the a Command Group and added parallel that it would handle them as separate threads. I was testing this out with my group and when I put a Timer.delay in the execute method of one command, it held up all execution. Is this multi-threaded based? Are there other ways to run things within threads within the WPILibJ?
|
Re: Command Based Programming (Threads?)
If you're using Timer.delay than you're fundamentally missing the purpose of the command based structure... essentially you should be split into three different parts (the code before the wait, the wait itself, and the code after the wait) and they should all be non-interruptive commands.
|
Re: Command Based Programming (Threads?)
Command-based programming is just an extension of iterative that makes it easier to schedule and trigger actions. It gives you most of the benefits of multithreading without the added complexity of worrying about deadlocks and race conditions.
All commands run in the same thread, so they can't ever block. When commands are run, they are added to a queue which the scheduler executes iteratively. |
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
From the wpilib website: When a command is scheduled, the Scheduler checks to make sure that no other commands are using the same subsystems that the new command requires. If one or more of the subsystems is currently in use, and the current command is interruptible, it will be interrupted and the new command will be scheduled. If the current command is not interruptible, the new command will fail to be scheduled. |
Re: Command Based Programming (Threads?)
Quote:
Quote:
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
And yes, the currently running command would have to end before any other command that requires the same subsystem could be scheduled. This is only true if you set the command's interruptible property to false. |
Re: Command Based Programming (Threads?)
The way commands are run in the Command Based Structure are via a queue. The execute method of each scheduled command is executed in sequence (I assume in the order added to the queue). This means that each command's execute method shouldn't contain anything that will sleep the thread (delays) or otherwise tie the thread up for long periods of time (intensive camera calculations). Such actions will lead to decreased robot responsiveness.
There are many other ways to implement non-blocking timers, if you describe what it is you mean I can help point you in the right direction. I believe by "non-interruptive" Arhowk meant non-blocking. As far as interrupts on a command, they are only used when a new command is added to the queue that uses the SAME subsystem as another queued command. So, if the first is not interruptible, the second command will not run, if it is, the first command will be interrupted and the second will take over. Command Interrupts do not refer to commands using different subsystems in any way. |
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
The way the Command Based Structure is designed is to keep a list of all currently running commands. Every ~20ms each of these commands will be executed one at a time. Each of the commands should be designed to execute very quickly so it does not delay the execution of the other commands that are being executed. This means, if you delay the execution by some means this will cause the other commands after it in the list to wait on the delayed command.
|
Re: Command Based Programming (Threads?)
Our thought was to have an image processing Command, that would have taken a bit more time and processing power, that ran in a parallel Command Group, but since it isn't really parallel, then the main thread would be stuck in the Image Processing command until it returned.
This non-threaded functionality could affect our driving (Essentially Everything) because if the thread is working on image processing then it is not available to change speed on our motors. (This is just a simple example.) Im coaching the team and talking about parallel processing and trying to show an example based on the API that is given. The more I work with it, the more I find it isn't really there. It's alright that it is not in there, but the documentation says: "... instead of waiting for the child to finish, a CommandGroup will have it run at the same time as the subsequent Commands. ..." |
Re: Command Based Programming (Threads?)
Quote:
Last night we created a separate thread that will run our vision processing so it doesn't slow down the processing of commands. I can assist you with this if you'd like. |
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
Code:
public FiringCommandGroup extends CommandGroup {Commands also have a timeSinceInitialixed() method that will return the time in seconds since the command was initialized(aka started). This could be used to check to see if a command has been running for a certain period of time. |
Re: Command Based Programming (Threads?)
Thanks for the response. I hope you don't mind some follow-up questions: Quote:
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
Code:
public class WaitCommand extends Command {As to the second question. Something like this would work for a single command doing multiple things based on time. Code:
public void execute() { |
Re: Command Based Programming (Threads?)
Quote:
- the first time it is executed it notes the system time so that it can compute the value of "timeSinceInitialized" each time it is executed. |
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
- "something" will be executed every 20ms as long as "timeSinceInitialized()" is less than "sometime" |
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
So this codeA: Code:
public FiringCommandGroup extends CommandGroup {Code:
public void execute() {whereas codeB executes "something" repeatedly, then waits, then executes "something2" repeatedly. Yes? |
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
Code:
public class ShootCommand extends Command {2. Do nothing for 1 second. 3. After 1 second return true in isFinished(). 4. When isFinished() returns true the end() method is called. 5. Run logic for fire2 in end. Keep in mind, the logic for both fire1 and fire2 will need to execute quickly. Meaning no Timer.delays or long running loops. |
Re: Command Based Programming (Threads?)
Quote:
|
Re: Command Based Programming (Threads?)
Quote:
However, the former approach (#15) is the preferred option for modularity sake. Integrating the wait with both executions turn the command, not into a modular command, but rather a defined sequence. If the sequence is regular (e.g. load hopper, wait, shoot) than it would be preferential to use a CommandGroup, for readability sake. If the sequence is irregular (e.g. turn 15 degrees, wait, shoot at low velocity) than the command will only be used once and more commands of similar conditions (e.g. turn 30 degrees, wait, shoot at high velocity) would have to be developed. In this case, it would be easier to make a turn command, a wait command, and a shoot command and plug them in in a similar fashion to post #15. |
Re: Command Based Programming (Threads?)
Quote:
My team and I prefer the method I provided in post #15. It's easier to read, easier to modify, and more modular. Here is an example from our competition code from last season. Notice how easy it is to read exactly what the ShootSeries command does. Code:
public class ShootSeries extends CommandGroup {Code:
public class PickUpDeploy extends CommandBase {Code:
public class SetShooterPosition extends CommandBase {Code:
public class ResetArmCommand extends CommandBase {Code:
public class PickUp extends Subsystem {Code:
public class Shooter extends Subsystem {Also, here is our code from this season so far. |
| All times are GMT -5. The time now is 11:10. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi