Command: Basically some action that you can define. This might be an autonomous mode action, or a teleop action, or something you use in both modes. initialize() sets up the action to happen, then execute() is repeatedly called until isFinished() is true, then end() is called to clean up.
Example:
Code:
public class MoveToPositionCommand extends Command {
private int encoderTicksToDrive;
public MoveToPositionCommand(int encoderTicksToDrive) {
this.encoderTicksToDrive = encoderTicksToDrive;
require(driveSubsystem);
}
public void initialize() {
driveSubsystem.resetEncoders();
}
public void execute() {
int leftEncoder = driveSubsystem.getLeftEncoder();
int rightEncoder = driveSubsystem.getRightEncoder();
double turn = DriveSubsystem.SOME_TURN_CONSTANT * (rightEncoder - leftEncoder);
double throttle = DriveSubsystem.AUTO_DRIVE_SPEED;
driveSubsystem.drive(throttle, turn);
}
public boolean isFinished() {
return driveSubsystem.getLeftEncoder() > encoderTicksToDrive || driveSubsystem.getRightEncoder() > encoderTicksToDrive;
}
public void end() {
driveSubsystem.drive(0, 0);
}
}
Subsystem: Exactly what it sounds like. A virtual representation of a robot subsystem. Subsystems have WPILib objects and define methods that commands can use
Example:
Code:
public class DriveSubsystem {
public static final double SOME_CONSTANT = 42;
CANTalon leftDrive, rightDrive;
Encoder leftEncoder, rightEncoder;
public DriveSubsystem() {
// initialize objects
}
public void drive(double throttle, double turn) {
// do the driving
}
// other methods that commands need, etc
}
Edit: oops I just realized I posted Java in the C++ forum. It shouldn't be too difficult to translate though
