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