Commit | Line | Data |
---|---|---|
38a404b3 KZ |
1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | ||
3 | import org.usfirst.frc.team3501.robot.Constants; | |
ac77a7b8 | 4 | import org.usfirst.frc.team3501.robot.MathLib; |
5483fde9 | 5 | import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive; |
04f2cb52 | 6 | |
a1c76caf CZ |
7 | import com.ctre.CANTalon; |
8 | ||
bf921ece | 9 | import edu.wpi.first.wpilibj.ADXRS450_Gyro; |
4a75c629 | 10 | import edu.wpi.first.wpilibj.DoubleSolenoid; |
73e8adbc | 11 | import edu.wpi.first.wpilibj.DoubleSolenoid.Value; |
fa4e4a97 | 12 | import edu.wpi.first.wpilibj.Encoder; |
ccbc35ae | 13 | import edu.wpi.first.wpilibj.RobotDrive; |
38a404b3 KZ |
14 | import edu.wpi.first.wpilibj.command.Subsystem; |
15 | ||
16 | public 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; | |
fc01fb0f CZ |
40 | private final DoubleSolenoid leftDriveTrainPiston, rightDriveTrainPiston; |
41 | private final DoubleSolenoid gearManipulatorPiston; | |
cc42bd52 | 42 | |
bf921ece | 43 | private ADXRS450_Gyro imu; |
e12d6901 | 44 | |
366f1cfd | 45 | public boolean shouldBeClimbing = false; |
cef1f36d | 46 | |
cc42bd52 ME |
47 | private DriveTrain() { |
48 | // MOTOR CONTROLLERS | |
49 | frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT); | |
50 | frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT); | |
51 | rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT); | |
52 | rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT); | |
53 | ||
54 | // ENCODERS | |
55 | leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A, | |
3e2738c4 | 56 | Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X); |
cc42bd52 | 57 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, |
3e2738c4 | 58 | Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X); |
cc42bd52 | 59 | |
b634ebbc CZ |
60 | leftEncoder.setDistancePerPulse(INCHES_PER_PULSE); |
61 | rightEncoder.setDistancePerPulse(INCHES_PER_PULSE); | |
cc42bd52 ME |
62 | |
63 | // ROBOT DRIVE | |
64 | robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight); | |
8e4c083c | 65 | |
bf921ece | 66 | this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT); |
af4491f7 | 67 | |
ca372ce8 | 68 | // TODO: Not sure if MODULE_NUMBER should be the same for both |
fc01fb0f CZ |
69 | leftDriveTrainPiston = new DoubleSolenoid( |
70 | Constants.DriveTrain.PISTON_MODULE, | |
ca372ce8 AD |
71 | Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD, |
72 | Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE); | |
fc01fb0f CZ |
73 | rightDriveTrainPiston = new DoubleSolenoid( |
74 | Constants.DriveTrain.PISTON_MODULE, | |
ca372ce8 AD |
75 | Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD, |
76 | Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE); | |
fc01fb0f CZ |
77 | |
78 | gearManipulatorPiston = new DoubleSolenoid( | |
79 | Constants.DriveTrain.PISTON_MODULE, | |
80 | Constants.DriveTrain.GEAR_MANIPULATOR_PISTON_FORWARD, | |
81 | Constants.DriveTrain.GEAR_MANIPULATOR_PISTON_REVERSE); | |
cc42bd52 ME |
82 | } |
83 | ||
84 | public static DriveTrain getDriveTrain() { | |
85 | if (driveTrain == null) { | |
86 | driveTrain = new DriveTrain(); | |
87 | } | |
88 | return driveTrain; | |
89 | } | |
90 | ||
91 | // DRIVE METHODS | |
ac77a7b8 | 92 | public void setMotorValues(double left, double right) { |
b70398a7 CZ |
93 | left = MathLib.restrictToRange(left, -1.0, 1.0); |
94 | right = MathLib.restrictToRange(right, -1.0, 1.0); | |
ac77a7b8 | 95 | |
cc42bd52 ME |
96 | frontLeft.set(left); |
97 | rearLeft.set(left); | |
98 | ||
b081e34b ME |
99 | frontRight.set(-right); |
100 | rearRight.set(-right); | |
cc42bd52 ME |
101 | } |
102 | ||
103 | public void joystickDrive(final double thrust, final double twist) { | |
b081e34b | 104 | robotDrive.arcadeDrive(thrust, twist, true); |
cc42bd52 ME |
105 | } |
106 | ||
107 | public void stop() { | |
108 | setMotorValues(0, 0); | |
109 | } | |
110 | ||
f0a71840 CZ |
111 | public double getLeftMotorVal() { |
112 | return (frontLeft.get() + rearLeft.get()) / 2; | |
cc42bd52 ME |
113 | } |
114 | ||
f0a71840 CZ |
115 | public double getRightMotorVal() { |
116 | return (frontRight.get() + rearRight.get()) / 2; | |
cc42bd52 ME |
117 | } |
118 | ||
cc42bd52 ME |
119 | // ENCODER METHODS |
120 | ||
3a5d9ac7 | 121 | public double getLeftEncoderDistance() { |
cc42bd52 ME |
122 | return leftEncoder.getDistance(); |
123 | } | |
124 | ||
3a5d9ac7 | 125 | public double getRightEncoderDistance() { |
cc42bd52 ME |
126 | return rightEncoder.getDistance(); |
127 | } | |
128 | ||
8e4c083c | 129 | public void printEncoderOutput() { |
174f415c CZ |
130 | System.out.println("left: " + getLeftEncoderDistance()); |
131 | System.out.println("right: " + getRightEncoderDistance()); | |
c846487e | 132 | // System.out.println(getAvgEncoderDistance()); |
8e4c083c CZ |
133 | } |
134 | ||
cc42bd52 ME |
135 | public double getAvgEncoderDistance() { |
136 | return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; | |
137 | } | |
138 | ||
139 | public void resetEncoders() { | |
140 | leftEncoder.reset(); | |
141 | rightEncoder.reset(); | |
142 | } | |
143 | ||
144 | public double getLeftSpeed() { | |
145 | return leftEncoder.getRate(); | |
146 | } | |
147 | ||
148 | public double getRightSpeed() { | |
149 | return rightEncoder.getRate(); | |
150 | } | |
151 | ||
152 | public double getSpeed() { | |
153 | return (getLeftSpeed() + getRightSpeed()) / 2.0; | |
154 | } | |
155 | ||
e12d6901 CZ |
156 | // ------Gyro------// |
157 | public double getAngle() { | |
c846487e | 158 | return this.imu.getAngle(); |
e12d6901 CZ |
159 | } |
160 | ||
161 | public void resetGyro() { | |
571fb5c9 | 162 | this.imu.reset(); |
e12d6901 CZ |
163 | } |
164 | ||
73e8adbc AD |
165 | /* |
166 | * @return a value that is the current setpoint for the piston kReverse or | |
167 | * KForward | |
168 | */ | |
fc01fb0f CZ |
169 | public Value getLeftDriveTrainPiston() { |
170 | return leftDriveTrainPiston.get(); | |
73e8adbc AD |
171 | } |
172 | ||
06415bd8 AD |
173 | /* |
174 | * @return a value that is the current setpoint for the piston kReverse or | |
175 | * KForward | |
176 | */ | |
fc01fb0f CZ |
177 | public Value getRightDriveTrainPiston() { |
178 | return rightDriveTrainPiston.get(); | |
06415bd8 AD |
179 | } |
180 | ||
73e8adbc AD |
181 | /* |
182 | * Changes the ball shift gear assembly to high | |
183 | */ | |
184 | public void setHighGear() { | |
fc01fb0f | 185 | changeGear(Constants.DriveTrain.FORWARD_PISTON_VALUE); |
73e8adbc AD |
186 | } |
187 | ||
188 | /* | |
189 | * Changes the ball shift gear assembly to low | |
190 | */ | |
191 | public void setLowGear() { | |
fc01fb0f | 192 | changeGear(Constants.DriveTrain.REVERSE_PISTON_VALUE); |
73e8adbc AD |
193 | } |
194 | ||
195 | /* | |
196 | * Changes the gear to a DoubleSolenoid.Value | |
197 | */ | |
198 | private void changeGear(DoubleSolenoid.Value gear) { | |
fc01fb0f CZ |
199 | leftDriveTrainPiston.set(gear); |
200 | rightDriveTrainPiston.set(gear); | |
201 | } | |
202 | ||
203 | public Value getGearManipulatorPistonValue() { | |
204 | return gearManipulatorPiston.get(); | |
205 | } | |
206 | ||
207 | public void extendGearManipulatorPiston() { | |
208 | gearManipulatorPiston.set(Constants.DriveTrain.FORWARD_PISTON_VALUE); | |
209 | } | |
210 | ||
211 | public void retractGearManipulatorPiston() { | |
212 | gearManipulatorPiston.set(Constants.DriveTrain.REVERSE_PISTON_VALUE); | |
e12d6901 CZ |
213 | } |
214 | ||
cc42bd52 ME |
215 | @Override |
216 | protected void initDefaultCommand() { | |
217 | setDefaultCommand(new JoystickDrive()); | |
218 | } | |
f56e6ebf CZ |
219 | |
220 | public void setCANTalonsBrakeMode(boolean mode) { | |
221 | frontLeft.enableBrakeMode(mode); | |
222 | rearLeft.enableBrakeMode(mode); | |
223 | ||
224 | frontRight.enableBrakeMode(mode); | |
225 | rearRight.enableBrakeMode(mode); | |
226 | } | |
38a404b3 | 227 | } |