Files
PathPlanning/Sampling_based_Planning/rrt_3D/rrt_star3D.py
T

107 lines
3.3 KiB
Python
Raw Normal View History

2020-06-23 19:37:10 -07:00
"""
This is rrt star code for 3D
@author: yue qi
"""
import numpy as np
from numpy.matlib import repmat
from collections import defaultdict
import time
2020-06-25 18:18:33 -07:00
import matplotlib.pyplot as plt
2020-06-23 19:37:10 -07:00
2020-06-24 13:58:07 -07:00
import os
import sys
2020-06-24 22:52:40 -07:00
2020-07-30 10:02:26 -07:00
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Sampling_based_Planning/")
2020-06-24 13:58:07 -07:00
from rrt_3D.env3D import env
2020-08-03 19:59:23 -07:00
from rrt_3D.utils3D import getDist, sampleFree, nearest, steer, isCollide, near, visualization, cost, path
2020-06-24 13:58:07 -07:00
2020-06-23 19:37:10 -07:00
class rrtstar():
def __init__(self):
self.env = env()
2020-08-03 19:59:23 -07:00
2020-06-25 01:53:19 -07:00
self.Parent = {}
2020-06-23 19:37:10 -07:00
self.V = []
2020-08-03 19:59:23 -07:00
# self.E = edgeset()
self.COST = {}
2020-06-23 19:37:10 -07:00
self.i = 0
2020-08-03 19:59:23 -07:00
self.maxiter = 12000 # at least 2000 in this env
2020-06-23 19:37:10 -07:00
self.stepsize = 0.5
2020-06-25 01:53:19 -07:00
self.gamma = 500
2020-08-03 19:59:23 -07:00
self.eta = self.stepsize
2020-06-23 19:37:10 -07:00
self.Path = []
2020-06-25 01:53:19 -07:00
self.done = False
2020-07-26 14:44:35 -07:00
self.x0 = tuple(self.env.start)
2020-07-26 22:04:23 -07:00
self.xt = tuple(self.env.goal)
2020-06-23 19:37:10 -07:00
2020-07-26 22:04:23 -07:00
self.V.append(self.x0)
self.ind = 0
2020-06-25 01:53:19 -07:00
def wireup(self,x,y):
2020-08-03 19:59:23 -07:00
# self.E.add_edge([x,y]) # add edge
2020-07-26 01:23:46 -07:00
self.Parent[x] = y
2020-06-23 19:37:10 -07:00
2020-06-25 01:53:19 -07:00
def removewire(self,xnear):
2020-07-26 01:23:46 -07:00
xparent = self.Parent[xnear]
2020-06-25 01:53:19 -07:00
a = [xnear,xparent]
2020-08-03 19:59:23 -07:00
# self.E.remove_edge(a) # remove and replace old the connection
2020-06-25 01:53:19 -07:00
def reached(self):
self.done = True
2020-07-26 22:04:23 -07:00
goal = self.xt
2020-06-25 01:53:19 -07:00
xn = near(self,self.env.goal)
2020-07-26 01:23:46 -07:00
c = [cost(self,tuple(x)) for x in xn]
2020-06-25 01:53:19 -07:00
xncmin = xn[np.argmin(c)]
2020-07-26 01:23:46 -07:00
self.wireup(goal , tuple(xncmin))
self.V.append(goal)
2020-06-25 01:53:19 -07:00
self.Path,self.D = path(self)
2020-06-23 19:37:10 -07:00
def run(self):
2020-07-26 14:44:35 -07:00
xnew = self.x0
2020-06-25 01:53:19 -07:00
print('start rrt*... ')
2020-06-25 18:18:33 -07:00
self.fig = plt.figure(figsize = (10,8))
while self.ind < self.maxiter:
2020-06-25 01:53:19 -07:00
xrand = sampleFree(self)
xnearest = nearest(self,xrand)
xnew = steer(self,xnearest,xrand)
2020-07-26 01:23:46 -07:00
collide, _ = isCollide(self,xnearest,xnew)
if not collide:
2020-06-25 01:53:19 -07:00
Xnear = near(self,xnew)
self.V.append(xnew) # add point
2020-08-03 19:59:23 -07:00
visualization(self)
2020-06-23 19:37:10 -07:00
# minimal path and minimal cost
2020-06-24 22:52:40 -07:00
xmin, cmin = xnearest, cost(self, xnearest) + getDist(xnearest, xnew)
2020-06-23 19:37:10 -07:00
# connecting along minimal cost path
2020-08-03 19:59:23 -07:00
Collide = []
2020-06-25 18:55:55 -07:00
for xnear in Xnear:
2020-07-26 01:23:46 -07:00
xnear = tuple(xnear)
2020-06-25 18:55:55 -07:00
c1 = cost(self, xnear) + getDist(xnew, xnear)
2020-07-26 01:23:46 -07:00
collide, _ = isCollide(self, xnew, xnear)
2020-08-03 19:59:23 -07:00
Collide.append(collide)
2020-07-26 01:23:46 -07:00
if not collide and c1 < cmin:
2020-06-25 18:55:55 -07:00
xmin, cmin = xnear, c1
self.wireup(xnew, xmin)
# rewire
2020-08-03 19:59:23 -07:00
for i in range(len(Xnear)):
collide = Collide[i]
xnear = tuple(Xnear[i])
2020-06-25 18:55:55 -07:00
c2 = cost(self, xnew) + getDist(xnew, xnear)
2020-07-26 01:23:46 -07:00
if not collide and c2 < cost(self, xnear):
2020-08-03 19:59:23 -07:00
# self.removewire(xnear)
2020-06-25 18:55:55 -07:00
self.wireup(xnear, xnew)
2020-06-23 19:37:10 -07:00
self.i += 1
2020-06-25 18:18:33 -07:00
self.ind += 1
2020-06-25 01:53:19 -07:00
# max sample reached
self.reached()
print('time used = ' + str(time.time()-starttime))
print('Total distance = '+str(self.D))
2020-06-23 19:37:10 -07:00
visualization(self)
2020-06-25 18:18:33 -07:00
plt.show()
2020-06-25 01:53:19 -07:00
2020-06-23 19:37:10 -07:00
if __name__ == '__main__':
p = rrtstar()
starttime = time.time()
p.run()
2020-06-25 01:53:19 -07:00