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
;
25 public static final int MAINTAIN_CLIMBED_POSITION
= 1;
26 private static DriveTrain driveTrain
;
28 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
29 private final RobotDrive robotDrive
;
30 private final Encoder leftEncoder
, rightEncoder
;
31 private final DoubleSolenoid leftGearPiston
, rightGearPiston
;
33 private ADXRS450_Gyro imu
;
35 private DriveTrain() {
37 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
38 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
39 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
40 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
43 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
44 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, Encoder
.EncodingType
.k4X
);
45 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
46 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, Encoder
.EncodingType
.k4X
);
48 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
49 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
52 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
54 this.imu
= new ADXRS450_Gyro(Constants
.DriveTrain
.GYRO_PORT
);
56 // TODO: Not sure if MODULE_NUMBER should be the same for both
57 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
58 Constants
.DriveTrain
.LEFT_GEAR_PISTON_FORWARD
,
59 Constants
.DriveTrain
.LEFT_GEAR_PISTON_REVERSE
);
60 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
61 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_FORWARD
,
62 Constants
.DriveTrain
.RIGHT_GEAR_PISTON_REVERSE
);
65 public static DriveTrain
getDriveTrain() {
66 if (driveTrain
== null) {
67 driveTrain
= new DriveTrain();
73 public void setMotorValues(final double left
, final double right
) {
77 frontRight
.set(-right
);
78 rearRight
.set(-right
);
81 public void joystickDrive(final double thrust
, final double twist
) {
82 robotDrive
.arcadeDrive(thrust
, twist
, true);
89 public double getLeftMotorVal() {
90 return (frontLeft
.get() + rearLeft
.get()) / 2;
93 public double getRightMotorVal() {
94 return (frontRight
.get() + rearRight
.get()) / 2;
99 public double getLeftEncoderDistance() {
100 return leftEncoder
.getDistance();
103 public double getRightEncoderDistance() {
104 return rightEncoder
.getDistance();
107 public void printEncoderOutput() {
108 // System.out.println("left: " + getLeftEncoderDistance());
109 // System.out.println("right: " + getRightEncoderDistance());
110 System
.out
.println(getAvgEncoderDistance());
111 System
.out
.println("left: " + getLeftEncoderDistance());
112 System
.out
.println("right: " + getRightEncoderDistance());
115 public double getAvgEncoderDistance() {
116 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
119 public void resetEncoders() {
121 rightEncoder
.reset();
124 public double getLeftSpeed() {
125 return leftEncoder
.getRate();
128 public double getRightSpeed() {
129 return rightEncoder
.getRate();
132 public double getSpeed() {
133 return (getLeftSpeed() + getRightSpeed()) / 2.0;
136 // ------Gyro------//
137 public double getAngle() {
138 return this.imu
.getAngle() - this.gyroZero
;
141 public void resetGyro() {
145 public double getZeroAngle() {
146 return this.gyroZero
;
150 * @return a value that is the current setpoint for the piston kReverse or
153 public Value
getLeftGearPistonValue() {
154 return leftGearPiston
.get();
158 * @return a value that is the current setpoint for the piston kReverse or
161 public Value
getRightGearPistonValue() {
162 return rightGearPiston
.get();
166 * Changes the ball shift gear assembly to high
168 public void setHighGear() {
169 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
173 * Changes the ball shift gear assembly to low
175 public void setLowGear() {
176 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
180 * Changes the gear to a DoubleSolenoid.Value
182 private void changeGear(DoubleSolenoid
.Value gear
) {
183 leftGearPiston
.set(gear
);
184 rightGearPiston
.set(gear
);
188 protected void initDefaultCommand() {
189 setDefaultCommand(new JoystickDrive());