rrtstar3D.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. This is rrt star code for 3D
  3. @author: yue qi
  4. """
  5. import numpy as np
  6. from numpy.matlib import repmat
  7. from collections import defaultdict
  8. import time
  9. import matplotlib.pyplot as plt
  10. import os
  11. import sys
  12. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Sampling_based_Planning/")
  13. from rrt_3D.env3D import env
  14. from rrt_3D.utils3D import getDist, sampleFree, nearest, steer, isCollide, near, visualization, cost, path
  15. class rrtstar():
  16. def __init__(self):
  17. self.env = env()
  18. self.Parent = {}
  19. self.V = []
  20. # self.E = edgeset()
  21. self.COST = {}
  22. self.i = 0
  23. self.maxiter = 12000 # at least 2000 in this env
  24. self.stepsize = 0.5
  25. self.gamma = 500
  26. self.eta = self.stepsize
  27. self.Path = []
  28. self.done = False
  29. self.x0 = tuple(self.env.start)
  30. self.xt = tuple(self.env.goal)
  31. self.V.append(self.x0)
  32. self.ind = 0
  33. def wireup(self,x,y):
  34. # self.E.add_edge([s,y]) # add edge
  35. self.Parent[x] = y
  36. def removewire(self,xnear):
  37. xparent = self.Parent[xnear]
  38. a = [xnear,xparent]
  39. # self.E.remove_edge(a) # remove and replace old the connection
  40. def reached(self):
  41. self.done = True
  42. goal = self.xt
  43. xn = near(self,self.env.goal)
  44. c = [cost(self,tuple(x)) for x in xn]
  45. xncmin = xn[np.argmin(c)]
  46. self.wireup(goal , tuple(xncmin))
  47. self.V.append(goal)
  48. self.Path,self.D = path(self)
  49. def run(self):
  50. xnew = self.x0
  51. print('start rrt*... ')
  52. self.fig = plt.figure(figsize = (10,8))
  53. while self.ind < self.maxiter:
  54. xrand = sampleFree(self)
  55. xnearest = nearest(self,xrand)
  56. xnew = steer(self,xnearest,xrand)
  57. collide, _ = isCollide(self,xnearest,xnew)
  58. if not collide:
  59. Xnear = near(self,xnew)
  60. self.V.append(xnew) # add point
  61. visualization(self)
  62. # minimal path and minimal cost
  63. xmin, cmin = xnearest, cost(self, xnearest) + getDist(xnearest, xnew)
  64. # connecting along minimal cost path
  65. Collide = []
  66. for xnear in Xnear:
  67. xnear = tuple(xnear)
  68. c1 = cost(self, xnear) + getDist(xnew, xnear)
  69. collide, _ = isCollide(self, xnew, xnear)
  70. Collide.append(collide)
  71. if not collide and c1 < cmin:
  72. xmin, cmin = xnear, c1
  73. self.wireup(xnew, xmin)
  74. # rewire
  75. for i in range(len(Xnear)):
  76. collide = Collide[i]
  77. xnear = tuple(Xnear[i])
  78. c2 = cost(self, xnew) + getDist(xnew, xnear)
  79. if not collide and c2 < cost(self, xnear):
  80. # self.removewire(xnear)
  81. self.wireup(xnear, xnew)
  82. self.i += 1
  83. self.ind += 1
  84. # max sample reached
  85. self.reached()
  86. print('time used = ' + str(time.time()-starttime))
  87. print('Total distance = '+str(self.D))
  88. visualization(self)
  89. plt.show()
  90. if __name__ == '__main__':
  91. p = rrtstar()
  92. starttime = time.time()
  93. p.run()