-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathramsetecommand.py
280 lines (241 loc) · 11.7 KB
/
ramsetecommand.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
# 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, Union, Optional, Tuple, overload
from wpimath.controller import (
PIDController,
RamseteController,
SimpleMotorFeedforwardMeters,
)
from wpimath.geometry import Pose2d
from wpimath.kinematics import (
ChassisSpeeds,
DifferentialDriveKinematics,
DifferentialDriveWheelSpeeds,
)
from wpimath.trajectory import Trajectory
from wpimath import units as units
from wpiutil import SendableBuilder
from wpilib import Timer
from .command import Command
from .subsystem import Subsystem
class RamseteCommand(Command):
"""
A command that uses a RAMSETE controller (RamseteController) to follow a trajectory
(Trajectory) with a differential drive.
The command handles trajectory-following, PID calculations, and feedforwards internally. This
is intended to be a more-or-less "complete solution" that can be used by teams without a great
deal of controls expertise.
Advanced teams seeking more flexibility (for example, those who wish to use the onboard PID
functionality of a "smart" motor controller) may use the secondary constructor that omits the PID
and feedforward functionality, returning only the raw wheel speeds from the RAMSETE controller.
"""
@overload
def __init__(
self,
trajectory: Trajectory,
pose: Callable[[], Pose2d],
controller: RamseteController,
kinematics: DifferentialDriveKinematics,
requirements: Tuple[Subsystem],
*,
outputMPS: Callable[[units.meters_per_second, units.meters_per_second], None],
):
...
@overload
def __init__(
self,
trajectory: Trajectory,
pose: Callable[[], Pose2d],
controller: RamseteController,
kinematics: DifferentialDriveKinematics,
requirements: Tuple[Subsystem],
*,
outputVolts: Callable[[units.volts, units.volts], None],
feedforward: SimpleMotorFeedforwardMeters,
leftController: PIDController,
rightController: PIDController,
wheelSpeeds: Callable[[], DifferentialDriveWheelSpeeds],
):
...
def __init__(
self,
trajectory: Trajectory,
pose: Callable[[], Pose2d],
controller: RamseteController,
kinematics: DifferentialDriveKinematics,
requirements: Tuple[Subsystem],
*,
outputMPS: Optional[
Callable[[units.meters_per_second, units.meters_per_second], None]
] = None,
outputVolts: Optional[Callable[[units.volts, units.volts], None]] = None,
feedforward: Optional[SimpleMotorFeedforwardMeters] = None,
leftController: Optional[PIDController] = None,
rightController: Optional[PIDController] = None,
wheelSpeeds: Optional[Callable[[], DifferentialDriveWheelSpeeds]] = None,
):
"""Constructs a new RamseteCommand that, when executed, will follow the provided trajectory. PID
control and feedforward are handled internally, and outputs are scaled -12 to 12 representing
units of volts.
Note: The controller 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 controller: The RAMSETE controller used to follow the trajectory.
:param kinematics: The kinematics for the robot drivetrain.
:param requirements: The subsystems to require.
REQUIRED KEYWORD PARAMETERS
Note: The output mode must be specified by the users of RamseteCommands. Users can specify ONE of
an output function that will supply the consumer with left and right speeds in `units.meters_per_second`
or in `units.volts`. If neither, or both, are supplied to the constructor a `RuntimeError` will be
raised.
:param outputVolts A function that consumes the computed left and right outputs in `units.volts`
:param outputMPS A function that consumes the computed left and right outputs in `units.meters_per_second`
Constructs a new RamseteCommand that, when executed, will follow the provided trajectory. PID
control and feedforward are handled internally, and outputs are scaled -12 to 12 representing
units of volts.
Note: The controller 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 controller: The RAMSETE controller used to follow the trajectory.
:param kinematics: The kinematics for the robot drivetrain.
:param requirements: The subsystems to require.
REQUIRED KEYWORD PARAMETERS
Note: The output mode must be specified by the users of RamseteCommands. Users can specify ONE of
an output function that will supply the consumer with left and right speeds in `units.meters_per_second`
or in `units.volts`. If neither, or both, are supplied to the constructor a `RuntimeError` will be
raised.
:param outputVolts A function that consumes the computed left and right outputs in `units.volts`
:param outputMPS A function that consumes the computed left and right outputs in `units.meters_per_second`
OPTIONAL PARAMETERS
When the following optional parameters are provided, when executed, the RamseteCommand will follow
provided trajectory. PID control and feedforward are handled internally, and the outputs are scaled
from -12 to 12 representing units of volts. If any one of the optional parameters are provided, each
other optional parameter becomes required.
:param feedforward The feedforward to use for the drive
:param leftController: The PIDController for the left side of the robot drive.
:param rightController: The PIDController for the right side of the robot drive.
:param wheelSpeeds: A function that supplies the speeds of the left and right sides of the robot
drive.
"""
super().__init__()
self._timer = Timer()
# Store all the requirements that are required
self.trajectory = trajectory
self.pose = pose
self.follower = controller
self.kinematics = kinematics
self.outputMPS = outputMPS
self.outputVolts = outputVolts
self.leftController = leftController
self.rightController = rightController
self.wheelspeeds = wheelSpeeds
self.feedforward = feedforward
self._prevSpeeds = DifferentialDriveWheelSpeeds()
self._prevTime = -1.0
# Required requirements check for output
if outputMPS is None and outputVolts is None:
raise RuntimeError(
f"Failed to instantiate RamseteCommand, no output consumer provided. Must provide either output in meters per second or volts."
)
if outputMPS is not None and outputVolts is not None:
raise RuntimeError(
f"Failed to instantiate RamseteCommand, too many consumers provided. Must provide either output in meters per second or volts but not both."
)
# Optional requirements checks. If any one of the optional parameters are not None, then all the optional
# requirements become required.
if (
feedforward is not None
or leftController is not None
or rightController is not None
or wheelSpeeds is not None
):
if (
feedforward is None
or leftController is None
or rightController is None
or wheelSpeeds is None
):
raise RuntimeError(
f"Failed to instantiate RamseteCommand, not all optional arguments were provided.\n \
Feedforward - {feedforward}, LeftController - {leftController}, RightController - {rightController}, WheelSpeeds - {wheelSpeeds} "
)
self.usePID = True
else:
self.usePID = False
# All the parameter checks pass, inform scheduler. Type ignoring is set explictitly for the linter because this
# implementation declares the tuple explicitly, whereas the general implementations use the unpack operator (*)
# to pass the requirements to the scheduler.
self.addRequirements(requirements) # type: ignore
def initialize(self):
self._prevTime = -1
initial_state = self.trajectory.sample(0)
self._prevSpeeds = self.kinematics.toWheelSpeeds(
ChassisSpeeds(
initial_state.velocity,
0,
initial_state.curvature * initial_state.velocity,
)
)
self._timer.restart()
if self.usePID:
self.leftController.reset()
self.rightController.reset()
def execute(self):
curTime = self._timer.get()
dt = curTime - self._prevTime
if self._prevTime < 0:
if self.outputVolts is not None:
self.outputVolts(0.0, 0.0)
if self.outputMPS is not None:
self.outputMPS(0.0, 0.0)
self._prevTime = curTime
return
targetWheelSpeeds = self.kinematics.toWheelSpeeds(
self.follower.calculate(self.pose(), self.trajectory.sample(curTime))
)
leftSpeedSetpoint = targetWheelSpeeds.left
rightSpeedSetpoint = targetWheelSpeeds.right
if self.usePID:
leftFeedforward = self.feedforward.calculate(
leftSpeedSetpoint, (leftSpeedSetpoint - self._prevSpeeds.left) / dt
)
rightFeedforward = self.feedforward.calculate(
rightSpeedSetpoint,
(rightSpeedSetpoint - self._prevSpeeds.right) / dt,
)
leftOutput = leftFeedforward + self.leftController.calculate(
self.wheelspeeds().left, leftSpeedSetpoint
)
rightOutput = rightFeedforward + self.rightController.calculate(
self.wheelspeeds().right, rightSpeedSetpoint
)
self.outputVolts(leftOutput, rightOutput)
else:
leftOutput = leftSpeedSetpoint
rightOutput = rightSpeedSetpoint
self.outputMPS(leftOutput, rightOutput)
self._prevSpeeds = targetWheelSpeeds
self._prevTime = curTime
def end(self, interrupted: bool):
self._timer.stop()
if interrupted:
if self.outputVolts is not None:
self.outputVolts(0.0, 0.0)
if self.outputMPS is not None:
self.outputMPS(0.0, 0.0)
def isFinished(self):
return self._timer.hasElapsed(self.trajectory.totalTime())
def initSendable(self, builder: SendableBuilder):
super().initSendable(builder)
builder.addDoubleProperty(
"leftVelocity", lambda: self._prevSpeeds.left, lambda *float: None
)
builder.addDoubleProperty(
"rightVelocity", lambda: self._prevSpeeds.right, lambda *float: None
)