add mathlib and smooth accelerate distance command
[3501/2015-FRC-Spark] / src / org / usfirst / frc3501 / RiceCatRobot / commands / SmoothAccelerateForDistance.java
diff --git a/src/org/usfirst/frc3501/RiceCatRobot/commands/SmoothAccelerateForDistance.java b/src/org/usfirst/frc3501/RiceCatRobot/commands/SmoothAccelerateForDistance.java
new file mode 100644 (file)
index 0000000..b774346
--- /dev/null
@@ -0,0 +1,53 @@
+package org.usfirst.frc3501.RiceCatRobot.commands;
+
+import org.usfirst.frc3501.RiceCatRobot.Robot;
+import org.usfirst.frc3501.RiceCatRobot.RobotMap.Direction;
+import org.usfirst.frc3501.RiceCatRobot.subsystems.DriveTrain;
+
+import edu.wpi.first.wpilibj.Timer;
+import edu.wpi.first.wpilibj.command.Command;
+
+/**
+ * This command will drive the specified distance at specified speed.
+ *
+ */
+public class SmoothAccelerateForDistance extends Command {
+  private double distance; // in units of cm
+  private double speed;
+
+  public SmoothAccelerateForDistance(double distance, 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;
+
+    // ensure distance is positive
+    this.distance = Math.max(distance, -distance);
+  }
+
+  @Override
+  protected void initialize() {
+    Robot.driveTrain.resetEncoders();
+    Robot.driveTrain.setMotorSpeeds(speed, speed);
+  }
+
+  @Override
+  protected void execute() {
+    // nothing to do since motor speeds already set
+  }
+
+  @Override
+  protected boolean isFinished() {
+    return Math.abs(Robot.driveTrain.getLeftDistance()) >= distance
+        && Math.abs(Robot.driveTrain.getRightDistance()) >= distance;
+  }
+
+  @Override
+  protected void end() {
+    Robot.driveTrain.stop();
+  }
+
+  @Override
+  protected void interrupted() {
+    end();
+  }
+}
\ No newline at end of file