1 package org
.usfirst
.frc
.team3501
.robot
.subsystems
;
3 import org
.usfirst
.frc
.team3501
.robot
.Constants
;
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
;
8 import edu
.wpi
.first
.wpilibj
.CANTalon
;
9 import edu
.wpi
.first
.wpilibj
.CounterBase
.EncodingType
;
10 import edu
.wpi
.first
.wpilibj
.DoubleSolenoid
;
11 import edu
.wpi
.first
.wpilibj
.DoubleSolenoid
.Value
;
12 import edu
.wpi
.first
.wpilibj
.Encoder
;
13 import edu
.wpi
.first
.wpilibj
.I2C
;
14 import edu
.wpi
.first
.wpilibj
.RobotDrive
;
15 import edu
.wpi
.first
.wpilibj
.command
.PIDSubsystem
;
17 public class DriveTrain
extends PIDSubsystem
{
18 // Encoder PID Proportional Constants P, I, and D
19 private static double EP
= 0.013, EI
= 0.000015, ED
= -0.002;
21 // Gyro PID Constants P, I, and D
22 private static double GP
= 0.018, GI
= 0.000015, GD
= 0;
23 private static double pidOutput
= 0;
25 // PID Controller tolerances for the error
26 private static double encoderTolerance
= 8.0, gyroTolerance
= 5.0;
28 // Current Drive Mode Default Drive Mode is Manual
29 private int DRIVE_MODE
= 1;
31 // Different Drive Modes
32 private static final int MANUAL_MODE
= 1, ENCODER_MODE
= 2, GYRO_MODE
= 3;
34 private Encoder leftEncoder
, rightEncoder
;
35 private CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
36 private RobotDrive robotDrive
;
39 private DoubleSolenoid leftGearPiston
, rightGearPiston
;
44 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
45 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
46 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
47 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
49 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
50 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
51 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, EncodingType
.k4X
);
52 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
53 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, EncodingType
.k4X
);
54 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
55 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
57 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
58 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
60 gyro
= new GyroLib(I2C
.Port
.kOnboard
, false);
62 DRIVE_MODE
= Constants
.DriveTrain
.ENCODER_MODE
;
67 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.LEFT_FORWARD
,
68 Constants
.DriveTrain
.LEFT_REVERSE
);
69 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.RIGHT_FORWARD
,
70 Constants
.DriveTrain
.RIGHT_REVERSE
);
71 Constants
.DriveTrain
.inverted
= false;
75 protected void initDefaultCommand() {
76 setDefaultCommand(new JoystickDrive());
79 // Print tne PID Output
80 public void printOutput() {
81 System
.out
.println("PIDOutput: " + pidOutput
);
84 private double getAvgEncoderDistance() {
85 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
88 // Whether or not the PID Controller thinks we have reached the target
90 public boolean reachedTarget() {
91 if (this.onTarget()) {
103 public void resetEncoders() {
105 rightEncoder
.reset();
108 public double getRightSpeed() {
109 return rightEncoder
.getRate(); // in inches per second
112 public double getLeftSpeed() {
113 return leftEncoder
.getRate(); // in inches per second
116 public double getSpeed() {
117 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
120 public double getRightDistance() {
121 return rightEncoder
.getDistance(); // in inches
124 public double getLeftDistance() {
125 return leftEncoder
.getDistance(); // in inches
128 // Get error between the setpoint of PID Controller and the current state of
130 public double getError() {
131 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
132 return Math
.abs(this.getSetpoint() - getAvgEncoderDistance());
134 return Math
.abs(this.getSetpoint() + getGyroAngle());
137 public double getGyroAngle() {
138 return gyro
.getRotationZ().getAngle();
141 public void resetGyro() {
145 public void printEncoder(int i
, int n
) {
147 System
.out
.println("Left: " + this.getLeftDistance());
148 System
.out
.println("Right: " + this.getRightDistance());
153 public void printGyroOutput() {
154 System
.out
.println("Gyro Angle" + -this.getGyroAngle());
158 * returns the PID output that is returned by the PID Controller
160 public double getOutput() {
164 // Updates the PID constants based on which control mode is being used
165 public void updatePID() {
166 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
167 this.getPIDController().setPID(EP
, EI
, ED
);
169 this.getPIDController().setPID(GP
, GD
, GI
);
172 public CANTalon
getFrontLeft() {
176 public CANTalon
getFrontRight() {
180 public CANTalon
getRearLeft() {
184 public CANTalon
getRearRight() {
188 public int getMode() {
193 * Method is a required method that the PID Subsystem uses to return the
194 * calculated PID value to the driver
196 * @param Gives the user the output from the PID algorithm that is calculated
199 * Body: Uses the output, does some filtering and drives the robot
202 protected void usePIDOutput(double output
) {
205 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
) {
206 double drift
= this.getLeftDistance() - this.getRightDistance();
207 if (Math
.abs(output
) > 0 && Math
.abs(output
) < 0.3)
208 output
= Math
.signum(output
) * 0.3;
210 right
= output
+ drift
* EP
/ 10;
212 else if (DRIVE_MODE
== Constants
.DriveTrain
.GYRO_MODE
) {
221 protected double returnPIDInput() {
222 return sensorFeedback();
226 * Checks the drive mode
228 * @return the current state of the robot in each state
229 * Average distance from both sides of tank drive for Encoder Mode
230 * Angle from the gyro in GYRO_MODE
232 private double sensorFeedback() {
233 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
234 return getAvgEncoderDistance();
235 else if (DRIVE_MODE
== Constants
.DriveTrain
.GYRO_MODE
)
236 return -this.getGyroAngle();
237 // counterclockwise is positive on joystick but we want it to be negative
243 * @param left and right setpoints to set to the left and right side of tank
244 * inverted is for Logan, wants the robot to invert all controls left = right
246 * negative input is required for the regular rotation because RobotDrive
247 * tankdrive method drives inverted
249 public void drive(double left
, double right
) {
250 // dunno why but inverted drive (- values is forward)
251 if (!Constants
.DriveTrain
.inverted
)
252 robotDrive
.tankDrive(-left
,
255 robotDrive
.tankDrive(right
, left
);
259 * constrains the distance to within -100 and 100 since we aren't going to
260 * drive more than 100 inches
262 * Configure Encoder PID
264 * Sets the setpoint to the PID subsystem
266 public void driveDistance(double dist
, double maxTimeOut
) {
267 dist
= MathLib
.constrain(dist
, -100, 100);
273 * Sets the encoder mode
274 * Updates the PID constants sets the tolerance and sets output/input ranges
275 * Enables the PID controllers
277 public void setEncoderPID() {
278 DRIVE_MODE
= Constants
.DriveTrain
.ENCODER_MODE
;
280 this.setAbsoluteTolerance(encoderTolerance
);
281 this.setOutputRange(-1.0, 1.0);
282 this.setInputRange(-200.0, 200.0);
288 * Updates the PID constants, sets the tolerance and sets output/input ranges
289 * Enables the PID controllers
291 private void setGyroPID() {
292 DRIVE_MODE
= Constants
.DriveTrain
.GYRO_MODE
;
294 this.getPIDController().setPID(GP
, GI
, GD
);
296 this.setAbsoluteTolerance(gyroTolerance
);
297 this.setOutputRange(-1.0, 1.0);
298 this.setInputRange(-360.0, 360.0);
303 * Turning method that should be used repeatedly in a command
305 * First constrains the angle to within -360 and 360 since that is as much as
308 * Configures Gyro PID and sets the setpoint as an angle
310 public void turnAngle(double angle
) {
311 angle
= MathLib
.constrain(angle
, -360, 360);
316 public void setMotorSpeeds(double left
, double right
) {
317 // positive setpoint to left side makes it go backwards
318 // positive setpoint to right side makes it go forwards.
319 frontLeft
.set(-left
);
321 frontRight
.set(right
);
322 rearRight
.set(right
);
326 * @return a value that is the current setpoint for the piston
327 * kReverse or kForward
329 public Value
getLeftGearPistonValue() {
330 return leftGearPiston
.get();
334 * @return a value that is the current setpoint for the piston
335 * kReverse or kForward
337 public Value
getRightGearPistonValue() {
338 return rightGearPiston
.get();
342 * Changes the ball shift gear assembly to high
344 public void setHighGear() {
345 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
349 * Changes the ball shift gear assembly to low
351 public void setLowGear() {
352 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
356 * changes the gear to a DoubleSolenoid.Value
358 public void changeGear(DoubleSolenoid
.Value gear
) {
359 leftGearPiston
.set(gear
);
360 rightGearPiston
.set(gear
);