1 package org
.usfirst
.frc
.team3501
.robot
.subsystems
;
3 import org
.usfirst
.frc
.team3501
.robot
.Constants
;
4 import org
.usfirst
.frc
.team3501
.robot
.MathLib
;
5 import org
.usfirst
.frc
.team3501
.robot
.commands
.driving
.JoystickDrive
;
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
{
17 public static double driveP
= 0.012, driveI
= 0.0011, driveD
= -0.002;
18 public static double smallTurnP
= 0.004, smallTurnI
= 0.0013,
20 public static double largeTurnP
= .003, largeTurnI
= .0012, largeTurnD
= .006;
21 public static double driveStraightGyroP
= 0.01;
23 public static final double WHEEL_DIAMETER
= 4; // inches
24 public static final double 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 public static final boolean DRIVE_BRAKE_MODE
= true;
33 public static final boolean DRIVE_COAST_MODE
= false;
35 private static DriveTrain driveTrain
;
37 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
38 private final RobotDrive robotDrive
;
39 private final Encoder leftEncoder
, rightEncoder
;
40 private final DoubleSolenoid leftGearPiston
, rightGearPiston
;
42 private ADXRS450_Gyro imu
;
44 public boolean shouldBeClimbing
= false;
46 private DriveTrain() {
48 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
49 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
50 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
51 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
54 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
55 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, Encoder
.EncodingType
.k4X
);
56 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
57 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, Encoder
.EncodingType
.k4X
);
59 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
60 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
63 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
65 this.imu
= new ADXRS450_Gyro(Constants
.DriveTrain
.GYRO_PORT
);
67 // TODO: Not sure if MODULE_NUMBER should be the same for both
68 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.PISTON_MODULE
,
69 Constants
.DriveTrain
.LEFT_GEAR_PISTON_FORWARD
,
70 Constants
.DriveTrain
.LEFT_GEAR_PISTON_REVERSE
);
71 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.PISTON_MODULE
,
72 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_FORWARD
,
73 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_REVERSE
);
76 public static DriveTrain
getDriveTrain() {
77 if (driveTrain
== null) {
78 driveTrain
= new DriveTrain();
84 public void setMotorValues(double left
, double right
) {
85 left
= MathLib
.restrictToRange(left
, -1.0, 1.0);
86 right
= MathLib
.restrictToRange(right
, -1.0, 1.0);
91 frontRight
.set(-right
);
92 rearRight
.set(-right
);
95 public void joystickDrive(final double thrust
, final double twist
) {
96 robotDrive
.arcadeDrive(thrust
, twist
, true);
100 setMotorValues(0, 0);
103 public double getLeftMotorVal() {
104 return (frontLeft
.get() + rearLeft
.get()) / 2;
107 public double getRightMotorVal() {
108 return (frontRight
.get() + rearRight
.get()) / 2;
113 public double getLeftEncoderDistance() {
114 return leftEncoder
.getDistance();
117 public double getRightEncoderDistance() {
118 return rightEncoder
.getDistance();
121 public void printEncoderOutput() {
122 System
.out
.println("left: " + getLeftEncoderDistance());
123 System
.out
.println("right: " + getRightEncoderDistance());
124 // System.out.println(getAvgEncoderDistance());
127 public double getAvgEncoderDistance() {
128 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
131 public void resetEncoders() {
133 rightEncoder
.reset();
136 public double getLeftSpeed() {
137 return leftEncoder
.getRate();
140 public double getRightSpeed() {
141 return rightEncoder
.getRate();
144 public double getSpeed() {
145 return (getLeftSpeed() + getRightSpeed()) / 2.0;
148 // ------Gyro------//
149 public double getAngle() {
150 return this.imu
.getAngle();
153 public void resetGyro() {
158 * @return a value that is the current setpoint for the piston kReverse or
161 public Value
getLeftGearPistonValue() {
162 return leftGearPiston
.get();
166 * @return a value that is the current setpoint for the piston kReverse or
169 public Value
getRightGearPistonValue() {
170 return rightGearPiston
.get();
174 * Changes the ball shift gear assembly to high
176 public void setHighGear() {
177 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
181 * Changes the ball shift gear assembly to low
183 public void setLowGear() {
184 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
188 * Changes the gear to a DoubleSolenoid.Value
190 private void changeGear(DoubleSolenoid
.Value gear
) {
191 leftGearPiston
.set(gear
);
192 rightGearPiston
.set(gear
);
196 protected void initDefaultCommand() {
197 setDefaultCommand(new JoystickDrive());
200 public void setCANTalonsBrakeMode(boolean mode
) {
201 frontLeft
.enableBrakeMode(mode
);
202 rearLeft
.enableBrakeMode(mode
);
204 frontRight
.enableBrakeMode(mode
);
205 rearRight
.enableBrakeMode(mode
);