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
* to drive at. Column 0 represents the time in seconds to drive for
* and column 1 represents the speed to drive at. E.g. { {1, 0.5},
* {2, 0.75} } will drive at 0.5 for 1 second and 0.75 for 2 seconds
- *
- * @param direction
- * this is Direction.Forward or Direction.Backward all speeds in the
- * timings array will apply in this direction.
*/
- public DriveForTimesSequence(double[][] timings, Direction direction) {
+ public DriveForTimesSequence(double[][] timings) {
for (int i = 0; i < timings.length; i++) {
double time = timings[i][0];
double speed = timings[i][1];
- addSequential(new DriveForTime(time, speed, direction));
+ addSequential(new DriveForTime(time, speed));
}
}
}
\ No newline at end of file