In order for our robot to run the code, we use a type of programming structure that is given to us in the form of a template. This structure is called Command Based Programming. When you have the code on your laptop, you will see something that looks like the photo. This document will only be looking at the files in the red square.
In Command Based programming, there are two main types of classes: subsystems and commands. There are also different types of classes such as Robot class, RobotContainer class, and OI class.
Subsystems and Commands
Subsystems are what defines the capabilities of each mechanism on the robot. You can think of it as different physical parts of the robot. Some examples are a climber, an intake, a ball shooter, and a drive train.
Commands are the operation of the robot, utilizing the capabilities that are in the subsystems. You can think of this as giving actions or commands to the different parts of the robot. For example, If your robot code has a subsystem for an intake, the program would have a command for taking in the balls called “intake” and this will spin a motor to take in the balls.
How Subsystems Work
Subsystems have methods where you can declare the hardware, like a motor, that will be used by the commands. These methods are then used to control different mechanisms on the robot.
If the robot has an intake arm, the intake arm will have to do 2 things. It has to move up and move down. In order to make this happen, you have to add two methods. The first method will be called public void armUp()methods. Inside this method you will set your motor to move up. The second method will be called. Inside this method you will set up your motor to move down. You can see how this looks by looking at the team’s code.
How Commands Work
Commands use many methods to define a robot's action. When a programmer creates a command or looks at the example command given, they will see that there are about 4 methods. They are:
initialized(): This is called before the command runs for the first time.
execute(): This method is the code that has the operations to complete a task. It is basically where the logic goes.
isFinished(): This tells if the action is done or not. After the code in execute() runs, it goes to the isFinished() method to see if the action is completed. It returns true when finished and false when not. If the action is completed and isFinished() returns true, it moves to end(). If the action is not completed, it continues until it is done.
end(): This has the code for what the robot should do after isFinished() returns true. This method is also called when the command is interrupted.
OI, Constants, RobotContainer, and Robot
In command based programming, you are given 3 different files that are outside of the subsystem and command folders. These 3 files are:
RobotContainer.java
Constants.java
Robot.java
Main.java
There is a 4th file that we add in called OI. This file isn’t added in with the template, but we sometimes add this file in for joystick code.
RobotContainer.java
This is where a lot of the robot code is declared. The robot's subsystems and commands are defined in RobotContainer, along with OI devices, and commands, and mapping buttons to commands.
Constants.java
Constants sets up mapping for device ports and is a good place to declare constant variables. The team uses it for physical wiring of the robot. We use the Constants file to set up motor controllers and buttons on the control panel.
Robot.java
This is where a lot of the robot code is declared. The robot's subsystems and commands are defined in RobotContainer, along with OI devices, and commands, and mapping buttons to commands.
Main.java
The Main file should be left alone and no edits should be made to it. Changing it will cause the robot code to break.
OI.java
The OI (Operator Interface) is a class where you declare and get information from other operator interface devices such as joystick, xbox controllers, and wii remotes.