Add command group for ToggleWinch
[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;;
60
cc42bd52 61 private DriveTrain() {
06df4cd4 62
9ffd117a 63 driveController = new PIDController(driveP, driveI, driveD);
542234fe 64 gyroController = new PIDController(turnP, turnI, turnD);
06df4cd4 65
63c60c71
MW
66 // PID TUNING
67
cc42bd52
ME
68 // MOTOR CONTROLLERS
69 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
70 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
71 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
72 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
73
74 // ENCODERS
75 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
3e2738c4 76 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
cc42bd52 77 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
3e2738c4 78 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
cc42bd52 79
b634ebbc
CZ
80 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
81 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
cc42bd52
ME
82
83 // ROBOT DRIVE
84 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
8e4c083c 85
bf921ece 86 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
af4491f7 87
ca372ce8 88 // TODO: Not sure if MODULE_NUMBER should be the same for both
58bcc21d 89 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
90 Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD,
91 Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE);
58bcc21d 92 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
93 Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD,
94 Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE);
ffb43c5c
RR
95
96 CLIMBER_SPEED = Constants.DriveTrain.CLIMBER_SPEED;
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);
731aef6d 121 this.isClimbing = true;
cc42bd52
ME
122 }
123
124 public void joystickDrive(final double thrust, final double twist) {
b081e34b 125 robotDrive.arcadeDrive(thrust, twist, true);
cc42bd52
ME
126 }
127
128 public void stop() {
129 setMotorValues(0, 0);
731aef6d 130 this.isClimbing = false;
cc42bd52
ME
131 }
132
f0a71840
CZ
133 public double getLeftMotorVal() {
134 return (frontLeft.get() + rearLeft.get()) / 2;
cc42bd52
ME
135 }
136
f0a71840
CZ
137 public double getRightMotorVal() {
138 return (frontRight.get() + rearRight.get()) / 2;
cc42bd52
ME
139 }
140
cc42bd52
ME
141 // ENCODER METHODS
142
3a5d9ac7 143 public double getLeftEncoderDistance() {
cc42bd52
ME
144 return leftEncoder.getDistance();
145 }
146
3a5d9ac7 147 public double getRightEncoderDistance() {
cc42bd52
ME
148 return rightEncoder.getDistance();
149 }
150
8e4c083c 151 public void printEncoderOutput() {
174f415c
CZ
152 System.out.println("left: " + getLeftEncoderDistance());
153 System.out.println("right: " + getRightEncoderDistance());
c846487e 154 // System.out.println(getAvgEncoderDistance());
8e4c083c
CZ
155 }
156
cc42bd52
ME
157 public double getAvgEncoderDistance() {
158 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
159 }
160
161 public void resetEncoders() {
162 leftEncoder.reset();
163 rightEncoder.reset();
164 }
165
166 public double getLeftSpeed() {
167 return leftEncoder.getRate();
168 }
169
170 public double getRightSpeed() {
171 return rightEncoder.getRate();
172 }
173
174 public double getSpeed() {
175 return (getLeftSpeed() + getRightSpeed()) / 2.0;
176 }
177
e12d6901
CZ
178 // ------Gyro------//
179 public double getAngle() {
c846487e 180 return this.imu.getAngle();
e12d6901
CZ
181 }
182
183 public void resetGyro() {
571fb5c9 184 this.imu.reset();
e12d6901
CZ
185 }
186
73e8adbc
AD
187 /*
188 * @return a value that is the current setpoint for the piston kReverse or
189 * KForward
190 */
06415bd8 191 public Value getLeftGearPistonValue() {
73e8adbc
AD
192 return leftGearPiston.get();
193 }
194
06415bd8
AD
195 /*
196 * @return a value that is the current setpoint for the piston kReverse or
197 * KForward
198 */
199 public Value getRightGearPistonValue() {
200 return rightGearPiston.get();
201 }
202
73e8adbc
AD
203 /*
204 * Changes the ball shift gear assembly to high
205 */
206 public void setHighGear() {
207 changeGear(Constants.DriveTrain.HIGH_GEAR);
208 }
209
210 /*
211 * Changes the ball shift gear assembly to low
212 */
213 public void setLowGear() {
214 changeGear(Constants.DriveTrain.LOW_GEAR);
215 }
216
217 /*
218 * Changes the gear to a DoubleSolenoid.Value
219 */
220 private void changeGear(DoubleSolenoid.Value gear) {
221 leftGearPiston.set(gear);
222 rightGearPiston.set(gear);
e12d6901
CZ
223 }
224
cc42bd52
ME
225 @Override
226 protected void initDefaultCommand() {
227 setDefaultCommand(new JoystickDrive());
228 }
ffb43c5c
RR
229
230 public boolean isClimbing() {
231 return this.isClimbing;
232 }
233
731aef6d
RR
234 public void setClimbing(boolean isClimbing) {
235 this.isClimbing = isClimbing;
236 }
237
ffb43c5c
RR
238 public double getClimbingSpeed() {
239 return this.CLIMBER_SPEED;
240 }
241
38a404b3 242}