66from config import *
77
88
9- def log (s : str ):
10- if B_DEBUG :
11- print (s , file = open ("/tmp/autotech/logs" , "a" ))
129class WebotsSimulationGymEnvironment (gym .Env ):
1310 """
1411 One environment for each vehicle
@@ -19,11 +16,20 @@ class WebotsSimulationGymEnvironment(gym.Env):
1916
2017 def __init__ (self , simulation_rank : int , vehicle_rank : int ):
2118
22- log ( "vvvvvvvvvvvvvvvvvvvvvvvvvv Initialisation vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" )
19+
2320 super ().__init__ ()
2421 self .simulation_rank = simulation_rank
2522 self .vehicle_rank = vehicle_rank
2623
24+ self .handler = logging .FileHandler (f"/tmp/autotech/Voiture_{ self .simulation_rank } _{ self .vehicle_rank } .log" )
25+ self .handler .setFormatter (FORMATTER )
26+ self .log = logging .getLogger ("SERVER" )
27+ self .log .setLevel (level = LOG_LEVEL )
28+ self .log .addHandler (self .handler )
29+
30+
31+ self .log .info ("Initialisation started" )
32+
2733 # this is only true if lidar_horizontal_resolution = camera_horizontal_resolution
2834 box_min = np .zeros ([2 , context_size , lidar_horizontal_resolution ], dtype = np .float32 )
2935 box_max = np .ones ([2 , context_size , lidar_horizontal_resolution ], dtype = np .float32 ) * 30
@@ -34,7 +40,7 @@ def __init__(self, simulation_rank: int, vehicle_rank: int):
3440 if not os .path .exists ("/tmp/autotech" ):
3541 os .mkdir ("/tmp/autotech" )
3642
37- log (f"SERVER { simulation_rank } _ { vehicle_rank } : { simulation_rank } _ { vehicle_rank } " )
43+ self . log . debug (f"Creation of the pipes " )
3844
3945 os .mkfifo (f"/tmp/autotech/{ simulation_rank } _{ vehicle_rank } toserver.pipe" )
4046 os .mkfifo (f"/tmp/autotech/serverto{ simulation_rank } _{ vehicle_rank } .pipe" )
@@ -46,12 +52,13 @@ def __init__(self, simulation_rank: int, vehicle_rank: int):
4652 webots { __file__ .rsplit ('/' , 1 )[0 ]} /worlds/piste{ simulation_rank % n_map } .wbt --mode=fast --minimize --batch --stdout &
4753 echo $! { simulation_rank } _{ vehicle_rank } >>/tmp/autotech/simulationranks
4854 """ )
49- log (f"SERVER{ simulation_rank } _{ vehicle_rank } : serverto{ simulation_rank } _{ vehicle_rank } .pipe" )
55+
56+ self .log .debug ("Connection to the vehicle" )
5057 self .fifo_w = open (f"/tmp/autotech/serverto{ simulation_rank } _{ vehicle_rank } .pipe" , "wb" )
51- log ( f"SERVER { simulation_rank } _ { vehicle_rank } : { simulation_rank } _ { vehicle_rank } toserver.pipe " )
58+ self . log . debug ( "Connection to the supervisor " )
5259 self .fifo_r = open (f"/tmp/autotech/{ simulation_rank } _{ vehicle_rank } toserver.pipe" , "rb" )
5360
54- log ( "----------------------------- Inititalisation --------------------------------- " )
61+ self . log . info ( "Initialisation finished \n " )
5562
5663 def reset (self , seed = 0 ):
5764 # basically useless function
@@ -60,26 +67,26 @@ def reset(self, seed=0):
6067 # this is true for lidar_horizontal_resolution = camera_horizontal_resolution
6168 self .context = obs = np .zeros ([2 , context_size , lidar_horizontal_resolution ], dtype = np .float32 )
6269 info = {}
63- log (f"SERVER { self . simulation_rank } _ { self . vehicle_rank } : finished reset " )
70+ self . log . info (f"reset finished\n " )
6471 return obs , info
6572
6673 def step (self , action ):
6774
68- log ( "vvvvvvvvvvvvvvvvvvvvvvvvvv STEP vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv " )
69- log (f"SERVER { self . simulation_rank } _ { self . vehicle_rank } : sending { action = } " )
75+ self . log . info ( "Starting step " )
76+ self . log . info (f"sending { action = } " )
7077 self .fifo_w .write (action .tobytes ())
7178 self .fifo_w .flush ()
7279
7380 # communication with the supervisor
74- log ( f"SERVER { self .simulation_rank } _ { self . vehicle_rank } : trying to read the fifo_r " )
81+ self .log . debug ( " trying to get info from supervisor " )
7582 cur_state = np .frombuffer (self .fifo_r .read (np .dtype (np .float32 ).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution )), dtype = np .float32 )
76- log (f"SERVER { self . simulation_rank } _ { self . vehicle_rank } : received { cur_state = } " )
83+ self . log . info (f"received { cur_state = } " )
7784 reward = np .frombuffer (self .fifo_r .read (np .dtype (np .float32 ).itemsize ), dtype = np .float32 )[0 ] # scalar
78- log (f"SERVER { self . simulation_rank } _ { self . vehicle_rank } : received { reward = } " )
85+ self . log . info (f"received { reward = } " )
7986 done = np .frombuffer (self .fifo_r .read (np .dtype (np .bool ).itemsize ), dtype = np .bool )[0 ] # scalar
80- log (f"SERVER { self . simulation_rank } _ { self . vehicle_rank } : received { done = } " )
87+ self . log . info (f"received { done = } " )
8188 truncated = np .frombuffer (self .fifo_r .read (np .dtype (np .bool ).itemsize ), dtype = np .bool )[0 ] # scalar
82- log (f"SERVER { self . simulation_rank } _ { self . vehicle_rank } : received { truncated = } " )
89+ self . log . info (f"received { truncated = } " )
8390 info = {}
8491
8592 cur_state = np .nan_to_num (cur_state [n_sensors :], nan = 0. , posinf = 30. )
@@ -92,8 +99,7 @@ def step(self, action):
9299 [lidar_obs [None ], camera_obs [None ]]
93100 ], axis = 1 )
94101
95- log (f"SERVER{ self .simulation_rank } _{ self .vehicle_rank } : step over" )
96- log ("----------------------------- STEP ---------------------------------" )
102+ self .log .info ("step over" )
97103
98104 return obs , reward , done , truncated , info
99105
0 commit comments