-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrobot.py
192 lines (171 loc) · 6.8 KB
/
robot.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
from math import sin, cos, pi
from mapa import *
from quadtree import *
from matplotlib import pyplot as plt
#Mode
QUAD = 0
GRID = 1
quadLen = []
mapLen = []
shot = 1
class RodaSimulacao(object):
def __init__(self):
self.baseDados = open("datasets/mapeamento_esparso.txt",'r')
self.map = Mapa()
self.quadRoot = Quadtree()
self.k = None
self.center = [9300,-6600]
self.scale = 4.
self.followRobot = False
self.pos = [0,0]
self.map.scale = self.scale
self.map.center = self.center
self.quadRoot.scale = self.scale
self.quadRoot.center = self.center
self.goal = self.start = None
self.mode = GRID
self.interative = True
self.sweepArea = True
def listenControls(self):
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
pygame.quit()
return
if event.type == pygame.VIDEORESIZE:
w, h = event.size
pygame.display.set_mode((w,h),pygame.RESIZABLE)
if event.type == pygame.KEYDOWN:
self.k=event.key
if event.type == pygame.KEYUP:
self.k = None
if self.k != None:
if self.k == pygame.K_UP:
self.center[1]+=300
if self.k == pygame.K_DOWN:
self.center[1]-=300
if self.k == pygame.K_RIGHT:
self.center[0]+=300
if self.k == pygame.K_LEFT:
self.center[0]-=300
if self.k == pygame.K_EQUALS:
self.scale+=0.25
if self.k == pygame.K_MINUS:
self.scale-=0.25
if self.k == pygame.K_l:
self.followRobot = not self.followRobot
if self.k == pygame.K_s:
self.mode += 1
self.mode %= 2
if self.start != None:
if self.mode == QUAD:
self.quadRoot.pathPlannig(pygame.display.get_surface(),self.start,self.goal)
if self.mode == GRID:
self.map.pathPlannig(pygame.display.get_surface(),self.start,self.goal)
if self.k == pygame.K_q:
self.quadRoot.mode += 1
self.quadRoot.mode %= 2
if self.k == pygame.K_i:
self.interative = not self.interative
if self.k == pygame.K_a:
global shot
pygame.image.save(pygame.display.get_surface(),"screenshot_{0}.png".format(shot))
shot += 1
print "scale: ", self.scale
print "center: ", self.center
self.map.scale = self.scale
self.map.center = self.center
self.quadRoot.scale = self.scale
self.quadRoot.center = self.center
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.start == None:
self.start = list(pygame.mouse.get_pos())
elif self.goal == None:
self.goal = list(pygame.mouse.get_pos())
else:
self.goal = self.start = None
if self.mode == QUAD:
self.quadRoot.pathPlannig(pygame.display.get_surface(),self.start,self.goal)
if self.mode == GRID:
self.map.pathPlannig(pygame.display.get_surface(),self.start,self.goal)
if event.button == 3:
self.goal = self.start = None
def run(self):
pygame.init()
pygame.display.set_mode((700,700),pygame.RESIZABLE)
pygame.display.update()
plt.ion()
f = plt.figure()
for l in self.baseDados:
l = l.split(',')
x,y,th = [float(i) for i in l[:3]]
leituras = [int(i) for i in l[3:]]
self.pos = [x,y]
if self.followRobot:
self.center = self.pos
self.map.center = self.pos
self.quadRoot.center = self.pos
if self.sweepArea:
if leituras != []:
self.map.putObstaculoSweep((x,y,th),leituras)
self.quadRoot.putObstaculoSweep((x,y,th),leituras)
else:
anguloLeitura = 180
for leitura in leituras:
if(leitura <= 5000):
angle = anguloLeitura+th-90
rad = angle*pi/180.
lx, ly = x+cos(rad)*leitura, y+sin(rad)*leitura
self.quadRoot.putObstaculo((lx,ly))
self.map.putObstaculo(lx,ly)
anguloLeitura -= 1
quadLen.append(len(self.quadRoot))
mapLen.append(len(self.map))
##################
# Show Results #
##################
if self.interative:
self.listenControls()
screen = pygame.display.get_surface()
screen.fill((204,204,204))
if self.mode == QUAD:
self.quadRoot.draw(screen)
self.quadRoot.drawRobot(screen,x,y,th,leituras)
if self.mode == GRID:
self.map.draw(screen)
self.map.drawRobot(screen,x,y,th,leituras)
f.clear()
plt.plot(quadLen)
f.axes[0].set_xlim([0,388])
f.canvas.get_tk_widget().update()
f.canvas.draw()
pygame.display.update()
pygame.time.wait(33)
global shot
pygame.image.save(pygame.display.get_surface(),"imgs/screenshot_{0}.png".format(shot))
shot += 1
print "Quadtree max: ", max(quadLen)
print "Quadtree size: ", quadLen[-1]
print "Map size: ", mapLen[-1]
f.clear()
plt.plot(quadLen)
# plt.plot(mapLen)
# f.axes[0].set_xlim([0,388])
f.canvas.draw()
while(True):
self.listenControls()
screen = pygame.display.get_surface()
screen.fill((204,204,204))
if self.mode == GRID:
self.map.draw(screen)
self.map.drawPathPlanning(screen)
if self.mode == QUAD:
self.quadRoot.draw(screen)
self.quadRoot.drawPathPlanning(screen)
f.canvas.get_tk_widget().update()
pygame.display.update()
if __name__ == "__main__":
r = RodaSimulacao()
r.mode = QUAD
r.mode = GRID
r.run()