Command Based Programming

Objective of the Command Based Robot

From the WpiLib site:

WPILib supports a method of writing programs called "Command based programming". Command based programming is a design pattern to help you organize your robot programs. 

Some of the characteristics of robot programs that might be different from other desktop programs are:
  • Activities happen over time, for example a sequence of steps to shoot a Frisbee or raise an elevator and place a tube on a goal.
  • These activities occur concurrently, that is it might be desirable for an elevator, wrist and gripper to all be moving into a pickup position at the same time to increase robot performance.
  • It is desirable to test the robot mechanisms and activities each individually to help debug your robot.
  • Often the program needs to be augmented with additional autonomous programs at the last minute, perhaps at competitions, so easily extendable code is important.
Command based programming supports all these goals easily to make the robot program much simpler than using some less structured technique.

Structure of the Layers

The Command Based Robot has the following structure:


Commands

Commands run over a subsystem or set of subsystems to coordinate a specific action.

Each subsystem has a default command, and that command typically takes user inputs from the Joysticks (or OI layer) and translates those actions into motor movements.  For instance, the DefaultChassisCommand should be reading Joystick values and setting the left and right motor speeds appropriately by calling routines in the ChassisSubsystem.

In general, a command runs until it ends, or until it is interrupted by another command.  If no commands are running for a subsystem, the scheduler will re-schedule the default command for that subsystem.  That means there could be several concurrently active commands running - one for each subsystem on the robot!

All currently active commands in the robot are called at approx 50Hz, or once every 20ms.  

A command has the following flow: