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
.I2C
.Port
;
14 import edu
.wpi
.first
.wpilibj
.RobotDrive
;
15 import edu
.wpi
.first
.wpilibj
.command
.Subsystem
;
17 public class DriveTrain
extends Subsystem
{
18 public static double driveP
= 0.006, driveI
= 0.001, driveD
= -0.002;
19 public static double defaultGyroP
= 0.004, defaultGyroI
= 0.0013,
20 defaultGyroD
= -0.005;
21 public static double driveP
= 0.008, driveI
= 0.001, driveD
= -0.002;
22 public static double defaultGyroP
= 0.009, defaultGyroI
= 0.00000,
23 defaultGyroD
= -0.000;
24 private double gyroZero
= 0;
26 public static final double WHEEL_DIAMETER
= 6; // inches
27 public static final int ENCODER_PULSES_PER_REVOLUTION
= 256;
28 public static final double INCHES_PER_PULSE
= WHEEL_DIAMETER
* Math
.PI
29 / ENCODER_PULSES_PER_REVOLUTION
;
31 private static DriveTrain driveTrain
;
32 private final CANTalon frontLeft
, frontRight
, rearLeft
, rearRight
;
33 private final RobotDrive robotDrive
;
34 private final Encoder leftEncoder
, rightEncoder
;
35 private final DoubleSolenoid leftGearPiston
, rightGearPiston
;
37 private ADXRS450_Gyro imu
;
40 private DriveTrain() {
42 frontLeft
= new CANTalon(Constants
.DriveTrain
.FRONT_LEFT
);
43 frontRight
= new CANTalon(Constants
.DriveTrain
.FRONT_RIGHT
);
44 rearLeft
= new CANTalon(Constants
.DriveTrain
.REAR_LEFT
);
45 rearRight
= new CANTalon(Constants
.DriveTrain
.REAR_RIGHT
);
48 leftEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_LEFT_A
,
49 Constants
.DriveTrain
.ENCODER_LEFT_B
, false, Encoder
.EncodingType
.k4X
);
50 rightEncoder
= new Encoder(Constants
.DriveTrain
.ENCODER_RIGHT_A
,
51 Constants
.DriveTrain
.ENCODER_RIGHT_B
, false, Encoder
.EncodingType
.k4X
);
53 leftEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
54 rightEncoder
.setDistancePerPulse(INCHES_PER_PULSE
);
57 robotDrive
= new RobotDrive(frontLeft
, rearLeft
, frontRight
, rearRight
);
59 this.imu
= new ADXRS450_Gyro(Constants
.DriveTrain
.GYRO_PORT
);
60 shifter
= DoubleSolenoid(10, Constants
.DriveTrain
.SHIFTER_FORWARD
,
61 Constants
.DriveTrain
.SHIFTER_REVERSE
);
62 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
63 Constants
.DriveTrain
.LEFT_FORWARD
, Constants
.DriveTrain
.LEFT_REVERSE
);
64 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
65 Constants
.DriveTrain
.RIGHT_FORWARD
, Constants
.DriveTrain
.RIGHT_REVERSE
);
67 this.imu
= BNO055
.getInstance(BNO055
.opmode_t
.OPERATION_MODE_IMUPLUS
,
68 BNO055
.vector_type_t
.VECTOR_EULER
, Port
.kOnboard
, (byte) 0x28);
69 gyroZero
= imu
.getHeading();
71 leftGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
72 Constants
.DriveTrain
.LEFT_FORWARD
, Constants
.DriveTrain
.LEFT_REVERSE
);
73 rightGearPiston
= new DoubleSolenoid(Constants
.DriveTrain
.MODULE_NUMBER
,
74 Constants
.DriveTrain
.RIGHT_FORWARD
, Constants
.DriveTrain
.RIGHT_REVERSE
);
77 public static DriveTrain
getDriveTrain() {
78 if (driveTrain
== null) {
79 driveTrain
= new DriveTrain();
85 public void setMotorValues(final double left
, final double right
) {
89 frontRight
.set(-right
);
90 rearRight
.set(-right
);
93 public void joystickDrive(final double thrust
, final double twist
) {
94 robotDrive
.arcadeDrive(thrust
, twist
, true);
101 public double getLeftMotorVal() {
102 return (frontLeft
.get() + rearLeft
.get()) / 2;
105 public double getRightMotorVal() {
106 return (frontRight
.get() + rearRight
.get()) / 2;
111 public double getLeftEncoderDistance() {
112 return leftEncoder
.getDistance();
115 public double getRightEncoderDistance() {
116 return rightEncoder
.getDistance();
119 public void printEncoderOutput() {
120 // System.out.println("left: " + getLeftEncoderDistance());
121 // System.out.println("right: " + getRightEncoderDistance());
122 System
.out
.println(getAvgEncoderDistance());
123 System
.out
.println("left: " + getLeftEncoderDistance());
124 System
.out
.println("right: " + getRightEncoderDistance());
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() - this.gyroZero
;
153 public void resetGyro() {
156 public double getAngle() {
157 if (!this.imu
.isInitialized())
159 return this.imu
.getHeading() - this.gyroZero
;
162 public void resetGyro() {
163 this.gyroZero
= this.getAngle();
167 public double getZeroAngle() {
168 return this.gyroZero
;
172 * @return a value that is the current setpoint for the piston kReverse or
175 public Value
getGearPistonValue() {
176 return leftGearPiston
.get();
180 * Changes the ball shift gear assembly to high
182 public void setHighGear() {
183 changeGear(Constants
.DriveTrain
.HIGH_GEAR
);
187 * Changes the ball shift gear assembly to low
189 public void setLowGear() {
190 changeGear(Constants
.DriveTrain
.LOW_GEAR
);
194 * Changes the gear to a DoubleSolenoid.Value
196 private void changeGear(DoubleSolenoid
.Value gear
) {
197 leftGearPiston
.set(gear
);
198 rightGearPiston
.set(gear
);
202 protected void initDefaultCommand() {
203 setDefaultCommand(new JoystickDrive());