/
Simulate.py
72 lines (60 loc) · 2.56 KB
/
Simulate.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
from planning.Planner import Planner
import warnings
import time
from Control.dict_control import Controller
import traceback
import sys
from Simulator.Simulator import Simulator
from Simulator.Visualise import Visualise
import time
import pygame
warnings.filterwarnings("ignore", category=DeprecationWarning)
SHOWLABELS = False # Doesn't work on DICE
FPS = 40
DELAY = 3 # frames before a command is executed
# DON'T SET HIGHER DELAY THAN DELAY UNTIL A NEXT COMMAND IS READ
# No mechanism for queing commands
class Main:
def __init__(self):
# Initialise world
left_def = {'x' : 20, 'y' : 160, 'angle':1.1, 'velocity': 0}
left_atk = {'x' : 300, 'y' : 150, 'angle':1.5, 'velocity': 0}
right_def = {'x' : 450, 'y' : 110, 'angle':4, 'velocity': 0}
right_atk = {'x' : 180, 'y' : 85, 'angle':3.14, 'velocity': 0}
ball = {'x' : 450, 'y' : 150, 'angle':1.5, 'velocity': 4}
initial_state = {'our_defender':left_def, 'our_attacker':left_atk, 'their_attacker':right_atk, 'their_defender':right_def, 'ball':ball}
# Set up planner(s)
left_def_planner = Planner('left', 2, attacker=False)
left_atk_planner = None #Planner('left', 2, attacker=True)
right_def_planner = Planner('right', 2, attacker=False)
right_atk_planner = Planner('right', 2, attacker=True)
# Create simulator
self.sim = Simulator(left_def=left_def_planner,left_atk=left_atk_planner, right_def=right_def_planner,
right_atk=right_atk_planner, world=initial_state, fps=FPS)
# Create visualisor
self.disp = Visualise(labels=SHOWLABELS)
self.disp.set_world(self.sim.get_world_new())
self.disp.show()
self.control_loop()
def control_loop(self):
"""
The main loop for the control system. Runs until ESC is pressed.
Takes a frame from the camera; processes it, gets the world state;
gets the actions for the robots to perform; passes it to the robot
controllers before finally updating the GUI.
"""
clock = pygame.time.Clock()
counter = 0
sim = self.sim
while True:
if counter % 5 == 0: # Not allowed to read a command every frame
self.sim.read_commands(delay=DELAY)
sim.step()
self.disp.set_world(sim.get_world_new())
if counter % 5 == 0:
self.disp.read_messages()
self.disp.show()
counter = counter + 1
clock.tick(FPS)
if __name__ == '__main__':
Main()