Commit | Line | Data |
---|---|---|
38a404b3 KZ |
1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | ||
3 | import org.usfirst.frc.team3501.robot.Constants; | |
5483fde9 | 4 | import org.usfirst.frc.team3501.robot.commands.driving.JoystickDrive; |
9b35aa55 | 5 | import org.usfirst.frc.team3501.robot.utils.BNO055; |
04f2cb52 | 6 | |
a1c76caf CZ |
7 | import com.ctre.CANTalon; |
8 | ||
bf921ece | 9 | import edu.wpi.first.wpilibj.ADXRS450_Gyro; |
4a75c629 | 10 | import edu.wpi.first.wpilibj.DoubleSolenoid; |
73e8adbc | 11 | import edu.wpi.first.wpilibj.DoubleSolenoid.Value; |
fa4e4a97 | 12 | import edu.wpi.first.wpilibj.Encoder; |
174f415c | 13 | import edu.wpi.first.wpilibj.I2C.Port; |
ccbc35ae | 14 | import edu.wpi.first.wpilibj.RobotDrive; |
38a404b3 KZ |
15 | import edu.wpi.first.wpilibj.command.Subsystem; |
16 | ||
17 | public class DriveTrain extends Subsystem { | |
571fb5c9 CZ |
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; | |
9b35aa55 CZ |
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; | |
e12d6901 CZ |
24 | private double gyroZero = 0; |
25 | ||
b634ebbc CZ |
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 | ||
cc42bd52 ME |
31 | private static DriveTrain driveTrain; |
32 | private final CANTalon frontLeft, frontRight, rearLeft, rearRight; | |
33 | private final RobotDrive robotDrive; | |
34 | private final Encoder leftEncoder, rightEncoder; | |
73e8adbc | 35 | private final DoubleSolenoid leftGearPiston, rightGearPiston; |
cc42bd52 | 36 | |
bf921ece | 37 | private ADXRS450_Gyro imu; |
9b35aa55 | 38 | private BNO055 imu; |
e12d6901 | 39 | |
cc42bd52 ME |
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, | |
3e2738c4 | 49 | Constants.DriveTrain.ENCODER_LEFT_B, false, Encoder.EncodingType.k4X); |
cc42bd52 | 50 | rightEncoder = new Encoder(Constants.DriveTrain.ENCODER_RIGHT_A, |
3e2738c4 | 51 | Constants.DriveTrain.ENCODER_RIGHT_B, false, Encoder.EncodingType.k4X); |
cc42bd52 | 52 | |
b634ebbc CZ |
53 | leftEncoder.setDistancePerPulse(INCHES_PER_PULSE); |
54 | rightEncoder.setDistancePerPulse(INCHES_PER_PULSE); | |
cc42bd52 ME |
55 | |
56 | // ROBOT DRIVE | |
57 | robotDrive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight); | |
8e4c083c | 58 | |
bf921ece | 59 | this.imu = new ADXRS450_Gyro(Constants.DriveTrain.GYRO_PORT); |
4a75c629 AD |
60 | shifter = DoubleSolenoid(10, Constants.DriveTrain.SHIFTER_FORWARD, |
61 | Constants.DriveTrain.SHIFTER_REVERSE); | |
73e8adbc AD |
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); | |
174f415c CZ |
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(); | |
af4491f7 | 70 | |
cc42bd52 ME |
71 | } |
72 | ||
73 | public static DriveTrain getDriveTrain() { | |
74 | if (driveTrain == null) { | |
75 | driveTrain = new DriveTrain(); | |
76 | } | |
77 | return driveTrain; | |
78 | } | |
79 | ||
80 | // DRIVE METHODS | |
81 | public void setMotorValues(final double left, final double right) { | |
82 | frontLeft.set(left); | |
83 | rearLeft.set(left); | |
84 | ||
b081e34b ME |
85 | frontRight.set(-right); |
86 | rearRight.set(-right); | |
cc42bd52 ME |
87 | } |
88 | ||
89 | public void joystickDrive(final double thrust, final double twist) { | |
b081e34b | 90 | robotDrive.arcadeDrive(thrust, twist, true); |
cc42bd52 ME |
91 | } |
92 | ||
93 | public void stop() { | |
94 | setMotorValues(0, 0); | |
95 | } | |
96 | ||
f0a71840 CZ |
97 | public double getLeftMotorVal() { |
98 | return (frontLeft.get() + rearLeft.get()) / 2; | |
cc42bd52 ME |
99 | } |
100 | ||
f0a71840 CZ |
101 | public double getRightMotorVal() { |
102 | return (frontRight.get() + rearRight.get()) / 2; | |
cc42bd52 ME |
103 | } |
104 | ||
cc42bd52 ME |
105 | // ENCODER METHODS |
106 | ||
3a5d9ac7 | 107 | public double getLeftEncoderDistance() { |
cc42bd52 ME |
108 | return leftEncoder.getDistance(); |
109 | } | |
110 | ||
3a5d9ac7 | 111 | public double getRightEncoderDistance() { |
cc42bd52 ME |
112 | return rightEncoder.getDistance(); |
113 | } | |
114 | ||
8e4c083c | 115 | public void printEncoderOutput() { |
f0a71840 CZ |
116 | // System.out.println("left: " + getLeftEncoderDistance()); |
117 | // System.out.println("right: " + getRightEncoderDistance()); | |
118 | System.out.println(getAvgEncoderDistance()); | |
174f415c CZ |
119 | System.out.println("left: " + getLeftEncoderDistance()); |
120 | System.out.println("right: " + getRightEncoderDistance()); | |
8e4c083c CZ |
121 | } |
122 | ||
cc42bd52 ME |
123 | public double getAvgEncoderDistance() { |
124 | return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; | |
125 | } | |
126 | ||
127 | public void resetEncoders() { | |
128 | leftEncoder.reset(); | |
129 | rightEncoder.reset(); | |
130 | } | |
131 | ||
132 | public double getLeftSpeed() { | |
133 | return leftEncoder.getRate(); | |
134 | } | |
135 | ||
136 | public double getRightSpeed() { | |
137 | return rightEncoder.getRate(); | |
138 | } | |
139 | ||
140 | public double getSpeed() { | |
141 | return (getLeftSpeed() + getRightSpeed()) / 2.0; | |
142 | } | |
143 | ||
e12d6901 CZ |
144 | // ------Gyro------// |
145 | public double getAngle() { | |
bf921ece | 146 | return this.imu.getAngle() - this.gyroZero; |
e12d6901 CZ |
147 | } |
148 | ||
149 | public void resetGyro() { | |
571fb5c9 | 150 | this.imu.reset(); |
af4491f7 AD |
151 | |
152 | public double getAngle() { | |
153 | if (!this.imu.isInitialized()) | |
154 | return -1; | |
155 | return this.imu.getHeading() - this.gyroZero; | |
156 | } | |
157 | ||
158 | public void resetGyro() { | |
159 | this.gyroZero = this.getAngle(); | |
160 | ||
e12d6901 CZ |
161 | } |
162 | ||
163 | public double getZeroAngle() { | |
164 | return this.gyroZero; | |
af4491f7 AD |
165 | } |
166 | ||
73e8adbc AD |
167 | /* |
168 | * @return a value that is the current setpoint for the piston kReverse or | |
169 | * KForward | |
170 | */ | |
171 | public Value getLeftGearPistonValue() { | |
172 | return leftGearPiston.get(); | |
173 | } | |
174 | ||
175 | /* | |
176 | * @return a value that is the current setppoint for the piston kReverse or | |
177 | * kForward | |
178 | */ | |
179 | public Value getRightGearPistonValue() { | |
180 | return rightGearPiston.get(); | |
181 | } | |
182 | ||
183 | /* | |
184 | * Changes the ball shift gear assembly to high | |
185 | */ | |
186 | public void setHighGear() { | |
187 | changeGear(Constants.DriveTrain.HIGH_GEAR); | |
188 | } | |
189 | ||
190 | /* | |
191 | * Changes the ball shift gear assembly to low | |
192 | */ | |
193 | public void setLowGear() { | |
194 | changeGear(Constants.DriveTrain.LOW_GEAR); | |
195 | } | |
196 | ||
197 | /* | |
198 | * Changes the gear to a DoubleSolenoid.Value | |
199 | */ | |
200 | private void changeGear(DoubleSolenoid.Value gear) { | |
201 | leftGearPiston.set(gear); | |
202 | rightGearPiston.set(gear); | |
e12d6901 CZ |
203 | } |
204 | ||
cc42bd52 ME |
205 | @Override |
206 | protected void initDefaultCommand() { | |
207 | setDefaultCommand(new JoystickDrive()); | |
208 | } | |
38a404b3 KZ |
209 | |
210 | } |