add DriveForTime command
[3501/2015-FRC-Spark] / src / org / usfirst / frc3501 / RiceCatRobot / commands / DriveForTime.java
CommitLineData
f11ce98e
KZ
1package org.usfirst.frc3501.RiceCatRobot.commands;
2
3import org.usfirst.frc3501.RiceCatRobot.Robot;
4import org.usfirst.frc3501.RiceCatRobot.RobotMap.Direction;
7682c821 5import org.usfirst.frc3501.RiceCatRobot.subsystems.DriveTrain;
f11ce98e
KZ
6
7import edu.wpi.first.wpilibj.Timer;
8import edu.wpi.first.wpilibj.command.Command;
9
10/**
11 * This command takes a time in seconds which is how long it should run
12 *
13 */
95adbe68 14public class DriveForTime extends Command {
7682c821
D
15 private double seconds;
16 private double speed;
17 private Timer timer;
18 private Direction direction;
19
95adbe68 20 public DriveForTime(double seconds, double speed, Direction direction) {
7682c821
D
21 // limit speed to the range [0, MOTOR_MAX_VAL]
22 this.speed = Math.max(speed, -speed);
23 this.speed = Math.min(speed, DriveTrain.MOTOR_MAX_VAL);
24
25 this.seconds = seconds;
26 this.direction = direction;
27 }
28
29 @Override
30 protected void initialize() {
31 timer = new Timer();
32 timer.reset();
33 timer.start();
34 }
35
36 @Override
37 protected void execute() {
38 if (direction == Direction.FORWARD) {
39 Robot.driveTrain.setMotorSpeeds(-speed, -speed);
40 } else if (direction == Direction.BACKWARD) {
41 Robot.driveTrain.setMotorSpeeds(speed, speed);
f11ce98e 42 }
7682c821
D
43 }
44
45 @Override
46 protected boolean isFinished() {
47 return timer.get() > seconds;
48 }
49
50 @Override
51 protected void end() {
52 Robot.driveTrain.setMotorSpeeds(0, 0);
53 }
54
55 @Override
56 protected void interrupted() {
57 end();
58 }
59}