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