f6e11f4c94268c7190d540edb3045e618f2e5488
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / commands / driving / DriveDistance.java
1 package org.usfirst.frc.team3501.robot.commands.driving;
2
3 import org.usfirst.frc.team3501.robot.Robot;
4 import org.usfirst.frc.team3501.robot.subsystems.DriveTrain;
5 import org.usfirst.frc.team3501.robot.utils.PIDController;
6
7 import edu.wpi.first.wpilibj.Preferences;
8 import edu.wpi.first.wpilibj.command.Command;
9
10 /**
11 * This command makes the robot drive a specified distance using encoders on the
12 * robot and using a feedback loop
13 *
14 * parameters: distance the robot will move in inches motorVal: the motor input
15 * to set the motors to
16 */
17 public class DriveDistance extends Command {
18 private DriveTrain driveTrain = Robot.getDriveTrain();
19 private double maxTimeOut = 10;
20 private double target;
21 private double zeroAngle;
22 private Preferences prefs;
23 private PIDController driveController;
24
25 private double gyroP;
26
27 public DriveDistance(double distance, double maxTimeOut) {
28 requires(driveTrain);
29 this.target = distance;
30 this.driveController = driveTrain.getDriveController();
31 this.driveController.setDoneRange(0.5);
32 this.driveController.setMaxOutput(motorVal);
33 this.driveController.setMinDoneCycles(5);
34 }
35
36 @Override
37 protected void initialize() {
38 this.driveTrain.resetEncoders();
39 this.driveController.setSetPoint(this.target);
40 }
41
42 @Override
43 protected void execute() {
44 double xVal = gyroP * (driveTrain.getAngle() - zeroAngle);
45 double yVal = driveController.calcPID(driveTrain.getAvgEncoderDistance());
46
47 double leftDrive = yVal - xVal;
48 double rightDrive = yVal + xVal;
49 this.driveTrain.setMotorValues(leftDrive, rightDrive);
50 }
51
52 @Override
53 protected boolean isFinished() {
54 return timeSinceInitialized() >= maxTimeOut
55 || this.driveController.isDone();
56 }
57
58 @Override
59 protected void end() {
60 }
61
62 @Override
63 protected void interrupted() {
64 end();
65 }
66 }