Commit | Line | Data |
---|---|---|
38a404b3 KZ |
1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | ||
3 | import org.usfirst.frc.team3501.robot.Constants; | |
5483fde9 | 4 | import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive; |
06df4cd4 | 5 | import org.usfirst.frc.team3501.robot.utils.PIDController; |
04f2cb52 | 6 | |
a1c76caf CZ |
7 | import com.ctre.CANTalon; |
8 | ||
bf921ece | 9 | import edu.wpi.first.wpilibj.ADXRS450_Gyro; |
4a75c629 | 10 | import edu.wpi.first.wpilibj.DoubleSolenoid; |
73e8adbc | 11 | import edu.wpi.first.wpilibj.DoubleSolenoid.Value; |
fa4e4a97 | 12 | import edu.wpi.first.wpilibj.Encoder; |
ccbc35ae | 13 | import edu.wpi.first.wpilibj.RobotDrive; |
38a404b3 KZ |
14 | import edu.wpi.first.wpilibj.command.Subsystem; |
15 | ||
16 | public 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 | |
06df4cd4 | 51 | private PIDController driveController; |
01ea2879 | 52 | private PIDController gyroController; |
06df4cd4 | 53 | |
ffb43c5c | 54 | private boolean isClimbing; |
9ee32d5e | 55 | public boolean shouldBeClimbing = false; |
06df4cd4 | 56 | |
0c15b549 | 57 | private DriveTrain() { |
06df4cd4 | 58 | |
63c60c71 | 59 | // PID TUNING |
0c15b549 | 60 | driveController = new PIDController(driveP, driveI, driveD); |
d3ed4499 | 61 | gyroController = new PIDController(turnP, turnI, turnD); |
63c60c71 | 62 | |
40d69167 MW |
63 | // PID TUNING |
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 | } |