This repository was archived by the owner on Jul 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoral.java
More file actions
293 lines (273 loc) · 12.4 KB
/
Copy pathCoral.java
File metadata and controls
293 lines (273 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package frc.robot.subsystems;
import edu.wpi.first.epilogue.Logged;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.RobotContainer;
import frc.robot.config.CoralConfig;
import frc.robot.config.Positions;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.IntSupplier;
@Logged
public class Coral extends SubsystemBase {
private CoralGrabber grabber = new CoralGrabber();
private Elevator elevator = new Elevator();
private PIDController movementController =
new PIDController(CoralConfig.MOVEMENT_P, CoralConfig.MOVEMENT_I, CoralConfig.MOVEMENT_D);
private CoralPivot coralPivot = new CoralPivot();
// heights for levels
private double[] elevatorScoringHeights = {0.0, 0.910, 1.31, 1.360};
// rotations for levels
private Rotation2d[] coralScoringRotations = {
Rotation2d.fromRadians(1),
Rotation2d.fromRadians(1.7),
Rotation2d.fromRadians(1.7),
Rotation2d.fromRadians(0.3)
};
private Rotation2d[] reefClearRotationsPrimary = {
Rotation2d.fromRadians(1.9), Rotation2d.fromRadians(1.9)
};
private Rotation2d[] reefClearRotationsSecondary = {
Rotation2d.fromRadians(1.5), Rotation2d.fromRadians(1.5)
};
private double[] reefClearHeights = {0.65, 1.0564};
public Coral() {
movementController.setTolerance(CoralConfig.POSITION_TOLERANCE, CoralConfig.VELOCITY_TOLERANCE);
movementController.enableContinuousInput(-Math.PI, Math.PI);
elevator.setDefaultCommand(elevator.setElevatorHeight(() -> 0.0));
coralPivot.setDefaultCommand(coralPivot.setPivotAngle(() -> Rotation2d.fromRadians(0.35)));
grabber.setDefaultCommand(
Commands.run(
() -> {
if (!elevator.isAtPosition()
|| !coralPivot.isPivotReady()
|| !RobotContainer.stopGrab) {
grabber.run();
} else {
grabber.stop();
}
},
grabber));
}
// scores the current coral on the selected level
private Command score(int idx, BooleanSupplier completeScore) {
return Commands.waitSeconds(0.3)
.andThen(coralPivot.setPivotAngle(() -> coralScoringRotations[idx]))
.alongWith(elevator.setElevatorHeight(() -> elevatorScoringHeights[idx]))
.withDeadline(
Commands.waitUntil(elevator::isAtPosition)
.andThen(Commands.waitUntil(coralPivot::isPivotReady), Commands.waitSeconds(0.8))
.withDeadline(Commands.waitSeconds(1.8))
.andThen(Commands.waitUntil(completeScore))
.andThen(grabber.releaseCoral()));
}
private boolean shouldComplete = false;
// clears the reef of algae
private Command reefClear(int idx, BooleanSupplier completeClear) {
return Commands.runOnce(() -> shouldComplete = false)
.andThen(
coralPivot
.setPivotAngle(
() -> {
if (shouldComplete) {
return reefClearRotationsSecondary[idx];
} else {
return reefClearRotationsPrimary[idx];
}
})
.alongWith(elevator.setElevatorHeight(() -> reefClearHeights[idx]))
.withDeadline(
Commands.waitUntil(elevator::isAtPosition)
.andThen(
Commands.waitUntil(coralPivot::isPivotReady), Commands.waitSeconds(0.4))
.withDeadline(Commands.waitSeconds(1.4))
.andThen(
Commands.waitUntil(completeClear),
Commands.runOnce(() -> shouldComplete = true),
grabber
.removeAlgae()
.withDeadline(
Commands.waitUntil(() -> !completeClear.getAsBoolean())
.andThen(Commands.waitUntil(completeClear))))));
}
// scores on the l4 level (button pressed the most often)
public Command l4Score(BooleanSupplier completeScore, boolean shouldHaveAutoTimeout) {
return Commands.runOnce(() -> shouldComplete = false)
.andThen(
coralPivot
.setPivotAngle(
() -> {
if (elevator.getPosition() > 1.0 || shouldComplete) {
return coralScoringRotations[3];
} else {
return coralScoringRotations[3].plus(Rotation2d.fromDegrees(15));
}
})
.alongWith(
elevator.setElevatorHeight(
() -> {
if (shouldComplete) {
return elevatorScoringHeights[3] - .15;
} else {
return elevatorScoringHeights[3];
}
}))
.withDeadline(
Commands.waitUntil(elevator::isAtPosition)
.andThen(
Commands.waitUntil(coralPivot::isPivotReady), Commands.waitSeconds(1.0))
.withDeadline(Commands.waitSeconds(2.0))
.andThen(
Commands.waitUntil(completeScore)
.raceWith(
Commands.defer(
() ->
shouldHaveAutoTimeout
? Commands.waitSeconds(4)
: Commands.run(() -> {}),
Set.of())))
.andThen(
Commands.runOnce(() -> shouldComplete = true),
grabber.releaseCoralL4())))
.andThen(
coralPivot
.setPivotAngle(() -> coralScoringRotations[3].minus(Rotation2d.fromDegrees(15)))
.until(coralPivot::isPivotReady))
.andThen(elevator.setElevatorHeight(() -> 0.0).until(elevator::isAtPosition));
}
// moves the elevator to score l1, kinda works, not really used
public Command l1Score(BooleanSupplier completeScore) {
return score(0, completeScore);
}
// moves the elevator to l2
public Command l2Score(BooleanSupplier completeScore) {
return score(1, completeScore);
}
// moves the elevator to l3
public Command l3Score(BooleanSupplier completeScore) {
return score(2, completeScore);
}
public Command l1ReefClear(BooleanSupplier completeClear) {
return reefClear(0, completeClear);
}
public Command l2ReefClear(BooleanSupplier completeClear) {
return reefClear(1, completeClear);
}
@Logged public Rotation2d positionAngle;
// goes to a specific position around the reef
public Command goToReef(Drivetrain drivetrain, IntSupplier idx, IntSupplier height) {
return drivetrain
.orbit(
Positions::getReef,
() -> {
positionAngle =
Positions.getIndividualReef(
idx.getAsInt(), height.getAsInt() == 3 ? CoralConfig.L4_OFFSET : 0)
.getTranslation()
.minus(Positions.getReef().getTranslation())
.getAngle();
return -movementController.calculate(
drivetrain
.getPose()
.getTranslation()
.minus(Positions.getReef().getTranslation())
.getAngle()
.getRadians(),
Positions.getIndividualReef(
idx.getAsInt(), height.getAsInt() == 3 ? CoralConfig.L4_OFFSET : 0)
.getTranslation()
.minus(Positions.getReef().getTranslation())
.getAngle()
.getRadians());
},
() -> CoralConfig.MOVEMENT_DISTANCE)
.until(
() ->
movementController.atSetpoint()
&& drivetrain.rotController.atSetpoint()
&& drivetrain.xController.atSetpoint())
.andThen(
Commands.waitSeconds(1.0)
.andThen(
Commands.runEnd(
() -> {
ChassisSpeeds speeds =
ChassisSpeeds.fromFieldRelativeSpeeds(
drivetrain.xController.calculate(
drivetrain.getPose().getX(),
Positions.getIndividualReef(
idx.getAsInt(),
height.getAsInt() == 3 ? CoralConfig.L4_OFFSET : 0)
.getX()),
drivetrain.yController.calculate(
drivetrain.getPose().getY(),
Positions.getIndividualReef(
idx.getAsInt(),
height.getAsInt() == 3 ? CoralConfig.L4_OFFSET : 0)
.getY()),
drivetrain.rotController.calculate(
drivetrain.getPose().getRotation().getRadians(),
Positions.getIndividualReef(
idx.getAsInt(),
height.getAsInt() == 3 ? CoralConfig.L4_OFFSET : 0)
.getRotation()
.getRadians()),
drivetrain.getPose().getRotation());
drivetrain.setChassisSpeeds(speeds);
},
() -> drivetrain.drive(0, 0, 0, false, false)))
.withDeadline(
Commands.defer(
() ->
height.getAsInt() == 3
? l4Score(
() ->
drivetrain.rotController.atSetpoint()
&& drivetrain.xController.atSetpoint(),
true)
: score(
height.getAsInt(),
() ->
drivetrain.rotController.atSetpoint()
&& drivetrain.xController.atSetpoint()),
Set.of(elevator, coralPivot, grabber))));
}
// picks up coral from ground
public Command pickUpCoral() {
return elevator
.setElevatorHeight(() -> CoralConfig.INTAKE_HEIGHT)
.alongWith(
coralPivot.setPivotAngle(
() -> {
// intake wiggle
if (Math.round(Timer.getFPGATimestamp() * 2.0) % 2 == 0) {
return CoralConfig.INTAKE_ANGLE_LOWER;
} else {
return CoralConfig.INTAKE_ANGLE_UPPER;
}
}))
.withDeadline(
Commands.waitUntil(elevator::isAtPosition)
.andThen(Commands.waitUntil(coralPivot::isPivotReady), grabber.grabCoral()));
}
// picks up coral from source
public Command pickUpCoralSource() {
return elevator
.setElevatorHeight(() -> CoralConfig.INTAKE_HEIGHT_SOURCE)
.alongWith(coralPivot.setPivotAngle(() -> CoralConfig.INTAKE_ANGLE_SOURCE))
.withDeadline(
Commands.waitUntil(elevator::isAtPosition)
.andThen(Commands.waitUntil(coralPivot::isPivotReady), grabber.grabCoral()));
}
// safe state if we get a coral in the bot
public Command safeState() {
return elevator
.setElevatorHeight(() -> CoralConfig.INTAKE_HEIGHT_SOURCE)
.alongWith(coralPivot.setPivotAngle(() -> Rotation2d.fromDegrees(75)));
}
}