Commands
WPILib provides a “Command-Based” way of programming the robot. With this approach we define commands with the conditions under which the command runs, the action to be taken and the subsystem(s) required for the command. The alternative is to create our own state machine logic that is monitoring all of the operator and sensor inputs and then deciding which commands should be started or stopped for each iteration. With Command-Based code, we just need to define the commands and the command schedule runs behind the scenes and takes care of the rest. See the WPILib references for more details on this approach.
There are many different structures of commands that can be used for different circumstances:
- Commands that run once
- Commands that run continuously until interrupted by another command or a mode change
- Commands that run continuously until a specific condition is met
- Commands that run initialization code before they start, other code continuously, and a third block of code when they are stopped.
There are also different ways to define commands in the code:
- Define them inline where they are needed (usually the Robot Container)
- Define them in command “factories” in the subsystem. This allows subsystem details to be hidden from the other code.
- Define them in a separate file as a subclass of the Command class. This is normally only needed for complex commands.
In addition to individual commands, there are also command compositions that can pull together sequences, parallel actions or any combination.
References:
WPILib What Is “Command-Based” Programming?:
https://docs.wpilib.org/en/stable/docs/software/commandbased/what-is-command-based.html

Discussion