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