1313import math
1414import matplotlib .pyplot as plt
1515
16- show_animation = False
16+ show_animation = True
1717
1818
1919class RTree (object ):
@@ -131,8 +131,8 @@ def __init__(self, start, goal,
131131 self .start = start
132132 self .goal = goal
133133
134- self .minrand = randArea [0 ]
135- self .maxrand = randArea [1 ]
134+ self .minrand = randArea [0 ]
135+ self .maxrand = randArea [1 ]
136136 self .expandDis = expandDis
137137 self .goalSampleRate = goalSampleRate
138138 self .maxIter = maxIter
@@ -158,7 +158,9 @@ def __init__(self, start, goal,
158158 def plan (self , animation = True ):
159159
160160 self .startId = self .tree .realWorldToNodeId (self .start )
161+ print ("startId: " , self .startId )
161162 self .goalId = self .tree .realWorldToNodeId (self .goal )
163+ print ("goalId: " , self .goalId )
162164
163165 # add goal to the samples
164166 self .samples [self .goalId ] = self .goal
@@ -193,13 +195,15 @@ def plan(self, animation=True):
193195 C = np .dot (np .dot (U , np .diag (
194196 [1.0 , 1.0 , np .linalg .det (U ) * np .linalg .det (np .transpose (Vh ))])), Vh )
195197
198+ self .samples .update (self .informedSample (
199+ 200 , cBest , cMin , xCenter , C ))
196200 foundGoal = False
197201 # run until done
198202 while (iterations < self .maxIter ):
199203 if len (self .vertex_queue ) == 0 and len (self .edge_queue ) == 0 :
200204 # Using informed rrt star way of computing the samples
201205 self .samples .update (self .informedSample (
202- 200 , cBest , cMin , xCenter , C ))
206+ 50 , cBest , cMin , xCenter , C ))
203207 # prune the tree
204208 self .r = 2.0
205209 if iterations != 0 :
@@ -266,11 +270,10 @@ def plan(self, animation=True):
266270 self .updateGraph ()
267271
268272 # visualize new edge
269-
270273 if animation :
271- self .drawGraph (xCenter = xCenter , cBest = cBest ,
272- cMin = cMin , etheta = etheta , samples = self .samples .values (),
273- start = firstCoord , end = secondCoord , plan = plan )
274+ self .drawGraph (xCenter = xCenter , cBest = cBest ,
275+ cMin = cMin , etheta = etheta , samples = self .samples .values (),
276+ start = firstCoord , end = secondCoord , tree = self . nodes )
274277
275278 for edge in self .edge_queue :
276279 if (edge [0 ] == bestEdge [1 ]):
@@ -280,7 +283,7 @@ def plan(self, animation=True):
280283 (edge [0 ], bestEdge [1 ]))
281284 if (edge [1 ] == bestEdge [1 ]):
282285 if self .g_scores [edge [1 ]] + self .computeDistanceCost (edge [1 ], bestEdge [1 ]) >= self .g_scores [self .goalId ]:
283- if (edge [1 ], best_edge [1 ]) in self .edge_queue :
286+ if (edge [1 ], bestEdge [1 ]) in self .edge_queue :
284287 self .edge_queue .remove (
285288 (edge [1 ], bestEdge [1 ]))
286289 else :
@@ -409,7 +412,7 @@ def bestInVertexQueue(self):
409412 v_plus_vals = [(v , self .g_scores [v ] + self .computeHeuristicCost (v , self .goalId ))
410413 for v in self .vertex_queue ]
411414 v_plus_vals = sorted (v_plus_vals , key = lambda x : x [1 ])
412-
415+ # print(v_plus_vals)
413416 return v_plus_vals [0 ][0 ]
414417
415418 def bestInEdgeQueue (self ):
@@ -510,8 +513,8 @@ def updateGraph(self):
510513 # store the parent and child
511514 self .nodes [succesor ] = currId
512515
513- def drawGraph (self , xCenter = None , cBest = None , cMin = None , etheta = None ,
514- samples = None , start = None , end = None , plan = None ):
516+ def drawGraph (self , xCenter = None , cBest = None , cMin = None , etheta = None ,
517+ samples = None , start = None , end = None , tree = None ):
515518 print ("Plotting Graph" )
516519 plt .clf ()
517520 for rnd in samples :
@@ -521,13 +524,13 @@ def drawGraph(self, xCenter=None, cBest=None, cMin=None, etheta=None,
521524 self .plot_ellipse (xCenter , cBest , cMin , etheta )
522525
523526 if start is not None and end is not None :
524- plt .plot ([start [0 ], start [1 ]], [end [0 ], end [1 ]], "-g" )
527+ plt .plot ([start [0 ], start [1 ]], [end [0 ], end [1 ]], "-g" )
525528
526- # for node in self.nodeList :
527- # if node.parent is not None :
528- # if node.x or node.y is not None:
529- # plt.plot([node.x, self.nodeList[node.parent].x], [
530- # node.y, self.nodeList[node.parent].y] , "-g ")
529+ if tree is not None and len ( tree ) != 0 :
530+ for key , value in tree . items () :
531+ keyCoord = self . tree . nodeIdToRealWorldCoord ( key )
532+ valueCoord = self .tree . nodeIdToRealWorldCoord ( value )
533+ plt . plot ( keyCoord , valueCoord , "-r " )
531534
532535 for (ox , oy , size ) in self .obstacleList :
533536 plt .plot (ox , oy , "ok" , ms = 30 * size )
@@ -570,7 +573,7 @@ def main():
570573 ]
571574
572575 bitStar = BITStar (start = [0 , 0 ], goal = [2 , 4 ], obstacleList = obstacleList ,
573- randArea = [- 2 , 15 ])
576+ randArea = [0 , 15 ])
574577 path = bitStar .plan (animation = show_animation )
575578 print ("Done" )
576579
0 commit comments