Commit | Line | Data |
---|---|---|
38a404b3 KZ |
1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | ||
3 | import org.usfirst.frc.team3501.robot.Constants; | |
33141cdd KZ |
4 | import org.usfirst.frc.team3501.robot.GyroLib; |
5 | import org.usfirst.frc.team3501.robot.MathLib; | |
6 | import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive; | |
111dc444 | 7 | |
38a404b3 | 8 | import edu.wpi.first.wpilibj.CANTalon; |
111dc444 | 9 | import edu.wpi.first.wpilibj.CounterBase.EncodingType; |
d9c04720 | 10 | import edu.wpi.first.wpilibj.DoubleSolenoid; |
2aea5cc2 | 11 | import edu.wpi.first.wpilibj.DoubleSolenoid.Value; |
111dc444 | 12 | import edu.wpi.first.wpilibj.Encoder; |
b54ad73b | 13 | import edu.wpi.first.wpilibj.I2C; |
33141cdd KZ |
14 | import edu.wpi.first.wpilibj.RobotDrive; |
15 | import edu.wpi.first.wpilibj.command.PIDSubsystem; | |
16 | ||
17 | public class DriveTrain extends PIDSubsystem { | |
2a099bc6 KZ |
18 | private static double kp = 0.013, ki = 0.000015, kd = -0.002; |
19 | private static double gp = 0.018, gi = 0.000015, gd = 0; | |
33141cdd KZ |
20 | private static double pidOutput = 0; |
21 | private static double encoderTolerance = 8.0, gyroTolerance = 5.0; | |
22 | private int DRIVE_MODE = 1; | |
23 | ||
24 | private static final int MANUAL_MODE = 1, ENCODER_MODE = 2, GYRO_MODE = 3; | |
38a404b3 | 25 | |
1884c3cf | 26 | private Encoder leftEncoder, rightEncoder; |
d7bf2340 | 27 | private CANTalon frontLeft, frontRight, rearLeft, rearRight; |
33141cdd KZ |
28 | private RobotDrive robotDrive; |
29 | ||
30 | private GyroLib gyro; | |
d9c04720 SC |
31 | private DoubleSolenoid leftGearPiston, rightGearPiston; |
32 | // Drivetrain specific constants that relate to the inches per pulse value for | |
33 | // the encoders | |
34 | private final static double WHEEL_DIAMETER = 6.0; // in inches | |
35 | private final static double PULSES_PER_ROTATION = 256; // in pulses | |
36 | private final static double OUTPUT_SPROCKET_DIAMETER = 2.0; // in inches | |
37 | private final static double WHEEL_SPROCKET_DIAMETER = 3.5; // in inches | |
38 | public final static double INCHES_PER_PULSE = (((Math.PI) | |
39 | * OUTPUT_SPROCKET_DIAMETER / PULSES_PER_ROTATION) | |
40 | / WHEEL_SPROCKET_DIAMETER) * WHEEL_DIAMETER; | |
41 | ||
42 | // Drivetrain specific constants that relate to the PID controllers | |
43 | private final static double Kp = 1.0, Ki = 0.0, | |
44 | Kd = 0.0 * (OUTPUT_SPROCKET_DIAMETER / PULSES_PER_ROTATION) | |
54c588d9 | 45 | / (WHEEL_SPROCKET_DIAMETER) * WHEEL_DIAMETER; |
71d73690 | 46 | |
d7bf2340 | 47 | public DriveTrain() { |
33141cdd KZ |
48 | super(kp, ki, kd); |
49 | ||
d7bf2340 KZ |
50 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); |
51 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); | |
52 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); | |
53 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); | |
1884c3cf | 54 | |
33141cdd | 55 | robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight); |
d7bf2340 KZ |
56 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, |
57 | Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X); | |
58 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, | |
59 | Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X); | |
45bdf5b9 KZ |
60 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
61 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
33141cdd KZ |
62 | |
63 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
64 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
65 | ||
66 | gyro = new GyroLib(I2C.Port.kOnboard, false); | |
67 | ||
68 | DRIVE_MODE = Constants.DriveTrain.ENCODER_MODE; | |
69 | setEncoderPID(); | |
70 | this.disable(); | |
71 | gyro.start(); | |
d004deee | 72 | |
d9c04720 SC |
73 | leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.LEFT_FORWARD, |
74 | +Constants.DriveTrain.LEFT_REVERSE); | |
75 | rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.RIGHT_FORWARD, | |
76 | Constants.DriveTrain.RIGHT_REVERSE); | |
54c588d9 | 77 | Constants.DriveTrain.inverted = false; |
d7bf2340 KZ |
78 | } |
79 | ||
80 | @Override | |
81 | protected void initDefaultCommand() { | |
33141cdd KZ |
82 | setDefaultCommand(new JoystickDrive()); |
83 | } | |
84 | ||
85 | public void printOutput() { | |
86 | System.out.println("PIDOutput: " + pidOutput); | |
87 | } | |
88 | ||
89 | private double getAvgEncoderDistance() { | |
90 | return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; | |
91 | } | |
92 | ||
93 | public boolean reachedTarget() { | |
94 | if (this.onTarget()) { | |
95 | this.disable(); | |
96 | return true; | |
97 | } else { | |
98 | return false; | |
99 | } | |
100 | } | |
101 | ||
102 | public void stop() { | |
103 | drive(0, 0); | |
d7bf2340 KZ |
104 | } |
105 | ||
106 | public void resetEncoders() { | |
107 | leftEncoder.reset(); | |
108 | rightEncoder.reset(); | |
109 | } | |
110 | ||
d7bf2340 | 111 | public double getRightSpeed() { |
6833a887 | 112 | return rightEncoder.getRate(); // in inches per second |
d7bf2340 KZ |
113 | } |
114 | ||
115 | public double getLeftSpeed() { | |
6833a887 | 116 | return leftEncoder.getRate(); // in inches per second |
d7bf2340 KZ |
117 | } |
118 | ||
119 | public double getSpeed() { | |
6833a887 | 120 | return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second |
d7bf2340 KZ |
121 | } |
122 | ||
d7bf2340 | 123 | public double getRightDistance() { |
6833a887 | 124 | return rightEncoder.getDistance(); // in inches |
d7bf2340 KZ |
125 | } |
126 | ||
d7bf2340 | 127 | public double getLeftDistance() { |
6833a887 | 128 | return leftEncoder.getDistance(); // in inches |
d7bf2340 KZ |
129 | } |
130 | ||
33141cdd KZ |
131 | public double getError() { |
132 | if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) | |
133 | return Math.abs(this.getSetpoint() - getAvgEncoderDistance()); | |
134 | else | |
135 | return Math.abs(this.getSetpoint() + getGyroAngle()); | |
d7bf2340 KZ |
136 | } |
137 | ||
33141cdd KZ |
138 | public double getGyroAngle() { |
139 | return gyro.getRotationZ().getAngle(); | |
140 | } | |
141 | ||
142 | public void resetGyro() { | |
143 | gyro.reset(); | |
144 | } | |
145 | ||
146 | public void printEncoder(int i, int n) { | |
147 | if (i % n == 0) { | |
148 | System.out.println("Left: " + this.getLeftDistance()); | |
149 | System.out.println("Right: " + this.getRightDistance()); | |
150 | ||
151 | } | |
152 | } | |
153 | ||
154 | public void printGyroOutput() { | |
155 | System.out.println("Gyro Angle" + -this.getGyroAngle()); | |
156 | } | |
157 | ||
158 | public double getOutput() { | |
159 | return pidOutput; | |
160 | } | |
161 | ||
162 | public void updatePID() { | |
163 | if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) | |
164 | this.getPIDController().setPID(kp, ki, kd); | |
165 | else | |
166 | this.getPIDController().setPID(gp, gd, gi); | |
167 | } | |
168 | ||
169 | public CANTalon getFrontLeft() { | |
170 | return frontLeft; | |
171 | } | |
172 | ||
173 | public CANTalon getFrontRight() { | |
174 | return frontRight; | |
175 | } | |
176 | ||
177 | public CANTalon getRearLeft() { | |
178 | return rearLeft; | |
179 | } | |
180 | ||
181 | public CANTalon getRearRight() { | |
182 | return rearRight; | |
183 | } | |
184 | ||
185 | public int getMode() { | |
186 | return DRIVE_MODE; | |
187 | } | |
188 | ||
189 | @Override | |
190 | protected void usePIDOutput(double output) { | |
191 | double left = 0; | |
192 | double right = 0; | |
193 | if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) { | |
194 | double drift = this.getLeftDistance() - this.getRightDistance(); | |
195 | if (Math.abs(output) > 0 && Math.abs(output) < 0.3) | |
196 | output = Math.signum(output) * 0.3; | |
197 | left = output; | |
198 | right = output + drift * kp / 10; | |
199 | } | |
200 | else if (DRIVE_MODE == Constants.DriveTrain.GYRO_MODE) { | |
201 | left = output; | |
202 | right = -output; | |
203 | } | |
204 | drive(left, right); | |
205 | pidOutput = output; | |
d7bf2340 KZ |
206 | } |
207 | ||
33141cdd KZ |
208 | @Override |
209 | protected double returnPIDInput() { | |
210 | return sensorFeedback(); | |
d7bf2340 | 211 | } |
33141cdd KZ |
212 | |
213 | private double sensorFeedback() { | |
214 | if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) | |
215 | return getAvgEncoderDistance(); | |
216 | else if (DRIVE_MODE == Constants.DriveTrain.GYRO_MODE) | |
217 | return -this.getGyroAngle(); | |
218 | // counterclockwise is positive on joystick but we want it to be negative | |
219 | else | |
220 | return 0; | |
221 | } | |
222 | ||
223 | public void drive(double left, double right) { | |
33141cdd | 224 | // dunno why but inverted drive (- values is forward) |
54c588d9 CZ |
225 | if (!Constants.DriveTrain.inverted) |
226 | robotDrive.tankDrive(-left, | |
227 | -right); | |
228 | else | |
229 | robotDrive.tankDrive(right, left); | |
33141cdd KZ |
230 | } |
231 | ||
232 | public void driveDistance(double dist, double maxTimeOut) { | |
233 | dist = MathLib.constrain(dist, -100, 100); | |
234 | setEncoderPID(); | |
235 | setSetpoint(dist); | |
236 | } | |
237 | ||
238 | public void setEncoderPID() { | |
239 | DRIVE_MODE = Constants.DriveTrain.ENCODER_MODE; | |
240 | this.updatePID(); | |
241 | this.setAbsoluteTolerance(encoderTolerance); | |
242 | this.setOutputRange(-1.0, 1.0); | |
243 | this.setInputRange(-200.0, 200.0); | |
244 | this.enable(); | |
245 | } | |
246 | ||
247 | private void setGyroPID() { | |
248 | DRIVE_MODE = Constants.DriveTrain.GYRO_MODE; | |
249 | this.updatePID(); | |
250 | this.getPIDController().setPID(gp, gi, gd); | |
251 | ||
252 | this.setAbsoluteTolerance(gyroTolerance); | |
253 | this.setOutputRange(-1.0, 1.0); | |
254 | this.setInputRange(-360.0, 360.0); | |
255 | this.enable(); | |
256 | } | |
257 | ||
258 | public void turnAngle(double angle) { | |
259 | angle = MathLib.constrain(angle, -360, 360); | |
260 | setGyroPID(); | |
261 | setSetpoint(angle); | |
262 | } | |
263 | ||
264 | public void setMotorSpeeds(double left, double right) { | |
265 | // positive setpoint to left side makes it go backwards | |
266 | // positive setpoint to right side makes it go forwards. | |
267 | frontLeft.set(-left); | |
268 | rearLeft.set(-left); | |
269 | frontRight.set(right); | |
270 | rearRight.set(right); | |
271 | } | |
272 | ||
2a099bc6 KZ |
273 | public Value getLeftGearPistonValue() { |
274 | return leftGearPiston.get(); | |
275 | } | |
276 | ||
277 | public Value getRightGearPistonValue() { | |
278 | return rightGearPiston.get(); | |
279 | } | |
280 | ||
281 | public void setHighGear() { | |
282 | changeGear(Constants.DriveTrain.HIGH_GEAR); | |
283 | } | |
284 | ||
285 | public void setLowGear() { | |
286 | changeGear(Constants.DriveTrain.LOW_GEAR); | |
287 | } | |
288 | ||
289 | public void changeGear(DoubleSolenoid.Value gear) { | |
290 | leftGearPiston.set(gear); | |
291 | rightGearPiston.set(gear); | |
292 | } | |
38a404b3 | 293 | } |