fix bugs
[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 import org.usfirst.frc.team3501.robot.utils.PIDController;
6
7 import com.ctre.CANTalon;
8
9 import edu.wpi.first.wpilibj.ADXRS450_Gyro;
10 import edu.wpi.first.wpilibj.DoubleSolenoid;
11 import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
12 import edu.wpi.first.wpilibj.Encoder;
13 import edu.wpi.first.wpilibj.RobotDrive;
14 import edu.wpi.first.wpilibj.command.Subsystem;
15
16 public class DriveTrain extends Subsystem {
17
18 public static double driveP, driveI, driveD;
19 public static double turnP, turnI, turnD;
20
21 public static double driveStraightGyroP = 0.01;
22
23 public static final String DRIVE_P_Val = "DriveP";
24 public static final String DRIVE_I_Val = "DriveI";
25 public static final String DRIVE_D_Val = "DriveD";
26 public static final String DRIVE_TARGET_DIST = "SET_DIST";
27 public static final String DRIVE_MOTOR_VAL = "SPEED";
28 public static final String GYRO_P_Val = "GyroP";
29 public static final String GYRO_I_Val = "GyroI";
30 public static final String GYRO_D_Val = "GyroD";
31 public static final String GYRO_TARGET_ANGLE = "SET_ANGLE";
32 public static final int PID_ERROR = -1;
33 public static final int TARGET_DISTANCE_ERROR = -1;
34
35 public static final double WHEEL_DIAMETER = 6; // inches
36 public static final int ENCODER_PULSES_PER_REVOLUTION = 256;
37 public static final double INCHES_PER_PULSE = WHEEL_DIAMETER * Math.PI
38 / ENCODER_PULSES_PER_REVOLUTION;
39
40 public static final double MAINTAIN_CLIMBED_POSITION = 0;
41 public static final double TIME_TO_CLIMB_FOR = 0;
42 public static final double CLIMBER_SPEED = 0;
43
44 private static DriveTrain driveTrain;
45
46 private final CANTalon frontLeft, frontRight, rearLeft, rearRight;
47 private final RobotDrive robotDrive;
48 private final Encoder leftEncoder, rightEncoder;
49 private final DoubleSolenoid leftGearPiston, rightGearPiston;
50
51 private ADXRS450_Gyro imu;
52
53 public boolean shouldBeClimbing = false;
54
55 private PIDController driveController;
56 private PIDController gyroController;
57
58 private boolean isClimbing;
59 private static double CLIMBER_SPEED;;
60 public boolean shouldBeClimbing = false;
61
62 private DriveTrain() {
63
64 driveController = new PIDController(driveP, driveI, driveD);
65 gyroController = new PIDController(turnP, turnI, turnD);
66
67 // PID TUNING
68
69 // MOTOR CONTROLLERS
70 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
71 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
72 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
73 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
74
75 // ENCODERS
76 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
77 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
78 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
79 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
80
81 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
82 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
83
84 // ROBOT DRIVE
85 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
86
87 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
88
89 // TODO: Not sure if MODULE_NUMBER should be the same for both
90 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
91 Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD,
92 Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE);
93 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
94 Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD,
95 Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE);
96 }
97
98 public PIDController getDriveController() {
99 return this.driveController;
100 }
101
102 public PIDController getGyroController() {
103 return this.gyroController;
104 }
105
106 public static DriveTrain getDriveTrain() {
107 if (driveTrain == null) {
108 driveTrain = new DriveTrain();
109 }
110 return driveTrain;
111 }
112
113 // DRIVE METHODS
114 public void setMotorValues(final double left, final double right) {
115 frontLeft.set(left);
116 rearLeft.set(left);
117
118 frontRight.set(-right);
119 rearRight.set(-right);
120 }
121
122 public void joystickDrive(final double thrust, final double twist) {
123 robotDrive.arcadeDrive(thrust, twist, true);
124 }
125
126 public void stop() {
127 setMotorValues(0, 0);
128 }
129
130 public double getLeftMotorVal() {
131 return (frontLeft.get() + rearLeft.get()) / 2;
132 }
133
134 public double getRightMotorVal() {
135 return (frontRight.get() + rearRight.get()) / 2;
136 }
137
138 // ENCODER METHODS
139
140 public double getLeftEncoderDistance() {
141 return leftEncoder.getDistance();
142 }
143
144 public double getRightEncoderDistance() {
145 return rightEncoder.getDistance();
146 }
147
148 public void printEncoderOutput() {
149 System.out.println("left: " + getLeftEncoderDistance());
150 System.out.println("right: " + getRightEncoderDistance());
151 // System.out.println(getAvgEncoderDistance());
152 }
153
154 public double getAvgEncoderDistance() {
155 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
156 }
157
158 public void resetEncoders() {
159 leftEncoder.reset();
160 rightEncoder.reset();
161 }
162
163 public double getLeftSpeed() {
164 return leftEncoder.getRate();
165 }
166
167 public double getRightSpeed() {
168 return rightEncoder.getRate();
169 }
170
171 public double getSpeed() {
172 return (getLeftSpeed() + getRightSpeed()) / 2.0;
173 }
174
175 // ------Gyro------//
176 public double getAngle() {
177 return this.imu.getAngle();
178 }
179
180 public void resetGyro() {
181 this.imu.reset();
182 }
183
184 /*
185 * @return a value that is the current setpoint for the piston kReverse or
186 * KForward
187 */
188 public Value getLeftGearPistonValue() {
189 return leftGearPiston.get();
190 }
191
192 /*
193 * @return a value that is the current setpoint for the piston kReverse or
194 * KForward
195 */
196 public Value getRightGearPistonValue() {
197 return rightGearPiston.get();
198 }
199
200 /*
201 * Changes the ball shift gear assembly to high
202 */
203 public void setHighGear() {
204 changeGear(Constants.DriveTrain.HIGH_GEAR);
205 }
206
207 /*
208 * Changes the ball shift gear assembly to low
209 */
210 public void setLowGear() {
211 changeGear(Constants.DriveTrain.LOW_GEAR);
212 }
213
214 /*
215 * Changes the gear to a DoubleSolenoid.Value
216 */
217 private void changeGear(DoubleSolenoid.Value gear) {
218 leftGearPiston.set(gear);
219 rightGearPiston.set(gear);
220 }
221
222 @Override
223 protected void initDefaultCommand() {
224 setDefaultCommand(new JoystickDrive());
225 }
226 }