Lesson 3 - Commands and Subsystems


Part 1 - What are Subsystems?

Subsystems are abstractions (aka a software representation) of a physical part or parts of the robot.

For example, there could be a Motor Subsystem that controls a motor, or a shooting subsystem which uses multiple motors to shoot a object.

Subsystems in action

Subsystems are represented in java as classes who extend the SubsystemBase abstract class

The only inherited method we will talk about is the periodic() method.

The periodic() method runs every 20 ms in the background. The typical use for periodic() is code you want to run in the background once in a while. such as spinning the motor, checking if it's done, etc..

Some examples of what we use periodic() for include:

Other methods are added as needed to fully encapsualte the component in question.

For example, a Motor Subsystem could have a run() method to start the motor, and a stop() method to stop the motor.


Part 2 - What are Commands?

A Command uses 1 or more subsystems to complete an specific action, such as picking up an object, scanning the feild, scoring a note, etc.

They are managed by the CommandScheduler, which schedules all commands in the queue.
The CommandScheduler also ensures that overlapping Commands (those who use the same Subsystems) dont run at the same time.

For more information about commands running in parallel, visit lesson 4.

These commands are represented in java as classes which extends the Command abstract class,
and 38 inherited methods, but only 5 matter for creating commands for now: initialize(), execute(), end(), isFinished(), and addRequirements()

initialize():

initialize() runs when the command is initially executed by the command scheduler and only runs once.
This method is used to prepare the robot to perform an action.

execute():

execute() is run every command scheduler run (40 MS) during the command's lifetime.
You can use this to continually run code for the duration of the command.

isFinished():

isFinished() is executed at the end of every command scheduler run (40 MS).
It returns a boolean value which if true, ends the program without interruptions.

You use this method to tell the robot when to end your command.

end(boolean interrupted):

end(boolean interrupted) is called when the command ends.

It takes a boolean indicating if the command was interrupted or not, i.e.
takes the value of false if isFinished() is true (meaning the command was NOT forcefully terminated), and false otherwise

addRequirements(Subsystem s):

addRequirements(Subsystem s) you do not override this inherited method

Instead, this method is used to register the subsystems that a command uses to the CommandScheduler

This prevents the parrallel execution two different commands that use overlapping subsystems.

Below is an example command that uses our previously defined Motor Subsystem to run a motor for 1 second.