-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathControls.java
More file actions
1145 lines (1082 loc) · 47.5 KB
/
Controls.java
File metadata and controls
1145 lines (1082 loc) · 47.5 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package frc.robot;
import static edu.wpi.first.units.Units.MetersPerSecond;
import static edu.wpi.first.units.Units.RadiansPerSecond;
import static edu.wpi.first.units.Units.RotationsPerSecond;
import static edu.wpi.first.units.Units.Seconds;
import static edu.wpi.first.units.Units.Volts;
import com.ctre.phoenix6.swerve.SwerveModule.DriveRequestType;
import com.ctre.phoenix6.swerve.SwerveRequest;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.filter.Debouncer.DebounceType;
import edu.wpi.first.units.measure.Time;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.GenericHID.RumbleType;
import edu.wpi.first.wpilibj.LEDPattern;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.shuffleboard.BuiltInWidgets;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import edu.wpi.first.wpilibj2.command.button.RobotModeTriggers;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import frc.robot.generated.BonkTunerConstants;
import frc.robot.generated.CompTunerConstants;
import frc.robot.subsystems.ArmPivot;
import frc.robot.subsystems.ElevatorSubsystem;
import frc.robot.subsystems.GroundArm;
import frc.robot.subsystems.SuperStructure;
import frc.robot.subsystems.auto.AutoAlgaeHeights;
import frc.robot.subsystems.auto.AutoAlign;
import frc.robot.subsystems.auto.BargeAlign;
import frc.robot.util.AlgaeIntakeHeight;
import frc.robot.util.BranchHeight;
import frc.robot.util.RobotType;
import frc.robot.util.ScoringMode;
import frc.robot.util.ScoringType;
import frc.robot.util.SoloScoringMode;
import java.util.function.BooleanSupplier;
public class Controls {
private static final int SOLO_CONTROLLER_PORT = 0;
private static final int DRIVER_CONTROLLER_PORT = 1;
private static final int OPERATOR_CONTROLLER_PORT = 2;
private static final int ARM_PIVOT_SPINNY_CLAW_CONTROLLER_PORT = 3;
private static final int ELEVATOR_CONTROLLER_PORT = 4;
private static final int CLIMB_TEST_CONTROLLER_PORT = 5;
private final CommandXboxController driverController;
private final CommandXboxController operatorController;
private final CommandXboxController armPivotSpinnyClawController;
private final CommandXboxController elevatorTestController;
private final CommandXboxController climbTestController;
private final CommandXboxController soloController;
private final Subsystems s;
private final Sensors sensors;
private final SuperStructure superStructure;
private BranchHeight branchHeight = BranchHeight.CORAL_LEVEL_FOUR;
private ScoringMode scoringMode = ScoringMode.CORAL;
public ScoringMode intakeMode = ScoringMode.CORAL;
private SoloScoringMode soloScoringMode = SoloScoringMode.NO_GAME_PIECE;
private AlgaeIntakeHeight algaeIntakeHeight = AlgaeIntakeHeight.ALGAE_LEVEL_THREE_FOUR;
// Swerve stuff
// setting the max speed nad other similar variables depending on which drivebase it is
public static final double MaxSpeed =
RobotType.getCurrent() == RobotType.BONK
? BonkTunerConstants.kSpeedAt12Volts.in(MetersPerSecond)
: CompTunerConstants.kSpeedAt12Volts.in(MetersPerSecond);
private final double MAX_ACCELERATION = 50;
private final double MAX_ROTATION_ACCELERATION = 50;
// kSpeedAt12Volts desired top speed
public static double MaxAngularRate =
RotationsPerSecond.of(0.75)
.in(RadiansPerSecond); // 3/4 of a rotation per second max angular velocity
/* Setting up bindings for necessary control of the swerve drive platform */
private final SwerveRequest.FieldCentric drive =
new SwerveRequest.FieldCentric()
.withDeadband(0.0001)
.withRotationalDeadband(0.0001)
.withDriveRequestType(DriveRequestType.Velocity);
private final Telemetry logger = new Telemetry(MaxSpeed);
private final BooleanSupplier driveSlowMode;
public Controls(Subsystems s, Sensors sensors, SuperStructure superStructure) {
driverController = new CommandXboxController(DRIVER_CONTROLLER_PORT);
operatorController = new CommandXboxController(OPERATOR_CONTROLLER_PORT);
armPivotSpinnyClawController = new CommandXboxController(ARM_PIVOT_SPINNY_CLAW_CONTROLLER_PORT);
elevatorTestController = new CommandXboxController(ELEVATOR_CONTROLLER_PORT);
climbTestController = new CommandXboxController(CLIMB_TEST_CONTROLLER_PORT);
soloController = new CommandXboxController(SOLO_CONTROLLER_PORT);
this.s = s;
this.sensors = sensors;
this.superStructure = superStructure;
driveSlowMode = driverController.start();
configureDrivebaseBindings();
configureSuperStructureBindings();
configureElevatorBindings();
configureArmPivotBindings();
configureClimbPivotBindings();
configureSpinnyClawBindings();
configureElevatorLEDBindings();
configureAutoAlignBindings();
configureGroundSpinnyBindings();
configureGroundArmBindings();
configureSoloControllerBindings();
Shuffleboard.getTab("Elevator")
.addBoolean("Intaking mode Algae", () -> intakeMode == ScoringMode.ALGAE);
Shuffleboard.getTab("Elevator")
.addString("Scoring Mode", () -> getSoloScoringMode().toString());
}
public SoloScoringMode getSoloScoringMode() {
return soloScoringMode;
}
private Trigger connected(CommandXboxController controller) {
return new Trigger(() -> controller.isConnected());
}
// takes the X value from the joystick, and applies a deadband and input scaling
private double getDriveX() {
// Joystick +Y is back
// Robot +X is forward
double input = MathUtil.applyDeadband(-driverController.getLeftY(), 0.1);
double inputScale = driveSlowMode.getAsBoolean() ? 0.5 : 1;
return input * MaxSpeed * inputScale;
}
// takes the Y value from the joystick, and applies a deadband and input scaling
private double getDriveY() {
// Joystick +X is right
// Robot +Y is left
double input = MathUtil.applyDeadband(-driverController.getLeftX(), 0.1);
double inputScale = driveSlowMode.getAsBoolean() ? 0.5 : 1;
return input * MaxSpeed * inputScale;
}
// takes the rotation value from the joystick, and applies a deadband and input scaling
private double getDriveRotate() {
// Joystick +X is right
// Robot +angle is CCW (left)
double input = MathUtil.applyDeadband(-driverController.getRightX(), 0.1);
double inputScale = driveSlowMode.getAsBoolean() ? 0.5 : 1;
return input * MaxSpeed * inputScale;
}
// all the current control bidings
private void configureDrivebaseBindings() {
if (s.drivebaseSubsystem == null) {
// Stop running this method
return;
}
// Note that X is defined as forward according to WPILib convention,
// and Y is defined as to the left according to WPILib convention.
// the driving command for just driving around
s.drivebaseSubsystem.setDefaultCommand(
// s.drivebaseSubsystem will execute this command periodically
// applying the request to drive with the inputs
s.drivebaseSubsystem
.applyRequest(
() -> {
boolean soloConnected = soloController.isConnected();
SwerveRequest.FieldCentric request =
drive
.withVelocityX(soloConnected ? getSoloDriveX() : getDriveX())
.withVelocityY(soloConnected ? getSoloDriveY() : getDriveY())
.withRotationalRate(
soloConnected ? getSoloDriveRotate() : getDriveRotate());
return request;
})
.withName("Drive"));
// various former controls that were previously used and could be referenced in the future
// operatorController
// .povUp()
// .whileTrue(
// s.drivebaseSubsystem
// .applyRequest(
// () ->
// drive
// .withVelocityX(MetersPerSecond.of(1.0))
// .withVelocityY(0)
// .withRotationalRate(0))
// .withName("1 m/s forward"));
// operatorController
// .povRight()
// .whileTrue(
// s.drivebaseSubsystem
// .applyRequest(
// () ->
// drive
// .withVelocityX(MetersPerSecond.of(2.0))
// .withVelocityY(0)
// .withRotationalRate(0))
// .withName("2 m/s forward"));
// driverController.a().whileTrue(s.drivebaseSubsystem.sysIdDynamic(Direction.kForward));
// driverController.b().whileTrue(s.drivebaseSubsystem.sysIdDynamic(Direction.kReverse));
// driverController.y().whileTrue(s.drivebaseSubsystem.sysIdQuasistatic(Direction.kForward));
// driverController.x().whileTrue(s.drivebaseSubsystem.sysIdQuasistatic(Direction.kReverse));
// driveController.a().whileTrue(s.drivebaseSubsystem.applyRequest(() ->
// brake));
// driveController.b().whileTrue(s.drivebaseSubsystem.applyRequest(() ->
// point.withModuleDirection(new Rotation2d(-driveController.getLeftY(),
// -driveController.getLeftX()))
// ));
// reset the field-centric heading on back button press
driverController
.back()
.onTrue(
s.drivebaseSubsystem
.runOnce(() -> s.drivebaseSubsystem.seedFieldCentric())
.alongWith(rumble(driverController, 0.5, Seconds.of(0.3)))
.withName("Reset gyro"));
// logging the telemetry
s.drivebaseSubsystem.registerTelemetry(logger::telemeterize);
// creats a swerve button that coasts the wheels
var swerveCoastButton =
Shuffleboard.getTab("Controls")
.add("Swerve Coast Mode", false)
.withWidget(BuiltInWidgets.kToggleButton)
.getEntry();
// coast the wheels
new Trigger(() -> swerveCoastButton.getBoolean(false))
.whileTrue(s.drivebaseSubsystem.coastMotors());
}
private void configureSuperStructureBindings() {
if (superStructure == null) {
return;
}
superStructure.setBranchHeightSupplier(() -> branchHeight);
// operator start button used for climb - bound in climb bindings
operatorController
.y()
.onTrue(
selectScoringHeight(
BranchHeight.CORAL_LEVEL_FOUR, AlgaeIntakeHeight.ALGAE_LEVEL_THREE_FOUR)
.withName("coral level 4, algae level 3-4"));
operatorController
.x()
.onTrue(
selectScoringHeight(
BranchHeight.CORAL_LEVEL_THREE, AlgaeIntakeHeight.ALGAE_LEVEL_TWO_THREE)
.withName("coral level 3, algae level 2-3"));
operatorController
.b()
.onTrue(
selectScoringHeight(
BranchHeight.CORAL_LEVEL_TWO, AlgaeIntakeHeight.ALGAE_LEVEL_TWO_THREE)
.withName("coral level 2, algae level 2-3"));
operatorController
.a()
.onTrue(
selectScoringHeight(BranchHeight.CORAL_LEVEL_ONE, AlgaeIntakeHeight.ALGAE_LEVEL_GROUND)
.withName("coral level 1, algae ground level"));
driverController
.povUp()
.onTrue(
selectScoringHeight(
BranchHeight.CORAL_LEVEL_FOUR, AlgaeIntakeHeight.ALGAE_LEVEL_THREE_FOUR)
.withName("coral level 4, algae level 3-4"));
driverController
.povLeft()
.onTrue(
selectScoringHeight(
BranchHeight.CORAL_LEVEL_THREE, AlgaeIntakeHeight.ALGAE_LEVEL_TWO_THREE)
.withName("coral level 3, algae level 2-3"));
driverController
.povRight()
.onTrue(
selectScoringHeight(
BranchHeight.CORAL_LEVEL_TWO, AlgaeIntakeHeight.ALGAE_LEVEL_TWO_THREE)
.withName("coral level 2, algae level 2-3"));
driverController
.povDown()
.onTrue(
selectScoringHeight(BranchHeight.CORAL_LEVEL_ONE, AlgaeIntakeHeight.ALGAE_LEVEL_GROUND)
.withName("coral level 1, algae ground level"));
driverController
.leftTrigger()
.onTrue(
Commands.deferredProxy(
() ->
switch (scoringMode) {
case CORAL -> getCoralBranchHeightCommand(ScoringType.DRIVER);
case ALGAE -> Commands.sequence(
superStructure.algaeProcessorScore(
driverController.rightBumper()),
Commands.waitSeconds(0.7),
getAlgaeIntakeCommand())
.withName("Processor score");
})
.withName("Schedule processor score"));
operatorController
.leftBumper()
.onTrue(
Commands.runOnce(() -> scoringMode = ScoringMode.ALGAE)
.alongWith(scoringModeSelectRumble())
.withName("Algae Scoring Mode"))
.onTrue(
Commands.runOnce(() -> CommandScheduler.getInstance().schedule(getAlgaeIntakeCommand()))
.withName("run algae intake"));
operatorController // should work???
.leftTrigger()
.onTrue(
Commands.runOnce(() -> scoringMode = ScoringMode.CORAL)
.alongWith(scoringModeSelectRumble())
.withName("Coral Scoring Mode"))
.onTrue(superStructure.coralPreIntake())
.onTrue(s.climbPivotSubsystem.toStow());
operatorController
.povLeft()
.onTrue(
Commands.deferredProxy(
() ->
switch (scoringMode) {
case CORAL -> superStructure.coralStow();
case ALGAE -> superStructure.algaeStow();
})
.withName("Stow"));
operatorController
.povDown()
.onTrue(
Commands.deferredProxy(
() ->
switch (scoringMode) {
case CORAL -> superStructure.coralPreIntake();
case ALGAE -> superStructure.algaeStow();
})
.withName("pre-intake, algae stow"));
driverController
.a()
.onTrue(s.elevatorSubsystem.runOnce(() -> {}).withName("elevator interruptor"))
.onTrue(
Commands.runOnce(
() -> {
Command intakeCommand =
switch (scoringMode) {
case CORAL -> superStructure
.coralIntake()
.alongWith(
s.elevatorLEDSubsystem != null
? s.elevatorLEDSubsystem
.tripleBlink(255, 92, 0, "Orange - Manual Coral Intake")
.asProxy()
: Commands.none())
.withName("Manual Coral Intake");
case ALGAE -> getAlgaeIntakeCommand();
};
CommandScheduler.getInstance().schedule(intakeCommand);
})
.withName("Driver Intake"));
driverController
.b()
.onTrue(
superStructure.quickGroundIntake(driverController.x()).withName("Quick Gound intake"));
if (sensors.armSensor != null) {
sensors
.armSensor
.inTrough()
.and(superStructure.inCoralPreIntakePosition())
.and(RobotModeTriggers.teleop())
.onTrue(
superStructure
.coralIntake()
.alongWith(
s.elevatorLEDSubsystem != null
? s.elevatorLEDSubsystem
.tripleBlink(255, 255, 0, "Yellow - Automatic Intake")
.asProxy()
: Commands.none())
.withName("Automatic Intake"));
}
if (sensors.armSensor != null) {
sensors
.armSensor
.inClaw()
.and(RobotModeTriggers.teleop())
.onTrue(
Commands.runOnce(
() -> {
switch (intakeMode) {
case CORAL -> soloScoringMode = SoloScoringMode.CORAL_IN_CLAW;
case ALGAE -> soloScoringMode = SoloScoringMode.ALGAE_IN_CLAW;
}
})
.withName("Set solo scoring mode"))
.onFalse(
Commands.runOnce(
() -> {
soloScoringMode = SoloScoringMode.NO_GAME_PIECE;
})
.withName("Clear solo scoring mode"));
}
if (s.climbPivotSubsystem.sensor != null) {
s.climbPivotSubsystem
.cageDetected()
.and(new Trigger(() -> s.climbPivotSubsystem.isClimbOut))
.and(RobotModeTriggers.teleop())
.onTrue(s.climbPivotSubsystem.toClimbed().withName("Automatic sensor Climbing"));
}
driverController
.rightTrigger()
.onTrue(
Commands.runOnce(
() -> {
Command scoreCommand =
switch (scoringMode) {
case CORAL -> getCoralBranchHeightCommand(ScoringType.DRIVER);
case ALGAE -> Commands.sequence(
BargeAlign.bargeScore(
s.drivebaseSubsystem,
superStructure,
() -> getDriveX(),
() -> getDriveY(),
() -> getDriveRotate(),
driverController.rightBumper()),
getAlgaeIntakeCommand())
.withName("Algae score then intake");
};
CommandScheduler.getInstance().schedule(scoreCommand);
})
.withName("score"));
}
private Command getAlgaeIntakeCommand() {
return switch (algaeIntakeHeight) {
case ALGAE_LEVEL_THREE_FOUR -> superStructure.algaeLevelThreeFourIntake();
case ALGAE_LEVEL_TWO_THREE -> superStructure.algaeLevelTwoThreeIntake();
case ALGAE_LEVEL_GROUND -> superStructure.algaeGroundIntake();
};
}
private Command getCoralBranchHeightCommand(ScoringType version) {
if (version == ScoringType.SOLOC_LEFT) {
return switch (branchHeight) {
case CORAL_LEVEL_FOUR -> superStructure
.coralLevelFour(soloController.rightBumper())
.andThen(() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE);
case CORAL_LEVEL_THREE -> superStructure
.coralLevelThree(
soloController.rightBumper(),
() -> AutoAlign.poseInPlace(AutoAlign.AlignType.LEFTB))
.andThen(() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE);
case CORAL_LEVEL_TWO -> superStructure
.coralLevelTwo(
soloController.rightBumper(),
() -> AutoAlign.poseInPlace(AutoAlign.AlignType.LEFTB))
.andThen(() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE);
case CORAL_LEVEL_ONE -> superStructure
.coralLevelOne(
soloController.rightBumper(), () -> AutoAlign.poseInPlace(AutoAlign.AlignType.L1LB))
.andThen(() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE);
};
} else if (version == ScoringType.SOLOC_RIGHT) {
return switch (branchHeight) {
case CORAL_LEVEL_FOUR -> superStructure
.coralLevelFour(soloController.rightBumper())
.andThen(() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE);
case CORAL_LEVEL_THREE -> superStructure
.coralLevelThree(
soloController.rightBumper(),
() -> AutoAlign.poseInPlace(AutoAlign.AlignType.RIGHTB))
.andThen(() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE);
case CORAL_LEVEL_TWO -> superStructure
.coralLevelTwo(
soloController.rightBumper(),
() -> AutoAlign.poseInPlace(AutoAlign.AlignType.RIGHTB))
.andThen(() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE);
case CORAL_LEVEL_ONE -> superStructure
.coralLevelOne(
soloController.rightBumper(), () -> AutoAlign.poseInPlace(AutoAlign.AlignType.L1RB))
.andThen(() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE);
};
} else {
return switch (branchHeight) {
case CORAL_LEVEL_FOUR -> superStructure.coralLevelFour(driverController.rightBumper());
case CORAL_LEVEL_THREE -> superStructure.coralLevelThree(
driverController.rightBumper(), () -> false);
case CORAL_LEVEL_TWO -> superStructure.coralLevelTwo(
driverController.rightBumper(), () -> false);
case CORAL_LEVEL_ONE -> superStructure.coralLevelOne(
driverController.rightBumper(), () -> false);
};
}
}
private void configureElevatorBindings() {
if (s.elevatorSubsystem == null) {
return;
}
RobotModeTriggers.disabled().onTrue(s.elevatorSubsystem.stop());
// Controls binding goes here
operatorController
.leftStick()
.whileTrue(
s.elevatorSubsystem
.startMovingVoltage(
() -> Volts.of(ElevatorSubsystem.UP_VOLTAGE * -operatorController.getLeftY()))
.withName("Elevator Manual Control"));
s.elevatorSubsystem.setRumble(
(rumble) -> {
elevatorTestController.setRumble(RumbleType.kBothRumble, rumble);
operatorController.setRumble(RumbleType.kBothRumble, rumble);
});
connected(elevatorTestController)
.and(elevatorTestController.y())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.CORAL_LEVEL_FOUR_POS)
.withName("Elevator L4"));
connected(elevatorTestController)
.and(elevatorTestController.x())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.CORAL_LEVEL_THREE_POS)
.withName("Elevator L3"));
connected(elevatorTestController)
.and(elevatorTestController.b())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.CORAL_LEVEL_TWO_POS)
.withName("Elevator L2"));
connected(elevatorTestController)
.and(elevatorTestController.a())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.CORAL_LEVEL_ONE_POS)
.withName("Elevator L1"));
connected(elevatorTestController)
.and(elevatorTestController.rightBumper())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.CORAL_INTAKE_POS)
.withName("Elevator IntakePos"));
connected(elevatorTestController)
.and(elevatorTestController.povUp())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.ALGAE_LEVEL_THREE_FOUR)
.withName("Elevator Algae L3-L4"));
connected(elevatorTestController)
.and(elevatorTestController.povLeft())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.ALGAE_LEVEL_TWO_THREE)
.withName("Elevator Algae L2-L3"));
connected(elevatorTestController)
.and(elevatorTestController.povRight())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.ALGAE_STOWED)
.withName("Elevator Algae Stowed"));
connected(elevatorTestController)
.and(elevatorTestController.povDown())
.onTrue(
s.elevatorSubsystem
.setLevel(ElevatorSubsystem.ALGAE_PROCESSOR_SCORE)
.withName("Elevator Processor"));
connected(elevatorTestController)
.and(elevatorTestController.leftBumper())
.whileTrue(s.elevatorSubsystem.holdCoastMode().withName("elevatortest hold coast mode"));
operatorController
.back()
.onTrue(
Commands.parallel(
s.elevatorSubsystem.resetPosZero(),
rumble(operatorController, 0.5, Seconds.of(0.3)))
.ignoringDisable(true)
.withName("Reset elevator zero"));
if (RobotBase.isSimulation()) {
s.elevatorSubsystem
.resetPosZero()
.ignoringDisable(true)
.withName("Sim - reset elevator zero")
.schedule();
}
// operatorController.rightBumper().whileTrue(s.elevatorSubsystem.holdCoastMode());
var elevatorCoastButton =
Shuffleboard.getTab("Controls")
.add("Elevator Coast Mode", false)
.withWidget(BuiltInWidgets.kToggleButton)
.getEntry();
new Trigger(() -> elevatorCoastButton.getBoolean(false))
.whileTrue(s.elevatorSubsystem.holdCoastMode());
// var elevatorZeroButton = new DigitalInput(Hardware.ELEVATOR_ZERO_BUTTON);
// new Trigger(() -> elevatorZeroButton.get())
// .debounce(1, DebounceType.kRising)
// .and(RobotModeTriggers.disabled())
// .onTrue(s.elevatorSubsystem.resetPosZero());
}
private void configureArmPivotBindings() {
if (s.armPivotSubsystem == null) {
return;
}
// Arm Controls binding goes here
operatorController
.rightStick()
.whileTrue(
s.armPivotSubsystem
.startMovingVoltage(() -> Volts.of(3 * operatorController.getRightY()))
.withName("Arm Manual Control"));
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.rightStick())
.whileTrue(
s.armPivotSubsystem
.startMovingVoltage(() -> Volts.of(3 * armPivotSpinnyClawController.getRightY()))
.withName("Arm Manual Control"));
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.povRight())
.onTrue(
s.armPivotSubsystem.moveToPosition(ArmPivot.CORAL_PRESET_L4).withName("Arm L4 Preset"));
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.povLeft())
.onTrue(
s.armPivotSubsystem.moveToPosition(ArmPivot.CORAL_PRESET_L3).withName("Arm L3 Preset"));
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.povUp())
.onTrue(
s.armPivotSubsystem.moveToPosition(ArmPivot.CORAL_PRESET_UP).withName("Arm Preset Up"));
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.povDown())
.onTrue(
s.armPivotSubsystem
.moveToPosition(ArmPivot.CORAL_PRESET_DOWN)
.withName("Arm Preset Down"));
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.y())
.onTrue(
s.armPivotSubsystem
.moveToPosition(ArmPivot.ALGAE_REMOVE)
.withName("Algae Preset Remove"));
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.b())
.onTrue(
s.armPivotSubsystem
.moveToPosition(ArmPivot.ALGAE_PROCESSOR_SCORE)
.withName("Algae Preset Score"));
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.a())
.onTrue(
s.armPivotSubsystem
.moveToPosition(ArmPivot.ALGAE_STOWED)
.withName("Algae Preset Stowed"));
}
private void configureClimbPivotBindings() {
if (s.climbPivotSubsystem == null) {
return;
}
if (s.elevatorLEDSubsystem != null) {
Command setClimbLEDs = s.elevatorLEDSubsystem.pulse(0, 0, 255, "Blue - Climb Extended");
s.climbPivotSubsystem.isClimbing().whileTrue(setClimbLEDs);
}
// regularly run the advanced climb check
s.climbPivotSubsystem.setDefaultCommand(
s.climbPivotSubsystem.advanceClimbCheck().withName("Advance Climb Check"));
// check if the climb controller is connected, and whne start is pressed move to the next climb
// position
connected(climbTestController)
.and(climbTestController.start())
.onTrue(s.climbPivotSubsystem.advanceClimbTarget());
// on start or right trigger, move to climb out or climbed respectively on operator
operatorController.start().onTrue(s.climbPivotSubsystem.toClimbOut());
operatorController.rightTrigger().onTrue(s.climbPivotSubsystem.toClimbed());
// manual control for climb test controller for negative direction
connected(climbTestController)
.and(climbTestController.rightTrigger(0.1))
.whileTrue(
s.climbPivotSubsystem
.moveClimbManual(
() ->
-0.6
* MathUtil.applyDeadband(
climbTestController.getRightTriggerAxis(), 0.1))
.withName("Climb Manual Control"));
// manual control for climb test controller for positive direction
connected(climbTestController)
.and(climbTestController.leftTrigger(0.1))
.whileTrue(
s.climbPivotSubsystem
.moveClimbManual(
() ->
0.6 * MathUtil.applyDeadband(climbTestController.getLeftTriggerAxis(), 0.1))
.withName("Climb Manual Control"));
// climb coast button
var climbCoastButton =
Shuffleboard.getTab("Controls")
.add("Climb Coast Mode", false)
.withWidget(BuiltInWidgets.kToggleButton)
.getEntry();
// utilizes the climb coast button and coasts the climb
new Trigger(() -> climbCoastButton.getBoolean(false))
.whileTrue(s.climbPivotSubsystem.coastMotors());
}
private void configureSpinnyClawBindings() {
if (s.spinnyClawSubsytem == null) {
return;
}
s.spinnyClawSubsytem.setScoringMode(() -> scoringMode);
// Claw controls bindings go here
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.leftBumper())
.whileTrue(s.spinnyClawSubsytem.coralHoldExtakePower());
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.rightBumper())
.whileTrue(s.spinnyClawSubsytem.coralHoldIntakePower());
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.leftTrigger())
.whileTrue(s.spinnyClawSubsytem.algaeHoldExtakePower());
connected(armPivotSpinnyClawController)
.and(armPivotSpinnyClawController.rightTrigger())
.whileTrue(s.spinnyClawSubsytem.algaeGripIntakePower());
driverController
.rightBumper()
.whileTrue(
Commands.deferredProxy(
() -> {
if (s.spinnyClawSubsytem.getCurrentCommand() != null) {
return Commands.none().withName("No manual spit - Spinny claw used");
} else {
return switch (scoringMode) {
case CORAL -> Commands.none().withName("No manual spit - Coral mode");
case ALGAE -> s.spinnyClawSubsytem.algaeExtakePower();
};
}
}));
}
private void configureElevatorLEDBindings() {
if (s.elevatorLEDSubsystem == null) {
return;
}
s.elevatorLEDSubsystem.setDefaultCommand(
s.elevatorLEDSubsystem.showScoringMode(() -> soloScoringMode));
if (s.elevatorSubsystem != null) {
Trigger hasBeenZeroed = new Trigger(s.elevatorSubsystem::getHasBeenZeroed);
Trigger subZeroPosition = new Trigger(s.elevatorSubsystem::getPositionSubZero);
Commands.waitSeconds(1)
.andThen(
s.elevatorLEDSubsystem
.blink(120, 0, 0, "Red - Elevator Not Zeroed")
.ignoringDisable(true))
.schedule();
hasBeenZeroed.onTrue(
s.elevatorLEDSubsystem
.colorSet(0, 170, 0, "Green - Elevator Zeroed")
.withTimeout(2)
.andThen(s.elevatorLEDSubsystem.colorSet(0, 32, 0, "dimmed Green - Elevator Zeroed"))
.ignoringDisable(true)
.withName("Green - Elevator Zeroed"));
RobotModeTriggers.disabled()
.and(hasBeenZeroed.negate())
.onTrue(
s.elevatorLEDSubsystem
.blink(120, 0, 0, "Red - Elevator Not Zeroed")
.ignoringDisable(true));
subZeroPosition.whileTrue(
Commands.waitSeconds(0.5)
.andThen(
s.elevatorLEDSubsystem
.blink(120, 0, 0, "Red - Elevator Position Error")
.ignoringDisable(true)
.withName("Red - Elevator Position Error")));
}
RobotModeTriggers.autonomous()
.whileTrue(s.elevatorLEDSubsystem.animate(LEDPattern.rainbow(255, 255), "Auto Rainbow"));
Timer teleopTimer = new Timer();
// when in teleop for less than 5 seconds after autononomous ends, restart the timer
RobotModeTriggers.autonomous()
.debounce(5, DebounceType.kFalling)
.and(RobotModeTriggers.teleop())
.onTrue(Commands.runOnce(() -> teleopTimer.restart()));
RobotModeTriggers.teleop()
.onFalse(Commands.runOnce(() -> teleopTimer.stop()).ignoringDisable(true));
Shuffleboard.getTab("Controls").addDouble("Teleop time", () -> teleopTimer.get());
new Trigger(() -> teleopTimer.hasElapsed(135 - 30))
.onTrue(
Commands.sequence(
s.elevatorLEDSubsystem
.colorSet(255, 0, 0, "red half blink - 30 sec remaining")
.withTimeout(0.5),
s.elevatorLEDSubsystem
.colorSet(255, 255, 0, "Yellow half blink - 30 sec remaining")
.withTimeout(0.5)));
new Trigger(() -> teleopTimer.hasElapsed(135 - 15))
.onTrue(
Commands.sequence(
s.elevatorLEDSubsystem
.colorSet(255, 0, 0, "Red half blink - 15 sec remaining")
.withTimeout(0.5),
s.elevatorLEDSubsystem
.colorSet(255, 255, 0, "Yellow half blink - 15 sec remaining")
.withTimeout(0.5)));
}
private void configureAutoAlignBindings() {
if (s.drivebaseSubsystem == null) {
return;
}
if (s.visionSubsystem != null) {
new Trigger(() -> s.visionSubsystem.getTimeSinceLastReading() >= 5)
.and(RobotModeTriggers.teleop())
.whileTrue(rumble(operatorController, 0.1, Seconds.of(10)));
}
driverController
.rightTrigger()
.and(() -> scoringMode == ScoringMode.CORAL)
.and(() -> branchHeight != BranchHeight.CORAL_LEVEL_ONE)
.whileTrue(AutoAlign.autoAlign(s.drivebaseSubsystem, this, AutoAlign.AlignType.RIGHTB));
driverController
.leftTrigger()
.and(() -> scoringMode == ScoringMode.CORAL)
.and(() -> branchHeight != BranchHeight.CORAL_LEVEL_ONE)
.whileTrue(AutoAlign.autoAlign(s.drivebaseSubsystem, this, AutoAlign.AlignType.LEFTB));
}
private void configureGroundSpinnyBindings() {
if (s.groundSpinny == null) {
return;
}
s.groundSpinny.setDefaultCommand(s.groundSpinny.holdFunnelIntakePower());
}
private void configureGroundArmBindings() {
if (s.groundArm == null) {
return;
}
s.groundArm.setDefaultCommand(
s.groundArm
.moveToPosition(GroundArm.STOWED_POSITION)
.andThen(Commands.idle())
.withName("Ground stowed position wait"));
operatorController
.rightBumper()
.whileTrue(
s.groundArm
.moveToPosition(GroundArm.GROUND_POSITION)
.andThen(Commands.idle())
.withName("ground up position"));
}
private Command selectScoringHeight(BranchHeight b, AlgaeIntakeHeight a) {
return Commands.runOnce(
() -> {
branchHeight = b;
algaeIntakeHeight = a;
if (intakeMode == ScoringMode.ALGAE
&& (sensors.armSensor == null || !sensors.armSensor.booleanInClaw())) {
CommandScheduler.getInstance().schedule(getAlgaeIntakeCommand());
}
})
.alongWith(heightSelectRumble());
}
private Command rumble(CommandXboxController controller, double vibration, Time duration) {
return Commands.startEnd(
() -> controller.getHID().setRumble(RumbleType.kBothRumble, vibration),
() -> controller.getHID().setRumble(RumbleType.kBothRumble, 0))
.withTimeout(duration)
.withName("Rumble Port " + controller.getHID().getPort());
}
private Command heightSelectRumble() {
return rumble(driverController, 0.5, Seconds.of(0.3))
.alongWith(rumble(operatorController, 0.5, Seconds.of(0.3)))
.withName("height select rumble");
}
private Command scoringModeSelectRumble() {
return rumble(driverController, 1.0, Seconds.of(0.5))
.alongWith(rumble(operatorController, 1.0, Seconds.of(0.5)))
.withName("height select rumble");
}
public void vibrateDriveController(double vibration) {
if (!DriverStation.isAutonomous()) {
driverController.getHID().setRumble(RumbleType.kBothRumble, vibration);
}
}
public void vibrateCoDriveController(double vibration) {
if (!DriverStation.isAutonomous()) {
operatorController.getHID().setRumble(RumbleType.kBothRumble, vibration);
}
}
private double getJoystickInput(double input) {
if (soloController.leftStick().getAsBoolean() || soloController.rightStick().getAsBoolean()) {
return 0; // stop driving if either stick is pressed
}
// Apply a deadband to the joystick input
double deadbandedInput = MathUtil.applyDeadband(input, 0.1);
return deadbandedInput;
}
// Drive for Solo controller
// takes the X value from the joystick, and applies a deadband and input scaling
private double getSoloDriveX() {
// Joystick +Y is back
// Robot +X is forward
return getJoystickInput(-soloController.getLeftY()) * MaxSpeed;
}
// takes the Y value from the joystick, and applies a deadband and input scaling
private double getSoloDriveY() {
// Joystick +X is right
// Robot +Y is left
return getJoystickInput(-soloController.getLeftX()) * MaxSpeed;
}
// takes the rotation value from the joystick, and applies a deadband and input scaling
private double getSoloDriveRotate() {
// Joystick +X is right
// Robot +angle is CCW (left)
return getJoystickInput(-soloController.getRightX()) * MaxSpeed;
}
private void configureSoloControllerBindings() {
// Barge + Auto align left + Select scoring mode Algae
soloController
.leftTrigger()
.onTrue(
Commands.runOnce(
() -> {
Command scoreCommand;
switch (soloScoringMode) {
case CORAL_IN_CLAW -> {
scoreCommand =
getCoralBranchHeightCommand(ScoringType.SOLOC_LEFT)
.until(
() ->
soloController.a().getAsBoolean()
|| soloController.b().getAsBoolean()
|| soloController.x().getAsBoolean()
|| soloController.y().getAsBoolean());
}
case ALGAE_IN_CLAW -> {
Command bargeScoreCommand =
BargeAlign.bargeScore(
s.drivebaseSubsystem,
superStructure,
() -> getSoloDriveX(),
() -> getSoloDriveY(),
() -> getSoloDriveRotate(),
soloController.rightBumper())
.withName("Algae score then intake");
scoreCommand =
Commands.sequence(
bargeScoreCommand,
Commands.runOnce(
() -> soloScoringMode = soloScoringMode.NO_GAME_PIECE));
}
case NO_GAME_PIECE -> {
scoreCommand =
Commands.parallel(
Commands.runOnce(() -> intakeMode = ScoringMode.ALGAE)
.alongWith(scoringModeSelectRumble())
.withName("Algae Scoring Mode"),
AutoAlgaeHeights.autoAlgaeIntakeCommand(
s.drivebaseSubsystem, superStructure, this)
.until(() -> sensors.armSensor.booleanInClaw()));
}
default -> scoreCommand = Commands.none();
}
CommandScheduler.getInstance().schedule(scoreCommand);