Commit | Line | Data |
---|---|---|
aa160dfd ME |
1 | package org.usfirst.frc.team3501.robot.commands.driving; |
2 | ||
f0a71840 | 3 | import org.usfirst.frc.team3501.robot.Constants; |
8af76159 | 4 | import org.usfirst.frc.team3501.robot.Constants.Direction; |
aa160dfd | 5 | import org.usfirst.frc.team3501.robot.Robot; |
f0a71840 CZ |
6 | import org.usfirst.frc.team3501.robot.subsystems.DriveTrain; |
7 | import org.usfirst.frc.team3501.robot.utils.PIDController; | |
aa160dfd ME |
8 | |
9 | import edu.wpi.first.wpilibj.command.Command; | |
10 | ||
11 | /** | |
9b9d6bed ME |
12 | * This command turns the robot for a specified angle in specified direction - |
13 | * either left or right | |
14 | * | |
f0a71840 CZ |
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 | |
aa160dfd ME |
18 | */ |
19 | public class TurnForAngle extends Command { | |
f0a71840 | 20 | private DriveTrain driveTrain = Robot.getDriveTrain(); |
aa160dfd | 21 | |
f0a71840 CZ |
22 | Direction direction; |
23 | private double maxTimeOut; | |
24 | private PIDController gyroController; | |
25 | ||
26 | private double target; | |
f0a71840 | 27 | |
571fb5c9 CZ |
28 | private double zeroAngle; |
29 | ||
f0a71840 | 30 | public TurnForAngle(double angle, Direction direction, double maxTimeOut) { |
d17d868d | 31 | requires(Robot.getDriveTrain()); |
f0a71840 CZ |
32 | this.direction = direction; |
33 | this.maxTimeOut = maxTimeOut; | |
571fb5c9 | 34 | this.target = Math.abs(angle); |
4288a3a4 E |
35 | <<<<<<< HEAD |
36 | ======= | |
37 | this.zeroAngle = driveTrain.getAngle(); | |
38 | >>>>>>> fix alot of unnecessary/not used code and init gyroController | |
f0a71840 | 39 | |
09ee7387 | 40 | <<<<<<< 3921ddf563ce7727724494c62d65c2ca0e8ff8cc |
90b435ce | 41 | this.gyroController = Robot.getDriveTrain().getGyroController(); |
09ee7387 ES |
42 | ======= |
43 | this.gyroP = driveTrain.turnP; | |
44 | this.gyroI = driveTrain.turnI; | |
45 | this.gyroD = driveTrain.turnD; | |
46 | ||
47 | this.gyroController = Robot.getDriveTrain().getDriveController(); | |
48 | >>>>>>> update code | |
f0a71840 CZ |
49 | this.gyroController.setDoneRange(1); |
50 | this.gyroController.setMinDoneCycles(5); | |
d17d868d | 51 | } |
52 | ||
d17d868d | 53 | @Override |
54 | protected void initialize() { | |
f0a71840 | 55 | this.driveTrain.resetEncoders(); |
f0a71840 | 56 | this.gyroController.setSetPoint(this.target); |
c726d04a | 57 | this.zeroAngle = driveTrain.getAngle(); |
d17d868d | 58 | } |
59 | ||
d17d868d | 60 | @Override |
61 | protected void execute() { | |
f0a71840 CZ |
62 | double xVal = 0; |
63 | ||
64 | xVal = this.gyroController | |
571fb5c9 | 65 | .calcPID(Math.abs(this.driveTrain.getAngle() - this.zeroAngle)); |
f0a71840 | 66 | |
571fb5c9 CZ |
67 | double leftDrive = 0; |
68 | double rightDrive = 0; | |
f0a71840 CZ |
69 | if (direction == Constants.Direction.RIGHT) { |
70 | leftDrive = xVal; | |
71 | rightDrive = -xVal; | |
571fb5c9 | 72 | } else if (direction == Constants.Direction.LEFT) { |
f0a71840 CZ |
73 | leftDrive = -xVal; |
74 | rightDrive = xVal; | |
75 | } | |
76 | ||
77 | this.driveTrain.setMotorValues(leftDrive, rightDrive); | |
d17d868d | 78 | } |
79 | ||
d17d868d | 80 | @Override |
81 | protected boolean isFinished() { | |
f0a71840 | 82 | return timeSinceInitialized() >= maxTimeOut || gyroController.isDone(); |
d17d868d | 83 | } |
84 | ||
d17d868d | 85 | @Override |
86 | protected void end() { | |
87 | } | |
88 | ||
d17d868d | 89 | @Override |
90 | protected void interrupted() { | |
f0a71840 | 91 | end(); |
d17d868d | 92 | } |
aa160dfd | 93 | } |