add seperate pid controllers
[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 <<<<<<< HEAD
36 =======
37 this.zeroAngle = driveTrain.getAngle();
38 >>>>>>> fix alot of unnecessary/not used code and init gyroController
39
40 this.gyroController = Robot.getDriveTrain().getGyroController();
41
42 <<<<<<< c56ee177a9387e78474e35cbaa7355c443d5ae8a
43 =======
44 this.gyroController = Robot.getDriveTrain().getGyroController();
45 >>>>>>> add seperate pid controllers
46 this.gyroController.setDoneRange(1);
47 this.gyroController.setMinDoneCycles(5);
48 }
49
50 @Override
51 protected void initialize() {
52 this.driveTrain.resetEncoders();
53 this.gyroController.setSetPoint(this.target);
54 this.zeroAngle = driveTrain.getAngle();
55 }
56
57 @Override
58 protected void execute() {
59 double xVal = 0;
60
61 xVal = this.gyroController
62 .calcPID(Math.abs(this.driveTrain.getAngle() - this.zeroAngle));
63
64 double leftDrive = 0;
65 double rightDrive = 0;
66 if (direction == Constants.Direction.RIGHT) {
67 leftDrive = xVal;
68 rightDrive = -xVal;
69 } else if (direction == Constants.Direction.LEFT) {
70 leftDrive = -xVal;
71 rightDrive = xVal;
72 }
73
74 this.driveTrain.setMotorValues(leftDrive, rightDrive);
75 }
76
77 @Override
78 protected boolean isFinished() {
79 return timeSinceInitialized() >= maxTimeOut || gyroController.isDone();
80 }
81
82 @Override
83 protected void end() {
84 }
85
86 @Override
87 protected void interrupted() {
88 end();
89 }
90 }