code review changes
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / subsystems / DriveTrain.java
CommitLineData
38a404b3
KZ
1package org.usfirst.frc.team3501.robot.subsystems;
2
3import org.usfirst.frc.team3501.robot.Constants;
ac77a7b8 4import org.usfirst.frc.team3501.robot.MathLib;
5483fde9 5import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive;
04f2cb52 6
a1c76caf
CZ
7import com.ctre.CANTalon;
8
bf921ece 9import edu.wpi.first.wpilibj.ADXRS450_Gyro;
4a75c629 10import edu.wpi.first.wpilibj.DoubleSolenoid;
73e8adbc 11import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
fa4e4a97 12import edu.wpi.first.wpilibj.Encoder;
ccbc35ae 13import edu.wpi.first.wpilibj.RobotDrive;
38a404b3
KZ
14import edu.wpi.first.wpilibj.command.Subsystem;
15
16public class DriveTrain extends Subsystem {
b70398a7
CZ
17 public static double driveP = 0.012, driveI = 0.0011, driveD = -0.002;
18 public static double smallTurnP = 0.004, smallTurnI = 0.0013,
19 smallTurnD = 0.005;
20 public static double largeTurnP = .003, largeTurnI = .0012, largeTurnD = .006;
c846487e 21 public static double driveStraightGyroP = 0.01;
e12d6901 22
ac77a7b8
CZ
23 public static final double WHEEL_DIAMETER = 4; // inches
24 public static final double ENCODER_PULSES_PER_REVOLUTION = 256;
b634ebbc
CZ
25 public static final double INCHES_PER_PULSE = WHEEL_DIAMETER * Math.PI
26 / ENCODER_PULSES_PER_REVOLUTION;
9dc69158
CZ
27
28 public static final double MAINTAIN_CLIMBED_POSITION = 0;
29 public static final double TIME_TO_CLIMB_FOR = 0;
30 public static final double CLIMBER_SPEED = 0;
31
cc42bd52 32 private static DriveTrain driveTrain;
b8791ca4 33
cc42bd52
ME
34 private final CANTalon frontLeft, frontRight, rearLeft, rearRight;
35 private final RobotDrive robotDrive;
36 private final Encoder leftEncoder, rightEncoder;
73e8adbc 37 private final DoubleSolenoid leftGearPiston, rightGearPiston;
cc42bd52 38
bf921ece 39 private ADXRS450_Gyro imu;
e12d6901 40
366f1cfd 41 public boolean shouldBeClimbing = false;
cef1f36d 42
cc42bd52
ME
43 private DriveTrain() {
44 // MOTOR CONTROLLERS
45 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
46 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
47 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
48 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
49
50 // ENCODERS
51 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
3e2738c4 52 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
cc42bd52 53 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
3e2738c4 54 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
cc42bd52 55
b634ebbc
CZ
56 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
57 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
cc42bd52
ME
58
59 // ROBOT DRIVE
60 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
8e4c083c 61
bf921ece 62 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
af4491f7 63
ca372ce8 64 // TODO: Not sure if MODULE_NUMBER should be the same for both
58bcc21d 65 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
66 Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD,
67 Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE);
58bcc21d 68 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
69 Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD,
70 Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE);
cc42bd52
ME
71 }
72
73 public static DriveTrain getDriveTrain() {
74 if (driveTrain == null) {
75 driveTrain = new DriveTrain();
76 }
77 return driveTrain;
78 }
79
80 // DRIVE METHODS
ac77a7b8 81 public void setMotorValues(double left, double right) {
b70398a7
CZ
82 left = MathLib.restrictToRange(left, -1.0, 1.0);
83 right = MathLib.restrictToRange(right, -1.0, 1.0);
ac77a7b8 84
cc42bd52
ME
85 frontLeft.set(left);
86 rearLeft.set(left);
87
b081e34b
ME
88 frontRight.set(-right);
89 rearRight.set(-right);
cc42bd52
ME
90 }
91
92 public void joystickDrive(final double thrust, final double twist) {
b081e34b 93 robotDrive.arcadeDrive(thrust, twist, true);
cc42bd52
ME
94 }
95
96 public void stop() {
97 setMotorValues(0, 0);
98 }
99
f0a71840
CZ
100 public double getLeftMotorVal() {
101 return (frontLeft.get() + rearLeft.get()) / 2;
cc42bd52
ME
102 }
103
f0a71840
CZ
104 public double getRightMotorVal() {
105 return (frontRight.get() + rearRight.get()) / 2;
cc42bd52
ME
106 }
107
cc42bd52
ME
108 // ENCODER METHODS
109
3a5d9ac7 110 public double getLeftEncoderDistance() {
cc42bd52
ME
111 return leftEncoder.getDistance();
112 }
113
3a5d9ac7 114 public double getRightEncoderDistance() {
cc42bd52
ME
115 return rightEncoder.getDistance();
116 }
117
8e4c083c 118 public void printEncoderOutput() {
174f415c
CZ
119 System.out.println("left: " + getLeftEncoderDistance());
120 System.out.println("right: " + getRightEncoderDistance());
c846487e 121 // System.out.println(getAvgEncoderDistance());
8e4c083c
CZ
122 }
123
cc42bd52
ME
124 public double getAvgEncoderDistance() {
125 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
126 }
127
128 public void resetEncoders() {
129 leftEncoder.reset();
130 rightEncoder.reset();
131 }
132
133 public double getLeftSpeed() {
134 return leftEncoder.getRate();
135 }
136
137 public double getRightSpeed() {
138 return rightEncoder.getRate();
139 }
140
141 public double getSpeed() {
142 return (getLeftSpeed() + getRightSpeed()) / 2.0;
143 }
144
e12d6901
CZ
145 // ------Gyro------//
146 public double getAngle() {
c846487e 147 return this.imu.getAngle();
e12d6901
CZ
148 }
149
150 public void resetGyro() {
571fb5c9 151 this.imu.reset();
e12d6901
CZ
152 }
153
73e8adbc
AD
154 /*
155 * @return a value that is the current setpoint for the piston kReverse or
156 * KForward
157 */
06415bd8 158 public Value getLeftGearPistonValue() {
73e8adbc
AD
159 return leftGearPiston.get();
160 }
161
06415bd8
AD
162 /*
163 * @return a value that is the current setpoint for the piston kReverse or
164 * KForward
165 */
166 public Value getRightGearPistonValue() {
167 return rightGearPiston.get();
168 }
169
73e8adbc
AD
170 /*
171 * Changes the ball shift gear assembly to high
172 */
173 public void setHighGear() {
174 changeGear(Constants.DriveTrain.HIGH_GEAR);
175 }
176
177 /*
178 * Changes the ball shift gear assembly to low
179 */
180 public void setLowGear() {
181 changeGear(Constants.DriveTrain.LOW_GEAR);
182 }
183
184 /*
185 * Changes the gear to a DoubleSolenoid.Value
186 */
187 private void changeGear(DoubleSolenoid.Value gear) {
b70398a7 188 System.out.println(gear);
73e8adbc
AD
189 leftGearPiston.set(gear);
190 rightGearPiston.set(gear);
e12d6901
CZ
191 }
192
cc42bd52
ME
193 @Override
194 protected void initDefaultCommand() {
195 setDefaultCommand(new JoystickDrive());
196 }
38a404b3 197}