rrt.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. RRT_2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. import math
  8. import numpy as np
  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. from rrt_2D import utils
  14. class Node:
  15. def __init__(self, n):
  16. self.x = n[0]
  17. self.y = n[1]
  18. self.parent = None
  19. class Rrt:
  20. def __init__(self, s_start, s_goal, step_len, goal_sample_rate, iter_max):
  21. self.s_start = Node(s_start)
  22. self.s_goal = Node(s_goal)
  23. self.step_len = step_len
  24. self.goal_sample_rate = goal_sample_rate
  25. self.iter_max = iter_max
  26. self.vertex = [self.s_start]
  27. self.env = env.Env()
  28. self.plotting = plotting.Plotting(s_start, s_goal)
  29. self.utils = utils.Utils()
  30. self.x_range = self.env.x_range
  31. self.y_range = self.env.y_range
  32. self.obs_circle = self.env.obs_circle
  33. self.obs_rectangle = self.env.obs_rectangle
  34. self.obs_boundary = self.env.obs_boundary
  35. def planning(self):
  36. for i in range(self.iter_max):
  37. node_rand = self.generate_random_node(self.goal_sample_rate)
  38. node_near = self.nearest_neighbor(self.vertex, node_rand)
  39. node_new = self.new_state(node_near, node_rand)
  40. if node_new and not self.utils.is_collision(node_near, node_new):
  41. self.vertex.append(node_new)
  42. dist, _ = self.get_distance_and_angle(node_new, self.s_goal)
  43. if dist <= self.step_len:
  44. self.new_state(node_new, self.s_goal)
  45. print(i)
  46. return self.extract_path(node_new)
  47. return None
  48. def generate_random_node(self, goal_sample_rate):
  49. delta = self.utils.delta
  50. if np.random.random() > goal_sample_rate:
  51. return Node((np.random.uniform(self.x_range[0] + delta, self.x_range[1] - delta),
  52. np.random.uniform(self.y_range[0] + delta, self.y_range[1] - delta)))
  53. return self.s_goal
  54. @staticmethod
  55. def nearest_neighbor(node_list, n):
  56. return node_list[int(np.argmin([math.hypot(nd.x - n.x, nd.y - n.y)
  57. for nd in node_list]))]
  58. def new_state(self, node_start, node_end):
  59. dist, theta = self.get_distance_and_angle(node_start, node_end)
  60. dist = min(self.step_len, dist)
  61. node_new = Node((node_start.x + dist * math.cos(theta),
  62. node_start.y + dist * math.sin(theta)))
  63. node_new.parent = node_start
  64. return node_new
  65. def extract_path(self, node_end):
  66. path = [(self.s_goal.x, self.s_goal.y)]
  67. node_now = node_end
  68. while node_now.parent is not None:
  69. node_now = node_now.parent
  70. path.append((node_now.x, node_now.y))
  71. return path
  72. @staticmethod
  73. def get_distance_and_angle(node_start, node_end):
  74. dx = node_end.x - node_start.x
  75. dy = node_end.y - node_start.y
  76. return math.hypot(dx, dy), math.atan2(dy, dx)
  77. def main():
  78. x_start = (2, 2) # Starting node
  79. x_goal = (49, 24) # Goal node
  80. rrt = Rrt(x_start, x_goal, 0.5, 0.03, 5000)
  81. path = rrt.planning()
  82. if path:
  83. rrt.plotting.animation(rrt.vertex, path, "Goal-bias RRT", True)
  84. else:
  85. print("No Path Found!")
  86. if __name__ == '__main__':
  87. main()