Fix conflicts from rebase
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / subsystems / DriveTrain.java
1 package org.usfirst.frc.team3501.robot.subsystems;
2
3 import org.usfirst.frc.team3501.robot.Constants;
4 import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive;
5
6 import com.ctre.CANTalon;
7
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;
14
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;
20
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
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;
31
32 private ADXRS450_Gyro imu;
33
34 private DriveTrain() {
35 // MOTOR CONTROLLERS
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);
40
41 // ENCODERS
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);
46
47 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
48 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
49
50 // ROBOT DRIVE
51 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
52
53 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
54
55 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.MODULE_NUMBER,
56 Constants.DriveTrain.LEFT_FORWARD, Constants.DriveTrain.LEFT_REVERSE);
57 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.MODULE_NUMBER,
58 Constants.DriveTrain.RIGHT_FORWARD, Constants.DriveTrain.RIGHT_REVERSE);
59 }
60
61 public static DriveTrain getDriveTrain() {
62 if (driveTrain == null) {
63 driveTrain = new DriveTrain();
64 }
65 return driveTrain;
66 }
67
68 // DRIVE METHODS
69 public void setMotorValues(final double left, final double right) {
70 frontLeft.set(left);
71 rearLeft.set(left);
72
73 frontRight.set(-right);
74 rearRight.set(-right);
75 }
76
77 public void joystickDrive(final double thrust, final double twist) {
78 robotDrive.arcadeDrive(thrust, twist, true);
79 }
80
81 public void stop() {
82 setMotorValues(0, 0);
83 }
84
85 public double getLeftMotorVal() {
86 return (frontLeft.get() + rearLeft.get()) / 2;
87 }
88
89 public double getRightMotorVal() {
90 return (frontRight.get() + rearRight.get()) / 2;
91 }
92
93 // ENCODER METHODS
94
95 public double getLeftEncoderDistance() {
96 return leftEncoder.getDistance();
97 }
98
99 public double getRightEncoderDistance() {
100 return rightEncoder.getDistance();
101 }
102
103 public void printEncoderOutput() {
104 // System.out.println("left: " + getLeftEncoderDistance());
105 // System.out.println("right: " + getRightEncoderDistance());
106 System.out.println(getAvgEncoderDistance());
107 System.out.println("left: " + getLeftEncoderDistance());
108 System.out.println("right: " + getRightEncoderDistance());
109 }
110
111 public double getAvgEncoderDistance() {
112 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
113 }
114
115 public void resetEncoders() {
116 leftEncoder.reset();
117 rightEncoder.reset();
118 }
119
120 public double getLeftSpeed() {
121 return leftEncoder.getRate();
122 }
123
124 public double getRightSpeed() {
125 return rightEncoder.getRate();
126 }
127
128 public double getSpeed() {
129 return (getLeftSpeed() + getRightSpeed()) / 2.0;
130 }
131
132 // ------Gyro------//
133 public double getAngle() {
134 return this.imu.getAngle() - this.gyroZero;
135 }
136
137 public void resetGyro() {
138 this.imu.reset();
139 }
140
141 public double getZeroAngle() {
142 return this.gyroZero;
143 }
144
145 /*
146 * @return a value that is the current setpoint for the piston kReverse or
147 * KForward
148 */
149 public Value getLeftGearPistonValue() {
150 return leftGearPiston.get();
151 }
152
153 /*
154 * @return a value that is the current setpoint for the piston kReverse or
155 * KForward
156 */
157 public Value getRightGearPistonValue() {
158 return rightGearPiston.get();
159 }
160
161 /*
162 * Changes the ball shift gear assembly to high
163 */
164 public void setHighGear() {
165 changeGear(Constants.DriveTrain.HIGH_GEAR);
166 }
167
168 /*
169 * Changes the ball shift gear assembly to low
170 */
171 public void setLowGear() {
172 changeGear(Constants.DriveTrain.LOW_GEAR);
173 }
174
175 /*
176 * Changes the gear to a DoubleSolenoid.Value
177 */
178 private void changeGear(DoubleSolenoid.Value gear) {
179 leftGearPiston.set(gear);
180 rightGearPiston.set(gear);
181 }
182
183 @Override
184 protected void initDefaultCommand() {
185 setDefaultCommand(new JoystickDrive());
186 }
187
188 }