fix zeroAngle error
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / commands / driving / TurnForAngle.java
1 package org.usfirst.frc.team3501.robot.commands.driving;
2
3 import org.usfirst.frc.team3501.robot.Constants;
4 import org.usfirst.frc.team3501.robot.Constants.Direction;
5 import org.usfirst.frc.team3501.robot.Robot;
6 import org.usfirst.frc.team3501.robot.subsystems.DriveTrain;
7 import org.usfirst.frc.team3501.robot.utils.PIDController;
8
9 import edu.wpi.first.wpilibj.command.Command;
10
11 /**
12 * This command turns the robot for a specified angle in specified direction -
13 * either left or right
14 *
15 * parameters: angle: the robot will turn - in degrees direction: the robot will
16 * turn - either right or left maxTimeOut: the max time this command will be
17 * allowed to run on for
18 */
19 public class TurnForAngle extends Command {
20 private DriveTrain driveTrain = Robot.getDriveTrain();
21
22 Direction direction;
23 private double maxTimeOut;
24 private PIDController gyroController;
25
26 private double target;
27
28 private double zeroAngle;
29
30 public TurnForAngle(double angle, Direction direction, double maxTimeOut) {
31 requires(Robot.getDriveTrain());
32 this.direction = direction;
33 this.maxTimeOut = maxTimeOut;
34 this.target = Math.abs(angle);
35
36 this.gyroController = Robot.getDriveTrain().getGyroController();
37
38 this.gyroController.setDoneRange(1);
39 this.gyroController.setMinDoneCycles(5);
40 }
41
42 @Override
43 protected void initialize() {
44 this.driveTrain.resetEncoders();
45 this.gyroController.setSetPoint(this.target);
46 this.zeroAngle = driveTrain.getAngle();
47 }
48
49 @Override
50 protected void execute() {
51 double xVal = 0;
52
53 xVal = this.gyroController
54 .calcPID(Math.abs(this.driveTrain.getAngle() - this.zeroAngle));
55
56 double leftDrive = 0;
57 double rightDrive = 0;
58 if (direction == Constants.Direction.RIGHT) {
59 leftDrive = xVal;
60 rightDrive = -xVal;
61 } else if (direction == Constants.Direction.LEFT) {
62 leftDrive = -xVal;
63 rightDrive = xVal;
64 }
65
66 this.driveTrain.setMotorValues(leftDrive, rightDrive);
67 }
68
69 @Override
70 protected boolean isFinished() {
71 return timeSinceInitialized() >= maxTimeOut || gyroController.isDone();
72 }
73
74 @Override
75 protected void end() {
76 }
77
78 @Override
79 protected void interrupted() {
80 end();
81 }
82 }