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 pathRobotContainer.java
More file actions
270 lines (258 loc) · 10.5 KB
/
Copy pathRobotContainer.java
File metadata and controls
270 lines (258 loc) · 10.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
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import choreo.auto.AutoChooser;
import choreo.auto.AutoFactory;
import com.studica.frc.AHRS;
import edu.wpi.first.epilogue.Logged;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.wpilibj.DataLogManager;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.smartdashboard.Field2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
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.Trigger;
import frc.robot.config.ControllerConfig;
import frc.robot.config.Positions;
import frc.robot.subsystems.*;
@Logged
public class RobotContainer {
private ClimbPivot climb = new ClimbPivot();
private Coral coral = new Coral();
private Algae algae = new Algae();
private CommandXboxController controller = new CommandXboxController(0);
private Drivetrain drivetrain = new Drivetrain();
private final AHRS gyro = new AHRS(AHRS.NavXComType.kMXP_SPI);
private AutoFactory autos =
new AutoFactory(
drivetrain::getPose,
(pose) -> drivetrain.poseEstimator.resetPose(pose),
drivetrain::followTrajectory,
true,
drivetrain);
// used to determine which section of the reef to score on, defaults at start to pole to front and
// left of driver
private int reefSection = 7;
public static boolean stopGrab = false;
// slows down robot during pickup
private Command pickup =
coral
.pickUpCoral()
.andThen(
Commands.runOnce(
() -> {
speedMultiplier = 1.0;
fieldRelative = true;
}));
// changes speed when picking up from source
private Command pickupSource =
coral.pickUpCoralSource().andThen(Commands.runOnce(() -> speedMultiplier = 1.0));
private Command safeState = coral.safeState();
private double speedMultiplier = 1.0;
private boolean fieldRelative = true;
private AutoChooser autoChooser = new AutoChooser();
@Logged public boolean autoDisabled = true;
private Field2d selectedSpot = new Field2d();
public RobotContainer() {
// how we selected where we were going to score
DataLogManager.start();
configureBindings();
SmartDashboard.putData("scheduler", CommandScheduler.getInstance());
autoChooser.addCmd(
"Left",
() ->
Commands.sequence(
Commands.waitSeconds(2), coral.goToReef(drivetrain, () -> 11, () -> 1)));
autoChooser.addCmd(
"Right",
() ->
Commands.sequence(
Commands.waitSeconds(2), coral.goToReef(drivetrain, () -> 3, () -> 1)));
// SmartDashboard.putData("Auto Chooser", autoChooser);
SmartDashboard.putData("Selected Score Locationn", selectedSpot);
}
// all controller bindings
private void configureBindings() {
// if left trigger is pressed, shifts selected reef section to score on left one, works while
// robot is disabled for autonomous
controller
.leftTrigger()
.onTrue(
Commands.runOnce(
() -> {
if (reefSection == 11) {
reefSection = 0;
} else {
reefSection += 1;
}
})
.ignoringDisable(true));
// if right trigger is pressed, shifts selected reef section to score on right one, works while
// robot is disabled for autonomous
controller
.rightTrigger()
.onTrue(
Commands.runOnce(
() -> {
if (reefSection == 0) {
reefSection = 11;
} else {
reefSection -= 1;
}
})
.ignoringDisable(true));
// when right toggle goes up, cancels ground coral pickup or initiates ground coral pickup
new Trigger(() -> controller.getRightY() > 0.9)
.onTrue(
Commands.runOnce(
() -> {
if (pickup.isScheduled()) {
pickupSource.cancel();
pickup.cancel();
speedMultiplier = 1.0;
fieldRelative = true;
} else {
pickup.schedule();
speedMultiplier = 0.3;
fieldRelative = false;
}
}));
// when right toggle goes down, cancels station coral pickup or initiates station coral pickup
new Trigger(() -> controller.getRightY() < -0.9)
.onTrue(
Commands.runOnce(
() -> {
if (pickupSource.isScheduled()) {
pickupSource.cancel();
pickup.cancel();
speedMultiplier = 1.0;
fieldRelative = true;
} else {
pickupSource.schedule();
speedMultiplier = 0.4;
}
}));
// when upper plus button is pressed, cancels safe state or initiates safe state
controller
.povUp()
.onTrue(
Commands.runOnce(
() -> {
if (safeState.isScheduled()) {
safeState.cancel();
} else {
safeState.schedule();
speedMultiplier = 1.0;
}
}));
// when x is pressed and if auto is disabled, raises elevator for l4 scoring, when left stick is
// pressed, completes scoring motion
// is auto is enabled, automatically goes to previously selected reef section and l4 scores
controller
.x()
.whileTrue(
Commands.deferredProxy(
() -> {
if (autoDisabled) {
return Commands.startEnd(
() -> speedMultiplier = 0.25, () -> speedMultiplier = 1.0)
.withDeadline(coral.l4Score(controller.leftStick(), false));
} else {
return coral.goToReef(drivetrain, () -> reefSection, () -> 3);
}
}));
// when y is pressed and if auto is disabled, raises elevator for l3 scoring, when left stick is
// pressed, completes scoring motion
// is auto is enabled, automatically goes to previously selected reef section and l3 scores
controller
.y()
.whileTrue(
Commands.deferredProxy(
() -> {
if (autoDisabled) {
return Commands.startEnd(
() -> speedMultiplier = 0.25, () -> speedMultiplier = 1.0)
.withDeadline(coral.l3Score(controller.leftStick()));
} else {
return coral.goToReef(drivetrain, () -> reefSection, () -> 2);
}
}));
// when a is pressed and if auto is disabled, raises elevator for l2 scoring, when left stick is
// pressed, completes scoring motion
// is auto is enabled, automatically goes to previously selected reef section and l2 scores
controller
.a()
.whileTrue(
Commands.deferredProxy(
() -> {
if (autoDisabled) {
return Commands.startEnd(
() -> speedMultiplier = 0.25, () -> speedMultiplier = 1.0)
.withDeadline(coral.l2Score(controller.leftStick()));
} else {
return coral.goToReef(drivetrain, () -> reefSection, () -> 1);
}
}));
// when b is pressed and if auto is disabled, raises elevator for l1 scoring, when left stick is
// pressed, completes scoring motion
// is auto is enabled, automatically goes to previously selected reef section and l1 scores
/* controller
.b()
.whileTrue(
Commands.deferredProxy(
() -> {
if (autoDisabled) {
return Commands.startEnd(
() -> speedMultiplier = 0.25, () -> speedMultiplier = 1.0)
.withDeadline(coral.l1Score(controller.leftStick()));
} else {
return coral.goToReef(drivetrain, () -> reefSection, () -> 0);
}
}));*/
// clearing algae off reef, down on plus for l1 clear, right on plus for l2 clear
controller.povDown().onTrue(coral.l1ReefClear(controller.leftStick()));
controller.povRight().onTrue(coral.l2ReefClear(controller.leftStick()));
// driving the robot
drivetrain.setDefaultCommand(
Commands.run(
() ->
drivetrain.drive(
applyDeadband(-controller.getLeftY() * speedMultiplier),
applyDeadband(-controller.getLeftX() * speedMultiplier),
applyDeadband(-controller.getRightX() * speedMultiplier),
true,
true),
drivetrain));
// deploy climber
controller.leftBumper().whileTrue(climb.pivotClimb());
// release climber and deploy algae for climb unless not nearing end of match
controller
.rightBumper()
.whileTrue(
climb
.pivotRelease()
.alongWith(algae.deployForClimb().unless(() -> DriverStation.getMatchTime() > 25)));
// when left plus pressed, disable or enable auto, defaults at disabled
controller.povLeft().onTrue(Commands.runOnce(() -> autoDisabled = !autoDisabled));
controller.back().onTrue(Commands.runOnce(() -> gyro.zeroYaw()));
controller.leftStick().onTrue(Commands.runOnce(() -> stopGrab = true));
controller.leftStick().onFalse(Commands.runOnce(() -> stopGrab = false));
}
// deadbands for driving
private double applyDeadband(double value) {
return MathUtil.applyDeadband(value, ControllerConfig.DEADBAND);
}
// sets the pose for auto-align
public void periodic() {
selectedSpot.setRobotPose(Positions.getIndividualReef(reefSection));
}
// autonomous command
public Command getAutonomousCommand() {
return Commands.sequence(
Commands.waitSeconds(1), coral.goToReef(drivetrain, () -> reefSection, () -> 3));
}
}