/
trajectory.py
280 lines (212 loc) · 12.4 KB
/
trajectory.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
from abc import ABC, abstractmethod
import math
from typing import Mapping, List
from swerve_controller.control_model import ControlModelBase
from .errors import IncompleteTrajectoryException
from .drive_module import DriveModule
from .profile import LinearProfile, MultiPointLinearProfile, ProfilePoint, TransientValueProfile
from .states import BodyState, DriveModuleDesiredValues, DriveModuleMeasuredValues, BodyMotion
class DriveModuleProfile(object):
def __init__(self, steering_profile: TransientValueProfile, drive_profile: TransientValueProfile):
self.steering_profile = steering_profile
self.drive_profile = drive_profile
def profile_for_steering(self) -> TransientValueProfile:
return self.steering_profile
def profile_for_drive(self) -> TransientValueProfile:
return self.drive_profile
# A collection of position / velocity / acceleration profiles
class LinearBodyMotionTrajectory(object):
def __init__(self, current: BodyState, desired: BodyMotion, min_trajectory_time_in_seconds: float):
self.start_state = current
self.end_state = desired
self.min_trajectory_time_in_seconds = min_trajectory_time_in_seconds
self.profile = [
LinearProfile(current.motion_in_body_coordinates.linear_velocity.x, desired.linear_velocity.x),
LinearProfile(current.motion_in_body_coordinates.linear_velocity.y, desired.linear_velocity.y),
LinearProfile(current.motion_in_body_coordinates.linear_velocity.z, desired.linear_velocity.z),
LinearProfile(current.motion_in_body_coordinates.angular_velocity.x, desired.angular_velocity.x),
LinearProfile(current.motion_in_body_coordinates.angular_velocity.y, desired.angular_velocity.y),
LinearProfile(current.motion_in_body_coordinates.angular_velocity.z, desired.angular_velocity.z),
]
def body_motion_at(self, time_fraction: float) -> BodyMotion:
return BodyMotion(
self.profile[0].value_at(time_fraction),
self.profile[1].value_at(time_fraction),
self.profile[5].value_at(time_fraction)
)
def time_span(self) -> float:
return self.min_trajectory_time_in_seconds
class ModuleStateTrajectory(ABC):
@abstractmethod
def align_module_profiles(self):
pass
@abstractmethod
def time_span(self) -> float:
pass
@abstractmethod
def value_for_module_at(self, id: str, time_fraction: float) -> DriveModuleMeasuredValues:
pass
class LinearDriveModuleStateTrajectory(ModuleStateTrajectory):
def __init__(self, drive_modules: List[DriveModule], min_trajectory_time_in_seconds: float):
self.modules = drive_modules
self.start_states: List[DriveModuleMeasuredValues] = []
self.end_states: List[DriveModuleDesiredValues] = []
self.min_trajectory_time_in_seconds = min_trajectory_time_in_seconds
# Kinda want a constant jerk profile
self.profiles: Mapping[str, List[TransientValueProfile]] = {}
def align_module_profiles(self):
if len(self.start_states) == 0 or len(self.end_states) == 0:
raise IncompleteTrajectoryException()
# for each profile adjust it in time such that none of the velocities / accelerations are too high for the motors to handle
# Then scale the profiles to match in time.
pass
def _create_profiles(self):
if len(self.start_states) == 0:
return
if len(self.end_states) == 0:
return
self.profiles.clear()
for i in range(len(self.modules)):
start = self.start_states[i]
end = self.end_states[i]
end_steering_angle = end.steering_angle_in_radians if not math.isinf(end.steering_angle_in_radians) else start.orientation_in_body_coordinates.z
module_profiles = [
# Orientation
LinearProfile(start.orientation_in_body_coordinates.z, end_steering_angle),
# Drive velocity
LinearProfile(start.drive_velocity_in_module_coordinates.x, end.drive_velocity_in_meters_per_second),
]
self.profiles[self.modules[i].name] = module_profiles
def set_current_state(self, states: List[DriveModuleMeasuredValues]):
if len(states) != len(self.modules):
raise ValueError(f"The length of the drive module states list ({ len(states) }) does not match the number of drive modules.")
self.start_states = states
self._create_profiles()
def set_desired_end_state(self, states: List[DriveModuleDesiredValues]):
if len(states) != len(self.modules):
raise ValueError(f"The length of the drive module states list ({ len(states) }) does not match the number of drive modules.")
self.end_states = states
self._create_profiles()
def time_span(self) -> float:
return self.min_trajectory_time_in_seconds
def value_for_module_at(self, id: str, time_fraction: float) -> DriveModuleMeasuredValues:
if len(self.start_states) == 0 or len(self.end_states) == 0:
raise IncompleteTrajectoryException()
if not id in self.profiles:
raise ValueError(f"There are no profiles for a drive module with name { id }")
steering_module: DriveModule = None
for x in self.modules:
if x.name == id:
steering_module = x
break
profiles = self.profiles[id]
return DriveModuleMeasuredValues(
steering_module.name,
steering_module.steering_axis_xy_position.x,
steering_module.steering_axis_xy_position.y,
profiles[0].value_at(time_fraction),
profiles[0].first_derivative_at(time_fraction),
profiles[0].second_derivative_at(time_fraction),
profiles[0].third_derivative_at(time_fraction),
profiles[1].value_at(time_fraction),
profiles[1].first_derivative_at(time_fraction),
profiles[1].second_derivative_at(time_fraction),
)
class BodyControlledDriveModuleTrajectory(ModuleStateTrajectory):
def __init__(self, drive_modules: List[DriveModule], control_model: ControlModelBase, min_trajectory_time_in_seconds: float, min_body_to_module_resolution_per_second: float):
self.modules = drive_modules
self.control_model = control_model
self.start_state_modules: List[DriveModuleMeasuredValues] = []
self.end_state_body: BodyState = None
self.min_trajectory_time_in_seconds = min_trajectory_time_in_seconds
self.min_body_to_module_resolution_per_second = min_body_to_module_resolution_per_second
# Kinda want a constant jerk profile
self.module_profiles: Mapping[str, List[MultiPointLinearProfile]] = {}
def align_module_profiles(self):
if len(self.start_state_modules) == 0 or len(self.end_state_body) == 0:
raise IncompleteTrajectoryException()
# for each profile adjust it in time such that none of the velocities / accelerations are too high for the motors to handle
# Then scale the profiles to match in time.
pass
def _create_profiles(self):
if len(self.start_state_modules) == 0:
return
if self.end_state_body is None:
return
drive_module_end_states = self.control_model.state_of_wheel_modules_from_body_motion(self.end_state_body)
# Create the module profiles with start and end states only. The intermediate states will be added as we
# iterate through the body profile
self.module_profiles.clear()
for i in range(len(self.modules)):
start = self.start_state_modules[i]
end = drive_module_end_states[i][0]
end_steering_angle = end.steering_angle_in_radians if not math.isinf(end.steering_angle_in_radians) else start.orientation_in_body_coordinates.z
module_profiles = [
# Orientation
MultiPointLinearProfile(start.orientation_in_body_coordinates.z, end_steering_angle),
# Drive velocity
MultiPointLinearProfile(start.drive_velocity_in_module_coordinates.x, end.drive_velocity_in_meters_per_second),
]
self.module_profiles[self.modules[i].name] = module_profiles
# Compute the body profile
start_state_body = self.control_model.body_motion_from_wheel_module_states(self.start_state_modules)
body_profiles = [
LinearProfile(start_state_body.linear_velocity.x, self.end_state_body.linear_velocity.x),
LinearProfile(start_state_body.linear_velocity.y, self.end_state_body.linear_velocity.y),
LinearProfile(start_state_body.linear_velocity.z, self.end_state_body.linear_velocity.z),
LinearProfile(start_state_body.angular_velocity.x, self.end_state_body.angular_velocity.x),
LinearProfile(start_state_body.angular_velocity.y, self.end_state_body.angular_velocity.y),
LinearProfile(start_state_body.angular_velocity.z, self.end_state_body.angular_velocity.z),
]
# Compute intermediate steps for the modules
number_of_frames = math.ceil(self.min_trajectory_time_in_seconds * self.min_body_to_module_resolution_per_second)
# We don't include the start and end item because those are already there
for i in range(1, number_of_frames):
time_fraction = float(i) / float(number_of_frames)
body_motion_at_time = BodyMotion(
body_profiles[0].value_at(time_fraction),
body_profiles[1].value_at(time_fraction),
body_profiles[5].value_at(time_fraction)
)
drive_module_states = self.control_model.state_of_wheel_modules_from_body_motion(body_motion_at_time)
for i in range(len(self.modules)):
# There are two options. For now pick the first one. Realistically we should be picking the one that matches
# the previous state and the other modules best
intermediate_state_for_module = drive_module_states[i][0]
# Orientation
self.module_profiles[self.modules[i].name][0].add_value(time_fraction, intermediate_state_for_module.steering_angle_in_radians)
# Drive velocity
self.module_profiles[self.modules[i].name][1].add_value(time_fraction, intermediate_state_for_module.drive_velocity_in_meters_per_second)
def set_current_state(self, module_states: List[DriveModuleMeasuredValues]):
if len(module_states) != len(self.modules):
raise ValueError(f"The length of the drive module states list ({ len(module_states) }) does not match the number of drive modules.")
self.start_state_modules = module_states
self._create_profiles()
def set_desired_end_state(self, body_state: BodyState):
self.end_state_body = body_state
self._create_profiles()
def time_span(self) -> float:
return self.min_trajectory_time_in_seconds
def value_for_module_at(self, id: str, time_fraction: float) -> DriveModuleMeasuredValues:
if len(self.start_state_modules) == 0 or self.end_state_body is None:
raise IncompleteTrajectoryException()
if not id in self.module_profiles:
raise ValueError(f"There are no profiles for a drive module with name { id }")
steering_module: DriveModule = None
for x in self.modules:
if x.name == id:
steering_module = x
break
profiles = self.module_profiles[id]
return DriveModuleMeasuredValues(
steering_module.name,
steering_module.steering_axis_xy_position.x,
steering_module.steering_axis_xy_position.y,
profiles[0].value_at(time_fraction),
profiles[0].first_derivative_at(time_fraction),
profiles[0].second_derivative_at(time_fraction),
profiles[0].third_derivative_at(time_fraction),
profiles[1].value_at(time_fraction),
profiles[1].first_derivative_at(time_fraction),
profiles[1].second_derivative_at(time_fraction),
)