rrt.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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, x_start, x_goal, step_len, goal_sample_rate, iter_max):
  21. self.xI = Node(x_start)
  22. self.xG = Node(x_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.xI]
  27. self.env = env.Env()
  28. self.plotting = plotting.Plotting(x_start, x_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.random_state(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.xG)
  43. if dist <= self.step_len:
  44. self.new_state(node_new, self.xG)
  45. return self.extract_path(node_new)
  46. return None
  47. def random_state(self, goal_sample_rate):
  48. delta = self.utils.delta
  49. if np.random.random() > goal_sample_rate:
  50. return Node((np.random.uniform(self.x_range[0] + delta, self.x_range[1] - delta),
  51. np.random.uniform(self.y_range[0] + delta, self.y_range[1] - delta)))
  52. return self.xG
  53. def nearest_neighbor(self, node_list, n):
  54. return self.vertex[int(np.argmin([math.hypot(nd.x - n.x, nd.y - n.y)
  55. for nd in node_list]))]
  56. def new_state(self, node_start, node_end):
  57. dist, theta = self.get_distance_and_angle(node_start, node_end)
  58. dist = min(self.step_len, dist)
  59. node_new = Node((node_start.x + dist * math.cos(theta),
  60. node_start.y + dist * math.sin(theta)))
  61. node_new.parent = node_start
  62. return node_new
  63. def extract_path(self, node_end):
  64. path = [(self.xG.x, self.xG.y)]
  65. node_now = node_end
  66. while node_now.parent is not None:
  67. node_now = node_now.parent
  68. path.append((node_now.x, node_now.y))
  69. return path
  70. @staticmethod
  71. def get_distance_and_angle(node_start, node_end):
  72. dx = node_end.x - node_start.x
  73. dy = node_end.y - node_start.y
  74. return math.hypot(dx, dy), math.atan2(dy, dx)
  75. def main():
  76. x_start = (2, 2) # Starting node
  77. x_goal = (49, 24) # Goal node
  78. rrt = Rrt(x_start, x_goal, 0.5, 0.03, 5000)
  79. path = rrt.planning()
  80. if path:
  81. rrt.plotting.animation(rrt.vertex, path, "RRT", True)
  82. else:
  83. print("No Path Found!")
  84. if __name__ == '__main__':
  85. main()