|
|
@@ -12,14 +12,16 @@ import matplotlib.pyplot as plt
|
|
|
import os
|
|
|
import sys
|
|
|
|
|
|
-sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Sampling-based_Planning/")
|
|
|
+sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Sampling_based_Planning/")
|
|
|
from rrt_3D.env3D import env
|
|
|
-from rrt_3D.utils3D import getDist, sampleFree, nearest, steer, isCollide, near, cost, path, edgeset, isinbound, isinside
|
|
|
+from rrt_3D.utils3D import getDist, sampleFree, nearest, steer, isCollide, near, cost, path, edgeset, isinbound, \
|
|
|
+ isinside
|
|
|
from rrt_3D.rrt3D import rrt
|
|
|
from rrt_3D.plot_util3D import make_get_proj, draw_block_list, draw_Spheres, draw_obb, draw_line, make_transparent
|
|
|
|
|
|
-class dynamic_rrt_3D():
|
|
|
-
|
|
|
+
|
|
|
+class dynamic_rrt_3D:
|
|
|
+
|
|
|
def __init__(self):
|
|
|
self.env = env()
|
|
|
self.x0, self.xt = tuple(self.env.start), tuple(self.env.goal)
|
|
|
@@ -27,18 +29,20 @@ class dynamic_rrt_3D():
|
|
|
self.current = tuple(self.env.start)
|
|
|
self.stepsize = 0.25
|
|
|
self.maxiter = 10000
|
|
|
- self.GoalProb = 0.05 # probability biased to the goal
|
|
|
- self.WayPointProb = 0.05 # probability falls back on to the way points
|
|
|
-
|
|
|
- self.V = [] # vertices
|
|
|
- self.Parent = {} # parent child relation
|
|
|
- self.Edge = set() # edge relation (node, parent node) tuple
|
|
|
+ self.GoalProb = 0.05 # probability biased to the goal
|
|
|
+ self.WayPointProb = 0.02 # probability falls back on to the way points
|
|
|
+ self.done = False
|
|
|
+ self.invalid = False
|
|
|
+
|
|
|
+ self.V = [] # vertices
|
|
|
+ self.Parent = {} # parent child relation
|
|
|
+ self.Edge = set() # edge relation (node, parent node) tuple
|
|
|
self.Path = []
|
|
|
- self.flag = {}# flag dictionary
|
|
|
+ self.flag = {} # flag dictionary
|
|
|
self.ind = 0
|
|
|
self.i = 0
|
|
|
|
|
|
-#--------Dynamic RRT algorithm
|
|
|
+ # --------Dynamic RRT algorithm
|
|
|
def RegrowRRT(self):
|
|
|
self.TrimRRT()
|
|
|
self.GrowRRT()
|
|
|
@@ -57,14 +61,14 @@ class dynamic_rrt_3D():
|
|
|
i += 1
|
|
|
self.CreateTreeFromNodes(S)
|
|
|
print('trimming complete...')
|
|
|
-
|
|
|
+
|
|
|
def InvalidateNodes(self, obstacle):
|
|
|
Edges = self.FindAffectedEdges(obstacle)
|
|
|
for edge in Edges:
|
|
|
qe = self.ChildEndpointNode(edge)
|
|
|
self.flag[qe] = 'Invalid'
|
|
|
|
|
|
-#--------Extend RRT algorithm-----
|
|
|
+ # --------Extend RRT algorithm-----
|
|
|
def initRRT(self):
|
|
|
self.V.append(self.x0)
|
|
|
self.flag[self.x0] = 'Valid'
|
|
|
@@ -72,12 +76,11 @@ class dynamic_rrt_3D():
|
|
|
def GrowRRT(self):
|
|
|
print('growing')
|
|
|
qnew = self.x0
|
|
|
- tree = None
|
|
|
distance_threshold = self.stepsize
|
|
|
self.ind = 0
|
|
|
while self.ind <= self.maxiter:
|
|
|
qtarget = self.ChooseTarget()
|
|
|
- qnearest = self.Nearest(tree, qtarget)
|
|
|
+ qnearest = self.Nearest(qtarget)
|
|
|
qnew, collide = self.Extend(qnearest, qtarget)
|
|
|
if not collide:
|
|
|
self.AddNode(qnearest, qnew)
|
|
|
@@ -96,14 +99,14 @@ class dynamic_rrt_3D():
|
|
|
if len(self.V) == 1:
|
|
|
i = 0
|
|
|
else:
|
|
|
- i = np.random.randint(0, high = len(self.V) - 1)
|
|
|
+ i = np.random.randint(0, high=len(self.V) - 1)
|
|
|
if 0 < p < self.GoalProb:
|
|
|
return self.xt
|
|
|
elif self.GoalProb < p < self.GoalProb + self.WayPointProb:
|
|
|
return self.V[i]
|
|
|
elif self.GoalProb + self.WayPointProb < p < 1:
|
|
|
return tuple(self.RandomState())
|
|
|
-
|
|
|
+
|
|
|
def RandomState(self):
|
|
|
# generate a random, obstacle free state
|
|
|
xrand = sampleFree(self, bias=0)
|
|
|
@@ -115,16 +118,16 @@ class dynamic_rrt_3D():
|
|
|
self.Edge.add((extended, nearest))
|
|
|
self.flag[extended] = 'Valid'
|
|
|
|
|
|
- def Nearest(self, tree, target):
|
|
|
+ def Nearest(self, target):
|
|
|
# TODO use kdTree to speed up search
|
|
|
return nearest(self, target, isset=True)
|
|
|
|
|
|
def Extend(self, nearest, target):
|
|
|
- extended, dist = steer(self, nearest, target, DIST = True)
|
|
|
+ extended, dist = steer(self, nearest, target, DIST=True)
|
|
|
collide, _ = isCollide(self, nearest, target, dist)
|
|
|
return extended, collide
|
|
|
|
|
|
-#--------Main function
|
|
|
+ # --------Main function
|
|
|
def Main(self):
|
|
|
# qstart = qgoal
|
|
|
self.x0 = tuple(self.env.goal)
|
|
|
@@ -132,33 +135,36 @@ class dynamic_rrt_3D():
|
|
|
self.xt = tuple(self.env.start)
|
|
|
self.initRRT()
|
|
|
self.GrowRRT()
|
|
|
- self.Path, D = path(self)
|
|
|
+ self.Path, D = self.path()
|
|
|
self.done = True
|
|
|
- self.visualization()
|
|
|
- plt.show()
|
|
|
+ self.visualization()
|
|
|
t = 0
|
|
|
while True:
|
|
|
# move the block while the robot is moving
|
|
|
new, _ = self.env.move_block(a=[0, 0, -0.2], mode='translation')
|
|
|
self.InvalidateNodes(new)
|
|
|
+ self.TrimRRT()
|
|
|
# if solution path contains invalid node
|
|
|
- self.done = True
|
|
|
self.visualization()
|
|
|
- plt.show()
|
|
|
- invalid = self.PathisInvalid(self.Path)
|
|
|
- if invalid:
|
|
|
+ self.invalid = self.PathisInvalid(self.Path)
|
|
|
+ if self.invalid:
|
|
|
self.done = False
|
|
|
self.RegrowRRT()
|
|
|
self.Path = []
|
|
|
- self.Path, D = path(self)
|
|
|
-
|
|
|
+ self.Path, D = self.path()
|
|
|
+ self.done = True
|
|
|
+ self.visualization()
|
|
|
if t == 8:
|
|
|
break
|
|
|
+ t += 1
|
|
|
+ self.visualization()
|
|
|
+ plt.show()
|
|
|
|
|
|
-#--------Additional utility functions
|
|
|
+ # --------Additional utility functions
|
|
|
def FindAffectedEdges(self, obstacle):
|
|
|
# scan the graph for the changed edges in the tree.
|
|
|
# return the end point and the affected
|
|
|
+ print('finding affected edges')
|
|
|
Affectededges = []
|
|
|
for e in self.Edge:
|
|
|
child, parent = e
|
|
|
@@ -171,44 +177,48 @@ class dynamic_rrt_3D():
|
|
|
return edge[0]
|
|
|
|
|
|
def CreateTreeFromNodes(self, Nodes):
|
|
|
- self.V = []
|
|
|
- Parent = {}
|
|
|
- edges = set()
|
|
|
- for v in Nodes:
|
|
|
- self.V.append(v)
|
|
|
- Parent[v] = self.Parent[v]
|
|
|
- edges.add((v, Parent[v]))
|
|
|
- self.Parent = Parent
|
|
|
- self.Edge = edges
|
|
|
+ print('creating tree')
|
|
|
+ # self.Parent = {node: self.Parent[node] for node in Nodes}
|
|
|
+ self.V = [node for node in Nodes]
|
|
|
+ self.Edge = {(node, self.Parent[node]) for node in Nodes}
|
|
|
+ # if self.invalid:
|
|
|
+ # del self.Parent[self.xt]
|
|
|
|
|
|
def PathisInvalid(self, path):
|
|
|
for edge in path:
|
|
|
if self.flag[tuple(edge[0])] == 'Invalid' or self.flag[tuple(edge[1])] == 'Invalid':
|
|
|
return True
|
|
|
|
|
|
- def path(self, Path=[], dist=0):
|
|
|
+ def path(self, dist=0):
|
|
|
+ Path=[]
|
|
|
x = self.xt
|
|
|
+ i = 0
|
|
|
while x != self.x0:
|
|
|
x2 = self.Parent[x]
|
|
|
Path.append(np.array([x, x2]))
|
|
|
dist += getDist(x, x2)
|
|
|
x = x2
|
|
|
+ if i > 10000:
|
|
|
+ print('Path is not found')
|
|
|
+ return
|
|
|
+ i+= 1
|
|
|
return Path, dist
|
|
|
-
|
|
|
-#--------Visualization specialized for dynamic RRT
|
|
|
+
|
|
|
+ # --------Visualization specialized for dynamic RRT
|
|
|
def visualization(self):
|
|
|
if self.ind % 100 == 0 or self.done:
|
|
|
V = np.array(self.V)
|
|
|
Path = np.array(self.Path)
|
|
|
start = self.env.start
|
|
|
goal = self.env.goal
|
|
|
- edges = []
|
|
|
- for i in self.Parent:
|
|
|
- edges.append([i,self.Parent[i]])
|
|
|
+ # edges = []
|
|
|
+ # for i in self.Parent:
|
|
|
+ # edges.append([i, self.Parent[i]])
|
|
|
+ edges = np.array([list(i) for i in self.Edge])
|
|
|
ax = plt.subplot(111, projection='3d')
|
|
|
# ax.view_init(elev=0.+ 0.03*initparams.ind/(2*np.pi), azim=90 + 0.03*initparams.ind/(2*np.pi))
|
|
|
# ax.view_init(elev=0., azim=90.)
|
|
|
- ax.view_init(elev=8., azim=120.)
|
|
|
+ ax.view_init(elev=0., azim=90.)
|
|
|
ax.clear()
|
|
|
# drawing objects
|
|
|
draw_Spheres(ax, self.env.balls)
|
|
|
@@ -229,13 +239,12 @@ class dynamic_rrt_3D():
|
|
|
dx, dy, dz = xmax - xmin, ymax - ymin, zmax - zmin
|
|
|
ax.get_proj = make_get_proj(ax, 1 * dx, 1 * dy, 2 * dy)
|
|
|
make_transparent(ax)
|
|
|
- #plt.xlabel('x')
|
|
|
- #plt.ylabel('y')
|
|
|
+ # plt.xlabel('x')
|
|
|
+ # plt.ylabel('y')
|
|
|
ax.set_axis_off()
|
|
|
plt.pause(0.0001)
|
|
|
|
|
|
|
|
|
-
|
|
|
if __name__ == '__main__':
|
|
|
rrt = dynamic_rrt_3D()
|
|
|
rrt.Main()
|