forked from rfccambridge/robocup-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
104 lines (94 loc) · 3.72 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
""" This is the control center, configures + launches all threads
To run: python3 main.py
(on Windows maybe just use: python main.py)
"""
import sys
import signal
import traceback
from gamestate import GameState
from vision import SSLVisionDataProvider
from refbox import RefboxDataProvider
from strategy import Strategy
from visualization import Visualizer
from comms import Comms
from simulator import Simulator
# whether or not we are running with real field and robots
IS_SIMULATION = True
VISION_ONLY = True # turns off command sending if real
CONTROL_BOTH_TEAMS = False
# we will control home team in a real match
HOME_TEAM = 'blue'
AWAY_TEAM = 'yellow' if HOME_TEAM == 'blue' else 'blue'
# which simulator initial setup to use (if simulating)
SIMULATION_SETUP = 'full_teams'
# which strategies each team is running (see strategy module)
HOME_STRATEGY = 'goalie_test'
AWAY_STRATEGY = None
# loop wait times for each thread - how much to sleep between loops
VISION_LOOP_SLEEP = .02
COMMS_SEND_LOOP_SLEEP = .1
COMMS_RECEIVE_LOOP_SLEEP = .1
CONTROL_LOOP_SLEEP = .1
SIMULATION_LOOP_SLEEP = .05
VISUALIZATION_LOOP_SLEEP = .05
GAME_LOOP_SLEEP = .1
if __name__ == '__main__':
VERBOSE = False
# initialize gamestate + all other modules
gamestate = GameState()
vision = SSLVisionDataProvider(gamestate)
refbox = RefboxDataProvider(gamestate)
home_comms = Comms(gamestate, HOME_TEAM)
away_comms = Comms(gamestate, AWAY_TEAM, True)
simulator = Simulator(gamestate)
home_strategy = Strategy(gamestate, HOME_TEAM)
away_strategy = Strategy(gamestate, AWAY_TEAM)
# choose which modules to run based on run conditions
print('Spinning up Threads...')
if IS_SIMULATION:
# spin up simulator to replace actual vision data + comms
simulator.start_simulating(SIMULATION_SETUP, SIMULATION_LOOP_SLEEP)
else:
# spin up ssl-vision data polling to update gamestate
vision.start_updating(VISION_LOOP_SLEEP)
if not VISION_ONLY:
# spin up comms to send commands to robots
home_comms.start_sending(COMMS_SEND_LOOP_SLEEP)
# home_comms.start_receiving(COMMS_RECEIVE_LOOP_SLEEP)
if CONTROL_BOTH_TEAMS:
away_comms.start_sending(COMMS_SEND_LOOP_SLEEP)
# away_comms.start_sending(COMMS_RECEIVE_LOOP_SLEEP)
refbox.start_updating()
# spin up strategy threads to control the robots
home_strategy.start_controlling(HOME_STRATEGY, CONTROL_LOOP_SLEEP)
if CONTROL_BOTH_TEAMS:
away_strategy.start_controlling(AWAY_STRATEGY, CONTROL_LOOP_SLEEP)
# initialize visualizer to show robots on screen
visualizer = Visualizer(gamestate, home_strategy, away_strategy)
# start the game - now everything should be going
gamestate.start_game(GAME_LOOP_SLEEP)
# Prepare to be interrupted by user
exit_signal_received = False
def exit_gracefully(signum, frame):
global exit_signal_received
if exit_signal_received:
return
else:
exit_signal_received = True
print('Exiting Everything')
# clean up all threads
vision.stop_updating()
refbox.stop_updating()
home_comms.stop_sending_and_receiving()
away_comms.stop_sending_and_receiving()
simulator.stop_simulating()
home_strategy.stop_controlling()
away_strategy.stop_controlling()
gamestate.end_game()
print('Done Cleaning Up All Threads')
sys.exit()
signal.signal(signal.SIGINT, exit_gracefully)
print('Running! Ctrl-c repeatedly to quit (C-c-k on eshell?!)')
# (visualizer runs on main thread to work on all platforms)
visualizer.visualization_loop(VISUALIZATION_LOOP_SLEEP)
traceback.print_stack()