Programming_Basics

Commands

Prerequisite: Subsystems Exercise

A Command represents an action that the robot can perform.

Command Scheduler

The Command Scheduler is a core utility in WPILib which orchestrates activity amongst subsystems.


Instantiation

Types of Commands

WPILib contains various types of Commands to achieve specific functionality. There are also helpful shortcuts available for implementing common tasks. Heres a shortlist of Command types that are used frequently in Cyber Cat code base:

After you complete the following exercise, check out the refernece material at the end of this document to explore these Command types.

Exercise: Create a Command Class to control the position of the Arm

Background: Commands:

Start with project from Subsystem Exercise

1) Add a position instance variable to the subsystem(arm).
Arm Position

2) Add a member functions(setter and getter functions) to set and get the position of the arm.

Arm Position Set Get

Create a Command class

1) Open project from Simulation section 2) Right-click on subsystems directory and select “Create a new class/commnad”
Instantiation

3) Select Command from the menu and type in a name for the command (e.g. “ArmPrint”) and press enter 4) Create an instance of subsytem(Use the same subsystem(e.g. Arm)ArmSubsystem, and add it to the command using addRequirements()
Instantiation

5) Set the postion of the arm in the execute() and access the postion variable using getPostion().
Instantiation

Bind the command

1) Add Arm subsysytem instance variable in ‘RobotContainer’. 2) Instantiate the subsystem, and set ‘Default’ command on Arm subsystem in the constructor.
Instantiation

Run Simulation

Simulate the robot code Choose the ‘Teleoperated’ option in the GUI
Simulation-GUI
Observe the print statements
Simulation

References