@@ -47,7 +47,7 @@ def __init__(self, start, goal, obstacle_list,
4747 self .expand_dis = expand_dis
4848 self .goal_sample_rate = goal_sample_rate
4949 self .max_iter = max_iter
50- self .obstacleList = obstacle_list
50+ self .obstacle_list = obstacle_list
5151 self .node_list = []
5252
5353 def planning (self , animation = True ):
@@ -59,14 +59,14 @@ def planning(self, animation=True):
5959
6060 self .node_list = [self .start ]
6161 for i in range (self .max_iter ):
62- rnd = self .get_random_point ()
63- nearest_ind = self .get_nearest_list_index (self .node_list , rnd )
62+ rnd_node = self .get_random_node ()
63+ nearest_ind = self .get_nearest_list_index (self .node_list , rnd_node )
6464 nearest_node = self .node_list [nearest_ind ]
6565
66- new_node = self .steer (rnd , nearest_node )
66+ new_node = self .steer (nearest_node , rnd_node )
6767 new_node .parent = nearest_node
6868
69- if not self .check_collision (new_node , self .obstacleList ):
69+ if not self .check_collision (new_node , self .obstacle_list ):
7070 continue
7171
7272 self .node_list .append (new_node )
@@ -78,16 +78,21 @@ def planning(self, animation=True):
7878 return self .generate_final_course (len (self .node_list ) - 1 )
7979
8080 if animation and i % 5 :
81- self .draw_graph (rnd )
81+ self .draw_graph (rnd_node )
8282
8383 return None # cannot find path
8484
85- def steer (self , rnd , nearest_node ):
86- new_node = self .Node (rnd [0 ], rnd [1 ])
87- d , theta = self .calc_distance_and_angle (nearest_node , new_node )
85+ def steer (self , from_node , to_node ):
86+ d , theta = self .calc_distance_and_angle (from_node , to_node )
8887 if d > self .expand_dis :
89- new_node .x = nearest_node .x + self .expand_dis * math .cos (theta )
90- new_node .y = nearest_node .y + self .expand_dis * math .sin (theta )
88+ x = from_node .x + self .expand_dis * math .cos (theta )
89+ y = from_node .y + self .expand_dis * math .sin (theta )
90+ else :
91+ x = to_node .x
92+ y = to_node .y
93+
94+ new_node = self .Node (x , y )
95+ new_node .parent = from_node
9196
9297 return new_node
9398
@@ -106,25 +111,25 @@ def calc_dist_to_goal(self, x, y):
106111 dy = y - self .end .y
107112 return math .sqrt (dx ** 2 + dy ** 2 )
108113
109- def get_random_point (self ):
114+ def get_random_node (self ):
110115 if random .randint (0 , 100 ) > self .goal_sample_rate :
111- rnd = [ random .uniform (self .min_rand , self .max_rand ),
112- random .uniform (self .min_rand , self .max_rand )]
116+ rnd = self . Node ( random .uniform (self .min_rand , self .max_rand ),
117+ random .uniform (self .min_rand , self .max_rand ))
113118 else : # goal point sampling
114- rnd = [ self .end .x , self .end .y ]
119+ rnd = self .Node ( self . end .x , self .end .y )
115120 return rnd
116121
117122 def draw_graph (self , rnd = None ):
118123 plt .clf ()
119124 if rnd is not None :
120- plt .plot (rnd [ 0 ] , rnd [ 1 ] , "^k" )
125+ plt .plot (rnd . x , rnd . y , "^k" )
121126 for node in self .node_list :
122127 if node .parent :
123128 plt .plot ([node .x , node .parent .x ],
124129 [node .y , node .parent .y ],
125130 "-g" )
126131
127- for (ox , oy , size ) in self .obstacleList :
132+ for (ox , oy , size ) in self .obstacle_list :
128133 plt .plot (ox , oy , "ok" , ms = 30 * size )
129134
130135 plt .plot (self .start .x , self .start .y , "xr" )
@@ -134,8 +139,8 @@ def draw_graph(self, rnd=None):
134139 plt .pause (0.01 )
135140
136141 @staticmethod
137- def get_nearest_list_index (node_list , rnd ):
138- dlist = [(node .x - rnd [ 0 ] ) ** 2 + (node .y - rnd [ 1 ] )
142+ def get_nearest_list_index (node_list , rnd_node ):
143+ dlist = [(node .x - rnd_node . x ) ** 2 + (node .y - rnd_node . y )
139144 ** 2 for node in node_list ]
140145 minind = dlist .index (min (dlist ))
141146
0 commit comments