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.01, driveI
= 0.00115, 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 private static DriveTrain driveTrain
;
30 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
31 private final RobotDrive robotDrive
;
32 private final Encoder leftEncoder
, rightEncoder
;
33 private final DoubleSolenoid rightDriveTrainPiston
;
34 // private final Solenoid leftDriveTrainPiston;
35 private final DoubleSolenoid gearManipulatorPiston
;
37 private ADXRS450_Gyro imu
;
39 private DriveTrain() {
41 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
42 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
43 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
44 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
47 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
48 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, Encoder
.EncodingType
.k4X
);
49 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
50 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, Encoder
.EncodingType
.k4X
);
52 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
53 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
56 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
58 this.imu
= new ADXRS450_Gyro(Constants
.DriveTrain
.GYRO_PORT
);
60 // TODO: Not sure if MODULE_NUMBER should be the same for both
61 // leftDriveTrainPiston = new Solenoid(Constants.DriveTrain.PISTON_MODULE,
62 // Constants.DriveTrain.LEFT_GEAR_PISTON_PORT);
63 rightDriveTrainPiston
= new DoubleSolenoid(
64 Constants
.DriveTrain
.PISTON_MODULE
,
65 Constants
.DriveTrain
.DRIVETRAIN_GEAR_FORWARD
,
66 Constants
.DriveTrain
.DRIVETRAIN_GEAR_REVERSE
);
68 gearManipulatorPiston
= new DoubleSolenoid(
69 Constants
.DriveTrain
.PISTON_MODULE
,
70 Constants
.DriveTrain
.GEAR_MANIPULATOR_PISTON_FORWARD
,
71 Constants
.DriveTrain
.GEAR_MANIPULATOR_PISTON_REVERSE
);
74 public static DriveTrain
getDriveTrain() {
75 if (driveTrain
== null) {
76 driveTrain
= new DriveTrain();
82 public void setMotorValues(double left
, double right
) {
83 left
= MathLib
.restrictToRange(left
, -1.0, 1.0);
84 right
= MathLib
.restrictToRange(right
, -1.0, 1.0);
89 frontRight
.set(-right
);
90 rearRight
.set(-right
);
93 public void joystickDrive(final double thrust
, final double twist
) {
94 if ((thrust
< 0.1 && thrust
> -0.1) && (twist
< 0.1 && twist
> -0.1))
95 robotDrive
.arcadeDrive(0, 0, true);
97 robotDrive
.arcadeDrive(thrust
, twist
, true);
100 public void tankDrive(double left
, double right
) {
101 robotDrive
.tankDrive(left
, right
);
105 setMotorValues(0, 0);
108 public double getLeftMotorVal() {
109 return (frontLeft
.get() + rearLeft
.get()) / 2;
112 public double getRightMotorVal() {
113 return (frontRight
.get() + rearRight
.get()) / 2;
118 public double getLeftEncoderDistance() {
119 return leftEncoder
.getDistance();
122 public double getRightEncoderDistance() {
123 return rightEncoder
.getDistance();
126 public void printEncoderOutput() {
127 System
.out
.println("left: " + getLeftEncoderDistance());
128 System
.out
.println("right: " + getRightEncoderDistance());
129 // System.out.println(getAvgEncoderDistance());
132 public double getAvgEncoderDistance() {
133 return (leftEncoder
.getDistance() + rightEncoder
.getDistance()) / 2;
136 public void resetEncoders() {
138 rightEncoder
.reset();
141 public double getLeftSpeed() {
142 return leftEncoder
.getRate();
145 public double getRightSpeed() {
146 return rightEncoder
.getRate();
149 public double getSpeed() {
150 return (getLeftSpeed() + getRightSpeed()) / 2.0;
153 // ------Gyro------//
154 public double getAngle() {
155 return this.imu
.getAngle();
158 public void resetGyro() {
163 * @return a value that is the current setpoint for the piston kReverse or
166 // public boolean getLeftDriveTrainPiston() {
167 // return leftDriveTrainPiston.get();
171 * @return a value that is the current setpoint for the piston kReverse or
174 public Value
getRightDriveTrainPiston() {
175 return rightDriveTrainPiston
.get();
179 * Changes the ball shift gear assembly to high
181 public void setHighGear() {
182 changeGear(Constants
.DriveTrain
.FORWARD_PISTON_VALUE
);
186 * Changes the ball shift gear assembly to low
188 public void setLowGear() {
189 changeGear(Constants
.DriveTrain
.REVERSE_PISTON_VALUE
);
193 * Changes the gear to a DoubleSolenoid.Value
195 private void changeGear(DoubleSolenoid
.Value gear
) {
196 System
.out
.println("shifting to " + gear
);
197 rightDriveTrainPiston
.set(gear
);
198 System
.out
.println("after: " + this.getRightDriveTrainPiston());
201 // if (gear == Constants.DriveTrain.FORWARD_PISTON_VALUE)
202 // leftDriveTrainPiston.set(Constants.DriveTrain.EXTEND_VALUE);
204 // leftDriveTrainPiston.set(Constants.DriveTrain.RETRACT_VALUE);
207 public Value
getGearManipulatorPistonValue() {
208 return gearManipulatorPiston
.get();
211 public void extendGearManipulatorPiston() {
212 gearManipulatorPiston
.set(Constants
.DriveTrain
.FORWARD_PISTON_VALUE
);
215 public void retractGearManipulatorPiston() {
216 gearManipulatorPiston
.set(Constants
.DriveTrain
.REVERSE_PISTON_VALUE
);
220 protected void initDefaultCommand() {
221 setDefaultCommand(new JoystickDrive());