Basic Command Based
Basic Command Based
1 The Basics
1.1Classes vs Objects
Classes are the template, objects are copies or instances.
Schedule
r
Subsyste
m
Claw
Comman
d
Drive
Train
Drive
2.1Subsystems
Subsystems are created for each group of the robot.
Examples of subsystems and what they contain:
1. DriveTrain
a. rightMotor
b. leftMotor
c. rightEncoder
d. leftEncoder
2. Arm
Open
Claw
Close
Claw
a.
b.
c.
d.
Roller
leftArmMotor
rightArmMotor
encoder
Each subsystem also has a default command, which is just a normal command that
youve selected as the default to run.
2.2Commands
Commands tell the subsystem to do stuff. For example, a driveForward command
will tell the driveTrain subsystem to move the motors at 1 power
Take picture
Calculate using vision processing
Turn bot
Shoot
Now you can do all of that with the push of one button!
2.3Scheduler
The scheduler controls/schedules the commands the robot runs.
For example:
You call the command DriveForward. Scheduler does:
Executes DriveForward, requires DriveTrain (subsystem)
Then you call Drive backward which requires the same subsystem as Drive
Forward:
Interrupts DriveForward, requires DriveTrain
Executes OpenClaw, requires Claw
Executes DriveBackwards, requires DriveTrain
Essentially, each new command that requires the same subsystem as an existing
one will kick out that command.
If nothing is being called, Scheduler will run the subsystems default command
3 Subsystems
3.1Creating the objects
Here is a sample of a DriveTrain subsystem. This is from the GearsBot sample code
which can be accessed in eclipse. (In eclipse: file>new>other>example robot java
program>(scroll down to bottom) gearsbot)
Private creates the objects. For example, private Encoder left_encoder; creates
an Encoder called left_encoder. We want private so that only that instance can see
the variable.
public DriveTrain() is the constructor. You can put parameters into the () if needed
super() means it will inherit subsystem stuff.
back_left_motor = new Talon(2); assigns the back_left_motor (instance)
SpeedController (object) to the Talon 2 [basically controls the speed of the motor
connected to it] (hardware).
3.3Functions
You can also create quick functions. These can be called from commands.
3.4SmartDashboard
You can put values, strings, data etc on the SmartDashboard which is very helpful in
debugging.
4 Commands
4.1Sample Command
Sets the power to 1 for 3 seconds (just logic, NOT actual code):
Init: setPower(1)
savedTime = getcurrentTime()
Execute:
4.2Calling Commands
These commands are called in OI, where you map the buttons to the command.
5 Robot
5.1Robot.java
This is the MAIN. Everything here is global. This is where the Scheduler lives and
tells to robot what to do.
Create global subsystems here.
5.2RobotMap.java
This creates global integers telling you where each input is. These integers are the
values of the port numbers and are used when setting an object to a port. For
example, in my DriveTrain subsystem, I will set DriveTrain.RightFrontDriveMotor to
Robot.RightFrontDriveMotor (which is the same thing as setting it to 3). That way,
its easy to change the numbers if wiring is changed, because they are all in one
place.