rs_rrt_star.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. RS_RRT_STAR_SMART 2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. import math
  8. import random
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11. from scipy.spatial.transform import Rotation as Rot
  12. import matplotlib.patches as patches
  13. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  14. "/../../Sampling-based Planning/")
  15. from rrt_2D import env
  16. from rrt_2D import plotting
  17. from rrt_2D import utils
  18. import rrt_2D.CurvesGenerator.reeds_shepp as rs
  19. class Node:
  20. def __init__(self, x, y, yaw):
  21. self.x = x
  22. self.y = y
  23. self.yaw = yaw
  24. self.path_x = []
  25. self.path_y = []
  26. self.paty_yaw = []
  27. self.parent = None
  28. self.cost = 0.0
  29. class RrtStarSmart:
  30. def __init__(self, sx, sy, syaw, gx, gy, gyaw, step_len,
  31. goal_sample_rate, search_radius, iter_max):
  32. self.s_start = Node(sx, sy, syaw)
  33. self.s_goal = Node(gx, gy, gyaw)
  34. self.step_len = step_len
  35. self.goal_sample_rate = goal_sample_rate
  36. self.search_radius = search_radius
  37. self.iter_max = iter_max
  38. self.curv = 1.0
  39. self.env = env.Env()
  40. self.utils = utils.Utils()
  41. self.fig, self.ax = plt.subplots()
  42. self.delta = self.utils.delta
  43. self.x_range = self.env.x_range
  44. self.y_range = self.env.y_range
  45. self.obs_circle = self.env.obs_circle
  46. self.obs_rectangle = self.env.obs_rectangle
  47. self.obs_boundary = self.env.obs_boundary
  48. self.V = [self.s_start]
  49. self.path = None
  50. def planning(self):
  51. for k in range(self.iter_max):
  52. node_rand = self.Sample()
  53. node_nearest = self.Nearest(self.V, node_rand)
  54. node_new = self.Steer(node_nearest, node_rand)
  55. def Steer(self, node_start, node_end):
  56. sx, sy, syaw = node_start.x, node_start.y, node_start.yaw
  57. gx, gy, gyaw = node_end.x, node_end.y, node_end.yaw
  58. maxc = self.curv
  59. path = rs.calc_optimal_path(sx, sy, syaw, gx, gy, gyaw, maxc, step_size=0.2)
  60. if not path:
  61. return None
  62. node_new = Node(path.x[-1], path.y[-1], path.yaw[-1])
  63. node_new.path_x = path.x
  64. node_new.path_y = path.y
  65. node_new.path_yaw = path.yaw
  66. node_new.cost = path.L
  67. node_new.parent = node_start
  68. return node_new
  69. def Sample(self):
  70. delta = self.utils.delta
  71. rnd = Node(random.uniform(self.x_range[0] + delta, self.x_range[1] - delta),
  72. random.uniform(self.y_range[0] + delta, self.y_range[1] - delta),
  73. random.uniform(-math.pi, math.pi))
  74. return rnd
  75. @staticmethod
  76. def Nearest(nodelist, n):
  77. return nodelist[int(np.argmin([(nd.x - n.x) ** 2 + (nd.y - n.y) ** 2
  78. for nd in nodelist]))]