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.MathLib; |
5 | import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive; | |
7670b3f4 KZ |
6 | import org.usfirst.frc.team3501.robot.sensors.GyroLib; |
7 | import org.usfirst.frc.team3501.robot.sensors.Lidar; | |
111dc444 | 8 | |
38a404b3 | 9 | import edu.wpi.first.wpilibj.CANTalon; |
111dc444 | 10 | import edu.wpi.first.wpilibj.CounterBase.EncodingType; |
d9c04720 | 11 | import edu.wpi.first.wpilibj.DoubleSolenoid; |
2aea5cc2 | 12 | import edu.wpi.first.wpilibj.DoubleSolenoid.Value; |
111dc444 | 13 | import edu.wpi.first.wpilibj.Encoder; |
b54ad73b | 14 | import edu.wpi.first.wpilibj.I2C; |
33141cdd KZ |
15 | import edu.wpi.first.wpilibj.RobotDrive; |
16 | import edu.wpi.first.wpilibj.command.PIDSubsystem; | |
17 | ||
18 | public class DriveTrain extends PIDSubsystem { | |
7a4df3c5 | 19 | // Current Drive Mode Default Drive Mode is Manual |
33141cdd | 20 | private int DRIVE_MODE = 1; |
fb75626b | 21 | private static double pidOutput = 0; |
38a404b3 | 22 | |
1884c3cf | 23 | private Encoder leftEncoder, rightEncoder; |
96215d97 | 24 | |
7e360ef5 | 25 | public static Lidar lidar; |
96215d97 | 26 | |
d7bf2340 | 27 | private CANTalon frontLeft, frontRight, rearLeft, rearRight; |
33141cdd KZ |
28 | private RobotDrive robotDrive; |
29 | ||
30 | private GyroLib gyro; | |
b3bdd589 | 31 | public DoubleSolenoid leftGearPiston, rightGearPiston; |
fb75626b | 32 | |
9e65c056 ME |
33 | // Drivetrain specific constants that relate to the inches per pulse value for |
34 | // the encoders | |
71d73690 | 35 | |
d7bf2340 | 36 | public DriveTrain() { |
fb75626b KZ |
37 | super(Constants.DriveTrain.kp, Constants.DriveTrain.ki, |
38 | Constants.DriveTrain.kd); | |
33141cdd | 39 | |
d7bf2340 KZ |
40 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); |
41 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); | |
42 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); | |
43 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); | |
1884c3cf | 44 | |
33141cdd | 45 | robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight); |
96215d97 | 46 | |
8bc0fed2 | 47 | lidar = new Lidar(I2C.Port.kMXP); |
d7bf2340 KZ |
48 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, |
49 | Constants.DriveTrain.ENCODER_LEFT_B, false, EncodingType.k4X); | |
50 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, | |
51 | Constants.DriveTrain.ENCODER_RIGHT_B, false, EncodingType.k4X); | |
45bdf5b9 KZ |
52 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); |
53 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
33141cdd KZ |
54 | |
55 | leftEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
56 | rightEncoder.setDistancePerPulse(Constants.DriveTrain.INCHES_PER_PULSE); | |
57 | ||
58 | gyro = new GyroLib(I2C.Port.kOnboard, false); | |
59 | ||
60 | DRIVE_MODE = Constants.DriveTrain.ENCODER_MODE; | |
61 | setEncoderPID(); | |
62 | this.disable(); | |
63 | gyro.start(); | |
d004deee | 64 | |
faabd6f8 | 65 | leftGearPiston = new DoubleSolenoid(10, Constants.DriveTrain.LEFT_FORWARD, |
7a4df3c5 | 66 | Constants.DriveTrain.LEFT_REVERSE); |
faabd6f8 KZ |
67 | rightGearPiston = new DoubleSolenoid(10, |
68 | Constants.DriveTrain.RIGHT_FORWARD, | |
d9c04720 | 69 | Constants.DriveTrain.RIGHT_REVERSE); |
96215d97 | 70 | Constants.DriveTrain.inverted = false; |
d7bf2340 KZ |
71 | } |
72 | ||
73 | @Override | |
74 | protected void initDefaultCommand() { | |
33141cdd KZ |
75 | setDefaultCommand(new JoystickDrive()); |
76 | } | |
77 | ||
7a4df3c5 | 78 | // Print tne PID Output |
33141cdd KZ |
79 | public void printOutput() { |
80 | System.out.println("PIDOutput: " + pidOutput); | |
81 | } | |
82 | ||
83 | private double getAvgEncoderDistance() { | |
84 | return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; | |
85 | } | |
86 | ||
7a4df3c5 KZ |
87 | // Whether or not the PID Controller thinks we have reached the target |
88 | // setpoint | |
33141cdd KZ |
89 | public boolean reachedTarget() { |
90 | if (this.onTarget()) { | |
91 | this.disable(); | |
92 | return true; | |
93 | } else { | |
94 | return false; | |
95 | } | |
96 | } | |
97 | ||
98 | public void stop() { | |
99 | drive(0, 0); | |
d7bf2340 KZ |
100 | } |
101 | ||
102 | public void resetEncoders() { | |
103 | leftEncoder.reset(); | |
104 | rightEncoder.reset(); | |
105 | } | |
106 | ||
7e360ef5 LM |
107 | public double getLidarDistance() { |
108 | return lidar.pidGet(); | |
96215d97 ME |
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 | ||
7a4df3c5 KZ |
131 | // Get error between the setpoint of PID Controller and the current state of |
132 | // the robot | |
33141cdd KZ |
133 | public double getError() { |
134 | if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) | |
135 | return Math.abs(this.getSetpoint() - getAvgEncoderDistance()); | |
136 | else | |
137 | return Math.abs(this.getSetpoint() + getGyroAngle()); | |
d7bf2340 KZ |
138 | } |
139 | ||
33141cdd KZ |
140 | public double getGyroAngle() { |
141 | return gyro.getRotationZ().getAngle(); | |
142 | } | |
143 | ||
144 | public void resetGyro() { | |
145 | gyro.reset(); | |
146 | } | |
147 | ||
148 | public void printEncoder(int i, int n) { | |
149 | if (i % n == 0) { | |
150 | System.out.println("Left: " + this.getLeftDistance()); | |
151 | System.out.println("Right: " + this.getRightDistance()); | |
152 | ||
153 | } | |
154 | } | |
155 | ||
156 | public void printGyroOutput() { | |
157 | System.out.println("Gyro Angle" + -this.getGyroAngle()); | |
158 | } | |
159 | ||
7a4df3c5 KZ |
160 | /* |
161 | * returns the PID output that is returned by the PID Controller | |
162 | */ | |
33141cdd KZ |
163 | public double getOutput() { |
164 | return pidOutput; | |
165 | } | |
166 | ||
7a4df3c5 | 167 | // Updates the PID constants based on which control mode is being used |
33141cdd KZ |
168 | public void updatePID() { |
169 | if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) | |
fb75626b KZ |
170 | this.getPIDController().setPID(Constants.DriveTrain.kp, |
171 | Constants.DriveTrain.ki, Constants.DriveTrain.kd); | |
33141cdd | 172 | else |
fb75626b KZ |
173 | this.getPIDController().setPID(Constants.DriveTrain.gp, |
174 | Constants.DriveTrain.gd, Constants.DriveTrain.gi); | |
33141cdd KZ |
175 | } |
176 | ||
177 | public CANTalon getFrontLeft() { | |
178 | return frontLeft; | |
179 | } | |
180 | ||
181 | public CANTalon getFrontRight() { | |
182 | return frontRight; | |
183 | } | |
184 | ||
185 | public CANTalon getRearLeft() { | |
186 | return rearLeft; | |
187 | } | |
188 | ||
189 | public CANTalon getRearRight() { | |
190 | return rearRight; | |
191 | } | |
192 | ||
193 | public int getMode() { | |
194 | return DRIVE_MODE; | |
195 | } | |
196 | ||
7a4df3c5 KZ |
197 | /* |
198 | * Method is a required method that the PID Subsystem uses to return the | |
199 | * calculated PID value to the driver | |
b3bdd589 | 200 | * |
7a4df3c5 KZ |
201 | * @param Gives the user the output from the PID algorithm that is calculated |
202 | * internally | |
b3bdd589 | 203 | * |
7a4df3c5 KZ |
204 | * Body: Uses the output, does some filtering and drives the robot |
205 | */ | |
33141cdd KZ |
206 | @Override |
207 | protected void usePIDOutput(double output) { | |
208 | double left = 0; | |
209 | double right = 0; | |
210 | if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) { | |
211 | double drift = this.getLeftDistance() - this.getRightDistance(); | |
212 | if (Math.abs(output) > 0 && Math.abs(output) < 0.3) | |
213 | output = Math.signum(output) * 0.3; | |
214 | left = output; | |
fb75626b | 215 | right = output + drift * Constants.DriveTrain.kp / 10; |
6c72e0a8 | 216 | drive(left, right); |
96215d97 | 217 | } else if (DRIVE_MODE == Constants.DriveTrain.GYRO_MODE) { |
33141cdd KZ |
218 | left = output; |
219 | right = -output; | |
6c72e0a8 | 220 | arcadeDrive(0, output); |
33141cdd | 221 | } |
33141cdd | 222 | pidOutput = output; |
d7bf2340 KZ |
223 | } |
224 | ||
33141cdd KZ |
225 | @Override |
226 | protected double returnPIDInput() { | |
227 | return sensorFeedback(); | |
d7bf2340 | 228 | } |
33141cdd | 229 | |
7a4df3c5 | 230 | /* |
c688e9da | 231 | * Checks the drive mode |
b3bdd589 | 232 | * |
7e360ef5 LM |
233 | * @return the current state of the robot in each state Average distance from |
234 | * both sides of tank drive for Encoder Mode Angle from the gyro in GYRO_MODE | |
7a4df3c5 | 235 | */ |
33141cdd KZ |
236 | private double sensorFeedback() { |
237 | if (DRIVE_MODE == Constants.DriveTrain.ENCODER_MODE) | |
238 | return getAvgEncoderDistance(); | |
239 | else if (DRIVE_MODE == Constants.DriveTrain.GYRO_MODE) | |
240 | return -this.getGyroAngle(); | |
241 | // counterclockwise is positive on joystick but we want it to be negative | |
242 | else | |
243 | return 0; | |
244 | } | |
245 | ||
7a4df3c5 KZ |
246 | /* |
247 | * @param left and right setpoints to set to the left and right side of tank | |
248 | * inverted is for Logan, wants the robot to invert all controls left = right | |
7e360ef5 LM |
249 | * and right = left negative input is required for the regular rotation |
250 | * because RobotDrive tankdrive method drives inverted | |
7a4df3c5 | 251 | */ |
33141cdd | 252 | public void drive(double left, double right) { |
9e65c056 | 253 | robotDrive.tankDrive(-left, -right); |
33141cdd | 254 | // dunno why but inverted drive (- values is forward) |
96215d97 | 255 | if (!Constants.DriveTrain.inverted) |
7e360ef5 | 256 | robotDrive.tankDrive(-left, -right); |
96215d97 ME |
257 | else |
258 | robotDrive.tankDrive(right, left); | |
33141cdd KZ |
259 | } |
260 | ||
6c72e0a8 KZ |
261 | public void arcadeDrive(double y, double twist) { |
262 | robotDrive.arcadeDrive(y, twist); | |
263 | } | |
264 | ||
7a4df3c5 KZ |
265 | /* |
266 | * constrains the distance to within -100 and 100 since we aren't going to | |
267 | * drive more than 100 inches | |
b3bdd589 | 268 | * |
7a4df3c5 | 269 | * Configure Encoder PID |
b3bdd589 | 270 | * |
7a4df3c5 KZ |
271 | * Sets the setpoint to the PID subsystem |
272 | */ | |
33141cdd KZ |
273 | public void driveDistance(double dist, double maxTimeOut) { |
274 | dist = MathLib.constrain(dist, -100, 100); | |
275 | setEncoderPID(); | |
276 | setSetpoint(dist); | |
277 | } | |
278 | ||
7a4df3c5 | 279 | /* |
7e360ef5 LM |
280 | * Sets the encoder mode Updates the PID constants sets the tolerance and sets |
281 | * output/input ranges Enables the PID controllers | |
7a4df3c5 | 282 | */ |
33141cdd KZ |
283 | public void setEncoderPID() { |
284 | DRIVE_MODE = Constants.DriveTrain.ENCODER_MODE; | |
285 | this.updatePID(); | |
fb75626b | 286 | this.setAbsoluteTolerance(Constants.DriveTrain.encoderTolerance); |
33141cdd KZ |
287 | this.setOutputRange(-1.0, 1.0); |
288 | this.setInputRange(-200.0, 200.0); | |
289 | this.enable(); | |
290 | } | |
291 | ||
7a4df3c5 | 292 | /* |
7e360ef5 LM |
293 | * Sets the Gyro Mode Updates the PID constants, sets the tolerance and sets |
294 | * output/input ranges Enables the PID controllers | |
7a4df3c5 | 295 | */ |
33141cdd KZ |
296 | private void setGyroPID() { |
297 | DRIVE_MODE = Constants.DriveTrain.GYRO_MODE; | |
298 | this.updatePID(); | |
fb75626b KZ |
299 | this.getPIDController().setPID(Constants.DriveTrain.gp, |
300 | Constants.DriveTrain.gi, Constants.DriveTrain.gd); | |
33141cdd | 301 | |
fb75626b | 302 | this.setAbsoluteTolerance(Constants.DriveTrain.gyroTolerance); |
33141cdd KZ |
303 | this.setOutputRange(-1.0, 1.0); |
304 | this.setInputRange(-360.0, 360.0); | |
305 | this.enable(); | |
306 | } | |
307 | ||
7a4df3c5 KZ |
308 | /* |
309 | * Turning method that should be used repeatedly in a command | |
b3bdd589 | 310 | * |
7a4df3c5 KZ |
311 | * First constrains the angle to within -360 and 360 since that is as much as |
312 | * we need to turn | |
b3bdd589 | 313 | * |
7a4df3c5 KZ |
314 | * Configures Gyro PID and sets the setpoint as an angle |
315 | */ | |
33141cdd KZ |
316 | public void turnAngle(double angle) { |
317 | angle = MathLib.constrain(angle, -360, 360); | |
318 | setGyroPID(); | |
319 | setSetpoint(angle); | |
320 | } | |
321 | ||
322 | public void setMotorSpeeds(double left, double right) { | |
323 | // positive setpoint to left side makes it go backwards | |
324 | // positive setpoint to right side makes it go forwards. | |
325 | frontLeft.set(-left); | |
326 | rearLeft.set(-left); | |
327 | frontRight.set(right); | |
328 | rearRight.set(right); | |
329 | } | |
330 | ||
7a4df3c5 | 331 | /* |
7e360ef5 LM |
332 | * @return a value that is the current setpoint for the piston kReverse or |
333 | * kForward | |
7a4df3c5 | 334 | */ |
2a099bc6 KZ |
335 | public Value getLeftGearPistonValue() { |
336 | return leftGearPiston.get(); | |
337 | } | |
338 | ||
7a4df3c5 | 339 | /* |
7e360ef5 LM |
340 | * @return a value that is the current setpoint for the piston kReverse or |
341 | * kForward | |
7a4df3c5 | 342 | */ |
2a099bc6 KZ |
343 | public Value getRightGearPistonValue() { |
344 | return rightGearPiston.get(); | |
345 | } | |
346 | ||
7a4df3c5 KZ |
347 | /* |
348 | * Changes the ball shift gear assembly to high | |
349 | */ | |
2a099bc6 KZ |
350 | public void setHighGear() { |
351 | changeGear(Constants.DriveTrain.HIGH_GEAR); | |
352 | } | |
353 | ||
7a4df3c5 KZ |
354 | /* |
355 | * Changes the ball shift gear assembly to low | |
356 | */ | |
2a099bc6 KZ |
357 | public void setLowGear() { |
358 | changeGear(Constants.DriveTrain.LOW_GEAR); | |
359 | } | |
360 | ||
7a4df3c5 KZ |
361 | /* |
362 | * changes the gear to a DoubleSolenoid.Value | |
363 | */ | |
2a099bc6 KZ |
364 | public void changeGear(DoubleSolenoid.Value gear) { |
365 | leftGearPiston.set(gear); | |
366 | rightGearPiston.set(gear); | |
367 | } | |
600a1a1c | 368 | |
38a404b3 | 369 | } |