77
88from controller import Supervisor
99supervisor = Supervisor ()
10-
1110import torch .nn as nn
1211
1312import psutil
@@ -82,7 +81,7 @@ def log(s: str):
8281
8382
8483
85- class WebotsVehicleGymEnvironment ( gym . Env ) :
84+ class WebotsVehicleManager :
8685 """
8786 One environment for each vehicle
8887
@@ -102,37 +101,36 @@ def __init__(self, vehicle_rank: int):
102101 # negative value so that the first reset is not skipped
103102 self .last_reset = - 1e6
104103
105- # Emitter
106- self .emitter = supervisor .getDevice (f"supervisor_emitter_{ vehicle_rank } " )
107- self .emitter .setChannel (2 * self .vehicle_rank )
104+ proc = psutil .Process (os .getpid ()) #current
105+ parent = proc .parent () #parent
106+ grandparent = parent .parent () if parent else None #grandparent
107+ pppid = str (grandparent .pid )
108108
109- # Receiver
110- self .receiver = supervisor .getDevice (f"supervisor_receiver_{ vehicle_rank } " )
111- self .receiver .enable (self .sensorTime )
112- self .receiver .setChannel (2 * self .vehicle_rank + 1 )
113109
110+ self .simulation_rank = int (
111+ re .search (
112+ pppid + r" (\d+)" ,
113+ open ("/tmp/autotech/simulationranks" , "r" ).read (),
114+ re .MULTILINE
115+ ).group (1 )
116+ )
114117
115- log (f"CLIENT{ simulation_rank } /{ vehicle_rank } _{ vehicle_rank } : begins init" )
116- log (f"CLIENT{ simulation_rank } /{ vehicle_rank } _{ vehicle_rank } : { simulation_rank } _{ vehicle_rank } toserver.pipe" )
117- self .fifo_w = open (f"/tmp/autotech/{ simulation_rank } _{ vehicle_rank } toserver.pipe" , "wb" )
118- log (f"CLIENT{ simulation_rank } /{ vehicle_rank } : serverto{ simulation_rank } _{ vehicle_rank } .pipe" )
119- self .fifo_r = open (f"/tmp/autotech/serverto{ simulation_rank } _{ vehicle_rank } .pipe" , "rb" )
120118
121- # Last data received from the car
122- self .last_data = np .zeros (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution , dtype = np .float32 )
119+ log (f"SUPERVISOR{ simulation_rank } /{ vehicle_rank } _{ vehicle_rank } : begins init" )
120+ log (f"SUPERVISOR{ simulation_rank } /{ self .vehicle_rank } : { self .simulation_rank } _{ self .vehicle_rank } tosupervisor.pipe" )
121+ self .fifo_r = open (f"/tmp/autotech/{ self .simulation_rank } _{ self .vehicle_rank } tosupervisor.pipe" , "rb" )
122+ log (f"SUPERVISOR{ simulation_rank } /{ vehicle_rank } _{ vehicle_rank } : { simulation_rank } _{ vehicle_rank } toserver.pipe" )
123+ self .fifo_w = open (f"/tmp/autotech/{ simulation_rank } _{ vehicle_rank } toserver.pipe" , "wb" )
124+
123125
124126 self .translation_field = supervisor .getFromDef (f"TT02_{ self .vehicle_rank } " ).getField ("translation" ) # may cause access issues ...
125127 self .rotation_field = supervisor .getFromDef (f"TT02_{ self .vehicle_rank } " ).getField ("rotation" ) # may cause access issues ...
126128
127129 # returns the lidar data of all vehicles
128130 def observe (self ):
129- # gets from Receiver
130- if self .receiver .getQueueLength () > 0 :
131- while self .receiver .getQueueLength () > 1 :
132- self .receiver .nextPacket ()
133- self .last_data = np .frombuffer (self .receiver .getBytes (), dtype = np .float32 )
131+ # gets from Vehicle
134132
135- return self .last_data
133+ return np . frombuffer ( self .fifo_r . read ( np . dtype ( np . float32 ). itemsize * ( n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution )), dtype = np . float32 )
136134
137135 # reset the gym environment reset
138136 def reset (self , seed = None ):
@@ -158,10 +156,9 @@ def reset(self, seed=None):
158156 return obs , info
159157
160158 # step function of the gym environment
161- def step (self , action ):
162- action_steering = np .linspace (- .4 , .4 , n_actions_steering , dtype = np .float32 )[action [0 ], None ]
163- action_speed = np .linspace (self .v_min , self .v_max , n_actions_speed , dtype = np .float32 )[action [1 ], None ]
164- self .emitter .send (np .array ([action_steering , action_speed ], dtype = np .float32 ).tobytes ())
159+ def step (self ):
160+ #action_steering = np.linspace(-.4, .4, n_actions_steering, dtype=np.float32)[action[0], None]
161+ #action_speed = np.linspace(self.v_min, self.v_max, n_actions_speed, dtype=np.float32)[action[1], None]
165162
166163 # we should add a beacon sensor pointing upwards to detect the beacon
167164 obs = self .observe ()
@@ -191,7 +188,7 @@ def step(self, action):
191188
192189def main ():
193190
194- envs = [WebotsVehicleGymEnvironment (i ) for i in range (n_vehicles )]
191+ envs = [WebotsVehicleManager (i ) for i in range (n_vehicles )]
195192 log (f"CLIENT ALL : envs created" )
196193 # check_env(env)
197194
@@ -218,22 +215,17 @@ def main():
218215 log (f"CLIENT ALL : begin step" )
219216 #Prédiction pour séléctionner une action à partir de l"observation
220217 for e in envs :
221- log (f"CLIENT{ simulation_rank } /{ e .vehicle_rank } : trying to read from fifo" )
222- action = np .frombuffer (e .fifo_r .read (np .dtype (np .int64 ).itemsize * 2 ), dtype = np .int64 )
223- log (f"CLIENT{ simulation_rank } /{ e .vehicle_rank } : received { action = } " )
224-
225- obs , reward , done , truncated , info = e .step (action )
226-
218+ obs , reward , done , truncated , info = e .step ()
227219 if done :
228220 obs , info = e .reset ()
229221
230- log (f"CLIENT { simulation_rank } /{ e .vehicle_rank } : sending { obs = } " )
222+ log (f"SUPERVISOR { simulation_rank } /{ e .vehicle_rank } : sending { obs = } " )
231223 e .fifo_w .write (obs .tobytes ())
232- log (f"CLIENT { simulation_rank } /{ e .vehicle_rank } : sending { reward = } " )
224+ log (f"SUPERVISOR { simulation_rank } /{ e .vehicle_rank } : sending { reward = } " )
233225 e .fifo_w .write (reward .tobytes ())
234- log (f"CLIENT { simulation_rank } /{ e .vehicle_rank } : sending { done = } " )
226+ log (f"SUPERVISOR { simulation_rank } /{ e .vehicle_rank } : sending { done = } " )
235227 e .fifo_w .write (done .tobytes ())
236- log (f"CLIENT { simulation_rank } /{ e .vehicle_rank } : sending { truncated = } " )
228+ log (f"SUPERVISOR { simulation_rank } /{ e .vehicle_rank } : sending { truncated = } " )
237229 e .fifo_w .write (truncated .tobytes ())
238230 e .fifo_w .flush ()
239231
0 commit comments