Add rudimentary auton strategies based on timing
[3501/stronghold-2016] / src / org / usfirst / frc / team3501 / robot / commands / driving / TimeDrive.java
diff --git a/src/org/usfirst/frc/team3501/robot/commands/driving/TimeDrive.java b/src/org/usfirst/frc/team3501/robot/commands/driving/TimeDrive.java
new file mode 100644 (file)
index 0000000..e9a8f9f
--- /dev/null
@@ -0,0 +1,58 @@
+package org.usfirst.frc.team3501.robot.commands.driving;
+
+import org.usfirst.frc.team3501.robot.Constants.Auton;
+import org.usfirst.frc.team3501.robot.Robot;
+
+import edu.wpi.first.wpilibj.Timer;
+import edu.wpi.first.wpilibj.command.Command;
+
+public class TimeDrive extends Command {
+  Timer timer;
+  double currentTime, targetTime, speed;
+
+  public TimeDrive() {
+    this(Auton.DEFAULT_TIME, Auton.DEFAULT_SPEED);
+  }
+
+  public TimeDrive(double time) {
+    this(time, Auton.DEFAULT_SPEED);
+  }
+
+  public TimeDrive(double time, double speed) {
+    requires(Robot.driveTrain);
+
+    timer = new Timer();
+    this.currentTime = 0;
+    this.targetTime = time;
+    this.speed = speed;
+  }
+
+  @Override
+  protected void initialize() {
+    timer.start();
+  }
+
+  @Override
+  protected void execute() {
+    currentTime = timer.get();
+
+    double output = speed * ((targetTime - currentTime) / (targetTime));
+
+    Robot.driveTrain.setMotorSpeeds(output, output);
+  }
+
+  @Override
+  protected boolean isFinished() {
+    return currentTime >= targetTime;
+  }
+
+  @Override
+  protected void end() {
+    Robot.driveTrain.stop();
+  }
+
+  @Override
+  protected void interrupted() {
+    end();
+  }
+}