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 double WHEEL_DIAMETER
= 6; // inches
24 public static final int ENCODER_PULSES_PER_REVOLUTION
= 256;
25 public static final double INCHES_PER_PULSE
= WHEEL_DIAMETER
* Math
.PI
26 / ENCODER_PULSES_PER_REVOLUTION
;
28 public static final double MAINTAIN_CLIMBED_POSITION
= 0;
29 public static final double TIME_TO_CLIMB_FOR
= 0;
30 public static final double CLIMBER_SPEED
= 0;
32 private static DriveTrain driveTrain
;
34 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
35 private final RobotDrive robotDrive
;
36 private final Encoder leftEncoder
, rightEncoder
;
37 private final DoubleSolenoid leftGearPiston
, rightGearPiston
;
39 private ADXRS450_Gyro imu
;
41 public boolean shouldBeClimbing
= false;
43 private PIDController driveController
;
44 private PIDController gyroController
;
46 private DriveTrain() {
48 driveController
= new PIDController(driveP
, driveI
, driveD
);
49 gyroController
= new PIDController(turnP
, turnI
, turnD
);
52 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
53 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
54 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
55 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
58 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
59 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, Encoder
.EncodingType
.k4X
);
60 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
61 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, Encoder
.EncodingType
.k4X
);
63 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
64 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
67 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
69 this.imu
= new ADXRS450_Gyro(Constants
.DriveTrain
.GYRO_PORT
);
71 // TODO: Not sure if MODULE_NUMBER should be the same for both
72 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.PISTON_MODULE
,
73 Constants
.DriveTrain
.LEFT_GEAR_PISTON_FORWARD
,
74 Constants
.DriveTrain
.LEFT_GEAR_PISTON_REVERSE
);
75 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.PISTON_MODULE
,
76 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_FORWARD
,
77 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_REVERSE
);
80 public PIDController
getDriveController() {
81 return this.driveController
;
84 public PIDController
getGyroController() {
85 return this.gyroController
;
88 public static DriveTrain
getDriveTrain() {
89 if (driveTrain
== null) {
90 driveTrain
= new DriveTrain();
96 public void setMotorValues(final double left
, final double right
) {
100 frontRight
.set(-right
);
101 rearRight
.set(-right
);
104 public void joystickDrive(final double thrust
, final double twist
) {
105 robotDrive
.arcadeDrive(thrust
, twist
, true);
109 setMotorValues(0, 0);
112 public double getLeftMotorVal() {
113 return (frontLeft
.get() + rearLeft
.get()) / 2;
116 public double getRightMotorVal() {
117 return (frontRight
.get() + rearRight
.get()) / 2;
122 public double getLeftEncoderDistance() {
123 return leftEncoder
.getDistance();
126 public double getRightEncoderDistance() {
127 return rightEncoder
.getDistance();
130 public void printEncoderOutput() {
131 System
.out
.println("left: " + getLeftEncoderDistance());
132 System
.out
.println("right: " + getRightEncoderDistance());
133 // System.out.println(getAvgEncoderDistance());
136 public double getAvgEncoderDistance() {
137 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
140 public void resetEncoders() {
142 rightEncoder
.reset();
145 public double getLeftSpeed() {
146 return leftEncoder
.getRate();
149 public double getRightSpeed() {
150 return rightEncoder
.getRate();
153 public double getSpeed() {
154 return (getLeftSpeed() + getRightSpeed()) / 2.0;
157 // ------Gyro------//
158 public double getAngle() {
159 return this.imu
.getAngle();
162 public void resetGyro() {
167 * @return a value that is the current setpoint for the piston kReverse or
170 public Value
getLeftGearPistonValue() {
171 return leftGearPiston
.get();
175 * @return a value that is the current setpoint for the piston kReverse or
178 public Value
getRightGearPistonValue() {
179 return rightGearPiston
.get();
183 * Changes the ball shift gear assembly to high
185 public void setHighGear() {
186 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
190 * Changes the ball shift gear assembly to low
192 public void setLowGear() {
193 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
197 * Changes the gear to a DoubleSolenoid.Value
199 private void changeGear(DoubleSolenoid
.Value gear
) {
200 leftGearPiston
.set(gear
);
201 rightGearPiston
.set(gear
);
205 protected void initDefaultCommand() {
206 setDefaultCommand(new JoystickDrive());