Vous êtes sur la page 1sur 6

Basic Command Based

1 The Basics
1.1Classes vs Objects
Classes are the template, objects are copies or instances.

1.2Iterative Robot: Most basic code


Init means initiate.
Execute means it runs after it is initiated. Runs repeatedly on a loop called every 20
milliseconds.
Things are executed in order.

2 Basic Command Based


Sample command based structure

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

2.2.1 Command groups


A command group puts together several commands in sequence
Sample (just logic, NOT actual code):
1.
2.
3.
4.

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 OpenClaw. Scheduler does:


Executes DriveForward, requires DriveTrain (subsystem)
Executes OpenClaw, requires Claw (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.2Initial Default Command

This sets the default command to TankDriveWithJoystick. Basically, if scheduler isnt


running anything else that uses the subsystem DriveTrain, it will run
TankDriveWithJoystick.

3.3Functions
You can also create quick functions. These can be called from commands.

This one resets the encoders when called.

This one will return the gyro angle.

3.4SmartDashboard
You can put values, strings, data etc on the SmartDashboard which is very helpful in
debugging.

This puts the number of left_encoders distance on the SmartDashboard next to


Left Distance

4 Commands
4.1Sample Command
Sets the power to 1 for 3 seconds (just logic, NOT actual code):
Init: setPower(1)
savedTime = getcurrentTime()
Execute:

isFinished: if currentTime savedTime > 3 seconds, then return true


end: setPower(0)
interrupt: call end()
*isFinished is like a flag. Its called every loop, if its false (like the flag isnt raised),
then the command keeps running. Once it returns true (like raising the flag),
scheduler tells command to end. This sample says if its been 3 seconds, raise the
flag!

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.

public static DriveTrain drivetrain; creates a public (global) DriveTrain (object)


named drivetrain (instance).
drivetrain = new DriveTrain() sets the drivetrain (instance) to a new instance of a
DriveTrain (object).

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.

Vous aimerez peut-être aussi