Theory
The command based paradigm is a popular way to write complex robot code in FRC and FTC.
The basic overview is:
- Write robot code in the structure of a command:
- Initialise (when the command starts)
- Execute (while the command is running)
- Finished (returns true when the command is done)
- End (when the command ends)
- Compose individual commands into larger command groups, in order to run commands in parallel, sequence, etc.
- Use subsystems to group related hardware access together, and thus also group commands related to each subsystem together.
- Declaratively bind commands to gamepad inputs and other events.
- A scheduler runs commands and polls bindings to start up new commands.
These features combine to make command based a great way to write robot code in a reusable and composable way. It also makes it easy to adjust and rearrange the way the commands are run and arranged. Importantly, it mostly decouples user input from robot control.
It allows you to easily run a bunch of robot processes independently at the same time, without worrying about conflicting requirements and states, and is easy to read and write.
Command based takes a little getting used to, but once you’ve given it a go, its got clear advantages over not using it.
Hopefully Mercurial can bring out the best in command based, and make it easy to use.
The combination of commands and a scheduler to run them forms a Finite State Machine, or at least, an automata that is similar enough. Command based’s automata is a dynamically composed and run one and many teams’ command based code could have hundreds to thousands of ‘states’ that one might write by hand in a traditional FSM. It is also common for a command to contain its own sub FSM, and Mercurial has good support for this.