create basic DriveFor command so it can be built upon
[3501/2015-FRC-Spark] / src / org / usfirst / frc3501 / RiceCatRobot / commands / DriveFor.java
index 6e61e616bd0bf16d4b09942224edef3c73908be4..b614a076416dde37517d9670a15d047fa8bee1b2 100644 (file)
@@ -2,6 +2,7 @@ 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;
@@ -11,66 +12,48 @@ import edu.wpi.first.wpilibj.command.Command;
  *
  */
 public class DriveFor extends Command {
-    private double seconds;
-    private Timer timer;
-    private Direction direction;
-
-    public DriveFor(double seconds, Direction direction) {
-        this.seconds = seconds;
-        this.direction = direction;
-
-    }
-
-    @Override
-    protected void initialize() {
-        timer = new Timer();
-        timer.reset();
-        timer.start();
-    }
-
-    @Override
-    protected void execute() {
-        System.out.println(timer.get());
-        if (direction == Direction.FORWARD) {
-            if (timer.get() < seconds * 0.2) { // for the first 20% of time, run
-                                               // the robot at -.5 speed
-                Robot.driveTrain.arcadeDrive(-0.3, 0);
-            } else if (timer.get() >= seconds * 0.2
-                    && timer.get() <= seconds * 0.8) { // for the +20% - 75%
-                                                       // time, move the robot
-                                                       // at -.3 speed.
-                Robot.driveTrain.arcadeDrive(-0.5, 0);
-            } else if (timer.get() < seconds) {
-                Robot.driveTrain.arcadeDrive(-0.25, 0);
-            } else {
-                Robot.driveTrain.arcadeDrive(0, 0);
-            }
-        } else if (direction == Direction.BACKWARD) {
-            if (timer.get() < seconds * 0.2) {
-                Robot.driveTrain.arcadeDrive(0.3, 0);
-            } else if (timer.get() >= seconds * 0.2
-                    && timer.get() <= seconds * 0.8) {
-                Robot.driveTrain.arcadeDrive(0.5, 0);
-            } else if (timer.get() < seconds) {
-                Robot.driveTrain.arcadeDrive(0.25, 0);
-            } else {
-                Robot.driveTrain.arcadeDrive(0, 0);
-            }
-        }
-    }
-
-    @Override
-    protected boolean isFinished() {
-        return timer.get() > seconds;
-    }
-
-    @Override
-    protected void end() {
-        Robot.driveTrain.arcadeDrive(0, 0);
-    }
-
-    @Override
-    protected void interrupted() {
-        end();
+  private double seconds;
+  private double speed;
+  private Timer timer;
+  private Direction direction;
+
+  public DriveFor(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);
+
+    this.seconds = seconds;
+    this.direction = direction;
+  }
+
+  @Override
+  protected void initialize() {
+    timer = new Timer();
+    timer.reset();
+    timer.start();
+  }
+
+  @Override
+  protected void execute() {
+    if (direction == Direction.FORWARD) {
+      Robot.driveTrain.setMotorSpeeds(-speed, -speed);
+    } else if (direction == Direction.BACKWARD) {
+      Robot.driveTrain.setMotorSpeeds(speed, speed);
     }
-}
+  }
+
+  @Override
+  protected boolean isFinished() {
+    return timer.get() > seconds;
+  }
+
+  @Override
+  protected void end() {
+    Robot.driveTrain.setMotorSpeeds(0, 0);
+  }
+
+  @Override
+  protected void interrupted() {
+    end();
+  }
+}
\ No newline at end of file