move pidcontroller into drivetrain
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / subsystems / DriveTrain.java
CommitLineData
38a404b3
KZ
1package org.usfirst.frc.team3501.robot.subsystems;
2
3import org.usfirst.frc.team3501.robot.Constants;
5483fde9 4import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive;
06df4cd4 5import org.usfirst.frc.team3501.robot.utils.PIDController;
04f2cb52 6
a1c76caf
CZ
7import com.ctre.CANTalon;
8
bf921ece 9import edu.wpi.first.wpilibj.ADXRS450_Gyro;
4a75c629 10import edu.wpi.first.wpilibj.DoubleSolenoid;
73e8adbc 11import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
fa4e4a97 12import edu.wpi.first.wpilibj.Encoder;
ccbc35ae 13import edu.wpi.first.wpilibj.RobotDrive;
38a404b3
KZ
14import edu.wpi.first.wpilibj.command.Subsystem;
15
16public class DriveTrain extends Subsystem {
34ffcd03
ES
17
18 public static double driveP, driveI, driveD;
542234fe 19 public static double turnP, turnI, turnD;
1479f285 20
c846487e 21 public static double driveStraightGyroP = 0.01;
e12d6901 22
665abef7
MW
23 public static final String DRIVE_P_Val = "DriveP";
24 public static final String DRIVE_I_Val = "DriveI";
25 public static final String DRIVE_D_Val = "DriveD";
26 public static final String DRIVE_TARGET_DIST = "SET_DIST";
27 public static final String DRIVE_MOTOR_VAL = "SPEED";
28 public static final String GYRO_P_Val = "GyroP";
29 public static final String GYRO_I_Val = "GyroI";
30 public static final String GYRO_D_Val = "GyroD";
31 public static final String GYRO_TARGET_ANGLE = "SET_ANGLE";
32 public static final int PID_ERROR = -1;
33 public static final int TARGET_DISTANCE_ERROR = -1;
34
b634ebbc
CZ
35 public static final double WHEEL_DIAMETER = 6; // inches
36 public static final int ENCODER_PULSES_PER_REVOLUTION = 256;
37 public static final double INCHES_PER_PULSE = WHEEL_DIAMETER * Math.PI
38 / ENCODER_PULSES_PER_REVOLUTION;
9dc69158
CZ
39
40 public static final double MAINTAIN_CLIMBED_POSITION = 0;
41 public static final double TIME_TO_CLIMB_FOR = 0;
42 public static final double CLIMBER_SPEED = 0;
43
cc42bd52 44 private static DriveTrain driveTrain;
b8791ca4 45
cc42bd52
ME
46 private final CANTalon frontLeft, frontRight, rearLeft, rearRight;
47 private final RobotDrive robotDrive;
48 private final Encoder leftEncoder, rightEncoder;
73e8adbc 49 private final DoubleSolenoid leftGearPiston, rightGearPiston;
cc42bd52 50
bf921ece 51 private ADXRS450_Gyro imu;
e12d6901 52
366f1cfd 53 public boolean shouldBeClimbing = false;
cef1f36d 54
06df4cd4 55 private PIDController driveController;
01ea2879 56 private PIDController gyroController;
06df4cd4 57
ffb43c5c
RR
58 private boolean isClimbing;
59 private static double CLIMBER_SPEED;;
47140f21 60 public boolean shouldBeClimbing = false;
ffb43c5c 61
0c15b549 62 private PIDController driveController;
06df4cd4 63
0c15b549 64 private DriveTrain() {
06df4cd4 65
63c60c71 66 // PID TUNING
0c15b549
E
67 driveController = new PIDController(driveP, driveI, driveD);
68 gyroController = new PIDController(turnP, turnI, turnD
63c60c71 69
cc42bd52
ME
70 // MOTOR CONTROLLERS
71 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
72 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
73 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
74 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
75
76 // ENCODERS
77 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
3e2738c4 78 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
cc42bd52 79 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
3e2738c4 80 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
cc42bd52 81
b634ebbc
CZ
82 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
83 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
cc42bd52
ME
84
85 // ROBOT DRIVE
86 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
8e4c083c 87
bf921ece 88 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
af4491f7 89
ca372ce8 90 // TODO: Not sure if MODULE_NUMBER should be the same for both
58bcc21d 91 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
92 Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD,
93 Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE);
58bcc21d 94 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
95 Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD,
96 Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE);
cc42bd52
ME
97 }
98
06df4cd4
E
99 public PIDController getDriveController() {
100 return this.driveController;
101 }
102
90b435ce
ES
103 public PIDController getGyroController() {
104 return this.gyroController;
105 }
106
cc42bd52
ME
107 public static DriveTrain getDriveTrain() {
108 if (driveTrain == null) {
109 driveTrain = new DriveTrain();
110 }
111 return driveTrain;
112 }
113
114 // DRIVE METHODS
115 public void setMotorValues(final double left, final double right) {
116 frontLeft.set(left);
117 rearLeft.set(left);
118
b081e34b
ME
119 frontRight.set(-right);
120 rearRight.set(-right);
cc42bd52
ME
121 }
122
123 public void joystickDrive(final double thrust, final double twist) {
b081e34b 124 robotDrive.arcadeDrive(thrust, twist, true);
cc42bd52
ME
125 }
126
127 public void stop() {
128 setMotorValues(0, 0);
129 }
130
f0a71840
CZ
131 public double getLeftMotorVal() {
132 return (frontLeft.get() + rearLeft.get()) / 2;
cc42bd52
ME
133 }
134
f0a71840
CZ
135 public double getRightMotorVal() {
136 return (frontRight.get() + rearRight.get()) / 2;
cc42bd52
ME
137 }
138
cc42bd52
ME
139 // ENCODER METHODS
140
3a5d9ac7 141 public double getLeftEncoderDistance() {
cc42bd52
ME
142 return leftEncoder.getDistance();
143 }
144
3a5d9ac7 145 public double getRightEncoderDistance() {
cc42bd52
ME
146 return rightEncoder.getDistance();
147 }
148
8e4c083c 149 public void printEncoderOutput() {
174f415c
CZ
150 System.out.println("left: " + getLeftEncoderDistance());
151 System.out.println("right: " + getRightEncoderDistance());
c846487e 152 // System.out.println(getAvgEncoderDistance());
8e4c083c
CZ
153 }
154
cc42bd52
ME
155 public double getAvgEncoderDistance() {
156 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
157 }
158
159 public void resetEncoders() {
160 leftEncoder.reset();
161 rightEncoder.reset();
162 }
163
164 public double getLeftSpeed() {
165 return leftEncoder.getRate();
166 }
167
168 public double getRightSpeed() {
169 return rightEncoder.getRate();
170 }
171
172 public double getSpeed() {
173 return (getLeftSpeed() + getRightSpeed()) / 2.0;
174 }
175
e12d6901
CZ
176 // ------Gyro------//
177 public double getAngle() {
c846487e 178 return this.imu.getAngle();
e12d6901
CZ
179 }
180
181 public void resetGyro() {
571fb5c9 182 this.imu.reset();
e12d6901
CZ
183 }
184
73e8adbc
AD
185 /*
186 * @return a value that is the current setpoint for the piston kReverse or
187 * KForward
188 */
06415bd8 189 public Value getLeftGearPistonValue() {
73e8adbc
AD
190 return leftGearPiston.get();
191 }
192
06415bd8
AD
193 /*
194 * @return a value that is the current setpoint for the piston kReverse or
195 * KForward
196 */
197 public Value getRightGearPistonValue() {
198 return rightGearPiston.get();
199 }
200
73e8adbc
AD
201 /*
202 * Changes the ball shift gear assembly to high
203 */
204 public void setHighGear() {
205 changeGear(Constants.DriveTrain.HIGH_GEAR);
206 }
207
208 /*
209 * Changes the ball shift gear assembly to low
210 */
211 public void setLowGear() {
212 changeGear(Constants.DriveTrain.LOW_GEAR);
213 }
214
215 /*
216 * Changes the gear to a DoubleSolenoid.Value
217 */
218 private void changeGear(DoubleSolenoid.Value gear) {
219 leftGearPiston.set(gear);
220 rightGearPiston.set(gear);
e12d6901
CZ
221 }
222
cc42bd52
ME
223 @Override
224 protected void initDefaultCommand() {
225 setDefaultCommand(new JoystickDrive());
226 }
38a404b3 227}