rrt.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """
  2. RRT_2D
  3. @author: huiming zhou
  4. """
  5. import math
  6. import numpy as np
  7. import os
  8. import sys
  9. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  10. "/../../Sampling-based Planning/")
  11. from rrt_2D import env
  12. from rrt_2D import plotting
  13. class Node:
  14. def __init__(self, n):
  15. self.x = n[0]
  16. self.y = n[1]
  17. self.parent = None
  18. class Rrt:
  19. def __init__(self, x_start, x_goal, expand_len, goal_sample_rate, iter_limit):
  20. self.xI = Node(x_start)
  21. self.xG = Node(x_goal)
  22. self.expand_len = expand_len
  23. self.goal_sample_rate = goal_sample_rate
  24. self.iter_limit = iter_limit
  25. self.vertex = [self.xI]
  26. self.env = env.Env()
  27. self.plotting = plotting.Plotting(x_start, x_goal)
  28. self.x_range = self.env.x_range
  29. self.y_range = self.env.y_range
  30. self.obs_circle = self.env.obs_circle
  31. self.obs_rectangle = self.env.obs_rectangle
  32. self.obs_boundary = self.env.obs_boundary
  33. def planning(self):
  34. for i in range(self.iter_limit):
  35. node_rand = self.random_state(self.goal_sample_rate)
  36. node_near = self.nearest_neighbor(self.vertex, node_rand)
  37. node_new = self.new_state(node_near, node_rand)
  38. if node_new and not self.check_collision(node_new):
  39. self.vertex.append(node_new)
  40. dist, _ = self.get_distance_and_angle(node_new, self.xG)
  41. if dist <= self.expand_len:
  42. self.new_state(node_new, self.xG)
  43. return self.extract_path(node_new)
  44. return None
  45. def random_state(self, goal_sample_rate):
  46. if np.random.random() > goal_sample_rate:
  47. return Node((np.random.uniform(self.x_range[0], self.x_range[1]),
  48. np.random.uniform(self.y_range[0], self.y_range[1])))
  49. return self.xG
  50. def nearest_neighbor(self, node_list, n):
  51. return self.vertex[int(np.argmin([math.hypot(nd.x - n.x, nd.y - n.y)
  52. for nd in node_list]))]
  53. def new_state(self, node_start, node_end):
  54. node_new = Node((node_start.x, node_start.y))
  55. dist, theta = self.get_distance_and_angle(node_new, node_end)
  56. dist = min(self.expand_len, dist)
  57. node_new.x += dist * math.cos(theta)
  58. node_new.y += dist * math.sin(theta)
  59. node_new.parent = node_start
  60. return node_new
  61. def extract_path(self, node_end):
  62. path = [(self.xG.x, self.xG.y)]
  63. node_now = node_end
  64. while node_now.parent is not None:
  65. node_now = node_now.parent
  66. path.append((node_now.x, node_now.y))
  67. return path
  68. def check_collision(self, node_end):
  69. for (ox, oy, r) in self.obs_circle:
  70. if math.hypot(node_end.x - ox, node_end.y - oy) <= r:
  71. return True
  72. for (ox, oy, w, h) in self.obs_rectangle:
  73. if 0 <= (node_end.x - ox) <= w and 0 <= (node_end.y - oy) <= h:
  74. return True
  75. for (ox, oy, w, h) in self.obs_boundary:
  76. if 0 <= (node_end.x - ox) <= w and 0 <= (node_end.y - oy) <= h:
  77. return True
  78. return False
  79. @staticmethod
  80. def get_distance_and_angle(node_start, node_end):
  81. dx = node_end.x - node_start.x
  82. dy = node_end.y - node_start.y
  83. return math.hypot(dx, dy), math.atan2(dy, dx)
  84. def main():
  85. x_start = (2, 2) # Starting node
  86. x_goal = (49, 28) # Goal node
  87. rrt = Rrt(x_start, x_goal, 0.4, 0.05, 2000)
  88. path = rrt.planning()
  89. if path:
  90. rrt.plotting.animation(rrt.vertex, path)
  91. else:
  92. print("No Path Found!")
  93. if __name__ == '__main__':
  94. main()