-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
240 lines (207 loc) · 8.35 KB
/
main.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
import time
import os
import sys
import signal
import __builtin__
import webInterface
from webInterface.interface import Interface
from webInterface.interface import RunningState
def clearModule(mod):
deleteList = []
for modName in sys.modules:
if mod in modName:
deleteList.append(modName)
for modName in deleteList:
del (sys.modules[modName])
def startIA():
#Re-import modules to handle source modification
clearModule("affichage")
clearModule("boards")
clearModule("cartographie")
clearModule("intelligence")
clearModule("robots")
import affichage
import boards
import cartographie
import intelligence
import robots
from cartographie.lecteurCarte import LecteurCarte
from cartographie.chercheurChemin import ChercheurChemin
from intelligence.robot import Robot
from intelligence.lecteurRobot import LecteurRobot
from intelligence.executeurObjectif import ExecuteurObjectif
global isRaspberry
# Detection du rapsberry
isRaspberry = "arm" in os.popen("uname -m").read()
# isRaspberry = "arm" in os.uname()[4]
if isRaspberry: # raspberry
screen = False
robotConnected = True
drawGraph = False and screen
else:
screen = False
robotConnected = False
drawGraph = False and screen
fichierCarte = ""
fichierObjectif = ""
fichierRobot = ""
if webInterface.instance and webInterface.instance.runningParameters:
fichierCarte = webInterface.instance.runningParameters.mapFile
fichierObjectif = webInterface.instance.runningParameters.objectiveFile
fichierRobot = webInterface.instance.runningParameters.robotFile
robotConnected = webInterface.instance.runningParameters.robotConnected
fenetre = None
# creation du robot
print "Reading robot file"
lecteurRobot = LecteurRobot(fichierRobot)
if lecteurRobot.tree is None:
print "ERROR: Can't find robot file"
return
robot = lecteurRobot.lire()
if webInterface.instance:
webInterface.instance.addCallableObject(robot)
# creation du lecteur de carte
print "Reading map file"
carte = LecteurCarte(fichierCarte, robot.largeur)
listePointInteret = carte.lire() # chargement de la carte
if len(listePointInteret) == 0:
print "ERROR: Empty map"
return
if webInterface.instance and webInterface.instance.mapBackground is None :
webInterface.instance.mapBackground = carte.getFond()
# creation de l'afficihage de la carte
if screen:
print "Creating map view"
from affichage.afficheurCarte import AfficheurCarte
affichage = AfficheurCarte(fichierCarte, listePointInteret, 0.25, 300)
fenetre = affichage.fenetre
affichage.afficherCarte() # affichage de la carte
# creation du pathfinding
print "Initializing pathfinding"
chercher = ChercheurChemin(carte.getTaille(), carte.getHash(), listePointInteret, fenetre)
if fenetre and drawGraph:
chercher.graph.dessiner(fenetre)
if fenetre:
fenetre.win.redraw()
print "Initializing robot"
simulation = robot.initialiser(chercher, listePointInteret, fenetre, not robotConnected)
if simulation and robotConnected:
return
if simulation:
print("ERROR: The program is in SIMULATION mode! Some boards can't be detected.")
print("Statring in 2s.")
time.sleep(2)
if not webInterface.instance or (webInterface.instance and webInterface.instance.runningState == RunningState.PLAY):
print "Creating IA"
IA = ExecuteurObjectif(robot, fichierObjectif, fichierCarte, chercher, fenetre) # creation de l'IA
if len(IA.listeObjectifs) == 0:
print "ERROR: Empty objective list"
return
IA.afficherObjectifs()
print "Running IA"
IA.executerObjectifs() # execution de l'IA
elif webInterface.instance and webInterface.instance.runningState == RunningState.MANUAL:
IA = ExecuteurObjectif(robot, fichierObjectif, fichierCarte, chercher, fenetre) # creation de l'IA
#if webInterface.instance:
# webInterface.instance.addCallableObject(IA)
print "Running MANUAL mode"
robot.matchDuration = 999999
robot.attendreDepart()
while webInterface.instance.runningState != RunningState.STOP:
time.sleep(1)
print("End of the match, waiting")
while True:
if webInterface.instance and webInterface.instance.runningState != RunningState.PLAY:
if robot:
robot.closeConnections()
return
time.sleep(1)
# Don't close connexions to keep score displayed
# robot.closeConnections()
time.sleep(5)
__builtin__.stopThread=False
def signal_handler(signal, frame):
__builtin__.stopThread=True
print("Exit requested, stopping IA...")
time.sleep(0.5)
print("Done.")
exit()
import traceback
def main():
signal.signal(signal.SIGINT, signal_handler)
#testPathfinding()
# create web interface
interface = Interface()
if webInterface.instance:
while True:
while webInterface.instance.runningState == RunningState.STOP:
time.sleep(0.5)
try:
startIA()
except Exception as e:
print "Execution error: {} \n {}".format(e.args, traceback.format_exc())
webInterface.instance.runningState = RunningState.STOP
webInterface.instance.clearCallableObjectList()
def testPathfinding():
import time
import os
from cartographie.lecteurCarte import LecteurCarte
#from cartographie.chercheurChemin import ChercheurChemin
from cartographie.chercheurCheminObjet import ChercheurChemin
from cartographie.chercheurCheminLigne import ChercheurChemin as ChercheurCheminLigne
# Pour tester le pathfinding, cliquez a deux endroits sur la carte
fichierCarte = "cartes/carte_2020_TDS.xml"
screen = True
robotConnected = False
drawGraph = False and screen
fenetre = None
largeurRobot = 100
print "Reading map file"
carte = LecteurCarte(fichierCarte, largeurRobot)
listePointInteret = carte.lire()
# creation de l'afficihage de la carte
if screen:
print "Creating map view"
from affichage.afficheurCarte import AfficheurCarte
affichage = AfficheurCarte(fichierCarte, listePointInteret, 0.25, largeurRobot)
fenetre = affichage.fenetre
affichage.afficherCarte() # affichage de la carte
print "Initializing pathfinding"
chercher = ChercheurChemin(carte.getTaille(), carte.getHash(), listePointInteret, fenetre)
chercherLigne = ChercheurCheminLigne(carte.getTaille(), carte.getHash(), listePointInteret, fenetre)
if fenetre and drawGraph:
chercher.graph.dessiner(fenetre)
fenetre.win.redraw()
if screen:
while True:
click1 = fenetre.win.getMouse()
click2 = fenetre.win.getMouse()
x1 = click1.getX() / fenetre.ratio - fenetre.offset
y1 = click1.getY() / fenetre.ratio - fenetre.offset
x2 = click2.getX() / fenetre.ratio - fenetre.offset
y2 = click2.getY() / fenetre.ratio - fenetre.offset
print "({} {}) ({}, {})".format(x1, y1, x2, y2)
#chercheur graph
startTime = time.time()
listMouvement = chercher.trouverChemin(x1, y1, x2, y2, listePointInteret)
print("Pathfinding Time {}ms\r\n".format( (time.time()-startTime)*1000) )
if listMouvement is None or len(listMouvement) == 0:
print "WARNING Path Not Found"
else:
for ligne in listMouvement:
ligne.setCouleur("green")
ligne.dessiner(fenetre)
fenetre.win.redraw()
#chercheur ligne
startTime = time.time()
listMouvement = chercherLigne.trouverChemin(x1, y1, x2, y2, listePointInteret)
print("Pathfinding Time {}ms\r\n".format( (time.time()-startTime)*1000) )
if listMouvement is None or len(listMouvement) == 0:
print "WARNING Path Not Found"
else:
for ligne in listMouvement:
ligne.setCouleur("green")
ligne.dessiner(fenetre)
fenetre.win.redraw()
if __name__ == "__main__":
main()