-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRT_holonomic.py
292 lines (247 loc) · 9.46 KB
/
RRT_holonomic.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import pygame
from random import randint as ri
pygame.init()
import time
import numpy as np
screen = pygame.display.set_mode([500, 550])
SIDE_x = 20
SIDE_y = 40
WINDOW_width = 440
WINDOW_height = 400
GAME_border = 3
WHITE=(255,255,255)
BLUE=(0,0,255)
BLACK=(0,0,0)
RED=(255,0,0)
GREEN=(0,255,0)
RAND=(120,120,120)
YELLOW = (0,0,102)
C1=(39,38,53)
C2=(19,51,62)
C3=(31,3,24)
screen.fill(WHITE)
INT_MAX = 100000000000000
robot_radius = 8
class Environment:
def __init__ (self, colour, x, y, width, height):
self.colour = colour
self.x = x
self.y = y
self.width = width
self.height = height
def create(self,screen):
pygame.draw.rect(screen, self.colour, [self.x, self.y,self.width ,self.height])
def point_inside_game(self,x,y):
if x>SIDE_x+GAME_border and x<SIDE_x + WINDOW_width - GAME_border:
if y>SIDE_y+GAME_border and y < SIDE_y + WINDOW_height - GAME_border:
return(True)
return(False)
def random_point(self): #Random Point Generator
x_random = ri(SIDE_x+GAME_border , SIDE_x + WINDOW_width - GAME_border - 1)
y_random = ri(SIDE_y+GAME_border , SIDE_y + WINDOW_height - GAME_border - 1 )
return((x_random, y_random))
def point_inside_rec(self,xr,yr,wr,hr,x,y): # Point inside given Rectangle ?
if x> xr and x < xr + wr:
if y > yr and y < yr + hr:
return(True)
return(False)
def p2p_dist(self,p1,p2): # Point to Point Distance
x1,y1=p1
x2,y2=p2
return ( ( (x1-x2)**2 + (y1-y2)**2 )**0.5 )
def ClickText(self): # Text on Environment
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render('CLICK HERE', True, WHITE)
textRect = text.get_rect()
textRect.center = (75, 495)
screen.blit(text, textRect)
def DesText(self,s,x=315,y=485): # Description Text
pygame.draw.rect(screen,WHITE,(125,470,500,30))
font = pygame.font.SysFont('segoeuisemilight', 15)
text = font.render('%s'%(s), True, BLACK)
textRect = text.get_rect()
#textRect.center = (255, 460)
textRect.center = (x, y)
screen.blit(text, textRect)
def ConfSpace():
#CS for circle
pygame.draw.circle(screen,BLACK,(100,150),20+robot_radius)
pygame.draw.circle(screen,BLACK,(360,300),30+robot_radius)
#CS for rectangle
pygame.draw.circle(screen,BLACK,(200,150),robot_radius)
pygame.draw.circle(screen,BLACK,(300,200),robot_radius)
pygame.draw.circle(screen,BLACK,(200,200),robot_radius)
pygame.draw.circle(screen,BLACK,(300,150),robot_radius)
# left side of rectangle
pygame.draw.rect(screen,BLACK,(200-robot_radius,150,100,50))
# right side of rectangle
pygame.draw.rect(screen,BLACK,(300,150,robot_radius,50))
# bottom side of rectangle
pygame.draw.rect(screen,BLACK,(200,150-robot_radius,100,robot_radius))
# top side of rectangle
pygame.draw.rect(screen,BLACK,(200,200,100,robot_radius))
def WheelTraj_holonomic(r, points):
r=10
theta_list=[]
print(len(points))
for i in range(len(points)-1):
px,py=points[i]
cx,cy=points[i+1]
theta=np.arctan2((py-cy),(px-cx))
theta_list.append(float(theta))
cx1=cx+r*np.cos(theta)
cy1=cy+r*np.sin(theta)
cx2=cx+r*np.cos(theta-2*(np.pi/3))
cy2=cy+r*np.sin(theta-2*(np.pi/3))
cx3=cx+r*np.cos(theta+2*(np.pi/3))
cy3=cy+r*np.sin(theta+2*(np.pi/3))
px1=px+r*np.cos(theta)
py1=py+r*np.sin(theta)
px2=px+r*np.cos(theta-2*(np.pi/3))
py2=py+r*np.sin(theta-2*(np.pi/3))
px3=px+r*np.cos(theta+2*(np.pi/3))
py3=py+r*np.sin(theta+2*(np.pi/3))
pygame.draw.line(screen, C1, (cx1,cy1), (px1,py1), 3)
pygame.draw.line(screen, C2, (cx2,cy2), (px2,py2), 3)
pygame.draw.line(screen, C3, (cx3,cy3), (px3,py3), 3)
if(len(theta_list)>1):
theta_old=theta_list[-2]
theta_new=theta_list[-1]
step=(theta_new-theta_old)/10
for j in range(10):
temp=theta_old+step*j
pygame.draw.circle(screen, C1, (int(cx+r*np.cos(temp)), int(cy+r*np.sin(temp))), 1)
pygame.draw.circle(screen, C2, (int(cx+r*np.cos(temp-2*(np.pi/3))), int(cy+r*np.sin(temp-2*(np.pi/3)))), 1)
pygame.draw.circle(screen, C3, (int(cx+r*np.cos(temp+2*(np.pi/3))), int(cy+r*np.sin(temp+2*(np.pi/3)))), 1)
def RRT(x,y,parent):
if (x,y) not in parent and screen.get_at((x,y)) != (0,0,0,255):
x_m,y_m=-1,-1
cur_min=INT_MAX
for v in parent:
if B1.p2p_dist(v,(x,y))<cur_min:
x_m,y_m=v
cur_min = B1.p2p_dist(v,(x,y))
good = True
ans=[]
theta=np.arctan2((y-y_m),(x-x_m));
for i in range(Step):
x_mid=x_m+i*np.cos(theta)
y_mid=y_m+i*np.sin(theta)
if screen.get_at((int(x_mid),int(y_mid))) == (0,0,0,255):
good=False
break
if(good):
ans=[int(x_m+(Step)*np.cos(theta)),int(y_m+Step*np.sin(theta))]
return(good,x_m,y_m,ans)
return(False,-1,-1,[])
running = True
#Environment for Game
# Grid with random Obstacles
pygame.draw.rect(screen,BLACK,(SIDE_x,SIDE_y,WINDOW_width,WINDOW_height),GAME_border)
pygame.draw.rect(screen,BLACK,(200,150,100,50))
pygame.draw.circle(screen,BLACK,(100,150),20)
pygame.draw.circle(screen,BLACK,(360,300),30)
B1 = Environment(BLACK, 25, 470, 100, 50)
B1.create(screen)
OBS=dict()
points = []
#Number of forward Steps towards random sampled point
# Step = 10
Step = 30
#Start stores a single point [Starting point- RED Point]
Start=[]
#End stores a set of destination point [Destination point- Green Point]
#Multiple points allowed to make the point appear bigger, and fast discovery,
#due to huge number of pixels in this game
End=set()
#parent stores the graph
parent=dict()
level=0
B1.ClickText()
B1.DesText("Instruction :",y=460)
B1.DesText("Click the BLACK button below to view Configuration Space for Obstacles")
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
if running==False:
break
m = pygame.mouse.get_pressed()
x,y = pygame.mouse.get_pos()
if m[0]==1:
if B1.point_inside_rec(B1.x,B1.y, B1.width, B1.height,x,y):
if level==0:
level+=1
B1.colour=RED
ConfSpace()
B1.DesText("Click the RED button and select the STARTING POINT")
elif level==1 and Start==[]:
level+=1
B1.colour=GREEN
B1.DesText("Click the GREEN button and select the DESTINATION POINT")
elif level==2 and Start:
level+=1
B1.colour=BLUE
B1.DesText("Click the BLUE button to view the RRT path and wheel trajectories")
elif level==3 and End!=set():
level+=1
B1.colour=BLUE
B1.DesText("Path is being explored using RRT Algorithm with wheel trajectories")
B1.create(screen)
B1.ClickText()
continue
elif level==1:
OBS[(x,y)]=1
elif level == 2 and Start==[]:
if B1.point_inside_game(x,y):
Start=(x,y)
pygame.draw.circle(screen, RED, (x, y), 10)
elif level == 3 :
if B1.point_inside_game(x,y):
End.add((x,y))
pygame.draw.circle(screen, GREEN, (x, y), 10)
if level>=4:
running = False
break
pygame.display.update()
running = True
parent[Start]=(-1,-1)
Trace=[]
Timer = time.time()
while(running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
x,y =B1.random_point()
if (time.time() - Timer) > 5:
Step=5
good,x_m,y_m,ans=RRT(x,y,parent)
if good and ans:
x_cur = ans[0]
y_cur = ans[1]
if screen.get_at((x_cur,y_cur)) != (0,0,0,255) and (x_cur,y_cur) not in parent:
parent[(x_cur,y_cur)]=(x_m,y_m)
if screen.get_at((x_cur,y_cur)) == (0, 255, 0, 255):
Trace=(x_cur,y_cur)
running = False
pygame.draw.line(screen, BLUE, (x_cur,y_cur), (x_m,y_m), 2)
pygame.display.update()
running = True
#This loop gets the route back to Start point
while(Trace and running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
while(Trace!=Start):
points.append(Trace)
x,y = parent[Trace]
pygame.draw.line(screen, GREEN, (x,y), Trace, 2)
Trace=(x,y)
pygame.display.update()
points.append(Start)
WheelTraj_holonomic(robot_radius, points)
#Quit
pygame.quit()