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