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
.Encoder
;
12 import edu
.wpi
.first
.wpilibj
.I2C
;
13 import edu
.wpi
.first
.wpilibj
.RobotDrive
;
14 import edu
.wpi
.first
.wpilibj
.command
.PIDSubsystem
;
16 public class DriveTrain
extends PIDSubsystem
{
17 private static double pidOutput
= 0;
18 private static double encoderTolerance
= 8.0, gyroTolerance
= 5.0;
19 private int DRIVE_MODE
= 1;
21 private static final int MANUAL_MODE
= 1, ENCODER_MODE
= 2, GYRO_MODE
= 3;
23 private Encoder leftEncoder
, rightEncoder
;
24 private CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
25 private RobotDrive robotDrive
;
28 private DoubleSolenoid leftGearPiston
, rightGearPiston
;
29 // Drivetrain specific constants that relate to the inches per pulse value for
31 private final static double WHEEL_DIAMETER
= 6.0; // in inches
32 private final static double PULSES_PER_ROTATION
= 256; // in pulses
33 private final static double OUTPUT_SPROCKET_DIAMETER
= 2.0; // in inches
34 private final static double WHEEL_SPROCKET_DIAMETER
= 3.5; // in inches
35 public final static double INCHES_PER_PULSE
= (((Math
.PI
)
36 * OUTPUT_SPROCKET_DIAMETER
/ PULSES_PER_ROTATION
)
37 / WHEEL_SPROCKET_DIAMETER
) * WHEEL_DIAMETER
;
39 // Drivetrain specific constants that relate to the PID controllers
40 private final static double Kp
= 1.0, Ki
= 0.0,
41 Kd
= 0.0 * (OUTPUT_SPROCKET_DIAMETER
/ PULSES_PER_ROTATION
)
42 / (WHEEL_SPROCKET_DIAMETER
) * WHEEL_DIAMETER
;
47 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
48 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
49 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
50 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
52 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
53 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
54 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, EncodingType
.k4X
);
55 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
56 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, EncodingType
.k4X
);
57 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
58 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
60 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
61 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
63 gyro
= new GyroLib(I2C
.Port
.kOnboard
, false);
65 DRIVE_MODE
= Constants
.DriveTrain
.ENCODER_MODE
;
70 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.LEFT_FORWARD
,
71 +Constants
.DriveTrain
.LEFT_REVERSE
);
72 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.RIGHT_FORWARD
,
73 Constants
.DriveTrain
.RIGHT_REVERSE
);
77 protected void initDefaultCommand() {
78 setDefaultCommand(new JoystickDrive());
81 public void printOutput() {
82 System
.out
.println("PIDOutput: " + pidOutput
);
85 private double getAvgEncoderDistance() {
86 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
89 public boolean reachedTarget() {
90 if (this.onTarget()) {
102 public void resetEncoders() {
104 rightEncoder
.reset();
107 public double getRightSpeed() {
108 return rightEncoder
.getRate(); // in inches per second
111 public double getLeftSpeed() {
112 return leftEncoder
.getRate(); // in inches per second
115 public double getSpeed() {
116 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
119 public double getRightDistance() {
120 return rightEncoder
.getDistance(); // in inches
123 public double getLeftDistance() {
124 return leftEncoder
.getDistance(); // in inches
127 public double getError() {
128 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
129 return Math
.abs(this.getSetpoint() - getAvgEncoderDistance());
131 return Math
.abs(this.getSetpoint() + getGyroAngle());
134 public double getGyroAngle() {
135 return gyro
.getRotationZ().getAngle();
138 public void resetGyro() {
142 public void printEncoder(int i
, int n
) {
144 System
.out
.println("Left: " + this.getLeftDistance());
145 System
.out
.println("Right: " + this.getRightDistance());
150 public void printGyroOutput() {
151 System
.out
.println("Gyro Angle" + -this.getGyroAngle());
154 public double getOutput() {
158 public void updatePID() {
159 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
160 this.getPIDController().setPID(kp
, ki
, kd
);
162 this.getPIDController().setPID(gp
, gd
, gi
);
165 public CANTalon
getFrontLeft() {
169 public CANTalon
getFrontRight() {
173 public CANTalon
getRearLeft() {
177 public CANTalon
getRearRight() {
181 public int getMode() {
186 protected void usePIDOutput(double output
) {
189 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
) {
190 double drift
= this.getLeftDistance() - this.getRightDistance();
191 if (Math
.abs(output
) > 0 && Math
.abs(output
) < 0.3)
192 output
= Math
.signum(output
) * 0.3;
194 right
= output
+ drift
* kp
/ 10;
196 else if (DRIVE_MODE
== Constants
.DriveTrain
.GYRO_MODE
) {
205 protected double returnPIDInput() {
206 return sensorFeedback();
209 private double sensorFeedback() {
210 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
211 return getAvgEncoderDistance();
212 else if (DRIVE_MODE
== Constants
.DriveTrain
.GYRO_MODE
)
213 return -this.getGyroAngle();
214 // counterclockwise is positive on joystick but we want it to be negative
219 public void drive(double left
, double right
) {
220 robotDrive
.tankDrive(-left
, -right
);
221 // dunno why but inverted drive (- values is forward)
224 public void driveDistance(double dist
, double maxTimeOut
) {
225 dist
= MathLib
.constrain(dist
, -100, 100);
230 public void setEncoderPID() {
231 DRIVE_MODE
= Constants
.DriveTrain
.ENCODER_MODE
;
233 this.setAbsoluteTolerance(encoderTolerance
);
234 this.setOutputRange(-1.0, 1.0);
235 this.setInputRange(-200.0, 200.0);
239 private void setGyroPID() {
240 DRIVE_MODE
= Constants
.DriveTrain
.GYRO_MODE
;
242 this.getPIDController().setPID(gp
, gi
, gd
);
244 this.setAbsoluteTolerance(gyroTolerance
);
245 this.setOutputRange(-1.0, 1.0);
246 this.setInputRange(-360.0, 360.0);
250 public void turnAngle(double angle
) {
251 angle
= MathLib
.constrain(angle
, -360, 360);
256 public void setMotorSpeeds(double left
, double right
) {
257 // positive setpoint to left side makes it go backwards
258 // positive setpoint to right side makes it go forwards.
259 frontLeft
.set(-left
);
261 frontRight
.set(right
);
262 rearRight
.set(right
);
265 private static double kp
= 0.013, ki
= 0.000015, kd
= -0.002;
266 private static double gp
= 0.018, gi
= 0.000015, gd
= 0;