FRC COMMAND PATTERN
Falkon Robotics Team 589
2020 Season
Steve Gustafson – Senior Mentor
WHAT’S IN THIS PRESENTATION
The command pattern is an industry recognized software architecture which is widely used as an extensible framework. It is one of many design patterns currently used by software professionals. Like all patterns, it is essential that coding is done consistent with the design of the framework.
If you’re not sure how and where your code fits in, ask for help.
INTRODUCTION
INTRODUCTION CONTINUED
THIS PRESENTATION IS BASED ON AN EXAMPLE
This presentation uses an example which comes with this year’s FRC extensions to Visual Studio Code
The example appears to have been developed before the 2020 re-write of the FRC command framework was complete
Except where otherwise noted, the code presented here complies with the final release version
START VS CODE 2020
Visual Studio Code has been extended with features specifically required for developing FRC code.
CREATE THE EXAMPLE PROJECT
CREATING THE HATCHBOT EXAMPLE
Complete filling out the page and be sure to enter “589” for the team number
WHERE THE FILES ARE - SUBSYSTEMS
“Subsystems are the basic unit of robot organization in the command-based paradigm. A subsystem is an abstraction for a collection of robot hardware that operates together as a unit.”
Section 3
Code Annotations and Explanations
WHAT A SUBSYSTEM CONTAINS
Public methods (#3 and #4) are simple methods that perform basic subsystem functions and are called by commands.
All subsystem java files will start with
“package frc.robot.subsystem” (line 8)
WHERE THE COMMANDS ARE
VERY SIMPLE COMMAND CLASS – GRAB HATCH
VERY SIMPLE COMMAND CLASS (CONTINUED)
This example is simple because the hatch subsystem contains only a pneumatic solenoid. Solenoids instantaneously open or close. There is no “in between” state, so all that needs to be done for “grabHatch()” is to open the forward air valve to extend the hatch. This only needs to be done once per button press. For this reason, we can do all the work in the “initialize() method. “isFinished()” can simply return “true” because the valve is now open. Motors are different and usually require more methods to be overridden.
KEY THING TO KEEP IN MIND – NO LOOPS!
WHERE BUTTONS, COMMANDS, AND SUBSYSTEMS COME TOGETHER
It all happens in RobotContainer.java
RobotContainer.java is often a pretty large file (it grows as commands and subsystems are added). It’s organized into well defined sections where specific kinds of things are done. Typically small groups of students are assigned to subsystems and their related commands, so there shouldn’t be issues accidently deleting/modifying other peoples work. But everyone will need to do some additions and changes to RobotContainer.java. Be careful to put your code in the right sections and not to modify other peoples code unless you coordinate with them!
We’ve seen where the subsystem and command classes are, but:
A FIRST LOOK INSIDE THE ROBOT CONTAINER
Note: Follow standard FRC Java guidelines and prefix class member names with “m_” (even if that is not your personal preference.) Adherence to coding standards is essential when developing code as a team.
INSIDE ROBOT CONTAINER – AUTONOMOUS AND DEFAULT COMMAND INSTANTIATION
[a] m_simpleAuto is declared as “Command” type and instantiates the “DriveDistance” command
[b] m_complexAuto is instantiated as the “ComplexAuto” command
[c] m_driverController in this example is an Xbox controller. We will use joysticks (very little code difference).
The m_chooser object is used to make autonomous commands available for selection in the Driver Station
CONTINUING WITH THE ROBOT CONTAINER – ROBOT CONTAINER CONSTRUCTOR
#1 Calls private method to bind buttons to commands
#2 Establishes a “default command” for the robot drive subsystem. (Default commands keep executing when no other command for its associated subsystem is executing)
#3 Passes a reference to m_robotDrive, and uses Java “Lambda” syntax to pass in two Java statements (we may decide not to do it this way)
#4 Adds two commands to the Driver Station autonomous chooser
#5 Puts the chooser (and its commands) on the dashboard
Notice the effective use of comments and well chosen names.
ROBOT CONTAINER - ESTABLISHING BUTTON BINDINGS (ROBOTCONTAINER.JAVA FILE)
For each button that will issue a command, a JoystickButton is instantiated. Its constructor is passed an instance of a driver control device (aka, Joystick)along with a constant identifying a specific button (line 90). Notice that a method on the newly instantiated instance is then called (such as in line 91. “.whenPressed(..)”), passing a “new” instance of command created by invoking the command’s constructor and passing in an instance of the command’s related subsystem. The subsystem instance is a member of the RobotContainer class.
There’s a lot going on in lines 90 and 91! This could be more easily understood by breaking these lines into more statements and not jamming them all together like this. It is not necessary to fully understand this – you can just follow examples.
The letters in boxes, [a], etc., show a correspondence between the command classes and the files that contain them.
Notice that both [a] and [b] use .whenPressed(..) but [c] uses .whenHeld(..). There are many variations.
POSSIBLE FUTURE ADDITIONS TO THIS PRESENTATION