-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.py
119 lines (97 loc) · 3.72 KB
/
sim.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
import pygame
import sys, os, getopt
import random
import math
import threading
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__))) # directory from which this script is ran
sim_dir = os.path.join(__location__, 'simulation/')
sys.path.insert(0, sim_dir)
from simulation.Stage_Competition import Stage_Competition
from simulation.Stage_GridChoose import Stage_GridChoose
from simulation.Stage_AlgorithmChoose import Stage_AlgorithmChoose
from simulation.Stage_Build import Stage_Build
from simulation.Sensors import Sensor_Converter
import simulation.AI_17 as AI_17
import simulation.AI_JED as AI_JED
robotNameToLoad = None
def main(argv):
global robotNameToLoad
try:
opts, args = getopt.getopt(argv, "r:", ["robot="])
except getopt.GetoptError:
print 'arguments are -r <robotname>'
for opt, arg in opts:
if opt in ("-r", "--robot"):
robotNameToLoad = arg
print 'robot %s loaded' % str(arg)
if __name__ == "__main__":
if not os.path.exists(os.path.join(sim_dir, 'robots')):
os.mkdir(os.path.join(sim_dir, 'robots'))
main(sys.argv[1:])
if robotNameToLoad is None:
robotNameToLoad = 'robot3'
# window position
# so that it isn't placed off-screen on small screens
x = 50
y = 5
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x, y)
pygame.init()
pygame.mixer.init()
global_grid_width = 100
screen = pygame.display.set_mode((1220, 800))
pygame.display.set_caption('IEEE 2017 UMKC Robotics')
clock = pygame.time.Clock()
build = Stage_Build(screen, saved=robotNameToLoad)
grid_choose = Stage_GridChoose(screen)
alg_choose = Stage_AlgorithmChoose(screen)
comp = Stage_Competition(screen)
stages = [build, grid_choose, alg_choose, comp]
currentStage = 0
previousStage = 0
inputData = None
threads = []
def handle_returnVal(val):
global currentStage, inputData
if val[0] == 'QUIT':
quit_program()
elif val[0] == 'NEXT_STAGE':
currentStage += 1
inputData = val[1]
elif val[0] == 'PREVIOUS_STAGE':
currentStage -= 1
inputData = None
def quit_program():
pygame.quit()
sys.exit()
while 1:
# do stage actions
if currentStage >= len(stages):
quit_program()
if previousStage != currentStage:
stages[currentStage].stage_input = inputData
if isinstance(stages[currentStage], Stage_Competition):
# load board
if isinstance(inputData[1], list):
stages[currentStage].board_template = inputData[1]
else: # should be int for round number
stages[currentStage].board_template = stages[currentStage].gameboard.generate_board_round(inputData[1])
stages[currentStage].gameboard.load_board(stages[currentStage].board_template)
sensorConv = Sensor_Converter(stages[currentStage].robot, inputData[0][0], inputData[0][1])
sensorConv.create_robot_sensors()
#if you want to do 1 step at a time with 'b', set to false
stages[currentStage].shouldPerformRobotMove = True
#choose an algorithm to load: AI_17,AI_JED
algorithm_chosen = inputData[2]
#create thread for algorithm
t1 = threading.Thread(target=algorithm_chosen.simulation_impl,args=([stages[currentStage].robot,stages[currentStage].options],))
t1.daemon = True
t1.start()
threads.append(t1)
previousStage = currentStage
returnVal = stages[currentStage].performAllStageActions()
handle_returnVal(returnVal)
# update screen
pygame.display.update()
# set ticks
clock.tick(60)