fix alot of unnecessary/not used code and init gyroController
[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;
542234fe 19 public static double turnP, turnI, turnD;
c846487e 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 42 private PIDController driveController;
01ea2879 43 private PIDController gyroController;
06df4cd4 44
cc42bd52 45 private DriveTrain() {
06df4cd4 46
9ffd117a 47 driveController = new PIDController(driveP, driveI, driveD);
542234fe 48 gyroController = new PIDController(turnP, turnI, turnD);
06df4cd4 49
cc42bd52
ME
50 // MOTOR CONTROLLERS
51 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
52 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
53 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
54 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
55
56 // ENCODERS
57 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
3e2738c4 58 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
cc42bd52 59 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
3e2738c4 60 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
cc42bd52 61
b634ebbc
CZ
62 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
63 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
cc42bd52
ME
64
65 // ROBOT DRIVE
66 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
8e4c083c 67
bf921ece 68 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
af4491f7 69
ca372ce8 70 // TODO: Not sure if MODULE_NUMBER should be the same for both
58bcc21d 71 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
72 Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD,
73 Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE);
58bcc21d 74 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
ca372ce8
AD
75 Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD,
76 Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE);
cc42bd52
ME
77 }
78
06df4cd4
E
79 public PIDController getDriveController() {
80 return this.driveController;
81 }
82
90b435ce
ES
83 public PIDController getGyroController() {
84 return this.gyroController;
85 }
86
cc42bd52
ME
87 public static DriveTrain getDriveTrain() {
88 if (driveTrain == null) {
89 driveTrain = new DriveTrain();
90 }
91 return driveTrain;
92 }
93
94 // DRIVE METHODS
95 public void setMotorValues(final double left, final double right) {
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 */
06415bd8 169 public Value getLeftGearPistonValue() {
73e8adbc
AD
170 return leftGearPiston.get();
171 }
172
06415bd8
AD
173 /*
174 * @return a value that is the current setpoint for the piston kReverse or
175 * KForward
176 */
177 public Value getRightGearPistonValue() {
178 return rightGearPiston.get();
179 }
180
73e8adbc
AD
181 /*
182 * Changes the ball shift gear assembly to high
183 */
184 public void setHighGear() {
185 changeGear(Constants.DriveTrain.HIGH_GEAR);
186 }
187
188 /*
189 * Changes the ball shift gear assembly to low
190 */
191 public void setLowGear() {
192 changeGear(Constants.DriveTrain.LOW_GEAR);
193 }
194
195 /*
196 * Changes the gear to a DoubleSolenoid.Value
197 */
198 private void changeGear(DoubleSolenoid.Value gear) {
199 leftGearPiston.set(gear);
200 rightGearPiston.set(gear);
e12d6901
CZ
201 }
202
cc42bd52
ME
203 @Override
204 protected void initDefaultCommand() {
205 setDefaultCommand(new JoystickDrive());
206 }
38a404b3 207}