Files
PathPlanning/Sampling_based_Planning/rrt_2D/rrt.py
T

117 lines
3.4 KiB
Python
Raw Normal View History

2020-06-24 13:00:25 -07:00
"""
RRT_2D
@author: huiming zhou
"""
2020-06-24 13:43:39 -07:00
import os
import sys
2020-07-24 13:40:44 -07:00
import math
import numpy as np
2020-06-24 13:43:39 -07:00
sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
2020-07-30 10:02:26 -07:00
"/../../Sampling_based_Planning/")
2020-06-24 13:00:25 -07:00
2020-07-30 10:02:26 -07:00
from Sampling_based_Planning.rrt_2D import env, plotting, utils
2020-06-23 19:37:10 -07:00
class Node:
def __init__(self, n):
self.x = n[0]
self.y = n[1]
self.parent = None
2020-06-24 12:55:13 -07:00
class Rrt:
2020-07-02 21:51:22 -07:00
def __init__(self, s_start, s_goal, step_len, goal_sample_rate, iter_max):
self.s_start = Node(s_start)
self.s_goal = Node(s_goal)
2020-06-24 19:41:29 -07:00
self.step_len = step_len
2020-06-24 12:55:13 -07:00
self.goal_sample_rate = goal_sample_rate
2020-06-24 19:41:29 -07:00
self.iter_max = iter_max
2020-07-02 21:51:22 -07:00
self.vertex = [self.s_start]
2020-06-23 19:37:10 -07:00
self.env = env.Env()
2020-07-02 21:51:22 -07:00
self.plotting = plotting.Plotting(s_start, s_goal)
2020-06-24 19:41:29 -07:00
self.utils = utils.Utils()
2020-06-23 19:37:10 -07:00
self.x_range = self.env.x_range
self.y_range = self.env.y_range
self.obs_circle = self.env.obs_circle
self.obs_rectangle = self.env.obs_rectangle
self.obs_boundary = self.env.obs_boundary
def planning(self):
2020-08-04 15:34:15 -07:00
print("z")
2020-06-24 19:41:29 -07:00
for i in range(self.iter_max):
2020-07-24 13:40:44 -07:00
node_rand = self.generate_random_node(self.goal_sample_rate)
2020-06-24 12:55:13 -07:00
node_near = self.nearest_neighbor(self.vertex, node_rand)
2020-06-23 19:37:10 -07:00
node_new = self.new_state(node_near, node_rand)
2020-06-24 19:41:29 -07:00
if node_new and not self.utils.is_collision(node_near, node_new):
2020-06-24 12:55:13 -07:00
self.vertex.append(node_new)
2020-07-02 21:51:22 -07:00
dist, _ = self.get_distance_and_angle(node_new, self.s_goal)
2020-06-23 19:37:10 -07:00
2020-06-24 19:41:29 -07:00
if dist <= self.step_len:
2020-07-02 21:51:22 -07:00
self.new_state(node_new, self.s_goal)
2020-07-27 15:58:52 -07:00
print(i)
2020-06-24 12:55:13 -07:00
return self.extract_path(node_new)
2020-06-23 19:37:10 -07:00
return None
2020-07-24 13:40:44 -07:00
def generate_random_node(self, goal_sample_rate):
2020-06-24 19:41:29 -07:00
delta = self.utils.delta
2020-06-24 12:55:13 -07:00
if np.random.random() > goal_sample_rate:
2020-06-24 19:41:29 -07:00
return Node((np.random.uniform(self.x_range[0] + delta, self.x_range[1] - delta),
np.random.uniform(self.y_range[0] + delta, self.y_range[1] - delta)))
2020-07-02 21:51:22 -07:00
return self.s_goal
2020-06-23 19:37:10 -07:00
2020-07-24 13:40:44 -07:00
@staticmethod
def nearest_neighbor(node_list, n):
return node_list[int(np.argmin([math.hypot(nd.x - n.x, nd.y - n.y)
for nd in node_list]))]
2020-06-23 19:37:10 -07:00
def new_state(self, node_start, node_end):
2020-06-24 19:41:29 -07:00
dist, theta = self.get_distance_and_angle(node_start, node_end)
2020-06-23 19:37:10 -07:00
2020-06-24 19:41:29 -07:00
dist = min(self.step_len, dist)
node_new = Node((node_start.x + dist * math.cos(theta),
2020-07-24 13:40:44 -07:00
node_start.y + dist * math.sin(theta)))
2020-06-23 19:37:10 -07:00
node_new.parent = node_start
return node_new
2020-06-24 12:55:13 -07:00
def extract_path(self, node_end):
2020-07-02 21:51:22 -07:00
path = [(self.s_goal.x, self.s_goal.y)]
2020-06-24 12:55:13 -07:00
node_now = node_end
2020-06-23 19:37:10 -07:00
while node_now.parent is not None:
node_now = node_now.parent
path.append((node_now.x, node_now.y))
return path
@staticmethod
def get_distance_and_angle(node_start, node_end):
dx = node_end.x - node_start.x
dy = node_end.y - node_start.y
return math.hypot(dx, dy), math.atan2(dy, dx)
2020-06-24 12:55:13 -07:00
def main():
x_start = (2, 2) # Starting node
2020-06-25 12:14:28 -07:00
x_goal = (49, 24) # Goal node
2020-06-23 19:37:10 -07:00
2020-08-04 15:34:15 -07:00
rrt = Rrt(x_start, x_goal, 0.5, 0.05, 10000)
2020-06-24 12:55:13 -07:00
path = rrt.planning()
if path:
2020-08-03 11:39:34 -07:00
rrt.plotting.animation(rrt.vertex, path, "RRT", True)
2020-06-24 12:55:13 -07:00
else:
print("No Path Found!")
if __name__ == '__main__':
main()