11import math
22from collections import namedtuple
33from roboticstoolbox .mobile .OccGrid import PolygonMap
4- import rvcprint
4+
5+ # import rvcprint
56from roboticstoolbox import *
67import numpy as np
78import matplotlib .pyplot as plt
89
910from spatialmath import Polygon2 , SE2 , base
1011from roboticstoolbox .mobile .PlannerBase import PlannerBase
1112from roboticstoolbox .mobile .DubinsPlanner import DubinsPlanner
13+
1214# from roboticstoolbox.mobile.OccupancyGrid import OccupancyGrid
1315from pgraph import DGraph
1416
15- class RRTPlanner (PlannerBase ):
1617
17- def __init__ (self ,
18- map = None ,
19- vehicle = None ,
20- curvature = None ,
21- expand_dis = 3.0 ,
22- path_resolution = 0.5 ,
23- stepsize = 0.2 ,
24- showsamples = False ,
25- npoints = 50 , ** kwargs ):
18+ class RRTPlanner (PlannerBase ):
19+ def __init__ (
20+ self ,
21+ map = None ,
22+ vehicle = None ,
23+ curvature = None ,
24+ expand_dis = 3.0 ,
25+ path_resolution = 0.5 ,
26+ stepsize = 0.2 ,
27+ showsamples = False ,
28+ npoints = 50 ,
29+ ** kwargs
30+ ):
2631
2732 super ().__init__ (ndims = 2 , ** kwargs )
2833
@@ -32,7 +37,7 @@ def __init__(self,
3237 self .map = map
3338 self .showsamples = showsamples
3439
35- self .g = DGraph (metric = ' SE2' )
40+ self .g = DGraph (metric = " SE2" )
3641
3742 self .vehicle = vehicle
3843 if curvature is None :
@@ -41,7 +46,7 @@ def __init__(self,
4146 else :
4247 curvature = 1
4348
44- print (' curvature' , curvature )
49+ print (" curvature" , curvature )
4550 self .dubins = DubinsPlanner (curvature = curvature , stepsize = stepsize )
4651
4752 # self.goal_yaw_th = np.deg2rad(1.0)
@@ -68,7 +73,7 @@ def plan(self, goal, animation=True, search_until_npoints=True):
6873 random_point = self .qrandom_free ()
6974
7075 if self .showsamples :
71- plt .plot (random_point [0 ], random_point [1 ], 'ok' , markersize = 2 )
76+ plt .plot (random_point [0 ], random_point [1 ], "ok" , markersize = 2 )
7277
7378 vnearest , d = self .g .closest (random_point )
7479
@@ -100,8 +105,7 @@ def plan(self, goal, animation=True, search_until_npoints=True):
100105 self .g .add_edge (vnew , vnearest , cost = pstatus .length )
101106 vnew .path = path
102107
103-
104- self .vehicle .polygon (random_point ).plot (color = 'b' , alpha = 0.1 )
108+ self .vehicle .polygon (random_point ).plot (color = "b" , alpha = 0.1 )
105109 plt .show ()
106110
107111 self .progress_end ()
@@ -119,7 +123,9 @@ def query(self, start):
119123 if vertex .path is not None :
120124 path = np .vstack ((path , vertex .path ))
121125
122- status = namedtuple ('RRTStatus' , ['length' , 'initial_d' , 'vertices' ])(cost , d , vpath )
126+ status = namedtuple ("RRTStatus" , ["length" , "initial_d" , "vertices" ])(
127+ cost , d , vpath
128+ )
123129
124130 return path , status
125131
@@ -142,7 +148,8 @@ def calc_dist_to_goal(self, x, y):
142148 def qrandom (self ):
143149 return self .random .uniform (
144150 low = (self .map .workspace [0 ], self .map .workspace [2 ], - np .pi ),
145- high = (self .map .workspace [1 ], self .map .workspace [3 ], np .pi ))
151+ high = (self .map .workspace [1 ], self .map .workspace [3 ], np .pi ),
152+ )
146153
147154 def qrandom_free (self ):
148155
@@ -155,23 +162,24 @@ def qrandom_free(self):
155162 def iscollision (self , q ):
156163 return self .map .iscollision (self .vehicle .polygon (q ))
157164
165+
158166if __name__ == "__main__" :
159167
160168 from roboticstoolbox .mobile .Vehicle import Bicycle
161169
162170 # start and goal configuration
163- qs = (2 , 8 , - np .pi / 2 )
164- qg = (8 , 2 , - np .pi / 2 )
171+ qs = (2 , 8 , - np .pi / 2 )
172+ qg = (8 , 2 , - np .pi / 2 )
165173
166174 # obstacle map
167175 map = PolygonMap (workspace = [0 , 10 ])
168176 map .add ([(5 , 50 ), (5 , 6 ), (6 , 6 ), (6 , 50 )])
169177 # map.add([(5, 0), (6, 0), (6, 4), (5, 4)])
170178 map .add ([(5 , 4 ), (5 , - 50 ), (6 , - 50 ), (6 , 4 )])
171179
172- l = 3
173- w = 1.5
174- v0 = Polygon2 ([(- l / 2 , w / 2 ), (- l / 2 , - w / 2 ), (l / 2 , - w / 2 ), (l / 2 , w / 2 )])
180+ l = 3
181+ w = 1.5
182+ v0 = Polygon2 ([(- l / 2 , w / 2 ), (- l / 2 , - w / 2 ), (l / 2 , - w / 2 ), (l / 2 , w / 2 )])
175183
176184 vehicle = Bicycle (steer_max = 0.4 , l = 2 , polygon = v0 )
177185
@@ -181,5 +189,4 @@ def iscollision(self, q):
181189 path , status = rrt .query (start = qs )
182190 rrt .plot (path )
183191
184-
185- plt .show (block = True )
192+ plt .show (block = True )
0 commit comments