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