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
.Lidar
;
6 import org
.usfirst
.frc
.team3501
.robot
.MathLib
;
7 import org
.usfirst
.frc
.team3501
.robot
.commands
.driving
.JoystickDrive
;
9 import edu
.wpi
.first
.wpilibj
.CANTalon
;
10 import edu
.wpi
.first
.wpilibj
.CounterBase
.EncodingType
;
11 import edu
.wpi
.first
.wpilibj
.DoubleSolenoid
;
12 import edu
.wpi
.first
.wpilibj
.DoubleSolenoid
.Value
;
13 import edu
.wpi
.first
.wpilibj
.Encoder
;
14 import edu
.wpi
.first
.wpilibj
.I2C
;
15 import edu
.wpi
.first
.wpilibj
.RobotDrive
;
16 import edu
.wpi
.first
.wpilibj
.command
.PIDSubsystem
;
18 public class DriveTrain
extends PIDSubsystem
{
19 private static double kp
= 0.013, ki
= 0.000015, kd
= -0.002;
20 private static double gp
= 0.018, gi
= 0.000015, gd
= 0;
21 private static double pidOutput
= 0;
23 // PID Controller tolerances for the error
24 private static double encoderTolerance
= 8.0, gyroTolerance
= 5.0;
26 // Current Drive Mode Default Drive Mode is Manual
27 private int DRIVE_MODE
= 1;
29 // Different Drive Modes
30 private static final int MANUAL_MODE
= 1, ENCODER_MODE
= 2, GYRO_MODE
= 3;
32 private Encoder leftEncoder
, rightEncoder
;
34 public static Lidar leftLidar
;
35 public static Lidar rightLidar
;
37 private CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
38 private RobotDrive robotDrive
;
41 private DoubleSolenoid leftGearPiston
, rightGearPiston
;
42 // Drivetrain specific constants that relate to the inches per pulse value for
44 private final static double WHEEL_DIAMETER
= 6.0; // in inches
45 private final static double PULSES_PER_ROTATION
= 256; // in pulses
46 private final static double OUTPUT_SPROCKET_DIAMETER
= 2.0; // in inches
47 private final static double WHEEL_SPROCKET_DIAMETER
= 3.5; // in inches
48 public final static double INCHES_PER_PULSE
= (((Math
.PI
)
49 * OUTPUT_SPROCKET_DIAMETER
/ PULSES_PER_ROTATION
)
50 / WHEEL_SPROCKET_DIAMETER
) * WHEEL_DIAMETER
;
52 // Drivetrain specific constants that relate to the PID controllers
53 private final static double Kp
= 1.0, Ki
= 0.0,
54 Kd
= 0.0 * (OUTPUT_SPROCKET_DIAMETER
/ PULSES_PER_ROTATION
)
55 / (WHEEL_SPROCKET_DIAMETER
) * WHEEL_DIAMETER
;
60 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
61 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
62 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
63 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
65 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
67 leftLidar
= new Lidar(I2C
.Port
.kOnboard
);
68 rightLidar
= new Lidar(I2C
.Port
.kOnboard
); // TODO: find port for second
70 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
71 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, EncodingType
.k4X
);
72 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
73 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, EncodingType
.k4X
);
74 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
75 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
77 leftEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
78 rightEncoder
.setDistancePerPulse(Constants
.DriveTrain
.INCHES_PER_PULSE
);
80 gyro
= new GyroLib(I2C
.Port
.kOnboard
, false);
82 DRIVE_MODE
= Constants
.DriveTrain
.ENCODER_MODE
;
87 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.LEFT_FORWARD
,
88 Constants
.DriveTrain
.LEFT_REVERSE
);
89 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.RIGHT_FORWARD
,
90 Constants
.DriveTrain
.RIGHT_REVERSE
);
91 Constants
.DriveTrain
.inverted
= false;
95 protected void initDefaultCommand() {
96 setDefaultCommand(new JoystickDrive());
99 // Print tne PID Output
100 public void printOutput() {
101 System
.out
.println("PIDOutput: " + pidOutput
);
104 private double getAvgEncoderDistance() {
105 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
108 // Whether or not the PID Controller thinks we have reached the target
110 public boolean reachedTarget() {
111 if (this.onTarget()) {
123 public void resetEncoders() {
125 rightEncoder
.reset();
128 public double getLeftLidarDistance() {
129 return leftLidar
.pidGet();
132 public double getsRightLidarDistance() {
133 return rightLidar
.pidGet();
136 public double getRightSpeed() {
137 return rightEncoder
.getRate(); // in inches per second
140 public double getLeftSpeed() {
141 return leftEncoder
.getRate(); // in inches per second
144 public double getSpeed() {
145 return (getLeftSpeed() + getRightSpeed()) / 2.0; // in inches per second
148 public double getRightDistance() {
149 return rightEncoder
.getDistance(); // in inches
152 public double getLeftDistance() {
153 return leftEncoder
.getDistance(); // in inches
156 // Get error between the setpoint of PID Controller and the current state of
158 public double getError() {
159 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
160 return Math
.abs(this.getSetpoint() - getAvgEncoderDistance());
162 return Math
.abs(this.getSetpoint() + getGyroAngle());
165 public double getGyroAngle() {
166 return gyro
.getRotationZ().getAngle();
169 public void resetGyro() {
173 public void printEncoder(int i
, int n
) {
175 System
.out
.println("Left: " + this.getLeftDistance());
176 System
.out
.println("Right: " + this.getRightDistance());
181 public void printGyroOutput() {
182 System
.out
.println("Gyro Angle" + -this.getGyroAngle());
186 * returns the PID output that is returned by the PID Controller
188 public double getOutput() {
192 // Updates the PID constants based on which control mode is being used
193 public void updatePID() {
194 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
195 this.getPIDController().setPID(kp
, ki
, kd
);
197 this.getPIDController().setPID(gp
, gd
, gi
);
200 public CANTalon
getFrontLeft() {
204 public CANTalon
getFrontRight() {
208 public CANTalon
getRearLeft() {
212 public CANTalon
getRearRight() {
216 public int getMode() {
221 * Method is a required method that the PID Subsystem uses to return the
222 * calculated PID value to the driver
224 * @param Gives the user the output from the PID algorithm that is calculated
227 * Body: Uses the output, does some filtering and drives the robot
230 protected void usePIDOutput(double output
) {
233 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
) {
234 double drift
= this.getLeftDistance() - this.getRightDistance();
235 if (Math
.abs(output
) > 0 && Math
.abs(output
) < 0.3)
236 output
= Math
.signum(output
) * 0.3;
238 right
= output
+ drift
* kp
/ 10;
239 } else if (DRIVE_MODE
== Constants
.DriveTrain
.GYRO_MODE
) {
248 protected double returnPIDInput() {
249 return sensorFeedback();
253 * Checks the drive mode
255 * @return the current state of the robot in each state
256 * Average distance from both sides of tank drive for Encoder Mode
257 * Angle from the gyro in GYRO_MODE
259 private double sensorFeedback() {
260 if (DRIVE_MODE
== Constants
.DriveTrain
.ENCODER_MODE
)
261 return getAvgEncoderDistance();
262 else if (DRIVE_MODE
== Constants
.DriveTrain
.GYRO_MODE
)
263 return -this.getGyroAngle();
264 // counterclockwise is positive on joystick but we want it to be negative
270 * @param left and right setpoints to set to the left and right side of tank
271 * inverted is for Logan, wants the robot to invert all controls left = right
273 * negative input is required for the regular rotation because RobotDrive
274 * tankdrive method drives inverted
276 public void drive(double left
, double right
) {
277 robotDrive
.tankDrive(-left
, -right
);
278 // dunno why but inverted drive (- values is forward)
279 if (!Constants
.DriveTrain
.inverted
)
280 robotDrive
.tankDrive(-left
,
283 robotDrive
.tankDrive(right
, left
);
287 * constrains the distance to within -100 and 100 since we aren't going to
288 * drive more than 100 inches
290 * Configure Encoder PID
292 * Sets the setpoint to the PID subsystem
294 public void driveDistance(double dist
, double maxTimeOut
) {
295 dist
= MathLib
.constrain(dist
, -100, 100);
301 * Sets the encoder mode
302 * Updates the PID constants sets the tolerance and sets output/input ranges
303 * Enables the PID controllers
305 public void setEncoderPID() {
306 DRIVE_MODE
= Constants
.DriveTrain
.ENCODER_MODE
;
308 this.setAbsoluteTolerance(encoderTolerance
);
309 this.setOutputRange(-1.0, 1.0);
310 this.setInputRange(-200.0, 200.0);
316 * Updates the PID constants, sets the tolerance and sets output/input ranges
317 * Enables the PID controllers
319 private void setGyroPID() {
320 DRIVE_MODE
= Constants
.DriveTrain
.GYRO_MODE
;
322 this.getPIDController().setPID(gp
, gi
, gd
);
324 this.setAbsoluteTolerance(gyroTolerance
);
325 this.setOutputRange(-1.0, 1.0);
326 this.setInputRange(-360.0, 360.0);
331 * Turning method that should be used repeatedly in a command
333 * First constrains the angle to within -360 and 360 since that is as much as
336 * Configures Gyro PID and sets the setpoint as an angle
338 public void turnAngle(double angle
) {
339 angle
= MathLib
.constrain(angle
, -360, 360);
344 public void setMotorSpeeds(double left
, double right
) {
345 // positive setpoint to left side makes it go backwards
346 // positive setpoint to right side makes it go forwards.
347 frontLeft
.set(-left
);
349 frontRight
.set(right
);
350 rearRight
.set(right
);
354 * @return a value that is the current setpoint for the piston
355 * kReverse or kForward
357 public Value
getLeftGearPistonValue() {
358 return leftGearPiston
.get();
362 * @return a value that is the current setpoint for the piston
363 * kReverse or kForward
365 public Value
getRightGearPistonValue() {
366 return rightGearPiston
.get();
370 * Changes the ball shift gear assembly to high
372 public void setHighGear() {
373 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
377 * Changes the ball shift gear assembly to low
379 public void setLowGear() {
380 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
384 * changes the gear to a DoubleSolenoid.Value
386 public void changeGear(DoubleSolenoid
.Value gear
) {
387 leftGearPiston
.set(gear
);
388 rightGearPiston
.set(gear
);