1- #!/usr/bin/python
2- # -*- coding: utf-8 -*-
31"""
4- @brief: Path Planning Sample Code with RRT for car like robot.
52
6- @author: AtsushiSakai(@Atsushi_twi)
3+ Path Planning Sample Code with RRT for car like robot.
74
8- @license: MIT
5+ author: AtsushiSakai(@Atsushi_twi)
96
107"""
118
1411import copy
1512import numpy as np
1613import reeds_shepp_path_planning
14+ import matplotlib .pyplot as plt
15+
16+ show_animation = True
1717
1818
1919class RRT ():
20- u """
20+ """
2121 Class for RRT Planning
2222 """
2323
2424 def __init__ (self , start , goal , obstacleList , randArea ,
25- goalSampleRate = 10 , maxIter = 1000 ):
26- u """
25+ goalSampleRate = 10 , maxIter = 200 ):
26+ """
2727 Setting Parameter
2828
2929 start:Start Position [x,y]
@@ -38,36 +38,33 @@ def __init__(self, start, goal, obstacleList, randArea,
3838 self .maxrand = randArea [1 ]
3939 self .goalSampleRate = goalSampleRate
4040 self .maxIter = maxIter
41+ self .obstacleList = obstacleList
4142
4243 def Planning (self , animation = True ):
43- u """
44+ """
4445 Pathplanning
4546
4647 animation: flag for animation on or off
4748 """
4849
4950 self .nodeList = [self .start ]
5051 for i in range (self .maxIter ):
51- print (i )
5252 rnd = self .get_random_point ()
5353 nind = self .GetNearestListIndex (self .nodeList , rnd )
5454
5555 newNode = self .steer (rnd , nind )
56- # print(newNode.cost)
5756
58- if self .CollisionCheck (newNode , obstacleList ):
57+ if self .CollisionCheck (newNode , self . obstacleList ):
5958 nearinds = self .find_near_nodes (newNode )
6059 newNode = self .choose_parent (newNode , nearinds )
6160 self .nodeList .append (newNode )
6261 self .rewire (newNode , nearinds )
6362
6463 if animation and i % 5 == 0 :
6564 self .DrawGraph (rnd = rnd )
66- matplotrecorder .save_frame () # save each frame
6765
6866 # generate coruse
6967 lastIndex = self .get_best_last_index ()
70- # print(lastIndex)
7168 path = self .gen_final_course (lastIndex )
7269 return path
7370
@@ -78,7 +75,7 @@ def choose_parent(self, newNode, nearinds):
7875 dlist = []
7976 for i in nearinds :
8077 tNode = self .steer (newNode , i )
81- if self .CollisionCheck (tNode , obstacleList ):
78+ if self .CollisionCheck (tNode , self . obstacleList ):
8279 dlist .append (tNode .cost )
8380 else :
8481 dlist .append (float ("inf" ))
@@ -104,7 +101,6 @@ def pi_2_pi(self, angle):
104101 return angle
105102
106103 def steer (self , rnd , nind ):
107- # print(rnd)
108104 curvature = 1.0
109105
110106 nearestNode = self .nodeList [nind ]
@@ -149,16 +145,16 @@ def get_best_last_index(self):
149145 for (i , node ) in enumerate (self .nodeList ):
150146 if self .calc_dist_to_goal (node .x , node .y ) <= XYTH :
151147 goalinds .append (i )
152- print ("OK XY TH num is" )
153- print (len (goalinds ))
148+ # print("OK XY TH num is")
149+ # print(len(goalinds))
154150
155151 # angle check
156152 fgoalinds = []
157153 for i in goalinds :
158154 if abs (self .nodeList [i ].yaw - self .end .yaw ) <= YAWTH :
159155 fgoalinds .append (i )
160- print ("OK YAW TH num is" )
161- print (len (fgoalinds ))
156+ # print("OK YAW TH num is")
157+ # print(len(fgoalinds))
162158
163159 mincost = min ([self .nodeList [i ].cost for i in fgoalinds ])
164160 for i in fgoalinds :
@@ -200,18 +196,17 @@ def rewire(self, newNode, nearinds):
200196 nearNode = self .nodeList [i ]
201197 tNode = self .steer (nearNode , nnode - 1 )
202198
203- obstacleOK = self .CollisionCheck (tNode , obstacleList )
199+ obstacleOK = self .CollisionCheck (tNode , self . obstacleList )
204200 imporveCost = nearNode .cost > tNode .cost
205201
206202 if obstacleOK and imporveCost :
207203 # print("rewire")
208204 self .nodeList [i ] = tNode
209205
210206 def DrawGraph (self , rnd = None ):
211- u """
207+ """
212208 Draw Graph
213209 """
214- import matplotlib .pyplot as plt
215210 plt .clf ()
216211 if rnd is not None :
217212 plt .plot (rnd .x , rnd .y , "^k" )
@@ -221,7 +216,7 @@ def DrawGraph(self, rnd=None):
221216 # plt.plot([node.x, self.nodeList[node.parent].x], [
222217 # node.y, self.nodeList[node.parent].y], "-g")
223218
224- for (ox , oy , size ) in obstacleList :
219+ for (ox , oy , size ) in self . obstacleList :
225220 plt .plot (ox , oy , "ok" , ms = 30 * size )
226221
227222 reeds_shepp_path_planning .plot_arrow (
@@ -258,7 +253,7 @@ def CollisionCheck(self, node, obstacleList):
258253
259254
260255class Node ():
261- u """
256+ """
262257 RRT Node
263258 """
264259
@@ -273,11 +268,8 @@ def __init__(self, x, y, yaw):
273268 self .parent = None
274269
275270
276- if __name__ == '__main__' :
271+ def main () :
277272 print ("Start rrt start planning" )
278- import matplotlib .pyplot as plt
279- import matplotrecorder
280- matplotrecorder .donothing = True
281273
282274 # ====Search Path with RRT====
283275 # obstacleList = [
@@ -305,17 +297,15 @@ def __init__(self, x, y, yaw):
305297 goal = [6.0 , 7.0 , math .radians (90.0 )]
306298
307299 rrt = RRT (start , goal , randArea = [- 2.0 , 15.0 ], obstacleList = obstacleList )
308- path = rrt .Planning (animation = False )
300+ path = rrt .Planning (animation = show_animation )
309301
310302 # Draw final path
311303 rrt .DrawGraph ()
312304 plt .plot ([x for (x , y ) in path ], [y for (x , y ) in path ], '-r' )
313305 plt .grid (True )
314306 plt .pause (0.001 )
315-
316- for i in range (10 ):
317- matplotrecorder .save_frame () # save each frame
318-
319307 plt .show ()
320308
321- # matplotrecorder.save_movie("animation.gif", 0.1)
309+
310+ if __name__ == '__main__' :
311+ main ()
0 commit comments