clean up MoveDistance
[3501/2015-FRC-Spark] / src / org / usfirst / frc3501 / RiceCatRobot / commands / MoveDistance.java
CommitLineData
c9ab1206
KZ
1package org.usfirst.frc3501.RiceCatRobot.commands;
2
4b8a525f 3import org.usfirst.frc3501.RiceCatRobot.robot.Robot;
ba4d69ce 4import org.usfirst.frc3501.RiceCatRobot.subsystems.DriveTrain;
c9ab1206
KZ
5
6import edu.wpi.first.wpilibj.command.Command;
7
8public class MoveDistance extends Command {
aa9c330e
D
9 private double distance; // in units of TODO
10 private double minSpeed; // in range (0, 1]
11 private double maxSpeed; // in range (0, 1]
12
13 public MoveDistance(double distance, double minSpeed, double maxSpeed) {
14 requires(Robot.driveTrain);
15 this.distance = distance;
16 this.minSpeed = Math.min(minSpeed, maxSpeed);
17 this.maxSpeed = Math.max(minSpeed, maxSpeed);
18 }
19
20 @Override
21 protected void initialize() {
22 Robot.driveTrain.resetEncoders();
23 }
24
25 @Override
26 protected void execute() {
27 // TODO: create a math helper library and move this function there
28 double speed = 4 * (minSpeed - maxSpeed)
29 * Math.pow((Robot.driveTrain.getAverageSpeed() / distance - 0.5), 2)
30 + maxSpeed;
31 Robot.driveTrain.setMotorSpeeds(speed, speed);
32 }
33
34 @Override
35 protected boolean isFinished() {
36 return Robot.driveTrain.getLeftDistance() > distance
37 && Robot.driveTrain.getRightDistance() > distance;
38 }
39
40 @Override
41 protected void end() {
42 Robot.driveTrain.stop();
43 }
44
45 @Override
46 protected void interrupted() {
47 end();
48 }
c9ab1206 49}