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
;
6 import com
.ctre
.CANTalon
;
8 import edu
.wpi
.first
.wpilibj
.ADXRS450_Gyro
;
9 import edu
.wpi
.first
.wpilibj
.DoubleSolenoid
;
10 import edu
.wpi
.first
.wpilibj
.DoubleSolenoid
.Value
;
11 import edu
.wpi
.first
.wpilibj
.Encoder
;
12 import edu
.wpi
.first
.wpilibj
.RobotDrive
;
13 import edu
.wpi
.first
.wpilibj
.command
.Subsystem
;
15 public class DriveTrain
extends Subsystem
{
16 public static double driveP
= 0.006, driveI
= 0.001, driveD
= -0.002;
17 public static double defaultGyroP
= 0.004, defaultGyroI
= 0.0013,
18 defaultGyroD
= -0.005;
19 private double gyroZero
= 0;
21 public static final double WHEEL_DIAMETER
= 6; // inches
22 public static final int ENCODER_PULSES_PER_REVOLUTION
= 256;
23 public static final double INCHES_PER_PULSE
= WHEEL_DIAMETER
* Math
.PI
24 / ENCODER_PULSES_PER_REVOLUTION
;
26 private static DriveTrain driveTrain
;
27 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
28 private final RobotDrive robotDrive
;
29 private final Encoder leftEncoder
, rightEncoder
;
30 private final DoubleSolenoid leftGearPiston
, rightGearPiston
;
32 private ADXRS450_Gyro imu
;
34 private DriveTrain() {
36 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
37 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
38 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
39 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
42 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
43 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, Encoder
.EncodingType
.k4X
);
44 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
45 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, Encoder
.EncodingType
.k4X
);
47 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
48 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
51 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
53 this.imu
= new ADXRS450_Gyro(Constants
.DriveTrain
.GYRO_PORT
);
54 shifter
= DoubleSolenoid(10, Constants
.DriveTrain
.SHIFTER_FORWARD
,
55 Constants
.DriveTrain
.SHIFTER_REVERSE
);
56 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
57 Constants
.DriveTrain
.LEFT_FORWARD
, Constants
.DriveTrain
.LEFT_REVERSE
);
58 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
59 Constants
.DriveTrain
.RIGHT_FORWARD
, Constants
.DriveTrain
.RIGHT_REVERSE
);
62 public static DriveTrain
getDriveTrain() {
63 if (driveTrain
== null) {
64 driveTrain
= new DriveTrain();
70 public void setMotorValues(final double left
, final double right
) {
74 frontRight
.set(-right
);
75 rearRight
.set(-right
);
78 public void joystickDrive(final double thrust
, final double twist
) {
79 robotDrive
.arcadeDrive(thrust
, twist
, true);
86 public double getLeftMotorVal() {
87 return (frontLeft
.get() + rearLeft
.get()) / 2;
90 public double getRightMotorVal() {
91 return (frontRight
.get() + rearRight
.get()) / 2;
96 public double getLeftEncoderDistance() {
97 return leftEncoder
.getDistance();
100 public double getRightEncoderDistance() {
101 return rightEncoder
.getDistance();
104 public void printEncoderOutput() {
105 // System.out.println("left: " + getLeftEncoderDistance());
106 // System.out.println("right: " + getRightEncoderDistance());
107 System
.out
.println(getAvgEncoderDistance());
110 public double getAvgEncoderDistance() {
111 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
114 public void resetEncoders() {
116 rightEncoder
.reset();
119 public double getLeftSpeed() {
120 return leftEncoder
.getRate();
123 public double getRightSpeed() {
124 return rightEncoder
.getRate();
127 public double getSpeed() {
128 return (getLeftSpeed() + getRightSpeed()) / 2.0;
131 // ------Gyro------//
132 public double getAngle() {
133 return this.imu
.getAngle() - this.gyroZero
;
136 public void resetGyro() {
140 public double getZeroAngle() {
141 return this.gyroZero
;
143 * @return a value that is the current setpoint for the piston kReverse or
146 public Value
getLeftGearPistonValue() {
147 return leftGearPiston
.get();
151 * @return a value that is the current setppoint for the piston kReverse or
154 public Value
getRightGearPistonValue() {
155 return rightGearPiston
.get();
159 * Changes the ball shift gear assembly to high
161 public void setHighGear() {
162 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
166 * Changes the ball shift gear assembly to low
168 public void setLowGear() {
169 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
173 * Changes the gear to a DoubleSolenoid.Value
175 private void changeGear(DoubleSolenoid
.Value gear
) {
176 leftGearPiston
.set(gear
);
177 rightGearPiston
.set(gear
);
181 protected void initDefaultCommand() {
182 setDefaultCommand(new JoystickDrive());