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 private static double kp
= 0.013, ki
= 0.000015, kd
= -0.002;
19 private static double gp
= 0.018, gi
= 0.000015, gd
= 0;
20 private static double pidOutput
= 0;
21 private static double encoderTolerance
= 8.0, gyroTolerance
= 5.0;
22 private int DRIVE_MODE
= 1;
24 private static final int MANUAL_MODE
= 1, ENCODER_MODE
= 2, GYRO_MODE
= 3;
26 private Encoder leftEncoder
, rightEncoder
;
27 private CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
28 private RobotDrive robotDrive
;
31 private DoubleSolenoid leftGearPiston
, rightGearPiston
;
32 // Drivetrain specific constants that relate to the inches per pulse value for
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
;
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
)
45 / (WHEEL_SPROCKET_DIAMETER
) * WHEEL_DIAMETER
;
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
);
55 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
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
);
60 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
61 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
63 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
64 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
66 gyro
= new GyroLib(I2C
.Port
.kOnboard
, false);
68 DRIVE_MODE
= Constants
.DriveTrain
.ENCODER_MODE
;
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
);
80 protected void initDefaultCommand() {
81 setDefaultCommand(new JoystickDrive());
84 public void printOutput() {
85 System
.out
.println("PIDOutput: " + pidOutput
);
88 private double getAvgEncoderDistance() {
89 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
92 public boolean reachedTarget() {
93 if (this.onTarget()) {
105 public void resetEncoders() {
107 rightEncoder
.reset();
110 public double getRightSpeed() {
111 return rightEncoder
.getRate(); // in inches per second
114 public double getLeftSpeed() {
115 return leftEncoder
.getRate(); // in inches per second
118 public double getSpeed() {
119 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
122 public double getRightDistance() {
123 return rightEncoder
.getDistance(); // in inches
126 public double getLeftDistance() {
127 return leftEncoder
.getDistance(); // in inches
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());
157 public double getOutput() {
161 public void updatePID() {
162 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
163 this.getPIDController().setPID(kp
, ki
, kd
);
165 this.getPIDController().setPID(gp
, gd
, gi
);
168 public CANTalon
getFrontLeft() {
172 public CANTalon
getFrontRight() {
176 public CANTalon
getRearLeft() {
180 public CANTalon
getRearRight() {
184 public int getMode() {
189 protected void usePIDOutput(double output
) {
192 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
) {
193 double drift
= this.getLeftDistance() - this.getRightDistance();
194 if (Math
.abs(output
) > 0 && Math
.abs(output
) < 0.3)
195 output
= Math
.signum(output
) * 0.3;
197 right
= output
+ drift
* kp
/ 10;
199 else if (DRIVE_MODE
== Constants
.DriveTrain
.GYRO_MODE
) {
208 protected double returnPIDInput() {
209 return sensorFeedback();
212 private double sensorFeedback() {
213 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
214 return getAvgEncoderDistance();
215 else if (DRIVE_MODE
== Constants
.DriveTrain
.GYRO_MODE
)
216 return -this.getGyroAngle();
217 // counterclockwise is positive on joystick but we want it to be negative
222 public void drive(double left
, double right
) {
223 robotDrive
.tankDrive(-left
, -right
);
224 // dunno why but inverted drive (- values is forward)
227 public void driveDistance(double dist
, double maxTimeOut
) {
228 dist
= MathLib
.constrain(dist
, -100, 100);
233 public void setEncoderPID() {
234 DRIVE_MODE
= Constants
.DriveTrain
.ENCODER_MODE
;
236 this.setAbsoluteTolerance(encoderTolerance
);
237 this.setOutputRange(-1.0, 1.0);
238 this.setInputRange(-200.0, 200.0);
242 private void setGyroPID() {
243 DRIVE_MODE
= Constants
.DriveTrain
.GYRO_MODE
;
245 this.getPIDController().setPID(gp
, gi
, gd
);
247 this.setAbsoluteTolerance(gyroTolerance
);
248 this.setOutputRange(-1.0, 1.0);
249 this.setInputRange(-360.0, 360.0);
253 public void turnAngle(double angle
) {
254 angle
= MathLib
.constrain(angle
, -360, 360);
259 public void setMotorSpeeds(double left
, double right
) {
260 // positive setpoint to left side makes it go backwards
261 // positive setpoint to right side makes it go forwards.
262 frontLeft
.set(-left
);
264 frontRight
.set(right
);
265 rearRight
.set(right
);
268 public Value
getLeftGearPistonValue() {
269 return leftGearPiston
.get();
272 public Value
getRightGearPistonValue() {
273 return rightGearPiston
.get();
276 public void setHighGear() {
277 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
280 public void setLowGear() {
281 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
284 public void changeGear(DoubleSolenoid
.Value gear
) {
285 leftGearPiston
.set(gear
);
286 rightGearPiston
.set(gear
);