forked from robotpy/robotpy-commands-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswervecontrollercommand.py
292 lines (265 loc) · 12.3 KB
/
swervecontrollercommand.py
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
# 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.
from __future__ import annotations
from typing import Callable, Optional, Union, Tuple
from typing_extensions import overload
from overtake import overtake
from wpimath.controller import (
HolonomicDriveController,
PIDController,
ProfiledPIDControllerRadians,
)
from wpimath.geometry import Pose2d, Rotation2d
from wpimath.kinematics import (
SwerveDrive2Kinematics,
SwerveDrive3Kinematics,
SwerveDrive4Kinematics,
SwerveDrive6Kinematics,
SwerveModuleState,
)
from wpimath.trajectory import Trajectory
from wpilib import Timer
from .command import Command
from .subsystem import Subsystem
class SwerveControllerCommand(Command):
"""
A command that uses two PID controllers (PIDController) and a ProfiledPIDController
(ProfiledPIDController) to follow a trajectory (Trajectory) with a swerve drive.
This command outputs the raw desired Swerve Module States (SwerveModuleState) in an
array. The desired wheel and module rotation velocities should be taken from those and used in
velocity PIDs.
The robot angle controller does not follow the angle given by the trajectory but rather goes
to the angle given in the final state of the trajectory.
"""
@overload
def __init__(
self,
trajectory: Trajectory,
pose: Callable[[], Pose2d],
kinematics: Union[
SwerveDrive2Kinematics,
SwerveDrive3Kinematics,
SwerveDrive4Kinematics,
SwerveDrive6Kinematics,
],
xController: PIDController,
yController: PIDController,
thetaController: ProfiledPIDControllerRadians,
desiredRotation: Callable[[], Rotation2d],
outputModuleStates: Callable[[SwerveModuleState], None],
requirements: Tuple[Subsystem],
) -> None:
"""
Constructs a new SwerveControllerCommand that when executed will follow the
provided trajectory. This command will not return output voltages but
rather raw module states from the position controllers which need to be put
into a velocity PID.
Note: The controllers will *not* set the outputVolts to zero upon
completion of the path- this is left to the user, since it is not
appropriate for paths with nonstationary endstates.
:param trajectory: The trajectory to follow.
:param pose: A function that supplies the robot pose - use one of the odometry classes to
provide this.
:param kinematics: The kinematics for the robot drivetrain. Can be kinematics for 2/3/4/6
SwerveKinematics.
:param outputModuleStates: The raw output module states from the position controllers.
:param requirements: The subsystems to require.
:param xController: The Trajectory Tracker PID controller
for the robot's x position.
:param yController: The Trajectory Tracker PID controller
for the robot's y position.
:param thetaController: The Trajectory Tracker PID controller
for angle for the robot.
:param desiredRotation: The angle that the drivetrain should be
facing. This is sampled at each time step.
"""
super().__init__()
self._trajectory = trajectory
self._pose = pose
self._kinematics = kinematics
self._outputModuleStates = outputModuleStates
self._controller = HolonomicDriveController(
xController, yController, thetaController
)
self._desiredRotation = desiredRotation
self._timer = Timer()
self.addRequirements(requirements) # type: ignore
@overload
def __init__(
self,
trajectory: Trajectory,
pose: Callable[[], Pose2d],
kinematics: Union[
SwerveDrive2Kinematics,
SwerveDrive3Kinematics,
SwerveDrive4Kinematics,
SwerveDrive6Kinematics,
],
xController: PIDController,
yController: PIDController,
thetaController: ProfiledPIDControllerRadians,
outputModuleStates: Callable[[SwerveModuleState], None],
requirements: Tuple[Subsystem],
) -> None:
"""
Constructs a new SwerveControllerCommand that when executed will follow the
provided trajectory. This command will not return output voltages but
rather raw module states from the position controllers which need to be put
into a velocity PID.
Note: The controllers will *not* set the outputVolts to zero upon
completion of the path- this is left to the user, since it is not
appropriate for paths with nonstationary endstates.
Note 2: The final rotation of the robot will be set to the rotation of
the final pose in the trajectory. The robot will not follow the rotations
from the poses at each timestep. If alternate rotation behavior is desired,
the other constructor with a supplier for rotation should be used.
:param trajectory: The trajectory to follow.
:param pose: A function that supplies the robot pose - use one of the odometry classes to
provide this.
:param kinematics: The kinematics for the robot drivetrain. Can be kinematics for 2/3/4/6
SwerveKinematics.
:param xController: The Trajectory Tracker PID controller
for the robot's x position.
:param yController: The Trajectory Tracker PID controller
for the robot's y position.
:param thetaController: The Trajectory Tracker PID controller
for angle for the robot.
:param outputModuleStates: The raw output module states from the position controllers.
:param requirements: The subsystems to require.
"""
super().__init__()
self._trajectory = trajectory
self._pose = pose
self._kinematics = kinematics
self._outputModuleStates = outputModuleStates
self._controller = HolonomicDriveController(
xController, yController, thetaController
)
self._desiredRotation = self._trajectory.states()[-1].pose.rotation
self._timer = Timer()
self.addRequirements(requirements) # type:ignore
@overload
def __init__(
self,
trajectory: Trajectory,
pose: Callable[[], Pose2d],
kinematics: Union[
SwerveDrive2Kinematics,
SwerveDrive3Kinematics,
SwerveDrive4Kinematics,
SwerveDrive6Kinematics,
],
controller: HolonomicDriveController,
desiredRotation: Callable[[], Rotation2d],
outputModuleStates: Callable[[SwerveModuleState], None],
requirements: Tuple[Subsystem],
) -> None:
"""
Constructs a new SwerveControllerCommand that when executed will follow the
provided trajectory. This command will not return output voltages but
rather raw module states from the position controllers which need to be put
into a velocity PID.
Note: The controllers will *not* set the outputVolts to zero upon
completion of the path- this is left to the user, since it is not
appropriate for paths with nonstationary endstates.
:param trajectory: The trajectory to follow.
:param pose: A function that supplies the robot pose - use one of the odometry classes to
provide this.
:param kinematics: The kinematics for the robot drivetrain. Can be kinematics for 2/3/4/6
SwerveKinematics.
:param controller: The HolonomicDriveController for the drivetrain.
:param desiredRotation: The angle that the drivetrain should be
facing. This is sampled at each time step.
:param outputModuleStates: The raw output module states from the position controllers.
:param requirements: The subsystems to require.
"""
super().__init__()
self._trajectory = trajectory
self._pose = pose
self._kinematics = kinematics
self._outputModuleStates = outputModuleStates
self._controller = controller
self._desiredRotation = desiredRotation
self._timer = Timer()
self.addRequirements(requirements) # type:ignore
@overload
def __init__(
self,
trajectory: Trajectory,
pose: Callable[[], Pose2d],
kinematics: Union[
SwerveDrive2Kinematics,
SwerveDrive3Kinematics,
SwerveDrive4Kinematics,
SwerveDrive6Kinematics,
],
controller: HolonomicDriveController,
outputModuleStates: Callable[[SwerveModuleState], None],
requirements: Tuple[Subsystem],
) -> None:
"""
Constructs a new SwerveControllerCommand that when executed will follow the
provided trajectory. This command will not return output voltages but
rather raw module states from the position controllers which need to be put
into a velocity PID.
Note: The controllers will *not* set the outputVolts to zero upon
completion of the path- this is left to the user, since it is not
appropriate for paths with nonstationary endstates.
Note 2: The final rotation of the robot will be set to the rotation of
the final pose in the trajectory. The robot will not follow the rotations
from the poses at each timestep. If alternate rotation behavior is desired,
the other constructor with a supplier for rotation should be used.
:param trajectory: The trajectory to follow.
:param pose: A function that supplies the robot pose - use one of the odometry classes to
provide this.
:param kinematics: The kinematics for the robot drivetrain. Can be kinematics for 2/3/4/6
SwerveKinematics.
:param controller: The HolonomicDriveController for the drivetrain.
:param outputModuleStates: The raw output module states from the position controllers.
:param requirements: The subsystems to require.
"""
super().__init__()
self._trajectory = trajectory
self._pose = pose
self._kinematics = kinematics
self._outputModuleStates = outputModuleStates
self._controller = controller
self._desiredRotation = self._trajectory.states()[-1].pose.rotation
self._timer = Timer()
self.addRequirements(requirements) # type: ignore
@overtake(runtime_type_checker="beartype")
def __init__(
self,
trajectory: Trajectory,
pose: Callable[[], Pose2d],
kinematics: Union[
SwerveDrive2Kinematics,
SwerveDrive3Kinematics,
SwerveDrive4Kinematics,
SwerveDrive6Kinematics,
],
outputModuleStates: Callable[[SwerveModuleState], None],
requirements: Tuple[Subsystem],
*,
controller: Optional[HolonomicDriveController] = None,
xController: Optional[PIDController] = None,
yController: Optional[PIDController] = None,
thetaController: Optional[ProfiledPIDControllerRadians] = None,
desiredRotation: Optional[Callable[[], Rotation2d]] = None,
):
...
def initialize(self):
self._timer.restart()
def execute(self):
curTime = self._timer.get()
desiredState = self._trajectory.sample(curTime)
targetChassisSpeeds = self._controller.calculate(
self._pose(), desiredState, self._desiredRotation()
)
targetModuleStates = self._kinematics.toSwerveModuleStates(targetChassisSpeeds)
self._outputModuleStates(targetModuleStates)
def end(self, interrupted):
self._timer.stop()
def isFinished(self):
return self._timer.hasElapsed(self._trajectory.totalTime())