-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrainSubsystem.java
More file actions
575 lines (467 loc) · 18.8 KB
/
BrainSubsystem.java
File metadata and controls
575 lines (467 loc) · 18.8 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package frc.robot.subsystems.brain;
import static edu.wpi.first.units.Units.Seconds;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import org.littletonrobotics.junction.Logger;
import org.xerosw.util.XeroSequenceCmd;
import org.xerosw.util.XeroTimer;
import edu.wpi.first.wpilibj.RobotState;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants.ReefLevel;
import frc.robot.RobotContainer;
import frc.robot.commands.robot.NullCmd ;
import frc.robot.commands.robot.collectalgaereef.CollectAlgaeReefGotoCmd;
import frc.robot.commands.robot.collectalgaereef.CollectAlgaeReefScoopCmd;
import frc.robot.commands.robot.collectcoral.CollectCoralCmd;
import frc.robot.commands.robot.placecoral.PlaceCoralCmd;
import frc.robot.commands.robot.scorealgae.ScoreAlgaeAfter;
import frc.robot.subsystems.climber.ClimberSubsystem;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.funnel.FunnelSubsystem;
import frc.robot.subsystems.grabber.GrabberSubsystem;
import frc.robot.subsystems.manipulator.ManipulatorConstants;
import frc.robot.subsystems.manipulator.ManipulatorSubsystem;
import frc.robot.subsystems.oi.CoralSide;
import frc.robot.subsystems.oi.OIConstants.LEDState;
import frc.robot.subsystems.oi.OIConstants.OILed;
import frc.robot.subsystems.oi.OISubsystem;
import frc.robot.util.ReefFaceInfo;
import frc.robot.util.ReefUtil;
public class BrainSubsystem extends SubsystemBase {
// The currently executing action, can be null if nothing is being executed
private RobotAction current_action_ ;
// The next action to be executed, can be null if no action is pending
private RobotAction next_action_ ;
// If true, we do not accept any robot actions
private boolean locked_ ;
// The command associated with the current robot action
private RobotActionCommandList current_robot_action_command_ ;
private int current_robot_action_command_index_ ;
// The command we are running
private XeroSequenceCmd current_cmd_ ;
// The OI subsystem, for LED control
private OISubsystem oi_ ;
// The level of coral to place
private ReefLevel coral_level_ ;
// The side of the coral to place
private CoralSide coral_side_ ;
// If true, there is algae on the reef where placing
private boolean algae_on_reef_ ;
// The game piece we are holding
private GamePiece gp_ ;
// If true, the left/right side switch has been initialized
private boolean leds_inited_ ;
// The number of times periodic has been called
private int periodic_count_ ;
// If true, we are clearing state
private boolean clearing_state_ ;
private boolean climb_signaled_ ;
private boolean going_down_ ;
private XeroTimer flash_timer_ ;
//
// Subsystems used to implement the robot actions that are
// managed by the brain subsystem. Remove th suppress warnings when
// we get the code to generate the commands required for the various
// robot actions.
//
private Drive db_ ;
private ManipulatorSubsystem m_ ;
private GrabberSubsystem g_ ;
private ClimberSubsystem c_ ;
private FunnelSubsystem f_ ;
private boolean placed_ok_ ;
public BrainSubsystem(OISubsystem oi, Drive db, ManipulatorSubsystem m, GrabberSubsystem g, ClimberSubsystem c, FunnelSubsystem f) {
oi_ = oi ;
db_ = db ;
m_ = m ;
g_ = g ;
c_ = c ;
f_ = f ;
locked_ = false ;
current_action_ = null ;
next_action_ = null ;
current_robot_action_command_ = null ;
current_robot_action_command_index_ = 0 ;
gp_ = GamePiece.NONE ;
leds_inited_ = false ;
periodic_count_ = 0 ;
climb_signaled_ = false ;
placed_ok_ = false ;
flash_timer_ = null ;
}
public void setGoingDown(boolean b) {
going_down_ = b ;
}
public GamePiece gp() {
return gp_ ;
}
public boolean placedOk() {
return placed_ok_ ;
}
public void setPlacedOk(boolean v) {
placed_ok_ = v ;
}
public void setGp(GamePiece gp) {
gp_ = gp ;
switch(gp) {
case NONE:
oi_.setLEDState(OILed.HoldingCoral, LEDState.Off) ;
oi_.setLEDState(OILed.HoldingAlgaeHigh, LEDState.Off) ;
break ;
case CORAL:
oi_.setLEDState(OILed.HoldingCoral, LEDState.On) ;
oi_.setLEDState(OILed.HoldingAlgaeHigh, LEDState.Off) ;
break ;
case ALGAE_HIGH:
oi_.setLEDState(OILed.HoldingCoral, LEDState.Off) ;
oi_.setLEDState(OILed.HoldingAlgaeHigh, LEDState.On) ;
break ;
}
}
public boolean doesReefHaveAlgae() {
return algae_on_reef_ ;
}
public void toggleAlgaeOnReef() {
algae_on_reef_ = !algae_on_reef_ ;
oi_.setLEDState(OILed.AlgaeOnReef, algae_on_reef_ ? LEDState.On : LEDState.Off) ;
}
public void clearAlgaeOnReef() {
algae_on_reef_ = false ;
oi_.setLEDState(OILed.AlgaeOnReef, algae_on_reef_ ? LEDState.On : LEDState.Off) ;
}
public void setCoralLevel(ReefLevel height) {
if (flash_timer_ != null) {
flash_timer_ = null ;
}
coral_level_ = height ;
oi_.setLevelLED(height);
//
// This is a HACK but its the safest way to do this
//
if ((current_action_ == RobotAction.CollectAlgaeReefKeep || current_action_ == RobotAction.CollectAlgaeReefEject) &&
current_cmd_ == null && current_robot_action_command_index_ == 1) {
current_cmd_ = new CollectAlgaeReefGotoCmd(this, m_, height) ;
current_cmd_.schedule() ;
}
}
public ReefLevel coralLevel() {
return coral_level_ ;
}
public ReefLevel algaeLevel() {
if (coral_level_ == ReefLevel.L1 || coral_level_ == ReefLevel.L2)
return ReefLevel.L2 ;
return ReefLevel.L3 ;
}
public void setCoralSide(CoralSide s) {
coral_side_ = s ;
oi_.setSideLED(s);
}
public CoralSide coralSide() {
return coral_side_ ;
}
private boolean isNextActionLegal(RobotAction act) {
boolean ret = true ;
if (current_action_ != null) {
switch(current_action_) {
case CollectCoral:
ret = (act == RobotAction.PlaceCoral) ;
break ;
case PlaceCoral:
ret = (act != RobotAction.PlaceCoral && act != RobotAction.ScoreAlgae) ;
break ;
case ScoreAlgae:
ret = (act == RobotAction.PlaceCoral) ;
break ;
case CollectAlgaeReefKeep:
ret = (act != RobotAction.PlaceCoral && act != RobotAction.CollectCoral) ;
break ;
case CollectAlgaeReefEject:
ret = (act != RobotAction.PlaceCoral && act != RobotAction.CollectCoral) ;
break ;
default:
break ;
}
}
return ret;
}
public void queueRobotAction(RobotAction action) {
if (!locked_ && RobotState.isEnabled() && RobotState.isTeleop()) {
//
// We can override the next action, until it becomes the current action
//
if (next_action_ != null) {
//
// Turn off the button for the current next action
//
oi_.setRobotActionLEDState(next_action_, LEDState.Off) ;
}
if (!isNextActionLegal(action)) {
oi_.flashDisplay();
return ;
}
else {
next_action_ = action ;
if (action != null) {
oi_.setRobotActionLEDState(next_action_, LEDState.On) ;
}
}
//
// If the current action is null, start the next action now
//
if (current_action_ == null) {
startNewAction() ;
}
}
}
public void coralOnFloor() {
flash_timer_ = new XeroTimer(Seconds.of(3.0)) ;
flash_timer_.start() ;
oi_.setLEDState(OILed.CoralL1, LEDState.Fast) ;
oi_.setLEDState(OILed.CoralL2, LEDState.Fast) ;
oi_.setLEDState(OILed.CoralL3, LEDState.Fast) ;
oi_.setLEDState(OILed.CoralL4, LEDState.Fast) ;
}
//
// This clears the state of the OI to a basic default, no actions
// scheduled state.
//
public void clearRobotActions() {
clearing_state_ = true ;
if (current_cmd_ != null) {
//
// If a current command is running, cancel it
//
current_cmd_.cancel() ;
}
current_cmd_ = null ;
current_action_ = null ;
next_action_ = null ;
oi_.clearAllActionLEDs() ;
clearing_state_ = false ;
}
public void lock() {
locked_ = true ;
}
public void unlock() {
locked_ = false ;
}
private void completedAction() {
oi_.setRobotActionLEDState(current_action_, LEDState.Off) ;
current_action_ = null ;
current_robot_action_command_ = null ;
current_robot_action_command_index_ = 0 ;
current_cmd_ = null ;
if (next_action_ != null) {
startNewAction() ;
}
}
public void execute() {
boolean cond = true ;
oi_.setLEDState(OILed.Execute, LEDState.Off) ;
if (current_robot_action_command_ == null || current_robot_action_command_index_ >= current_robot_action_command_.size()) {
//
// Gunner hit the execute button with nothing to execute
//
return ;
}
if (current_robot_action_command_.getCondition(current_robot_action_command_index_) != null) {
cond = current_robot_action_command_.getCondition(current_robot_action_command_index_).getAsBoolean() ;
}
if (cond && current_cmd_ == null && current_action_ != null && current_robot_action_command_ != null) {
current_cmd_ = current_robot_action_command_.getCommand(current_robot_action_command_index_++) ;
current_cmd_.schedule();
}
}
private boolean isCurrentActionLegal() {
boolean ret = true ;
switch(current_action_) {
case CollectCoral:
ret = (gp_ == GamePiece.NONE) ;
break ;
case PlaceCoral:
ret = (gp_ == GamePiece.CORAL) ;
break ;
case ScoreAlgae:
ret = (gp_ == GamePiece.ALGAE_HIGH) ;
break ;
case CollectAlgaeReefKeep:
ret = (gp_ == GamePiece.NONE) ;
break ;
case CollectAlgaeReefEject:
ret = (gp_ == GamePiece.NONE) ;
break ;
default:
break ;
}
return ret;
}
private String startNewAction() {
String status ;
going_down_ = false ;
//
// We are executing nothing, see if there are things to run queued
//
if (next_action_ != null) {
current_robot_action_command_index_ = 0 ;
current_action_ = next_action_ ;
next_action_ = null ;
if (isCurrentActionLegal()) {
oi_.setRobotActionLEDState(current_action_, LEDState.Fast) ;
current_robot_action_command_ = this.getRobotActionCommand(current_action_, coral_level_, coral_side_) ;
if (current_robot_action_command_ == null || current_robot_action_command_.size() == 0) {
status = current_action_.toString() + ":no command" ;
current_action_ = null ;
current_cmd_ = null ;
}
else {
current_cmd_ = current_robot_action_command_.getCommand(current_robot_action_command_index_++) ;
status = current_action_.toString() + ":" + current_cmd_.getName() ;
current_cmd_.schedule() ;
}
}
else {
current_action_ = null ;
next_action_ = null ;
status = "illegal action" ;
oi_.flashDisplay();
oi_.clearAllActionLEDs();
}
}
else {
status = "idle" ;
}
return status ;
}
private void initLEDs() {
if (oi_.sideSwitch()) {
coral_side_ = CoralSide.Right ;
}
else {
coral_side_ = CoralSide.Left ;
}
coral_level_ = ReefLevel.L4 ;
oi_.setLevelLED(coral_level_);
oi_.setSideLED(coral_side_);
}
@Override
public void periodic() {
String status = "" ;
if (flash_timer_ != null) {
if (flash_timer_.isExpired()) {
flash_timer_ = null ;
oi_.setLEDState(OILed.CoralL1, LEDState.Off) ;
oi_.setLEDState(OILed.CoralL2, LEDState.Off) ;
oi_.setLEDState(OILed.CoralL3, LEDState.Off) ;
oi_.setLEDState(OILed.CoralL4, LEDState.Off) ;
if (coral_level_ != null) {
oi_.setLevelLED(coral_level_);
}
}
}
if (current_action_ == RobotAction.PlaceCoral && going_down_) {
if (m_.getElevatorPosition().lte(ManipulatorConstants.Elevator.Positions.kReleaseGamePad)) {
RobotContainer.getInstance().gamepad().setLocked(false) ;
}
}
Logger.recordOutput("brain/holding", gp_.toString()) ;
Logger.recordOutput("brain/placedok", placed_ok_) ;
trackReefPlace() ;
if (c_.readyToClimb() && !climb_signaled_) {
//
// This does not really belong in the brain, but it is a good place to put it for now
//
oi_.setLEDState(OILed.ReadyToClimb, LEDState.Fast) ;
oi_.rumble(Seconds.of(2.0));
climb_signaled_ = true ;
}
if (current_cmd_ != null && current_cmd_.isComplete() && !clearing_state_) {
current_cmd_ = null ;
if (current_action_ != null && current_robot_action_command_index_ >= current_robot_action_command_.size()) {
//
// This was the last command in the current action, we are done so complete this action and move to
// the next action if there is one
//
completedAction() ;
}
}
if (!RobotState.isEnabled() || !RobotState.isTeleop()) {
clearRobotActions() ;
return ;
}
if (!leds_inited_ && periodic_count_ > 2) {
initLEDs() ;
leds_inited_ = true ;
}
periodic_count_++ ;
if (current_action_ == null) {
if (next_action_ != null) {
status = startNewAction() ;
}
else {
status = "idle" ;
}
}
else {
if (current_cmd_ == null) {
//
// The last command has finished and we are waiting for the execute button to schedule
// the next phase of this robot action.
//
oi_.setLEDState(OILed.Execute, LEDState.Fast) ;
status = current_action_.toString() + ":waiting" ;
}
else {
status = current_action_.toString() + ":" + current_cmd_.getName() ;
}
}
Logger.recordOutput("brain/status", status) ;
Logger.recordOutput("brain/locked", locked_) ;
Logger.recordOutput("brain/current_action", (current_action_ != null) ? current_action_.toString() : "none") ;
Logger.recordOutput("brain/next_action", next_action_ != null ? next_action_.toString() : "none") ;
;
}
private void trackReefPlace() {
Optional<ReefFaceInfo> info = ReefUtil.getTargetedReefFace(db_.getPose()) ;
if (info.isPresent()) {
Logger.recordOutput("brain/reefplace", info.get().getLeftScoringPose()) ;
}
}
private RobotActionCommandList getRobotActionCommand(RobotAction action, ReefLevel level, CoralSide side) {
List<XeroSequenceCmd> list = new ArrayList<XeroSequenceCmd>() ;
List<BooleanSupplier> conds = new ArrayList<BooleanSupplier>() ;
switch(action) {
case CollectCoral:
list.add(new CollectCoralCmd(this, oi_, m_, f_, g_, true)) ;
conds.add(null) ;
break ;
case PlaceCoral:
list.add(new NullCmd()) ;
conds.add(null) ;
list.add(new PlaceCoralCmd(this, db_, m_, g_, ReefLevel.AskBrain, CoralSide.AskBrain, true)) ;
conds.add(() -> { return ReefUtil.getTargetedReefFace(db_.getPose()).isPresent() ; }) ;
break ;
case ScoreAlgae:
list.add(new NullCmd()) ;
conds.add(null) ;
list.add(new ScoreAlgaeAfter(db_, this, m_, g_, false)) ;
conds.add(null) ;
break ;
case CollectAlgaeReefEject:
list.add(new CollectAlgaeReefGotoCmd(this, m_, ReefLevel.AskBrain)) ;
conds.add(null) ;
//list.add(new CollectAlgaeReefCmd(this, db_, m_, g_, ReefLevel.AskBrain, true, false)) ;
list.add(new CollectAlgaeReefScoopCmd(this, db_, m_, g_, ReefLevel.AskBrain, false, false)) ;
conds.add(() -> { return ReefUtil.getTargetedReefFace(db_.getPose()).isPresent() ; }) ;
break ;
case CollectAlgaeReefKeep:
list.add(new CollectAlgaeReefGotoCmd(this, m_, ReefLevel.AskBrain)) ;
conds.add(null) ;
list.add(new CollectAlgaeReefScoopCmd(this, db_, m_, g_, ReefLevel.AskBrain, true, false)) ;
conds.add(() -> { return ReefUtil.getTargetedReefFace(db_.getPose()).isPresent() ; }) ;
break ;
}
return new RobotActionCommandList(action, list, conds) ;
}
}