-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
92 lines (79 loc) · 3.09 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
# ____ ___ ______ ____ ______ __ /\_/\ ______
# | \ / _]| || || | / ]/ o o|| |
# | o )/ [_ | | | | | | / / | >;<|| |
# | _/| _]|_| |_| | | |_| |_|/ / | ||_| |_|
# | | | [_ | | | | | | / \_ | _ | | |
# | | | | | | | | | | \ || | | | |
# |__| |_____| |__| |____| |__| \____||_m|_m| |__|
#
# Run main.py with the arena and robot IDs:
# py -m main.py <Arena_ID> <Robot ID1> <Robot ID2> ...
#
# Spring 2024
# Karim Assi (UCLy, ESQESE, BSN)
# Spring 2022
# Titoua Knockart, Université Claude Bernard (UCBL), France
# 2021-2022
# Aleksei Apostolou, Daniel Duval, Célien Fiorelli, Geordi Gampio, Julina Matouassiloua (UCLy, ESQESE, BSN)
# Teachers
# Raphaël Cazorla, Florian Tholin, Olivier Georgeon
# Bachelor Sciences du Numérique. ESQESE. UCLy. France
import sys
import pyglet
import logging
import structlog
import csv
from petitbrain import Flock
from petitbrain.constants import TRACE_HEADERS, TRACE_FILE
import os
# Try to fix some mouse-press issue on Mac but it does not solve the problem
# https://github.com/pyglet/pyglet/issues/171
pyglet.options['osx_alt_loop'] = True
# Configure the logger
# File handler formatter
file_formatter = logging.Formatter('%(message)s')
# Console handler formatter
console_formatter = logging.Formatter('TRACE: %(message)s')
# console_formatter = logging.Formatter('%(levelname)s: %(name)s: %(message)s')
# Create the log directory if it does not exist to avoid error
if not os.path.exists("log"):
os.makedirs("log")
# File handler
file_handler = logging.FileHandler(TRACE_FILE)
file_handler.setLevel(logging.INFO) # Set the log level for the file
file_handler.setFormatter(file_formatter)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO) # Set the log level for the console
console_handler.setFormatter(console_formatter)
logging.basicConfig(level=logging.INFO, handlers=[file_handler, console_handler])
# Define a custom processor to format the log into a CSV row
def csv_processor(logger, method_name, event_dict):
# Extract values based on the headers defined
values = [event_dict.get(key, '').__str__() for key in TRACE_HEADERS]
# return values
return ','.join(values)
# Configure structlog
structlog.configure(
processors=[
structlog.processors.TimeStamper(fmt="%H:%M:%S", utc=False),
csv_processor
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
# Reset the trace file
with open(TRACE_FILE, mode='w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerow(TRACE_HEADERS)
# Check for the presence of the launch arguments
if len(sys.argv) < 3: # Argument 0 is "main.py" when launched in -m mode
print("Please provide the arena ID and the robot ID as arguments")
exit()
# Initialize the flock of robots
flock = Flock(sys.argv)
# Schedule the GUI update every 100ms
pyglet.clock.schedule_interval(flock.main, 0.1)
# Launch the GUI
pyglet.app.run()