2020
2121LQRplanner .show_animation = False
2222
23- STEP_SIZE = 0.05
23+ STEP_SIZE = 0.05 # step size of local path
24+ XYTH = 0.5 # [m] acceptance xy distance in final paths
2425
2526
2627class RRT ():
@@ -47,7 +48,7 @@ def __init__(self, start, goal, obstacleList, randArea,
4748 self .maxIter = maxIter
4849 self .obstacleList = obstacleList
4950
50- def Planning (self , animation = True ):
51+ def planning (self , animation = True ):
5152 """
5253 Pathplanning
5354
@@ -57,13 +58,13 @@ def Planning(self, animation=True):
5758 self .nodeList = [self .start ]
5859 for i in range (self .maxIter ):
5960 rnd = self .get_random_point ()
60- nind = self .GetNearestListIndex (self .nodeList , rnd )
61+ nind = self .get_nearest_index (self .nodeList , rnd )
6162
6263 newNode = self .steer (rnd , nind )
6364 if newNode is None :
6465 continue
6566
66- if self .CollisionCheck (newNode , self .obstacleList ):
67+ if self .check_collision (newNode , self .obstacleList ):
6768 nearinds = self .find_near_nodes (newNode )
6869 newNode = self .choose_parent (newNode , nearinds )
6970 if newNode is None :
@@ -72,7 +73,7 @@ def Planning(self, animation=True):
7273 self .rewire (newNode , nearinds )
7374
7475 if animation and i % 5 == 0 :
75- self .DrawGraph (rnd = rnd )
76+ self .draw_graph (rnd = rnd )
7677
7778 # generate coruse
7879 lastIndex = self .get_best_last_index ()
@@ -91,7 +92,7 @@ def choose_parent(self, newNode, nearinds):
9192 if tNode is None :
9293 continue
9394
94- if self .CollisionCheck (tNode , self .obstacleList ):
95+ if self .check_collision (tNode , self .obstacleList ):
9596 dlist .append (tNode .cost )
9697 else :
9798 dlist .append (float ("inf" ))
@@ -164,7 +165,6 @@ def get_random_point(self):
164165 random .uniform (- math .pi , math .pi )
165166 ]
166167 else : # goal point sampling
167- # print("goal sample")
168168 rnd = [self .end .x , self .end .y ]
169169
170170 node = Node (rnd [0 ], rnd [1 ])
@@ -174,8 +174,6 @@ def get_random_point(self):
174174 def get_best_last_index (self ):
175175 # print("get_best_last_index")
176176
177- XYTH = 0.5
178-
179177 goalinds = []
180178 for (i , node ) in enumerate (self .nodeList ):
181179 if self .calc_dist_to_goal (node .x , node .y ) <= XYTH :
@@ -197,7 +195,6 @@ def gen_final_course(self, goalind):
197195 node = self .nodeList [goalind ]
198196 for (ix , iy ) in zip (reversed (node .path_x ), reversed (node .path_y )):
199197 path .append ([ix , iy ])
200- # path.append([node.x, node.y])
201198 goalind = node .parent
202199 path .append ([self .start .x , self .start .y ])
203200 return path
@@ -224,20 +221,18 @@ def rewire(self, newNode, nearinds):
224221 if tNode is None :
225222 continue
226223
227- obstacleOK = self .CollisionCheck (tNode , self .obstacleList )
224+ obstacleOK = self .check_collision (tNode , self .obstacleList )
228225 imporveCost = nearNode .cost > tNode .cost
229226
230227 if obstacleOK and imporveCost :
231228 # print("rewire")
232229 self .nodeList [i ] = tNode
233230
234- def DrawGraph (self , rnd = None ):
235- """
236- Draw Graph
237- """
231+ def draw_graph (self , rnd = None ):
238232 plt .clf ()
239233 if rnd is not None :
240234 plt .plot (rnd .x , rnd .y , "^k" )
235+
241236 for node in self .nodeList :
242237 if node .parent is not None :
243238 plt .plot (node .path_x , node .path_y , "-g" )
@@ -252,23 +247,26 @@ def DrawGraph(self, rnd=None):
252247 plt .grid (True )
253248 plt .pause (0.01 )
254249
255- def GetNearestListIndex (self , nodeList , rnd ):
250+ def get_nearest_index (self , nodeList , rnd ):
256251 dlist = [(node .x - rnd .x ) ** 2 +
257252 (node .y - rnd .y ) ** 2
258253 for node in nodeList ]
259254 minind = dlist .index (min (dlist ))
260255
261256 return minind
262257
263- def CollisionCheck (self , node , obstacleList ):
258+ def check_collision (self , node , obstacleList ):
259+
260+ px = np .array (node .path_x )
261+ py = np .array (node .path_y )
264262
265263 for (ox , oy , size ) in obstacleList :
266- for ( ix , iy ) in zip ( node . path_x , node . path_y ):
267- dx = ox - ix
268- dy = oy - iy
269- d = dx * dx + dy * dy
270- if d <= size ** 2 :
271- return False # collision
264+ dx = ox - px
265+ dy = oy - py
266+ d = dx ** 2 + dy ** 2
267+ dmin = min ( d )
268+ if dmin <= size ** 2 :
269+ return False # collision
272270
273271 return True # safe
274272
@@ -305,16 +303,18 @@ def main():
305303 goal = [6.0 , 7.0 ]
306304
307305 rrt = RRT (start , goal , randArea = [- 2.0 , 15.0 ], obstacleList = obstacleList )
308- path = rrt .Planning (animation = show_animation )
306+ path = rrt .planning (animation = show_animation )
309307
310308 # Draw final path
311309 if show_animation :
312- rrt .DrawGraph ()
310+ rrt .draw_graph ()
313311 plt .plot ([x for (x , y ) in path ], [y for (x , y ) in path ], '-r' )
314312 plt .grid (True )
315313 plt .pause (0.001 )
316314 plt .show ()
317315
316+ print ("Done" )
317+
318318
319319if __name__ == '__main__' :
320320 main ()
0 commit comments