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