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
.BNO055
;
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.006, driveI
= 0.001, driveD
= -0.002;
18 public static double defaultGyroP
= 0.004, defaultGyroI
= 0.0013,
19 defaultGyroD
= -0.005;
20 public static double driveP
= 0.008, driveI
= 0.001, driveD
= -0.002;
21 public static double defaultGyroP
= 0.009, defaultGyroI
= 0.00000,
22 defaultGyroD
= -0.000;
23 private double gyroZero
= 0;
25 public static final double WHEEL_DIAMETER
= 6; // inches
26 public static final int ENCODER_PULSES_PER_REVOLUTION
= 256;
27 public static final double INCHES_PER_PULSE
= WHEEL_DIAMETER
* Math
.PI
28 / ENCODER_PULSES_PER_REVOLUTION
;
30 private static DriveTrain driveTrain
;
31 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
32 private final RobotDrive robotDrive
;
33 private final Encoder leftEncoder
, rightEncoder
;
34 private final DoubleSolenoid leftGearPiston
, rightGearPiston
;
36 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
);
59 shifter
= DoubleSolenoid(10, Constants
.DriveTrain
.SHIFTER_FORWARD
,
60 Constants
.DriveTrain
.SHIFTER_REVERSE
);
61 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
62 Constants
.DriveTrain
.LEFT_FORWARD
, Constants
.DriveTrain
.LEFT_REVERSE
);
63 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
64 Constants
.DriveTrain
.RIGHT_FORWARD
, Constants
.DriveTrain
.RIGHT_REVERSE
);
67 public static DriveTrain
getDriveTrain() {
68 if (driveTrain
== null) {
69 driveTrain
= new DriveTrain();
75 public void setMotorValues(final double left
, final double right
) {
79 frontRight
.set(-right
);
80 rearRight
.set(-right
);
83 public void joystickDrive(final double thrust
, final double twist
) {
84 robotDrive
.arcadeDrive(thrust
, twist
, true);
91 public double getLeftMotorVal() {
92 return (frontLeft
.get() + rearLeft
.get()) / 2;
95 public double getRightMotorVal() {
96 return (frontRight
.get() + rearRight
.get()) / 2;
101 public double getLeftEncoderDistance() {
102 return leftEncoder
.getDistance();
105 public double getRightEncoderDistance() {
106 return rightEncoder
.getDistance();
109 public void printEncoderOutput() {
110 // System.out.println("left: " + getLeftEncoderDistance());
111 // System.out.println("right: " + getRightEncoderDistance());
112 System
.out
.println(getAvgEncoderDistance());
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
;
148 * @return a value that is the current setpoint for the piston kReverse or
151 public Value
getLeftGearPistonValue() {
152 return leftGearPiston
.get();
156 * @return a value that is the current setppoint for the piston kReverse or
159 public Value
getRightGearPistonValue() {
160 return rightGearPiston
.get();
164 * Changes the ball shift gear assembly to high
166 public void setHighGear() {
167 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
171 * Changes the ball shift gear assembly to low
173 public void setLowGear() {
174 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
178 * Changes the gear to a DoubleSolenoid.Value
180 private void changeGear(DoubleSolenoid
.Value gear
) {
181 leftGearPiston
.set(gear
);
182 rightGearPiston
.set(gear
);
185 public double getAngle() {
186 if (!this.imu
.isInitialized())
188 return this.imu
.getHeading() - this.gyroZero
;
191 public void resetGyro() {
192 this.gyroZero
= this.getAngle();
196 public double getZeroAngle() {
197 return this.gyroZero
;
201 protected void initDefaultCommand() {
202 setDefaultCommand(new JoystickDrive());