From: David Date: Fri, 20 Nov 2015 18:47:03 +0000 (-0800) Subject: clean up DriveForTime X-Git-Url: http://challenge-bot.com/repos/?a=commitdiff_plain;h=0b38c075bbebc58d0a71f8eb19f0e82a5055b9b5;p=3501%2F2015-FRC-Spark clean up DriveForTime --- diff --git a/src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTime.java b/src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTime.java index 9f7d91b..a268a10 100644 --- a/src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTime.java +++ b/src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTime.java @@ -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 diff --git a/src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTimesSequence.java b/src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTimesSequence.java index 8b1321d..cfd1c12 100644 --- a/src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTimesSequence.java +++ b/src/org/usfirst/frc3501/RiceCatRobot/commands/DriveForTimesSequence.java @@ -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 diff --git a/src/org/usfirst/frc3501/RiceCatRobot/subsystems/DriveTrain.java b/src/org/usfirst/frc3501/RiceCatRobot/subsystems/DriveTrain.java index 0234667..f76fe42 100644 --- a/src/org/usfirst/frc3501/RiceCatRobot/subsystems/DriveTrain.java +++ b/src/org/usfirst/frc3501/RiceCatRobot/subsystems/DriveTrain.java @@ -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;