Skip to content

Latest commit

 

History

History
63 lines (45 loc) · 4.43 KB

File metadata and controls

63 lines (45 loc) · 4.43 KB

Command Based

Command Based is a way of structuring our code to make it easy to do complicated tasks, and write control logic in a concise way

Command based programming revolves around three concepts: Subsystems, Commands, and Triggers.

Subsystems

A Subsystem represents a system on our robot, like the drivetrain, elevator, arm, or intake, that will always operate as a unit. Each subsystem contains some associated hardware (motors, pistons, sensors, etc). They are the "nouns" of our robot, or things that it is.

Commands

Commands are the "verbs" of the robot, or what our robot does. Each Subsystem can be used by one Command at the same time, but Commands may use many Subsystems. Commands can be composed together, so the LineUp, Extend, and Outtake Commands might be put together to make a Score Command. Because each Subsystem can only be used by one Command at once, we are safe from multiple pieces of code trying to command the same motor to different speeds, for example.

Some hardware might not be stored in a Subsystem if multiple things can/should use it at the same time safely. For example, a vision setup can be read from by many things, and might not need to be locked by Commands. Therefore, it might not be stored in a Subsystem.

On the other hand, a roller that is driven by a motor can only go at one speed at a time. Therefore, we would wrap it in a Subsystem so that only one Command can use it at once.

Triggers

A Trigger is something which can start a Command. The classic form of this is a button on the driver's controller. Another common type is one which checks if the robot is enabled. One non-obvious Trigger we used in 2024 was one which checked when we had detected a game piece in the robot, which then triggered the Commands to flash our LEDs and vibrate the driver controller. Triggers can be made of any function that returns a boolean, which makes them very powerful, and can be composed together with boolean operators. Triggers can also be bound to a certain activation event. For example, we might want something to happen while a condition is true (whileTrue()), when a condition changes from false to true (onTrue()), or is true for some amount of time (debounce()).

Some large Commands are better represented by several Commands and some Triggers!

Subsystems, commands, and triggers are the building blocks of the robot's overall "superstructure". This will be covered in more detail in the Superstructure article.

Resources

Examples

Exercises

  • Make basic KitBot code using the Command-Based skeleton. You can follow this tutorial.

Notes

  • We prefer making simple Commands with Command factories, or methods in a subsystem that return a Command. These methods should be simple interactions like setTargetExtensionMeters() or extendIntake(). Then, you can use decorators as described here to compose the basic Commands into more complex sequences. Generally, we make these compositions in Robot and Superstructure, but you can also make single-Subsystem compositions within that Subsystem. See our code from previous years for examples of this pattern, or talk to a software lead.