update code
[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 = 0.004, turnI = 0.0013, turnD = -0.005;
20 public static double driveStraightGyroP = 0.01;
21
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;
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
31 private static DriveTrain driveTrain;
32
33 private final CANTalon frontLeft, frontRight, rearLeft, rearRight;
34 private final RobotDrive robotDrive;
35 private final Encoder leftEncoder, rightEncoder;
36 private final DoubleSolenoid leftGearPiston, rightGearPiston;
37
38 private ADXRS450_Gyro imu;
39
40 public boolean shouldBeClimbing = false;
41
42 private PIDController driveController;
43 private PIDController gyroController;
44
45 private DriveTrain() {
46
47 driveController = new PIDController(driveP, driveI, driveD);
48
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,
57 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
58 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
59 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
60
61 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
62 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
63
64 // ROBOT DRIVE
65 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
66
67 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
68
69 // TODO: Not sure if MODULE_NUMBER should be the same for both
70 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
71 Constants.DriveTrain.LEFT_GEAR_PISTON_FORWARD,
72 Constants.DriveTrain.LEFT_GEAR_PISTON_REVERSE);
73 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.PISTON_MODULE,
74 Constants.DriveTrain.RIGHT_GEAR_PISTON_FORWARD,
75 Constants.DriveTrain.RIGHT_GEAR_PISTON_REVERSE);
76 }
77
78 public PIDController getDriveController() {
79 return this.driveController;
80 }
81
82 public static DriveTrain getDriveTrain() {
83 if (driveTrain == null) {
84 driveTrain = new DriveTrain();
85 }
86 return driveTrain;
87 }
88
89 // DRIVE METHODS
90 public void setMotorValues(final double left, final double right) {
91 frontLeft.set(left);
92 rearLeft.set(left);
93
94 frontRight.set(-right);
95 rearRight.set(-right);
96 }
97
98 public void joystickDrive(final double thrust, final double twist) {
99 robotDrive.arcadeDrive(thrust, twist, true);
100 }
101
102 public void stop() {
103 setMotorValues(0, 0);
104 }
105
106 public double getLeftMotorVal() {
107 return (frontLeft.get() + rearLeft.get()) / 2;
108 }
109
110 public double getRightMotorVal() {
111 return (frontRight.get() + rearRight.get()) / 2;
112 }
113
114 // ENCODER METHODS
115
116 public double getLeftEncoderDistance() {
117 return leftEncoder.getDistance();
118 }
119
120 public double getRightEncoderDistance() {
121 return rightEncoder.getDistance();
122 }
123
124 public void printEncoderOutput() {
125 System.out.println("left: " + getLeftEncoderDistance());
126 System.out.println("right: " + getRightEncoderDistance());
127 // System.out.println(getAvgEncoderDistance());
128 }
129
130 public double getAvgEncoderDistance() {
131 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
132 }
133
134 public void resetEncoders() {
135 leftEncoder.reset();
136 rightEncoder.reset();
137 }
138
139 public double getLeftSpeed() {
140 return leftEncoder.getRate();
141 }
142
143 public double getRightSpeed() {
144 return rightEncoder.getRate();
145 }
146
147 public double getSpeed() {
148 return (getLeftSpeed() + getRightSpeed()) / 2.0;
149 }
150
151 // ------Gyro------//
152 public double getAngle() {
153 return this.imu.getAngle();
154 }
155
156 public void resetGyro() {
157 this.imu.reset();
158 }
159
160 /*
161 * @return a value that is the current setpoint for the piston kReverse or
162 * KForward
163 */
164 public Value getLeftGearPistonValue() {
165 return leftGearPiston.get();
166 }
167
168 /*
169 * @return a value that is the current setpoint for the piston kReverse or
170 * KForward
171 */
172 public Value getRightGearPistonValue() {
173 return rightGearPiston.get();
174 }
175
176 /*
177 * Changes the ball shift gear assembly to high
178 */
179 public void setHighGear() {
180 changeGear(Constants.DriveTrain.HIGH_GEAR);
181 }
182
183 /*
184 * Changes the ball shift gear assembly to low
185 */
186 public void setLowGear() {
187 changeGear(Constants.DriveTrain.LOW_GEAR);
188 }
189
190 /*
191 * Changes the gear to a DoubleSolenoid.Value
192 */
193 private void changeGear(DoubleSolenoid.Value gear) {
194 leftGearPiston.set(gear);
195 rightGearPiston.set(gear);
196 }
197
198 @Override
199 protected void initDefaultCommand() {
200 setDefaultCommand(new JoystickDrive());
201 }
202 }