Create constants for gear piston in 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;
04f2cb52 5
a1c76caf
CZ
6import com.ctre.CANTalon;
7
bf921ece 8import edu.wpi.first.wpilibj.ADXRS450_Gyro;
4a75c629 9import edu.wpi.first.wpilibj.DoubleSolenoid;
73e8adbc 10import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
fa4e4a97 11import edu.wpi.first.wpilibj.Encoder;
ccbc35ae 12import edu.wpi.first.wpilibj.RobotDrive;
38a404b3
KZ
13import edu.wpi.first.wpilibj.command.Subsystem;
14
15public class DriveTrain extends Subsystem {
571fb5c9
CZ
16 public static double driveP = 0.006, driveI = 0.001, driveD = -0.002;
17 public static double defaultGyroP = 0.004, defaultGyroI = 0.0013,
18 defaultGyroD = -0.005;
e12d6901
CZ
19 private double gyroZero = 0;
20
b634ebbc
CZ
21 public static final double WHEEL_DIAMETER = 6; // inches
22 public static final int ENCODER_PULSES_PER_REVOLUTION = 256;
23 public static final double INCHES_PER_PULSE = WHEEL_DIAMETER * Math.PI
24 / ENCODER_PULSES_PER_REVOLUTION;
25
cc42bd52
ME
26 private static DriveTrain driveTrain;
27 private final CANTalon frontLeft, frontRight, rearLeft, rearRight;
28 private final RobotDrive robotDrive;
29 private final Encoder leftEncoder, rightEncoder;
73e8adbc 30 private final DoubleSolenoid leftGearPiston, rightGearPiston;
cc42bd52 31
bf921ece 32 private ADXRS450_Gyro imu;
e12d6901 33
cc42bd52
ME
34 private DriveTrain() {
35 // MOTOR CONTROLLERS
36 frontLeft = new CANTalon(Constants.DriveTrain.FRONT_LEFT);
37 frontRight = new CANTalon(Constants.DriveTrain.FRONT_RIGHT);
38 rearLeft = new CANTalon(Constants.DriveTrain.REAR_LEFT);
39 rearRight = new CANTalon(Constants.DriveTrain.REAR_RIGHT);
40
41 // ENCODERS
42 leftEncoder = new Encoder(Constants.DriveTrain.ENCODER_LEFT_A,
3e2738c4 43 Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X);
cc42bd52 44 rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A,
3e2738c4 45 Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X);
cc42bd52 46
b634ebbc
CZ
47 leftEncoder.setDistancePerPulse(INCHES_PER_PULSE);
48 rightEncoder.setDistancePerPulse(INCHES_PER_PULSE);
cc42bd52
ME
49
50 // ROBOT DRIVE
51 robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
8e4c083c 52
bf921ece 53 this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT);
4a75c629
AD
54 shifter = DoubleSolenoid(10, Constants.DriveTrain.SHIFTER_FORWARD,
55 Constants.DriveTrain.SHIFTER_REVERSE);
73e8adbc
AD
56 leftGearPiston = new DoubleSolenoid(Constants.DriveTrain.MODULE_NUMBER,
57 Constants.DriveTrain.LEFT_FORWARD, Constants.DriveTrain.LEFT_REVERSE);
58 rightGearPiston = new DoubleSolenoid(Constants.DriveTrain.MODULE_NUMBER,
59 Constants.DriveTrain.RIGHT_FORWARD, Constants.DriveTrain.RIGHT_REVERSE);
cc42bd52
ME
60 }
61
62 public static DriveTrain getDriveTrain() {
63 if (driveTrain == null) {
64 driveTrain = new DriveTrain();
65 }
66 return driveTrain;
67 }
68
69 // DRIVE METHODS
70 public void setMotorValues(final double left, final double right) {
71 frontLeft.set(left);
72 rearLeft.set(left);
73
b081e34b
ME
74 frontRight.set(-right);
75 rearRight.set(-right);
cc42bd52
ME
76 }
77
78 public void joystickDrive(final double thrust, final double twist) {
b081e34b 79 robotDrive.arcadeDrive(thrust, twist, true);
cc42bd52
ME
80 }
81
82 public void stop() {
83 setMotorValues(0, 0);
84 }
85
f0a71840
CZ
86 public double getLeftMotorVal() {
87 return (frontLeft.get() + rearLeft.get()) / 2;
cc42bd52
ME
88 }
89
f0a71840
CZ
90 public double getRightMotorVal() {
91 return (frontRight.get() + rearRight.get()) / 2;
cc42bd52
ME
92 }
93
cc42bd52
ME
94 // ENCODER METHODS
95
3a5d9ac7 96 public double getLeftEncoderDistance() {
cc42bd52
ME
97 return leftEncoder.getDistance();
98 }
99
3a5d9ac7 100 public double getRightEncoderDistance() {
cc42bd52
ME
101 return rightEncoder.getDistance();
102 }
103
8e4c083c 104 public void printEncoderOutput() {
f0a71840
CZ
105 // System.out.println("left: " + getLeftEncoderDistance());
106 // System.out.println("right: " + getRightEncoderDistance());
107 System.out.println(getAvgEncoderDistance());
8e4c083c
CZ
108 }
109
cc42bd52
ME
110 public double getAvgEncoderDistance() {
111 return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2;
112 }
113
114 public void resetEncoders() {
115 leftEncoder.reset();
116 rightEncoder.reset();
117 }
118
119 public double getLeftSpeed() {
120 return leftEncoder.getRate();
121 }
122
123 public double getRightSpeed() {
124 return rightEncoder.getRate();
125 }
126
127 public double getSpeed() {
128 return (getLeftSpeed() + getRightSpeed()) / 2.0;
129 }
130
e12d6901
CZ
131 // ------Gyro------//
132 public double getAngle() {
bf921ece 133 return this.imu.getAngle() - this.gyroZero;
e12d6901
CZ
134 }
135
136 public void resetGyro() {
571fb5c9 137 this.imu.reset();
e12d6901
CZ
138 }
139
140 public double getZeroAngle() {
141 return this.gyroZero;
73e8adbc
AD
142 /*
143 * @return a value that is the current setpoint for the piston kReverse or
144 * KForward
145 */
146 public Value getLeftGearPistonValue() {
147 return leftGearPiston.get();
148 }
149
150 /*
151 * @return a value that is the current setppoint for the piston kReverse or
152 * kForward
153 */
154 public Value getRightGearPistonValue() {
155 return rightGearPiston.get();
156 }
157
158 /*
159 * Changes the ball shift gear assembly to high
160 */
161 public void setHighGear() {
162 changeGear(Constants.DriveTrain.HIGH_GEAR);
163 }
164
165 /*
166 * Changes the ball shift gear assembly to low
167 */
168 public void setLowGear() {
169 changeGear(Constants.DriveTrain.LOW_GEAR);
170 }
171
172 /*
173 * Changes the gear to a DoubleSolenoid.Value
174 */
175 private void changeGear(DoubleSolenoid.Value gear) {
176 leftGearPiston.set(gear);
177 rightGearPiston.set(gear);
e12d6901
CZ
178 }
179
cc42bd52
ME
180 @Override
181 protected void initDefaultCommand() {
182 setDefaultCommand(new JoystickDrive());
183 }
38a404b3
KZ
184
185}