-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrobot_container.py
More file actions
281 lines (245 loc) · 14.6 KB
/
Copy pathrobot_container.py
File metadata and controls
281 lines (245 loc) · 14.6 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
import os
import commands2
import commands2.button
from commands2 import cmd, InstantCommand
from commands2.button import CommandXboxController, Trigger
from pathplannerlib.auto import NamedCommands, PathPlannerAuto
from phoenix6 import swerve, utils
from wpilib import DriverStation, SendableChooser, XboxController, SmartDashboard, getDeployDirectory
from wpimath.geometry import Rotation2d, Pose2d
from wpimath.units import rotationsToRadians
from constants import Constants
from generated.tuner_constants import TunerConstants
from subsystems.climber import ClimberSubsystem
from subsystems.elevator import ElevatorSubsystem
from subsystems.funnel import FunnelSubsystem
from subsystems.intake import IntakeSubsystem
from subsystems.pivot import PivotSubsystem
from subsystems.superstructure import Superstructure
from subsystems.swerve import SwerveSubsystem
from subsystems.swerve.requests import DriverAssist
from subsystems.vision import VisionSubsystem
class RobotContainer:
def __init__(self) -> None:
self._max_speed = TunerConstants.speed_at_12_volts
self._max_angular_rate = rotationsToRadians(1)
self._driver_controller = commands2.button.CommandXboxController(0)
self._function_controller = commands2.button.CommandXboxController(1)
self.drivetrain = TunerConstants.create_drivetrain()
self.climber = ClimberSubsystem()
self.pivot = PivotSubsystem()
self.intake = IntakeSubsystem()
self.elevator = ElevatorSubsystem()
self.funnel = FunnelSubsystem()
self.vision = VisionSubsystem(
self.drivetrain,
Constants.VisionConstants.FRONT_RIGHT,
Constants.VisionConstants.FRONT_CENTER,
Constants.VisionConstants.FRONT_LEFT,
#Constants.VisionConstants.BACK_CENTER,
)
self.superstructure = Superstructure(
self.drivetrain, self.pivot, self.elevator, self.funnel, self.vision, self.climber, self.intake
)
self._setup_swerve_requests()
self._pathplanner_setup()
self._setup_controller_bindings()
def _pathplanner_setup(self):
# Register NamedCommands
NamedCommands.registerCommand("Default", self.superstructure.set_goal_command(Superstructure.Goal.DEFAULT))
NamedCommands.registerCommand("L4 Coral", self.superstructure.set_goal_command(Superstructure.Goal.L4_CORAL))
NamedCommands.registerCommand("L3 Coral", self.superstructure.set_goal_command(Superstructure.Goal.L3_CORAL))
NamedCommands.registerCommand("L2 Coral", self.superstructure.set_goal_command(Superstructure.Goal.L2_CORAL))
NamedCommands.registerCommand("L1 Coral", self.superstructure.set_goal_command(Superstructure.Goal.L1_CORAL))
NamedCommands.registerCommand("L2 Algae", self.superstructure.set_goal_command(Superstructure.Goal.L2_ALGAE))
NamedCommands.registerCommand("L3 Algae", self.superstructure.set_goal_command(Superstructure.Goal.L3_ALGAE))
NamedCommands.registerCommand("Processor", self.superstructure.set_goal_command(Superstructure.Goal.PROCESSOR))
NamedCommands.registerCommand("Net", self.superstructure.set_goal_command(Superstructure.Goal.NET))
NamedCommands.registerCommand("Funnel", self.superstructure.set_goal_command(Superstructure.Goal.FUNNEL))
NamedCommands.registerCommand("Floor", self.superstructure.set_goal_command(Superstructure.Goal.FLOOR))
NamedCommands.registerCommand("Hold", self.intake.set_desired_state_command(IntakeSubsystem.SubsystemState.HOLD))
NamedCommands.registerCommand("Coral Intake", self.intake.set_desired_state_command(IntakeSubsystem.SubsystemState.CORAL_INTAKE))
NamedCommands.registerCommand("Coral Output", self.intake.set_desired_state_command(IntakeSubsystem.SubsystemState.CORAL_OUTPUT))
NamedCommands.registerCommand("Algae Intake", self.intake.set_desired_state_command(IntakeSubsystem.SubsystemState.ALGAE_INTAKE))
NamedCommands.registerCommand("Algae Output", self.intake.set_desired_state_command(IntakeSubsystem.SubsystemState.ALGAE_OUTPUT))
NamedCommands.registerCommand("Funnel Intake", self.intake.set_desired_state_command(IntakeSubsystem.SubsystemState.FUNNEL_INTAKE).repeatedly().until(lambda: self.intake.has_coral() or utils.is_simulation()))
# Build AutoChooser
self._auto_chooser = SendableChooser()
for auto in os.listdir(os.path.join(getDeployDirectory(), 'pathplanner', 'autos')):
auto = auto.removesuffix(".auto")
if auto ==".DS_Store":
continue
self._auto_chooser.addOption(auto, PathPlannerAuto(auto, False))
self._auto_chooser.addOption(auto + " (Mirrored)", PathPlannerAuto(auto, True))
self._auto_chooser.setDefaultOption("None", cmd.none())
self._auto_chooser.onChange(
lambda _: self._set_correct_swerve_position()
)
self._auto_chooser.addOption("Basic Leave",
self.drivetrain.apply_request(lambda: self._robot_centric.with_velocity_x(1)).withTimeout(1.0)
)
SmartDashboard.putData("Selected Auto", self._auto_chooser)
def _set_correct_swerve_position(self) -> None:
chooser_selected = self._auto_chooser.getSelected()
try:
self.drivetrain.reset_pose(self._flip_pose_if_needed(chooser_selected._startingPose))
self.drivetrain.reset_rotation(chooser_selected._startingPose.rotation() + self.drivetrain.get_operator_forward_direction())
except AttributeError:
pass
@staticmethod
def _flip_pose_if_needed(pose: Pose2d) -> Pose2d:
if (DriverStation.getAlliance() or DriverStation.Alliance.kBlue) == DriverStation.Alliance.kRed:
flipped_x = Constants.FIELD_LAYOUT.getFieldLength() - pose.X()
flipped_y = Constants.FIELD_LAYOUT.getFieldWidth() - pose.Y()
flipped_rotation = Rotation2d(pose.rotation().radians()) + Rotation2d.fromDegrees(180)
return Pose2d(flipped_x, flipped_y, flipped_rotation)
return pose
def _setup_swerve_requests(self):
self._field_centric = (
swerve.requests.FieldCentric()
.with_deadband(0)
.with_rotational_deadband(0)
.with_drive_request_type(swerve.SwerveModule.DriveRequestType.VELOCITY)
.with_steer_request_type(swerve.SwerveModule.SteerRequestType.POSITION)
)
self._robot_centric: swerve.requests.RobotCentric = (
swerve.requests.RobotCentric()
.with_deadband(0)
.with_rotational_deadband(0)
.with_drive_request_type(swerve.SwerveModule.DriveRequestType.VELOCITY)
.with_steer_request_type(swerve.SwerveModule.SteerRequestType.POSITION)
)
self._driver_assist: DriverAssist = (
DriverAssist()
.with_deadband(self._max_speed * 0.01)
.with_rotational_deadband(self._max_angular_rate * 0.02)
.with_drive_request_type(swerve.SwerveModule.DriveRequestType.VELOCITY)
.with_steer_request_type(swerve.SwerveModule.SteerRequestType.POSITION)
.with_translation_pid(Constants.AutoAlignConstants.TRANSLATION_P, Constants.AutoAlignConstants.TRANSLATION_I, Constants.AutoAlignConstants.TRANSLATION_D)
.with_heading_pid(Constants.AutoAlignConstants.HEADING_P, Constants.AutoAlignConstants.HEADING_I, Constants.AutoAlignConstants.HEADING_D)
)
self._brake = swerve.requests.SwerveDriveBrake()
self._point = swerve.requests.PointWheelsAt()
@staticmethod
def rumble_command(controller: CommandXboxController, duration: float, intensity: float):
return cmd.sequence(
InstantCommand(lambda: controller.setRumble(XboxController.RumbleType.kBothRumble, intensity)),
cmd.waitSeconds(duration),
InstantCommand(lambda: controller.setRumble(XboxController.RumbleType.kBothRumble, 0))
)
def _setup_controller_bindings(self) -> None:
hid = self._driver_controller.getHID()
self.drivetrain.setDefaultCommand(
self.drivetrain.apply_request(
lambda: self._field_centric
.with_velocity_x(-hid.getLeftY() * self._max_speed)
.with_velocity_y(-hid.getLeftX() * self._max_speed)
.with_rotational_rate(-self._driver_controller.getRightX() * self._max_angular_rate)
)
)
self._driver_controller.leftBumper().whileTrue(
self.drivetrain.apply_request(
lambda: self._robot_centric
.with_velocity_x(-hid.getLeftY() * self._max_speed)
.with_velocity_y(-hid.getLeftX() * self._max_speed)
.with_rotational_rate(-self._driver_controller.getRightX() * self._max_angular_rate)
)
)
self._driver_controller.rightBumper().whileTrue(
self.intake.set_desired_state_command(self.intake.SubsystemState.CORAL_OUTPUT)
).onFalse(
self.intake.set_desired_state_command(self.intake.SubsystemState.HOLD)
)
self._driver_controller.a().whileTrue(self.drivetrain.apply_request(lambda: self._brake))
self._driver_controller.b().whileTrue(
self.drivetrain.apply_request(
lambda: self._point.with_module_direction(Rotation2d(-hid.getLeftY(), -hid.getLeftX()))
)
)
Trigger(lambda: self._driver_controller.getLeftTriggerAxis() > 0.75).onTrue(
self.drivetrain.runOnce(lambda: self._driver_assist.with_target_pose(self.drivetrain.get_closest_branch(self.drivetrain.BranchSide.LEFT)))
).whileTrue(
self.drivetrain.apply_request(
lambda: self._driver_assist
.with_velocity_x(-hid.getLeftY() * self._max_speed)
.with_velocity_y(-hid.getLeftX() * self._max_speed)
)
)
Trigger(lambda: self._driver_controller.getRightTriggerAxis() > 0.75).onTrue(
self.drivetrain.runOnce(lambda: self._driver_assist.with_target_pose(self.drivetrain.get_closest_branch(self.drivetrain.BranchSide.RIGHT)))
).whileTrue(
self.drivetrain.apply_request(
lambda: self._driver_assist
.with_velocity_x(-hid.getLeftY() * self._max_speed)
.with_velocity_y(-hid.getLeftX() * self._max_speed)
)
)
self._driver_controller.start().onTrue(self.drivetrain.runOnce(lambda: self.drivetrain.seed_field_centric()))
goal_bindings = {
self._function_controller.y(): self.superstructure.Goal.L4_CORAL,
self._function_controller.x(): self.superstructure.Goal.L3_CORAL,
self._function_controller.b(): self.superstructure.Goal.L2_CORAL,
self._function_controller.a(): self.superstructure.Goal.DEFAULT,
self._function_controller.y() & self._function_controller.start(): self.superstructure.Goal.NET,
self._function_controller.x() & self._function_controller.start(): self.superstructure.Goal.L3_ALGAE,
self._function_controller.b() & self._function_controller.start(): self.superstructure.Goal.L2_ALGAE,
self._function_controller.a() & self._function_controller.start(): self.superstructure.Goal.PROCESSOR,
self._function_controller.leftStick(): self.superstructure.Goal.L1_CORAL
}
for button, goal in goal_bindings.items():
if goal is self.superstructure.Goal.L3_ALGAE or goal is self.superstructure.Goal.NET or goal is self.superstructure.Goal.L2_ALGAE or goal is self.superstructure.Goal.PROCESSOR:
(button.whileTrue(
self.superstructure.set_goal_command(goal)
.alongWith(self.intake.set_desired_state_command(self.intake.SubsystemState.ALGAE_INTAKE)))
.onFalse(self.intake.set_desired_state_command(self.intake.SubsystemState.ALGAE_HOLD)))
else:
button.onTrue(self.superstructure.set_goal_command(goal))
self._function_controller.leftBumper().onTrue(
cmd.parallel(
self.superstructure.set_goal_command(self.superstructure.Goal.FUNNEL),
self.intake.set_desired_state_command(self.intake.SubsystemState.FUNNEL_INTAKE),
)
).onFalse(
cmd.parallel(
self.superstructure.set_goal_command(self.superstructure.Goal.DEFAULT),
self.intake.set_desired_state_command(self.intake.SubsystemState.HOLD)
)
)
(self._function_controller.leftBumper() & self._function_controller.back()).whileTrue(
cmd.parallel(
self.superstructure.set_goal_command(self.superstructure.Goal.FLOOR),
self.intake.set_desired_state_command(self.intake.SubsystemState.CORAL_INTAKE),
)
).onFalse(
cmd.parallel(
self.superstructure.set_goal_command(self.superstructure.Goal.DEFAULT),
self.intake.set_desired_state_command(self.intake.SubsystemState.HOLD),
)
)
(self._function_controller.povLeft() | self._function_controller.povUpLeft() | self._function_controller.povDownLeft()).onTrue(
cmd.parallel(
self.climber.set_desired_state_command(self.climber.SubsystemState.CLIMB_OUT),
self.superstructure.set_goal_command(self.superstructure.Goal.CLIMBING)
)
).onFalse(self.climber.set_desired_state_command(self.climber.SubsystemState.STOP))
(self._function_controller.povRight() | self._function_controller.povUpRight() | self._function_controller.povDownRight()).onTrue(
cmd.parallel(
self.climber.set_desired_state_command(self.climber.SubsystemState.CLIMB_IN),
self.superstructure.set_goal_command(self.superstructure.Goal.CLIMBING)
)
).onFalse(self.climber.set_desired_state_command(self.climber.SubsystemState.STOP))
self._function_controller.povUp().onTrue(
self.superstructure.set_goal_command(self.superstructure.Goal.FINISH)
)
self._function_controller.rightBumper().whileTrue(
self.intake.set_desired_state_command(self.intake.SubsystemState.CORAL_OUTPUT)
).onFalse(
self.intake.set_desired_state_command(self.intake.SubsystemState.HOLD)
)
(self._function_controller.rightBumper() & self._function_controller.start()).onTrue(
self.intake.set_desired_state_command(self.intake.SubsystemState.L1_OUTPUT)
).onFalse(
self.intake.set_desired_state_command(self.intake.SubsystemState.HOLD)
)
def get_autonomous_command(self) -> commands2.Command:
return self._auto_chooser.getSelected()