-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathreeds_shepp_path_planning.py
More file actions
510 lines (390 loc) · 15.4 KB
/
reeds_shepp_path_planning.py
File metadata and controls
510 lines (390 loc) · 15.4 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
"""
Reeds Shepp path planner sample code
author Atsushi Sakai(@Atsushi_twi)
co-author Videh Patel(@videh25) : Added the missing RS paths
co-author fishyy119(@fishyy119) : Improved runtime efficiency
"""
import pathlib
import sys
sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
import math
from typing import List, Tuple
import matplotlib.pyplot as plt
import numpy as np
from numpy.typing import NDArray
from utils.angle import angle_mod
show_animation = True
class Path:
"""
Path data container
"""
def __init__(self):
# course segment length (negative value is backward segment)
self.lengths = []
# course segment type char ("S": straight, "L": left, "R": right)
self.ctypes = []
self.L = 0.0 # Total lengths of the path
self.x = [] # x positions
self.y = [] # y positions
self.yaw = [] # orientations [rad]
self.directions = [] # directions (1:forward, -1:backward)
def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
if isinstance(x, list):
for (ix, iy, iyaw) in zip(x, y, yaw):
plot_arrow(ix, iy, iyaw)
else:
plt.arrow(x, y, length * math.cos(yaw), length * math.sin(yaw), fc=fc,
ec=ec, head_width=width, head_length=width)
plt.plot(x, y)
def pi_2_pi(x):
return angle_mod(x)
def mod2pi(x):
# Be consistent with fmod in cplusplus here.
v = np.mod(x, np.copysign(2.0 * math.pi, x))
if v < -math.pi:
v += 2.0 * math.pi
else:
if v > math.pi:
v -= 2.0 * math.pi
return v
def set_path(paths, lengths, ctypes, step_size):
path = Path()
path.ctypes = ctypes
path.lengths = lengths
path.L = sum(np.abs(lengths))
# check same path exist
for i_path in paths:
type_is_same = (i_path.ctypes == path.ctypes)
length_is_close = (sum(np.abs(i_path.lengths)) - path.L) <= step_size
if type_is_same and length_is_close:
return paths # same path found, so do not insert path
# check path is long enough
if path.L <= step_size:
return paths # too short, so do not insert path
paths.append(path)
return paths
def polar(x, y):
r = math.hypot(x, y)
theta = math.atan2(y, x)
return r, theta
def left_straight_left(x, y, phi):
u, t = polar(x - math.sin(phi), y - 1.0 + math.cos(phi))
if 0.0 <= t <= math.pi:
v = mod2pi(phi - t)
if 0.0 <= v <= math.pi:
return True, [t, u, v], ['L', 'S', 'L']
return False, [], []
def left_straight_right(x, y, phi):
u1, t1 = polar(x + math.sin(phi), y - 1.0 - math.cos(phi))
u1 = u1 ** 2
if u1 >= 4.0:
u = math.sqrt(u1 - 4.0)
theta = math.atan2(2.0, u)
t = mod2pi(t1 + theta)
v = mod2pi(t - phi)
if (t >= 0.0) and (v >= 0.0):
return True, [t, u, v], ['L', 'S', 'R']
return False, [], []
def left_x_right_x_left(x, y, phi):
zeta = x - math.sin(phi)
eeta = y - 1 + math.cos(phi)
u1, theta = polar(zeta, eeta)
if u1 <= 4.0:
A = math.acos(0.25 * u1)
t = mod2pi(A + theta + math.pi/2)
u = mod2pi(math.pi - 2 * A)
v = mod2pi(phi - t - u)
return True, [t, -u, v], ['L', 'R', 'L']
return False, [], []
def left_x_right_left(x, y, phi):
zeta = x - math.sin(phi)
eeta = y - 1 + math.cos(phi)
u1, theta = polar(zeta, eeta)
if u1 <= 4.0:
A = math.acos(0.25 * u1)
t = mod2pi(A + theta + math.pi/2)
u = mod2pi(math.pi - 2*A)
v = mod2pi(-phi + t + u)
return True, [t, -u, -v], ['L', 'R', 'L']
return False, [], []
def left_right_x_left(x, y, phi):
zeta = x - math.sin(phi)
eeta = y - 1 + math.cos(phi)
u1, theta = polar(zeta, eeta)
if u1 <= 4.0:
u = math.acos(1 - u1**2 * 0.125)
A = math.asin(2 * math.sin(u) / u1)
t = mod2pi(-A + theta + math.pi/2)
v = mod2pi(t - u - phi)
return True, [t, u, -v], ['L', 'R', 'L']
return False, [], []
def left_right_x_left_right(x, y, phi):
zeta = x + math.sin(phi)
eeta = y - 1 - math.cos(phi)
u1, theta = polar(zeta, eeta)
# Solutions refering to (2 < u1 <= 4) are considered sub-optimal in paper
# Solutions do not exist for u1 > 4
if u1 <= 2:
A = math.acos((u1 + 2) * 0.25)
t = mod2pi(theta + A + math.pi/2)
u = mod2pi(A)
v = mod2pi(phi - t + 2*u)
if ((t >= 0) and (u >= 0) and (v >= 0)):
return True, [t, u, -u, -v], ['L', 'R', 'L', 'R']
return False, [], []
def left_x_right_left_x_right(x, y, phi):
zeta = x + math.sin(phi)
eeta = y - 1 - math.cos(phi)
u1, theta = polar(zeta, eeta)
u2 = (20 - u1**2) / 16
if (0 <= u2 <= 1):
u = math.acos(u2)
A = math.asin(2 * math.sin(u) / u1)
t = mod2pi(theta + A + math.pi/2)
v = mod2pi(t - phi)
if (t >= 0) and (v >= 0):
return True, [t, -u, -u, v], ['L', 'R', 'L', 'R']
return False, [], []
def left_x_right90_straight_left(x, y, phi):
zeta = x - math.sin(phi)
eeta = y - 1 + math.cos(phi)
u1, theta = polar(zeta, eeta)
if u1 >= 2.0:
u = math.sqrt(u1**2 - 4) - 2
A = math.atan2(2, math.sqrt(u1**2 - 4))
t = mod2pi(theta + A + math.pi/2)
v = mod2pi(t - phi + math.pi/2)
if (t >= 0) and (v >= 0):
return True, [t, -math.pi/2, -u, -v], ['L', 'R', 'S', 'L']
return False, [], []
def left_straight_right90_x_left(x, y, phi):
zeta = x - math.sin(phi)
eeta = y - 1 + math.cos(phi)
u1, theta = polar(zeta, eeta)
if u1 >= 2.0:
u = math.sqrt(u1**2 - 4) - 2
A = math.atan2(math.sqrt(u1**2 - 4), 2)
t = mod2pi(theta - A + math.pi/2)
v = mod2pi(t - phi - math.pi/2)
if (t >= 0) and (v >= 0):
return True, [t, u, math.pi/2, -v], ['L', 'S', 'R', 'L']
return False, [], []
def left_x_right90_straight_right(x, y, phi):
zeta = x + math.sin(phi)
eeta = y - 1 - math.cos(phi)
u1, theta = polar(zeta, eeta)
if u1 >= 2.0:
t = mod2pi(theta + math.pi/2)
u = u1 - 2
v = mod2pi(phi - t - math.pi/2)
if (t >= 0) and (v >= 0):
return True, [t, -math.pi/2, -u, -v], ['L', 'R', 'S', 'R']
return False, [], []
def left_straight_left90_x_right(x, y, phi):
zeta = x + math.sin(phi)
eeta = y - 1 - math.cos(phi)
u1, theta = polar(zeta, eeta)
if u1 >= 2.0:
t = mod2pi(theta)
u = u1 - 2
v = mod2pi(phi - t - math.pi/2)
if (t >= 0) and (v >= 0):
return True, [t, u, math.pi/2, -v], ['L', 'S', 'L', 'R']
return False, [], []
def left_x_right90_straight_left90_x_right(x, y, phi):
zeta = x + math.sin(phi)
eeta = y - 1 - math.cos(phi)
u1, theta = polar(zeta, eeta)
if u1 >= 4.0:
u = math.sqrt(u1**2 - 4) - 4
A = math.atan2(2, math.sqrt(u1**2 - 4))
t = mod2pi(theta + A + math.pi/2)
v = mod2pi(t - phi)
if (t >= 0) and (v >= 0):
return True, [t, -math.pi/2, -u, -math.pi/2, v], ['L', 'R', 'S', 'L', 'R']
return False, [], []
def timeflip(travel_distances):
return [-x for x in travel_distances]
def reflect(steering_directions):
def switch_dir(dirn):
if dirn == 'L':
return 'R'
elif dirn == 'R':
return 'L'
else:
return 'S'
return[switch_dir(dirn) for dirn in steering_directions]
def generate_path(q0, q1, max_curvature, step_size):
dx = q1[0] - q0[0]
dy = q1[1] - q0[1]
dth = q1[2] - q0[2]
c = math.cos(q0[2])
s = math.sin(q0[2])
x = (c * dx + s * dy) * max_curvature
y = (-s * dx + c * dy) * max_curvature
step_size *= max_curvature
paths = []
path_functions = [left_straight_left, left_straight_right, # CSC
left_x_right_x_left, left_x_right_left, left_right_x_left, # CCC
left_right_x_left_right, left_x_right_left_x_right, # CCCC
left_x_right90_straight_left, left_x_right90_straight_right, # CCSC
left_straight_right90_x_left, left_straight_left90_x_right, # CSCC
left_x_right90_straight_left90_x_right] # CCSCC
for path_func in path_functions:
flag, travel_distances, steering_dirns = path_func(x, y, dth)
if flag:
for distance in travel_distances:
if (0.1*sum([abs(d) for d in travel_distances]) < abs(distance) < step_size):
print("Step size too large for Reeds-Shepp paths.")
return []
paths = set_path(paths, travel_distances, steering_dirns, step_size)
flag, travel_distances, steering_dirns = path_func(-x, y, -dth)
if flag:
for distance in travel_distances:
if (0.1*sum([abs(d) for d in travel_distances]) < abs(distance) < step_size):
print("Step size too large for Reeds-Shepp paths.")
return []
travel_distances = timeflip(travel_distances)
paths = set_path(paths, travel_distances, steering_dirns, step_size)
flag, travel_distances, steering_dirns = path_func(x, -y, -dth)
if flag:
for distance in travel_distances:
if (0.1*sum([abs(d) for d in travel_distances]) < abs(distance) < step_size):
print("Step size too large for Reeds-Shepp paths.")
return []
steering_dirns = reflect(steering_dirns)
paths = set_path(paths, travel_distances, steering_dirns, step_size)
flag, travel_distances, steering_dirns = path_func(-x, -y, dth)
if flag:
for distance in travel_distances:
if (0.1*sum([abs(d) for d in travel_distances]) < abs(distance) < step_size):
print("Step size too large for Reeds-Shepp paths.")
return []
travel_distances = timeflip(travel_distances)
steering_dirns = reflect(steering_dirns)
paths = set_path(paths, travel_distances, steering_dirns, step_size)
return paths
def calc_interpolate_dists_list(lengths: List[float], step_size: float) -> List[NDArray[np.floating]]:
interpolate_dists_list: List[NDArray[np.floating]] = []
for length in lengths:
d_dist = step_size if length >= 0.0 else -step_size
interp_core = np.arange(0.0, length, d_dist, dtype=np.float64)
interp_dists = np.empty(len(interp_core) + 1, dtype=np.float64)
interp_dists[:-1] = interp_core
interp_dists[-1] = length
interpolate_dists_list.append(interp_dists)
return interpolate_dists_list
def generate_local_course(
lengths: List[float],
modes: List[str],
max_curvature: float,
step_size: float,
) -> Tuple[NDArray[np.floating], NDArray[np.floating], NDArray[np.floating], NDArray[np.signedinteger]]:
interpolate_dists_list = calc_interpolate_dists_list(lengths, step_size * max_curvature)
total_len = sum(len(arr) for arr in interpolate_dists_list)
xs = np.empty(total_len, dtype=np.float64)
ys = np.empty_like(xs)
yaws = np.empty_like(xs)
directions = np.empty_like(xs, dtype=np.int32)
origin_x, origin_y, origin_yaw = 0.0, 0.0, 0.0
idx = 0
for interp_dists, mode, length in zip(interpolate_dists_list, modes, lengths):
n = len(interp_dists)
x_arr, y_arr, yaw_arr, dir_arr = interpolate_vectorized(
interp_dists, length, mode, max_curvature, origin_x, origin_y, origin_yaw
)
xs[idx : idx + n] = x_arr
ys[idx : idx + n] = y_arr
yaws[idx : idx + n] = yaw_arr
directions[idx : idx + n] = dir_arr
origin_x = x_arr[-1]
origin_y = y_arr[-1]
origin_yaw = yaw_arr[-1]
idx += n
return xs, ys, yaws, directions
def interpolate_vectorized(
dists: NDArray[np.floating],
length: float,
mode: str,
max_curvature: float,
origin_x: float,
origin_y: float,
origin_yaw: float,
) -> Tuple[NDArray[np.floating], NDArray[np.floating], NDArray[np.floating], NDArray[np.signedinteger]]:
if mode == "S":
x = origin_x + dists / max_curvature * math.cos(origin_yaw)
y = origin_y + dists / max_curvature * math.sin(origin_yaw)
yaw = np.full_like(dists, origin_yaw)
else: # curve
ldx = np.sin(dists) / max_curvature
if mode == "L": # left turn
ldy = (1.0 - np.cos(dists)) / max_curvature
yaw = origin_yaw + dists
else: # elif mode == "R": # right turn
ldy = (1.0 - np.cos(dists)) / (-max_curvature)
yaw = origin_yaw - dists
cos_oy = math.cos(-origin_yaw)
sin_oy = math.sin(-origin_yaw)
gdx = cos_oy * ldx + sin_oy * ldy
gdy = -sin_oy * ldx + cos_oy * ldy
x = origin_x + gdx
y = origin_y + gdy
direction = 1 if length > 0 else -1
return x, y, yaw, np.full_like(dists, direction, dtype=np.int32)
def calc_paths(sx, sy, syaw, gx, gy, gyaw, maxc, step_size):
q0 = [sx, sy, syaw]
q1 = [gx, gy, gyaw]
paths = generate_path(q0, q1, maxc, step_size)
for path in paths:
xs, ys, yaws, directions = generate_local_course(path.lengths, path.ctypes, maxc, step_size)
# convert global coordinate
local_pts = np.vstack([xs, ys, np.ones_like(xs)]) # shape: [3, N]
cos_y = np.cos(syaw)
sin_y = np.sin(syaw)
se2 = np.array([[cos_y, -sin_y, sx],[sin_y, cos_y, sy],[0, 0, 1]])
global_pts = se2 @ local_pts # shape: [3, N]
path.x = global_pts[0, :].tolist()
path.y = global_pts[1, :].tolist()
path.yaw = ((yaws + syaw + np.pi) % (2 * np.pi) - np.pi).tolist()
path.directions = directions.tolist()
path.lengths = [l / maxc for l in path.lengths]
path.L /= maxc
return paths
def reeds_shepp_path_planning(sx, sy, syaw, gx, gy, gyaw, maxc, step_size=0.2):
paths = calc_paths(sx, sy, syaw, gx, gy, gyaw, maxc, step_size)
if not paths:
return None, None, None, None, None # could not generate any path
# search minimum cost path
best_path_index = paths.index(min(paths, key=lambda p: abs(p.L)))
b_path = paths[best_path_index]
return b_path.x, b_path.y, b_path.yaw, b_path.ctypes, b_path.lengths
def main():
print("Reeds Shepp path planner sample start!!")
start_x = -1.0 # [m]
start_y = -4.0 # [m]
start_yaw = np.deg2rad(-20.0) # [rad]
end_x = 5.0 # [m]
end_y = 5.0 # [m]
end_yaw = np.deg2rad(25.0) # [rad]
curvature = 0.1
step_size = 0.05
xs, ys, yaws, modes, lengths = reeds_shepp_path_planning(start_x, start_y,
start_yaw, end_x,
end_y, end_yaw,
curvature,
step_size)
if not xs:
assert False, "No path"
if show_animation: # pragma: no cover
plt.cla()
plt.plot(xs, ys, label="final course " + str(modes))
print(f"{lengths=}")
# plotting
plot_arrow(start_x, start_y, start_yaw)
plot_arrow(end_x, end_y, end_yaw)
plt.legend()
plt.grid(True)
plt.axis("equal")
plt.show()
if __name__ == '__main__':
main()