public class DriveForTime extends Command {
private double seconds;
private double speed;
- private Timer timer;
- private Direction direction;
-
- public DriveForTime(double seconds, double speed, Direction direction) {
- // limit speed to the range [0, MOTOR_MAX_VAL]
- this.speed = Math.max(speed, -speed);
- this.speed = Math.min(speed, DriveTrain.MOTOR_MAX_VAL);
+ public DriveForTime(double seconds, double speed) {
+ // Since negative values in setMotorSpeeds(...) are forwards, we reverse
+ // speed here so positive input values will move the robot forwards.
+ this.speed = -speed;
this.seconds = seconds;
- this.direction = direction;
}
@Override
protected void initialize() {
- timer = new Timer();
- timer.reset();
- timer.start();
+ this.setTimeout(seconds);
+ Robot.driveTrain.setMotorSpeeds(speed, speed);
}
@Override
protected void execute() {
- if (direction == Direction.FORWARD) {
- Robot.driveTrain.setMotorSpeeds(-speed, -speed);
- } else if (direction == Direction.BACKWARD) {
- Robot.driveTrain.setMotorSpeeds(speed, speed);
- }
+ // nothing to do since motor speeds already set
}
@Override
protected boolean isFinished() {
- return timer.get() > seconds;
+ return this.isTimedOut();
}
@Override
protected void end() {
- Robot.driveTrain.setMotorSpeeds(0, 0);
+ Robot.driveTrain.stop();
}
@Override