clean up DriveForTime
authorDavid <david.dobervich@gmail.com>
Fri, 20 Nov 2015 18:47:03 +0000 (10:47 -0800)
committerDavid <david.dobervich@gmail.com>
Fri, 20 Nov 2015 18:47:03 +0000 (10:47 -0800)
src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTime.java
src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTimesSequence.java
src/org/usfirst/frc3501/RiceCatRobot/subsystems/DriveTrain.java

index 9f7d91bf2009f184e2c9a0f6b151014baf25fe17..a268a10704345efa763e0a03f23aefd105971713 100644 (file)
@@ -14,42 +14,33 @@ import edu.wpi.first.wpilibj.command.Command;
 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
index 8b1321df4cbf8cf8cabd2fb02631d074bda27f9a..cfd1c12c4ee185be99575d52643331bc2884a1d7 100644 (file)
@@ -21,17 +21,13 @@ public class DriveForTimesSequence extends CommandGroup {
    *          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
index 02346679ce35b48b4e4870a436a7fd656464d876..f76fe4287b28c3d26e25f1fd0251553eed7a7ffd 100644 (file)
@@ -51,6 +51,10 @@ public class DriveTrain extends Subsystem {
     return leftEncoder.getDistance();
   }
 
+  public void stop() {
+    setMotorSpeeds(0, 0);
+  }
+
   public void setMotorSpeeds(double leftSpeed, double rightSpeed) {
     if (Math.abs(leftSpeed) < RobotMap.DRIVE_DEAD_ZONE) {
       leftSpeed = 0;