code review changes and add code for braking cantalons
[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
f56e6ebf
CZ
32 public static final boolean DRIVE_BRAKE_MODE = true;
33 public static final boolean DRIVE_COAST_MODE = false;
34
cc42bd52 35 private static DriveTrain driveTrain;
b8791ca4 36
cc42bd52
ME
37 private final CANTalon frontLeft, frontRight, rearLeft, rearRight;
38 private final RobotDrive robotDrive;
39 private final Encoder leftEncoder, rightEncoder;
73e8adbc 40 private final DoubleSolenoid leftGearPiston, rightGearPiston;
cc42bd52 41
bf921ece 42 private ADXRS450_Gyro imu;
e12d6901 43
366f1cfd 44 public boolean shouldBeClimbing = false;
cef1f36d 45
cc42bd52
ME
46 private DriveTrain() {
47 // MOTOR CONTROLLERS
48 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
49 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
50 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
51 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
52
53 // ENCODERS
54 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
3e2738c4 55 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
cc42bd52 56 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
3e2738c4 57 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
cc42bd52 58
b634ebbc
CZ
59 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
60 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
cc42bd52
ME
61
62 // ROBOT DRIVE
63 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
8e4c083c 64
bf921ece 65 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
af4491f7 66
ca372ce8 67 // TODO: Not sure if MODULE_NUMBER should be the same for both
58bcc21d 68 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
69 Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD,
70 Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE);
58bcc21d 71 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
72 Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD,
73 Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE);
cc42bd52
ME
74 }
75
76 public static DriveTrain getDriveTrain() {
77 if (driveTrain == null) {
78 driveTrain = new DriveTrain();
79 }
80 return driveTrain;
81 }
82
83 // DRIVE METHODS
ac77a7b8 84 public void setMotorValues(double left, double right) {
b70398a7
CZ
85 left = MathLib.restrictToRange(left, -1.0, 1.0);
86 right = MathLib.restrictToRange(right, -1.0, 1.0);
ac77a7b8 87
cc42bd52
ME
88 frontLeft.set(left);
89 rearLeft.set(left);
90
b081e34b
ME
91 frontRight.set(-right);
92 rearRight.set(-right);
cc42bd52
ME
93 }
94
95 public void joystickDrive(final double thrust, final double twist) {
b081e34b 96 robotDrive.arcadeDrive(thrust, twist, true);
cc42bd52
ME
97 }
98
99 public void stop() {
100 setMotorValues(0, 0);
101 }
102
f0a71840
CZ
103 public double getLeftMotorVal() {
104 return (frontLeft.get() + rearLeft.get()) / 2;
cc42bd52
ME
105 }
106
f0a71840
CZ
107 public double getRightMotorVal() {
108 return (frontRight.get() + rearRight.get()) / 2;
cc42bd52
ME
109 }
110
cc42bd52
ME
111 // ENCODER METHODS
112
3a5d9ac7 113 public double getLeftEncoderDistance() {
cc42bd52
ME
114 return leftEncoder.getDistance();
115 }
116
3a5d9ac7 117 public double getRightEncoderDistance() {
cc42bd52
ME
118 return rightEncoder.getDistance();
119 }
120
8e4c083c 121 public void printEncoderOutput() {
174f415c
CZ
122 System.out.println("left: " + getLeftEncoderDistance());
123 System.out.println("right: " + getRightEncoderDistance());
c846487e 124 // System.out.println(getAvgEncoderDistance());
8e4c083c
CZ
125 }
126
cc42bd52
ME
127 public double getAvgEncoderDistance() {
128 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
129 }
130
131 public void resetEncoders() {
132 leftEncoder.reset();
133 rightEncoder.reset();
134 }
135
136 public double getLeftSpeed() {
137 return leftEncoder.getRate();
138 }
139
140 public double getRightSpeed() {
141 return rightEncoder.getRate();
142 }
143
144 public double getSpeed() {
145 return (getLeftSpeed() + getRightSpeed()) / 2.0;
146 }
147
e12d6901
CZ
148 // ------Gyro------//
149 public double getAngle() {
c846487e 150 return this.imu.getAngle();
e12d6901
CZ
151 }
152
153 public void resetGyro() {
571fb5c9 154 this.imu.reset();
e12d6901
CZ
155 }
156
73e8adbc
AD
157 /*
158 * @return a value that is the current setpoint for the piston kReverse or
159 * KForward
160 */
06415bd8 161 public Value getLeftGearPistonValue() {
73e8adbc
AD
162 return leftGearPiston.get();
163 }
164
06415bd8
AD
165 /*
166 * @return a value that is the current setpoint for the piston kReverse or
167 * KForward
168 */
169 public Value getRightGearPistonValue() {
170 return rightGearPiston.get();
171 }
172
73e8adbc
AD
173 /*
174 * Changes the ball shift gear assembly to high
175 */
176 public void setHighGear() {
177 changeGear(Constants.DriveTrain.HIGH_GEAR);
178 }
179
180 /*
181 * Changes the ball shift gear assembly to low
182 */
183 public void setLowGear() {
184 changeGear(Constants.DriveTrain.LOW_GEAR);
185 }
186
187 /*
188 * Changes the gear to a DoubleSolenoid.Value
189 */
190 private void changeGear(DoubleSolenoid.Value gear) {
191 leftGearPiston.set(gear);
192 rightGearPiston.set(gear);
e12d6901
CZ
193 }
194
cc42bd52
ME
195 @Override
196 protected void initDefaultCommand() {
197 setDefaultCommand(new JoystickDrive());
198 }
f56e6ebf
CZ
199
200 public void setCANTalonsBrakeMode(boolean mode) {
201 frontLeft.enableBrakeMode(mode);
202 rearLeft.enableBrakeMode(mode);
203
204 frontRight.enableBrakeMode(mode);
205 rearRight.enableBrakeMode(mode);
206 }
38a404b3 207}