rrt.py 3.5 KB

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