6
6
7
7
"""
8
8
9
- import random
10
9
import math
11
10
import numpy as np
12
11
import matplotlib .pyplot as plt
13
- from scipy .spatial import cKDTree
12
+ from scipy .spatial import KDTree
14
13
15
14
# parameter
16
15
N_SAMPLE = 500 # number of sample_points
@@ -36,19 +35,35 @@ def __str__(self):
36
35
str (self .cost ) + "," + str (self .parent_index )
37
36
38
37
39
- def prm_planning (sx , sy , gx , gy , ox , oy , rr ):
40
-
41
- obstacle_kd_tree = cKDTree (np .vstack ((ox , oy )).T )
38
+ def prm_planning (start_x , start_y , goal_x , goal_y ,
39
+ obstacle_x_list , obstacle_y_list , robot_radius , * , rng = None ):
40
+ """
41
+ Run probabilistic road map planning
42
+
43
+ :param start_x:
44
+ :param start_y:
45
+ :param goal_x:
46
+ :param goal_y:
47
+ :param obstacle_x_list:
48
+ :param obstacle_y_list:
49
+ :param robot_radius:
50
+ :param rng:
51
+ :return:
52
+ """
53
+ obstacle_kd_tree = KDTree (np .vstack ((obstacle_x_list , obstacle_y_list )).T )
42
54
43
- sample_x , sample_y = sample_points (sx , sy , gx , gy ,
44
- rr , ox , oy , obstacle_kd_tree )
55
+ sample_x , sample_y = sample_points (start_x , start_y , goal_x , goal_y ,
56
+ robot_radius ,
57
+ obstacle_x_list , obstacle_y_list ,
58
+ obstacle_kd_tree , rng )
45
59
if show_animation :
46
60
plt .plot (sample_x , sample_y , ".b" )
47
61
48
- road_map = generate_road_map (sample_x , sample_y , rr , obstacle_kd_tree )
62
+ road_map = generate_road_map (sample_x , sample_y ,
63
+ robot_radius , obstacle_kd_tree )
49
64
50
65
rx , ry = dijkstra_planning (
51
- sx , sy , gx , gy , road_map , sample_x , sample_y )
66
+ start_x , start_y , goal_x , goal_y , road_map , sample_x , sample_y )
52
67
53
68
return rx , ry
54
69
@@ -88,13 +103,13 @@ def generate_road_map(sample_x, sample_y, rr, obstacle_kd_tree):
88
103
89
104
sample_x: [m] x positions of sampled points
90
105
sample_y: [m] y positions of sampled points
91
- rr : Robot Radius[m]
106
+ robot_radius : Robot Radius[m]
92
107
obstacle_kd_tree: KDTree object of obstacles
93
108
"""
94
109
95
110
road_map = []
96
111
n_sample = len (sample_x )
97
- sample_kd_tree = cKDTree (np .vstack ((sample_x , sample_y )).T )
112
+ sample_kd_tree = KDTree (np .vstack ((sample_x , sample_y )).T )
98
113
99
114
for (i , ix , iy ) in zip (range (n_sample ), sample_x , sample_y ):
100
115
@@ -122,11 +137,11 @@ def dijkstra_planning(sx, sy, gx, gy, road_map, sample_x, sample_y):
122
137
"""
123
138
s_x: start x position [m]
124
139
s_y: start y position [m]
125
- gx : goal x position [m]
126
- gy : goal y position [m]
127
- ox : x position list of Obstacles [m]
128
- oy : y position list of Obstacles [m]
129
- rr : robot radius [m]
140
+ goal_x : goal x position [m]
141
+ goal_y : goal y position [m]
142
+ obstacle_x_list : x position list of Obstacles [m]
143
+ obstacle_y_list : y position list of Obstacles [m]
144
+ robot_radius : robot radius [m]
130
145
road_map: ??? [m]
131
146
sample_x: ??? [m]
132
147
sample_y: ??? [m]
@@ -215,17 +230,20 @@ def plot_road_map(road_map, sample_x, sample_y): # pragma: no cover
215
230
[sample_y [i ], sample_y [ind ]], "-k" )
216
231
217
232
218
- def sample_points (sx , sy , gx , gy , rr , ox , oy , obstacle_kd_tree ):
233
+ def sample_points (sx , sy , gx , gy , rr , ox , oy , obstacle_kd_tree , rng ):
219
234
max_x = max (ox )
220
235
max_y = max (oy )
221
236
min_x = min (ox )
222
237
min_y = min (oy )
223
238
224
239
sample_x , sample_y = [], []
225
240
241
+ if rng is None :
242
+ rng = np .random .default_rng ()
243
+
226
244
while len (sample_x ) <= N_SAMPLE :
227
- tx = (random .random () * (max_x - min_x )) + min_x
228
- ty = (random .random () * (max_y - min_y )) + min_y
245
+ tx = (rng .random () * (max_x - min_x )) + min_x
246
+ ty = (rng .random () * (max_y - min_y )) + min_y
229
247
230
248
dist , index = obstacle_kd_tree .query ([tx , ty ])
231
249
@@ -241,7 +259,7 @@ def sample_points(sx, sy, gx, gy, rr, ox, oy, obstacle_kd_tree):
241
259
return sample_x , sample_y
242
260
243
261
244
- def main ():
262
+ def main (rng = None ):
245
263
print (__file__ + " start!!" )
246
264
247
265
# start and goal position
@@ -280,7 +298,7 @@ def main():
280
298
plt .grid (True )
281
299
plt .axis ("equal" )
282
300
283
- rx , ry = prm_planning (sx , sy , gx , gy , ox , oy , robot_size )
301
+ rx , ry = prm_planning (sx , sy , gx , gy , ox , oy , robot_size , rng = rng )
284
302
285
303
assert rx , 'Cannot found path'
286
304
0 commit comments