66
77"""
88
9- import random
109import math
1110import numpy as np
1211import matplotlib .pyplot as plt
13- from scipy .spatial import cKDTree
12+ from scipy .spatial import KDTree
1413
1514# parameter
1615N_SAMPLE = 500 # number of sample_points
@@ -36,19 +35,35 @@ def __str__(self):
3635 str (self .cost ) + "," + str (self .parent_index )
3736
3837
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 )
4254
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 )
4559 if show_animation :
4660 plt .plot (sample_x , sample_y , ".b" )
4761
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 )
4964
5065 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 )
5267
5368 return rx , ry
5469
@@ -88,13 +103,13 @@ def generate_road_map(sample_x, sample_y, rr, obstacle_kd_tree):
88103
89104 sample_x: [m] x positions of sampled points
90105 sample_y: [m] y positions of sampled points
91- rr : Robot Radius[m]
106+ robot_radius : Robot Radius[m]
92107 obstacle_kd_tree: KDTree object of obstacles
93108 """
94109
95110 road_map = []
96111 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 )
98113
99114 for (i , ix , iy ) in zip (range (n_sample ), sample_x , sample_y ):
100115
@@ -122,11 +137,11 @@ def dijkstra_planning(sx, sy, gx, gy, road_map, sample_x, sample_y):
122137 """
123138 s_x: start x position [m]
124139 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]
130145 road_map: ??? [m]
131146 sample_x: ??? [m]
132147 sample_y: ??? [m]
@@ -215,17 +230,20 @@ def plot_road_map(road_map, sample_x, sample_y): # pragma: no cover
215230 [sample_y [i ], sample_y [ind ]], "-k" )
216231
217232
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 ):
219234 max_x = max (ox )
220235 max_y = max (oy )
221236 min_x = min (ox )
222237 min_y = min (oy )
223238
224239 sample_x , sample_y = [], []
225240
241+ if rng is None :
242+ rng = np .random .default_rng ()
243+
226244 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
229247
230248 dist , index = obstacle_kd_tree .query ([tx , ty ])
231249
@@ -241,7 +259,7 @@ def sample_points(sx, sy, gx, gy, rr, ox, oy, obstacle_kd_tree):
241259 return sample_x , sample_y
242260
243261
244- def main ():
262+ def main (rng = None ):
245263 print (__file__ + " start!!" )
246264
247265 # start and goal position
@@ -280,7 +298,7 @@ def main():
280298 plt .grid (True )
281299 plt .axis ("equal" )
282300
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 )
284302
285303 assert rx , 'Cannot found path'
286304
0 commit comments