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