add95564848a6857e5df98c002992a610d619572
[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 this.gyroController.setDoneRange(1);
38 this.gyroController.setMinDoneCycles(5);
39 }
40
41 @Override
42 protected void initialize() {
43 this.driveTrain.resetEncoders();
44 this.gyroController.setSetPoint(this.target);
45 this.zeroAngle = driveTrain.getAngle();
46 }
47
48 @Override
49 protected void execute() {
50 double xVal = 0;
51
52 xVal = this.gyroController
53 .calcPID(Math.abs(this.driveTrain.getAngle() - this.zeroAngle));
54
55 double leftDrive = 0;
56 double rightDrive = 0;
57 if (direction == Constants.Direction.RIGHT) {
58 leftDrive = xVal;
59 rightDrive = -xVal;
60 } else if (direction == Constants.Direction.LEFT) {
61 leftDrive = -xVal;
62 rightDrive = xVal;
63 }
64
65 this.driveTrain.setMotorValues(leftDrive, rightDrive);
66 }
67
68 @Override
69 protected boolean isFinished() {
70 return timeSinceInitialized() >= maxTimeOut || gyroController.isDone();
71 }
72
73 @Override
74 protected void end() {
75 }
76
77 @Override
78 protected void interrupted() {
79 end();
80 }
81 }