1 package org
.usfirst
.frc
.team3501
.robot
.subsystems
;
3 import org
.usfirst
.frc
.team3501
.robot
.Constants
;
4 import org
.usfirst
.frc
.team3501
.robot
.commands
.driving
.JoystickDrive
;
5 import org
.usfirst
.frc
.team3501
.robot
.utils
.PIDController
;
7 import com
.ctre
.CANTalon
;
9 import edu
.wpi
.first
.wpilibj
.ADXRS450_Gyro
;
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
.RobotDrive
;
14 import edu
.wpi
.first
.wpilibj
.command
.Subsystem
;
16 public class DriveTrain
extends Subsystem
{
18 public static double driveP
, driveI
, driveD
;
19 public static double turnP
, turnI
, turnD
;
21 public static double driveStraightGyroP
= 0.01;
23 public static final String DRIVE_P_Val
= "DriveP";
24 public static final String DRIVE_I_Val
= "DriveI";
25 public static final String DRIVE_D_Val
= "DriveD";
26 public static final String DRIVE_TARGET_DIST
= "SET_DIST";
27 public static final String DRIVE_MOTOR_VAL
= "SPEED";
28 public static final String GYRO_P_Val
= "GyroP";
29 public static final String GYRO_I_Val
= "GyroI";
30 public static final String GYRO_D_Val
= "GyroD";
31 public static final String GYRO_TARGET_ANGLE
= "SET_ANGLE";
32 public static final int PID_ERROR
= -1;
33 public static final int TARGET_DISTANCE_ERROR
= -1;
35 public static final double WHEEL_DIAMETER
= 6; // inches
36 public static final int ENCODER_PULSES_PER_REVOLUTION
= 256;
37 public static final double INCHES_PER_PULSE
= WHEEL_DIAMETER
* Math
.PI
38 / ENCODER_PULSES_PER_REVOLUTION
;
40 public static final double MAINTAIN_CLIMBED_POSITION
= 0;
41 public static final double TIME_TO_CLIMB_FOR
= 0;
42 public static final double CLIMBER_SPEED
= 0;
44 private static DriveTrain driveTrain
;
46 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
47 private final RobotDrive robotDrive
;
48 private final Encoder leftEncoder
, rightEncoder
;
49 private final DoubleSolenoid leftGearPiston
, rightGearPiston
;
51 private ADXRS450_Gyro imu
;
53 public boolean shouldBeClimbing
= false;
55 private PIDController driveController
;
56 private PIDController gyroController
;
58 private boolean isClimbing
;
59 private static double CLIMBER_SPEED
;;
61 private DriveTrain() {
63 driveController
= new PIDController(driveP
, driveI
, driveD
);
64 gyroController
= new PIDController(turnP
, turnI
, turnD
);
69 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
70 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
71 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
72 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
75 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
76 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, Encoder
.EncodingType
.k4X
);
77 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
78 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, Encoder
.EncodingType
.k4X
);
80 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
81 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
84 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
86 this.imu
= new ADXRS450_Gyro(Constants
.DriveTrain
.GYRO_PORT
);
88 // TODO: Not sure if MODULE_NUMBER should be the same for both
89 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.PISTON_MODULE
,
90 Constants
.DriveTrain
.LEFT_GEAR_PISTON_FORWARD
,
91 Constants
.DriveTrain
.LEFT_GEAR_PISTON_REVERSE
);
92 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.PISTON_MODULE
,
93 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_FORWARD
,
94 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_REVERSE
);
96 CLIMBER_SPEED
= Constants
.DriveTrain
.CLIMBER_SPEED
;
99 public PIDController
getDriveController() {
100 return this.driveController
;
103 public PIDController
getGyroController() {
104 return this.gyroController
;
107 public static DriveTrain
getDriveTrain() {
108 if (driveTrain
== null) {
109 driveTrain
= new DriveTrain();
115 public void setMotorValues(final double left
, final double right
) {
119 frontRight
.set(-right
);
120 rearRight
.set(-right
);
123 public void joystickDrive(final double thrust
, final double twist
) {
124 robotDrive
.arcadeDrive(thrust
, twist
, true);
128 setMotorValues(0, 0);
131 public double getLeftMotorVal() {
132 return (frontLeft
.get() + rearLeft
.get()) / 2;
135 public double getRightMotorVal() {
136 return (frontRight
.get() + rearRight
.get()) / 2;
141 public double getLeftEncoderDistance() {
142 return leftEncoder
.getDistance();
145 public double getRightEncoderDistance() {
146 return rightEncoder
.getDistance();
149 public void printEncoderOutput() {
150 System
.out
.println("left: " + getLeftEncoderDistance());
151 System
.out
.println("right: " + getRightEncoderDistance());
152 // System.out.println(getAvgEncoderDistance());
155 public double getAvgEncoderDistance() {
156 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
159 public void resetEncoders() {
161 rightEncoder
.reset();
164 public double getLeftSpeed() {
165 return leftEncoder
.getRate();
168 public double getRightSpeed() {
169 return rightEncoder
.getRate();
172 public double getSpeed() {
173 return (getLeftSpeed() + getRightSpeed()) / 2.0;
176 // ------Gyro------//
177 public double getAngle() {
178 return this.imu
.getAngle();
181 public void resetGyro() {
186 * @return a value that is the current setpoint for the piston kReverse or
189 public Value
getLeftGearPistonValue() {
190 return leftGearPiston
.get();
194 * @return a value that is the current setpoint for the piston kReverse or
197 public Value
getRightGearPistonValue() {
198 return rightGearPiston
.get();
202 * Changes the ball shift gear assembly to high
204 public void setHighGear() {
205 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
209 * Changes the ball shift gear assembly to low
211 public void setLowGear() {
212 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
216 * Changes the gear to a DoubleSolenoid.Value
218 private void changeGear(DoubleSolenoid
.Value gear
) {
219 leftGearPiston
.set(gear
);
220 rightGearPiston
.set(gear
);
224 protected void initDefaultCommand() {
225 setDefaultCommand(new JoystickDrive());
228 public boolean isClimbing() {
229 return this.isClimbing
;
232 public double getClimbingSpeed() {
233 return this.CLIMBER_SPEED
;