11#!/usr/bin/python
22# -*- coding: utf-8 -*-
33"""
4- @brief: Path Planning Sample Code with RRT for car like robot.
4+ @brief: Path Planning Sample Code with Closed roop RRT for car like robot.
55
66@author: AtsushiSakai(@Atsushi_twi)
77
2323accel = 0.1
2424curvature = 10.0
2525
26+ # TODO
27+ # 制約条件をいれる
28+
2629
2730class RRT ():
2831 u"""
@@ -48,6 +51,16 @@ def __init__(self, start, goal, obstacleList, randArea,
4851 self .obstacleList = obstacleList
4952 self .maxIter = maxIter
5053
54+ def try_goal_path (self ):
55+
56+ goal = Node (self .end .x , self .end .y , self .end .yaw )
57+
58+ newNode = self .steer (goal , len (self .nodeList ) - 1 )
59+
60+ if self .CollisionCheck (newNode , obstacleList ):
61+ # print("goal path is OK")
62+ self .nodeList .append (newNode )
63+
5164 def Planning (self , animation = True ):
5265 u"""
5366 Pathplanning
@@ -56,6 +69,9 @@ def Planning(self, animation=True):
5669 """
5770
5871 self .nodeList = [self .start ]
72+
73+ self .try_goal_path ()
74+
5975 for i in range (self .maxIter ):
6076 rnd = self .get_random_point ()
6177 nind = self .GetNearestListIndex (self .nodeList , rnd )
@@ -66,28 +82,52 @@ def Planning(self, animation=True):
6682 if self .CollisionCheck (newNode , obstacleList ):
6783 nearinds = self .find_near_nodes (newNode )
6884 newNode = self .choose_parent (newNode , nearinds )
85+
6986 self .nodeList .append (newNode )
7087 self .rewire (newNode , nearinds )
7188
89+ self .try_goal_path ()
90+
7291 if animation and i % 5 == 0 :
7392 self .DrawGraph (rnd = rnd )
7493 matplotrecorder .save_frame () # save each frame
7594
7695 # generate coruse
7796 path_indexs = self .get_best_last_indexs ()
7897
98+ flag , x , y , yaw , v , t , a , d = self .search_best_feasible_path (
99+ path_indexs )
100+
101+ return flag , x , y , yaw , v , t , a , d
102+
103+ def search_best_feasible_path (self , path_indexs ):
104+
105+ best_time = float ("inf" )
106+
79107 # pure pursuit tracking
80108 for ind in path_indexs :
81109 path = self .gen_final_course (ind )
82110
83111 flag , x , y , yaw , v , t , a , d = self .check_tracking_path_is_feasible (
84112 path )
85113
86- if flag :
114+ # print(t[-1])
115+ # print(ind)
116+
117+ if flag and best_time >= t [- 1 ]:
87118 print ("feasible path is found" )
88- break
119+ best_time = t [- 1 ]
120+ fx , fy , fyaw , fv , ft , fa , fd = x , y , yaw , v , t , a , d
89121
90- return flag , x , y , yaw , v , t , a , d
122+ # plt.show()
123+
124+ print ("best time is" )
125+ print (best_time )
126+
127+ if fx :
128+ return True , fx , fy , fyaw , fv , ft , fa , fd
129+ else :
130+ return False , None , None , None , None , None , None , None
91131
92132 def calc_tracking_path (self , path ):
93133 path = np .matrix (path [::- 1 ])
@@ -110,8 +150,7 @@ def calc_tracking_path(self, path):
110150 return path
111151
112152 def check_tracking_path_is_feasible (self , path ):
113- print ("check_tracking_path_is_feasible" )
114- path = np .matrix (path [::- 1 ])
153+ # print("check_tracking_path_is_feasible")
115154
116155 # save csv
117156 df = pd .DataFrame ()
@@ -137,6 +176,7 @@ def check_tracking_path_is_feasible(self, path):
137176 yaw = [self .pi_2_pi (iyaw ) for iyaw in yaw ]
138177
139178 if abs (yaw [- 1 ] - goal [2 ]) >= math .pi / 2.0 :
179+ print ("final angle is bad" )
140180 find_goal = False
141181
142182 travel = sum ([abs (iv ) * unicycle_model .dt for iv in v ])
@@ -147,10 +187,11 @@ def check_tracking_path_is_feasible(self, path):
147187
148188 if (travel / origin_travel ) >= 2.0 :
149189 print ("path is too long" )
150- # find_goal = False
190+ find_goal = False
151191
152- if not find_goal :
192+ if not self . CollisionCheckWithXY ( x , y , obstacleList ) :
153193 print ("This path is bad" )
194+ find_goal = False
154195
155196 # plt.clf
156197 # plt.plot(x, y, '-r')
@@ -216,13 +257,13 @@ def steer(self, rnd, nind):
216257
217258 def get_random_point (self ):
218259
219- if random .randint (0 , 100 ) > self .goalSampleRate :
220- rnd = [random .uniform (self .minrand , self .maxrand ),
221- random .uniform (self .minrand , self .maxrand ),
222- random .uniform (- math .pi , math .pi )
223- ]
224- else : # goal point sampling
225- rnd = [self .end .x , self .end .y , self .end .yaw ]
260+ # if random.randint(0, 100) > self.goalSampleRate:
261+ rnd = [random .uniform (self .minrand , self .maxrand ),
262+ random .uniform (self .minrand , self .maxrand ),
263+ random .uniform (- math .pi , math .pi )
264+ ]
265+ # else: # goal point sampling
266+ # rnd = [self.end.x, self.end.y, self.end.yaw]
226267
227268 node = Node (rnd [0 ], rnd [1 ], rnd [2 ])
228269
@@ -263,6 +304,8 @@ def gen_final_course(self, goalind):
263304 # path.append([node.x, node.y])
264305 goalind = node .parent
265306 path .append ([self .start .x , self .start .y , self .start .yaw ])
307+
308+ path = np .matrix (path [::- 1 ])
266309 return path
267310
268311 def calc_dist_to_goal (self , x , y ):
0 commit comments