Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. def RRT(robot, obstacles, startPoint, goalPoint):
  2.  
  3. max = 50
  4. points = dict()
  5. tree = dict()
  6. path = []
  7. while len(points) == 0:
  8. x = random.uniform(0.1,9.9)
  9. y = random.uniform(0.1,9.9)
  10. if not (withinPoly(obstacles,[x,y]) and not isCollisionFree(robot,(x,y),obstacles)):
  11. points[1] = (x,y)
  12. # Your code goes here.
  13. for i in range(2,max+1):
  14. x = random.uniform(0.1,9.9)
  15. y = random.uniform(0.1,9.9)
  16. if (x,y) in points.values():
  17. i-=1
  18. continue
  19. if withinPoly(obstacles,[x,y]) and not isCollisionFree(robot,(x,y),obstacles):
  20. i-=1
  21. continue
  22. if i == 2:
  23. # Make sure points 1 and 2 can connect before adding x,y to points
  24. if not lineWithinPoly(obstacles,[(x,y),points[1]]):
  25. tree[1] = [2]
  26. tree[2] = [1]
  27. points[i] = (x,y)
  28. continue
  29. else:
  30. i-=1
  31. continue
  32. d = sys.maxsize
  33. index = 0
  34. for j in list(tree):
  35. for k in tree[j]:
  36. if i == 3:
  37. if not lineWithinPoly(obstacles,[(x,y),points[k]]):
  38. if d > math.sqrt((x-points[k][0])**2+(y-points[k][1])**2):
  39. d = math.sqrt((x-points[k][0])**2+(y-points[k][1])**2)
  40. index = k
  41. elif not collisionDetection(tree,points,[points[k],(x,y)],j,k,1) and not lineWithinPoly(obstacles,[(x,y),points[k]]):
  42. if d > math.sqrt((x-points[k][0])**2+(y-points[k][1])**2):
  43. d = math.sqrt((x-points[k][0])**2+(y-points[k][1])**2)
  44. index = k
  45. if index != 0:
  46. points[i] = (x,y)
  47. if tree.get(index) == None:
  48. tree[index] = [i]
  49. else:
  50. tree[index].append(i)
  51. tree[i] = [index]
  52. else:
  53. i-=1
  54. continue
  55.  
  56. dStart = sys.maxsize
  57. dGoal = sys.maxsize
  58. indexS = 0
  59. indexG = 0
  60. for i in points:
  61. if not collisionDetection(tree,points,[startPoint,points.get(i)],0,i,1) and not lineWithinPoly(obstacles,[startPoint,points.get(i)]):
  62. if d > math.sqrt((startPoint[0]-points.get(i)[0])**2+(startPoint[1]-points.get(i)[1])**2):
  63. d = math.sqrt((startPoint[0]-points.get(i)[0])**2+(startPoint[1]-points.get(i)[1])**2)
  64. indexS = i
  65. if not collisionDetection(tree,points,[goalPoint,points.get(i)],0,i,1) and not lineWithinPoly(obstacles,[goalPoint,points.get(i)]):
  66. if d > math.sqrt((goalPoint[0]-points.get(i)[0])**2+(goalPoint[1]-points.get(i)[1])**2):
  67. d = math.sqrt((goalPoint[0]-points.get(i)[0])**2+(goalPoint[1]-points.get(i)[1])**2)
  68. indexG = i
  69. points[max+1] = startPoint
  70. points[max+2] = goalPoint
  71.  
  72. if indexS != 0:
  73. tree[max+1] = [indexS]
  74. print("start index = ",indexS)
  75. tree[indexS].append(max+1)
  76. else:
  77. print("failed connecting startPoint")
  78.  
  79. if indexG != 0:
  80. tree[max+2] = [indexG]
  81. tree[indexG].append(max+2)
  82. else:
  83. print("failed connecting endPoint")
  84.  
  85. print("points made:",str(points))
  86. print("")
  87. print("tree:",str(tree))
  88. return points, tree, path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement