-
Notifications
You must be signed in to change notification settings - Fork 0
/
RRT_WITH_PATH_BI.py
131 lines (110 loc) · 3.78 KB
/
RRT_WITH_PATH_BI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import matplotlib.pyplot as plt
import random
import math as m
from queue import Queue
#lets start with a simple rrt without any path storage
def get_theta(x1,y1,x2,y2):
theta = m.atan2(y2-y1,x2-x1)
return theta
def get_nearest(x_traj_s, y_traj_s, xr, yr):
best_dist = 999 #random initialization
best_point = [xr,yr]
for i in range(len(x_traj_s)):
dist = m.sqrt((x_traj_s[i]-xr)**2 + (y_traj_s[i]-yr)**2)
if dist< best_dist:
best_pt = [x_traj_s[i],y_traj_s[i]]
best_dist = dist
return best_pt
for i in range(5):
plt.clf()
start = (5,5)
goal = (random.uniform(0,10), random.uniform(0, 10))
iter = 0
buffer = 1000
x_traj_s = [start[0]]
y_traj_s = [start[1]]
x_traj_g = [goal[0]]
y_traj_g = [goal[1]]
stride = 0.09
sample_goal_rate = 5
#plotting starting and end positions
plt.plot(start[0],start[1],'go')
plt.plot(goal[0], goal[1], 'ro')
#Queue based- Remember to use tuples
tree1 = Queue()
tree2 = Queue()
tree1.put(start)
tree2.put(goal)
cameFrom1 = {}
cameFrom2 = {}
cameFrom1[start] = None
cameFrom2[goal] = None
n2goal = m.sqrt((goal[1]-start[1])**2 + (goal[0]-start[1])**2) #neareness to goal
n2start = m.sqrt((goal[1]-start[1])**2 + (goal[0]-start[1])**2) #neareness to start
n2each_other = m.sqrt((goal[1]-start[1])**2 + (goal[0]-start[1])**2)
while n2each_other>0.1:
"""
EVERY ONCE IN A WHILE SAMPLE GOAL POINT AS THE RANDOM POINT
"""
if random.uniform(0,100) > sample_goal_rate:
x_rands = random.uniform(0,10)
y_rands = random.uniform(0,10)
x_randg = random.uniform(0,10)
y_randg = random.uniform(0,10)
else:
x_rands = goal[0]
y_rands = goal[1]
x_randg = start[0]
y_randg = start[1]
#draw a line to the randomly generated point
nearest_s = get_nearest(x_traj_s,y_traj_s,x_rands,y_rands)
phi_s = get_theta(nearest_s[0],nearest_s[1],x_rands,y_rands)
x_gen_s = nearest_s[0] + m.cos(phi_s)*stride
y_gen_s = nearest_s[1] + m.sin(phi_s)*stride
nearest_g = get_nearest(x_traj_g,y_traj_g,x_gen_s,y_gen_s)
phi_g = get_theta(nearest_g[0],nearest_g[1],x_gen_s,y_gen_s)
x_gen_g = nearest_g[0] + m.cos(phi_g)*stride
y_gen_g = nearest_g[1] + m.sin(phi_g)*stride
x_traj_s.append(x_gen_s)
y_traj_s.append(y_gen_s)
x_traj_g.append(x_gen_g)
y_traj_g.append(y_gen_g)
plt.plot(x_gen_s,y_gen_s,'c.')
plt.plot(x_gen_g,y_gen_g,'g.')
plt.xlim(0,10)
plt.ylim(0,10)
plt.pause(0.01)
iter+=1
#tree1 structure
tree1.put(x_gen_s,y_gen_s)
cameFrom1[(x_gen_s, y_gen_s)] = (nearest_s[0], nearest_s[1])
#loop break logic
n2goal = m.sqrt((goal[1]-y_traj_s[-1])**2 + (goal[0]-x_traj_s[-1])**2)
n2n2each_other = m.sqrt((y_traj_g[-1]-y_traj_s[-1])**2 + (x_traj_g[-1]-x_traj_s[-1])**2)
if iter>buffer:
print("Path not found, buffer exceeded")
break
CONNECTION = True
while True:
if CONNECTION:
x_rand = goal[0]
y_rand = goal[1]
"""
EXTRACTION OF THE PATH
"""
#lets extract the path
#find nearest_s to goal
nearest_s = get_nearest(x_traj_s, y_traj_s, goal[0], goal[1])
current = (nearest_s[0], nearest_s[1]) #extracting whatever came last
pathx = []
pathy = []
while current!=start:
pathx.append(current[0])
pathy.append(current[1])
plt.plot(pathx[-1], pathy[-1], 'r.')
current = cameFrom1[current]
plt.pause(0.01)
final_path = []
for i in range(len(pathx)):
final_path.append([pathx[i],pathy[i]])
plt.show()