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
;
20 public static double turnP
, turnI
, turnD
;
22 public static double driveStraightGyroP
= 0.01;
24 public static final double WHEEL_DIAMETER
= 6; // inches
25 public static final int ENCODER_PULSES_PER_REVOLUTION
= 256;
26 public static final double INCHES_PER_PULSE
= WHEEL_DIAMETER
* Math
.PI
27 / ENCODER_PULSES_PER_REVOLUTION
;
29 public static final double MAINTAIN_CLIMBED_POSITION
= 0;
30 public static final double TIME_TO_CLIMB_FOR
= 0;
31 public static final double CLIMBER_SPEED
= 0;
33 private static DriveTrain driveTrain
;
35 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
36 private final RobotDrive robotDrive
;
37 private final Encoder leftEncoder
, rightEncoder
;
38 private final DoubleSolenoid leftGearPiston
, rightGearPiston
;
40 private ADXRS450_Gyro imu
;
42 public boolean shouldBeClimbing
= false;
44 private PIDController driveController
;
45 private PIDController gyroController
;
47 private DriveTrain() {
49 driveController
= new PIDController(driveP
, driveI
, driveD
);
50 gyroController
= new PIDController(turnP
, turnI
, turnD
);
53 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
54 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
55 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
56 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
59 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
60 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, Encoder
.EncodingType
.k4X
);
61 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
62 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, Encoder
.EncodingType
.k4X
);
64 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
65 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
68 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
70 this.imu
= new ADXRS450_Gyro(Constants
.DriveTrain
.GYRO_PORT
);
72 // TODO: Not sure if MODULE_NUMBER should be the same for both
73 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.PISTON_MODULE
,
74 Constants
.DriveTrain
.LEFT_GEAR_PISTON_FORWARD
,
75 Constants
.DriveTrain
.LEFT_GEAR_PISTON_REVERSE
);
76 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.PISTON_MODULE
,
77 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_FORWARD
,
78 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_REVERSE
);
81 public PIDController
getDriveController() {
82 return this.driveController
;
85 public PIDController
getGyroController() {
86 return this.gyroController
;
89 public static DriveTrain
getDriveTrain() {
90 if (driveTrain
== null) {
91 driveTrain
= new DriveTrain();
97 public void setMotorValues(final double left
, final double right
) {
101 frontRight
.set(-right
);
102 rearRight
.set(-right
);
105 public void joystickDrive(final double thrust
, final double twist
) {
106 robotDrive
.arcadeDrive(thrust
, twist
, true);
110 setMotorValues(0, 0);
113 public double getLeftMotorVal() {
114 return (frontLeft
.get() + rearLeft
.get()) / 2;
117 public double getRightMotorVal() {
118 return (frontRight
.get() + rearRight
.get()) / 2;
123 public double getLeftEncoderDistance() {
124 return leftEncoder
.getDistance();
127 public double getRightEncoderDistance() {
128 return rightEncoder
.getDistance();
131 public void printEncoderOutput() {
132 System
.out
.println("left: " + getLeftEncoderDistance());
133 System
.out
.println("right: " + getRightEncoderDistance());
134 // System.out.println(getAvgEncoderDistance());
137 public double getAvgEncoderDistance() {
138 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
141 public void resetEncoders() {
143 rightEncoder
.reset();
146 public double getLeftSpeed() {
147 return leftEncoder
.getRate();
150 public double getRightSpeed() {
151 return rightEncoder
.getRate();
154 public double getSpeed() {
155 return (getLeftSpeed() + getRightSpeed()) / 2.0;
158 // ------Gyro------//
159 public double getAngle() {
160 return this.imu
.getAngle();
163 public void resetGyro() {
168 * @return a value that is the current setpoint for the piston kReverse or
171 public Value
getLeftGearPistonValue() {
172 return leftGearPiston
.get();
176 * @return a value that is the current setpoint for the piston kReverse or
179 public Value
getRightGearPistonValue() {
180 return rightGearPiston
.get();
184 * Changes the ball shift gear assembly to high
186 public void setHighGear() {
187 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
191 * Changes the ball shift gear assembly to low
193 public void setLowGear() {
194 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
198 * Changes the gear to a DoubleSolenoid.Value
200 private void changeGear(DoubleSolenoid
.Value gear
) {
201 leftGearPiston
.set(gear
);
202 rightGearPiston
.set(gear
);
206 protected void initDefaultCommand() {
207 setDefaultCommand(new JoystickDrive());