rs_rrt_star.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if node_new:
  56. node_near =
  57. def Steer(self, node_start, node_end):
  58. sx, sy, syaw = node_start.x, node_start.y, node_start.yaw
  59. gx, gy, gyaw = node_end.x, node_end.y, node_end.yaw
  60. maxc = self.curv
  61. path = rs.calc_optimal_path(sx, sy, syaw, gx, gy, gyaw, maxc, step_size=0.2)
  62. if not path:
  63. return None
  64. node_new = Node(path.x[-1], path.y[-1], path.yaw[-1])
  65. node_new.path_x = path.x
  66. node_new.path_y = path.y
  67. node_new.path_yaw = path.yaw
  68. node_new.cost = path.L
  69. node_new.parent = node_start
  70. return node_new
  71. def Sample(self):
  72. delta = self.utils.delta
  73. rnd = Node(random.uniform(self.x_range[0] + delta, self.x_range[1] - delta),
  74. random.uniform(self.y_range[0] + delta, self.y_range[1] - delta),
  75. random.uniform(-math.pi, math.pi))
  76. return rnd
  77. @staticmethod
  78. def Nearest(nodelist, n):
  79. return nodelist[int(np.argmin([(nd.x - n.x) ** 2 + (nd.y - n.y) ** 2
  80. for nd in nodelist]))]