Commit | Line | Data |
---|---|---|
38a404b3 KZ |
1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | ||
3 | import org.usfirst.frc.team3501.robot.Constants; | |
5483fde9 | 4 | import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive; |
06df4cd4 | 5 | import org.usfirst.frc.team3501.robot.utils.PIDController; |
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 { | |
34ffcd03 ES |
17 | |
18 | public static double driveP, driveI, driveD; | |
c846487e CZ |
19 | public static double turnP = 0.004, turnI = 0.0013, turnD = -0.005; |
20 | public static double driveStraightGyroP = 0.01; | |
e12d6901 | 21 | |
b634ebbc CZ |
22 | public static final double WHEEL_DIAMETER = 6; // inches |
23 | public static final int ENCODER_PULSES_PER_REVOLUTION = 256; | |
24 | public static final double INCHES_PER_PULSE = WHEEL_DIAMETER * Math.PI | |
25 | / ENCODER_PULSES_PER_REVOLUTION; | |
9dc69158 CZ |
26 | |
27 | public static final double MAINTAIN_CLIMBED_POSITION = 0; | |
28 | public static final double TIME_TO_CLIMB_FOR = 0; | |
29 | public static final double CLIMBER_SPEED = 0; | |
30 | ||
cc42bd52 | 31 | private static DriveTrain driveTrain; |
b8791ca4 | 32 | |
cc42bd52 ME |
33 | private final CANTalon frontLeft, frontRight, rearLeft, rearRight; |
34 | private final RobotDrive robotDrive; | |
35 | private final Encoder leftEncoder, rightEncoder; | |
73e8adbc | 36 | private final DoubleSolenoid leftGearPiston, rightGearPiston; |
cc42bd52 | 37 | |
bf921ece | 38 | private ADXRS450_Gyro imu; |
e12d6901 | 39 | |
366f1cfd | 40 | public boolean shouldBeClimbing = false; |
cef1f36d | 41 | |
06df4cd4 E |
42 | private PIDController driveController; |
43 | ||
cc42bd52 | 44 | private DriveTrain() { |
06df4cd4 | 45 | |
9ffd117a | 46 | driveController = new PIDController(driveP, driveI, driveD); |
06df4cd4 | 47 | |
cc42bd52 ME |
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 |
58bcc21d | 69 | leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE, |
ca372ce8 AD |
70 | Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD, |
71 | Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE); | |
58bcc21d | 72 | rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE, |
ca372ce8 AD |
73 | Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD, |
74 | Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE); | |
cc42bd52 ME |
75 | } |
76 | ||
06df4cd4 E |
77 | public PIDController getDriveController() { |
78 | return this.driveController; | |
79 | } | |
80 | ||
cc42bd52 ME |
81 | public static DriveTrain getDriveTrain() { |
82 | if (driveTrain == null) { | |
83 | driveTrain = new DriveTrain(); | |
84 | } | |
85 | return driveTrain; | |
86 | } | |
87 | ||
88 | // DRIVE METHODS | |
89 | public void setMotorValues(final double left, final double right) { | |
90 | frontLeft.set(left); | |
91 | rearLeft.set(left); | |
92 | ||
b081e34b ME |
93 | frontRight.set(-right); |
94 | rearRight.set(-right); | |
cc42bd52 ME |
95 | } |
96 | ||
97 | public void joystickDrive(final double thrust, final double twist) { | |
b081e34b | 98 | robotDrive.arcadeDrive(thrust, twist, true); |
cc42bd52 ME |
99 | } |
100 | ||
101 | public void stop() { | |
102 | setMotorValues(0, 0); | |
103 | } | |
104 | ||
f0a71840 CZ |
105 | public double getLeftMotorVal() { |
106 | return (frontLeft.get() + rearLeft.get()) / 2; | |
cc42bd52 ME |
107 | } |
108 | ||
f0a71840 CZ |
109 | public double getRightMotorVal() { |
110 | return (frontRight.get() + rearRight.get()) / 2; | |
cc42bd52 ME |
111 | } |
112 | ||
cc42bd52 ME |
113 | // ENCODER METHODS |
114 | ||
3a5d9ac7 | 115 | public double getLeftEncoderDistance() { |
cc42bd52 ME |
116 | return leftEncoder.getDistance(); |
117 | } | |
118 | ||
3a5d9ac7 | 119 | public double getRightEncoderDistance() { |
cc42bd52 ME |
120 | return rightEncoder.getDistance(); |
121 | } | |
122 | ||
8e4c083c | 123 | public void printEncoderOutput() { |
174f415c CZ |
124 | System.out.println("left: " + getLeftEncoderDistance()); |
125 | System.out.println("right: " + getRightEncoderDistance()); | |
c846487e | 126 | // System.out.println(getAvgEncoderDistance()); |
8e4c083c CZ |
127 | } |
128 | ||
cc42bd52 ME |
129 | public double getAvgEncoderDistance() { |
130 | return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; | |
131 | } | |
132 | ||
133 | public void resetEncoders() { | |
134 | leftEncoder.reset(); | |
135 | rightEncoder.reset(); | |
136 | } | |
137 | ||
138 | public double getLeftSpeed() { | |
139 | return leftEncoder.getRate(); | |
140 | } | |
141 | ||
142 | public double getRightSpeed() { | |
143 | return rightEncoder.getRate(); | |
144 | } | |
145 | ||
146 | public double getSpeed() { | |
147 | return (getLeftSpeed() + getRightSpeed()) / 2.0; | |
148 | } | |
149 | ||
e12d6901 CZ |
150 | // ------Gyro------// |
151 | public double getAngle() { | |
c846487e | 152 | return this.imu.getAngle(); |
e12d6901 CZ |
153 | } |
154 | ||
155 | public void resetGyro() { | |
571fb5c9 | 156 | this.imu.reset(); |
e12d6901 CZ |
157 | } |
158 | ||
73e8adbc AD |
159 | /* |
160 | * @return a value that is the current setpoint for the piston kReverse or | |
161 | * KForward | |
162 | */ | |
06415bd8 | 163 | public Value getLeftGearPistonValue() { |
73e8adbc AD |
164 | return leftGearPiston.get(); |
165 | } | |
166 | ||
06415bd8 AD |
167 | /* |
168 | * @return a value that is the current setpoint for the piston kReverse or | |
169 | * KForward | |
170 | */ | |
171 | public Value getRightGearPistonValue() { | |
172 | return rightGearPiston.get(); | |
173 | } | |
174 | ||
73e8adbc AD |
175 | /* |
176 | * Changes the ball shift gear assembly to high | |
177 | */ | |
178 | public void setHighGear() { | |
179 | changeGear(Constants.DriveTrain.HIGH_GEAR); | |
180 | } | |
181 | ||
182 | /* | |
183 | * Changes the ball shift gear assembly to low | |
184 | */ | |
185 | public void setLowGear() { | |
186 | changeGear(Constants.DriveTrain.LOW_GEAR); | |
187 | } | |
188 | ||
189 | /* | |
190 | * Changes the gear to a DoubleSolenoid.Value | |
191 | */ | |
192 | private void changeGear(DoubleSolenoid.Value gear) { | |
193 | leftGearPiston.set(gear); | |
194 | rightGearPiston.set(gear); | |
e12d6901 CZ |
195 | } |
196 | ||
cc42bd52 ME |
197 | @Override |
198 | protected void initDefaultCommand() { | |
199 | setDefaultCommand(new JoystickDrive()); | |
200 | } | |
38a404b3 | 201 | } |