Skip to content

Commit e02d219

Browse files
committed
fix: this simulation and fifo work perfectly
1 parent e5e6fdb commit e02d219

4 files changed

Lines changed: 33 additions & 13 deletions

File tree

src/Simulateur/WebotsSimulationGymEnvironment.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class WebotsSimulationGymEnvironment(gym.Env):
1818
"""
1919

2020
def __init__(self, simulation_rank: int, vehicle_rank: int):
21+
22+
log("vvvvvvvvvvvvvvvvvvvvvvvvvv Initialisation vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
2123
super().__init__()
2224
self.simulation_rank = simulation_rank
2325
self.vehicle_rank = vehicle_rank
@@ -39,16 +41,17 @@ def __init__(self, simulation_rank: int, vehicle_rank: int):
3941
os.mkfifo(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}tosupervisor.pipe")
4042

4143
# --mode=fast --minimize --no-rendering --batch --stdout
42-
os.system(f"""
43-
webots {__file__.rsplit('/', 1)[0]}/worlds/piste{simulation_rank % n_map}.wbt --mode=fast --minimize --no-rendering --batch --stdout &
44-
echo $! {simulation_rank}_{vehicle_rank} >>/tmp/autotech/simulationranks
45-
""")
44+
if vehicle_rank == 0 :
45+
os.system(f"""
46+
webots {__file__.rsplit('/', 1)[0]}/worlds/piste{simulation_rank % n_map}.wbt --mode=fast --minimize --batch --stdout &
47+
echo $! {simulation_rank}_{vehicle_rank} >>/tmp/autotech/simulationranks
48+
""")
4649
log(f"SERVER{simulation_rank}_{vehicle_rank} : serverto{simulation_rank}_{vehicle_rank}.pipe")
4750
self.fifo_w = open(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe", "wb")
4851
log(f"SERVER{simulation_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe")
4952
self.fifo_r = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "rb")
5053

51-
log("-------------------------------------------------------------------")
54+
log("----------------------------- Inititalisation ---------------------------------")
5255

5356
def reset(self, seed=0):
5457
# basically useless function
@@ -61,6 +64,8 @@ def reset(self, seed=0):
6164
return obs, info
6265

6366
def step(self, action):
67+
68+
log("vvvvvvvvvvvvvvvvvvvvvvvvvv STEP vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
6469
log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : sending {action=}")
6570
self.fifo_w.write(action.tobytes())
6671
self.fifo_w.flush()
@@ -88,6 +93,7 @@ def step(self, action):
8893
], axis=1)
8994

9095
log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : step over")
96+
log("----------------------------- STEP ---------------------------------")
9197

9298
return obs, reward, done, truncated, info
9399

src/Simulateur/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
n_map = 2
55
n_simulations = 2
6-
n_vehicles = 1
6+
n_vehicles = 2
77
n_stupid_vehicles = 0
88
n_actions_steering = 16
99
n_actions_speed = 16

src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ def step(self):
109109
# sends observation to the supervisor
110110

111111
# First to be executed
112-
self.fifo_w.write(self.observe().tobytes())
112+
log(f"CLIENT{self.simulation_rank}/{self.i} : trying to write obs")
113+
obs = self.observe()
114+
log(f"CLIENT{self.simulation_rank}/{self.i} : driver sending {obs=}")
115+
self.fifo_w.write(obs.tobytes())
116+
self.fifo_w.flush()
113117

114118
log(f"CLIENT{self.simulation_rank}/{self.i} : trying to read from fifo")
115119
action = np.frombuffer(self.fifo_r.read(np.dtype(np.int64).itemsize * 2), dtype=np.int64)

src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,22 @@ def __init__(self, vehicle_rank: int):
129129
# returns the lidar data of all vehicles
130130
def observe(self):
131131
# gets from Vehicle
132-
133-
return np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32)
132+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : we observe")
133+
obs = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32)
134+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : observing {obs=}")
135+
return obs
136+
134137

135138
# reset the gym environment reset
136139
def reset(self, seed=None):
140+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : trying to reset vehicle")
137141
# this has to be done otherwise thec cars will shiver for a while sometimes when respawning and idk why
138142
if supervisor.getTime() - self.last_reset >= 1e-1:
143+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : getting info")
139144
self.last_reset = supervisor.getTime()
140145

141146
vehicle = supervisor.getFromDef(f"TT02_{self.vehicle_rank}")
142-
147+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : resetting all")
143148
self.checkpoint_manager.reset(seed)
144149
trans = self.checkpoint_manager.getTranslation()
145150
rot = self.checkpoint_manager.getRotation()
@@ -149,8 +154,9 @@ def reset(self, seed=None):
149154
self.checkpoint_manager.update()
150155

151156
vehicle.resetPhysics()
157+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : ok it's cooooooooool")
152158

153-
obs = self.observe()
159+
obs = np.zeros(n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution, dtype=np.float32)
154160
info = {}
155161
log(f"CLIENT{simulation_rank}/{self.vehicle_rank} : reset over")
156162
return obs, info
@@ -162,15 +168,17 @@ def step(self):
162168

163169
# we should add a beacon sensor pointing upwards to detect the beacon
164170
obs = self.observe()
171+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : observed {obs=}")
165172
sensor_data = obs[:n_sensors]
166-
173+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : sensor data {sensor_data=}")
167174
reward = 0
168175
done = np.False_
169176
truncated = np.False_
170177

171178
x, y, z = self.translation_field.getSFVec3f()
172179
b_past_checkpoint = self.checkpoint_manager.update(x, y)
173180
b_collided, = sensor_data # unpack sensor data
181+
log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : be_collided {b_collided=}")
174182

175183
if b_collided or (z < -10):
176184
#print(f"CLIENT{simulation_rank}/{self.vehicle_rank} : {b_collided=}, {z=}")
@@ -216,7 +224,7 @@ def main():
216224
#Prédiction pour séléctionner une action à partir de l"observation
217225
for e in envs:
218226
obs, reward, done, truncated, info = e.step()
219-
if done:
227+
if done:
220228
obs, info = e.reset()
221229

222230
log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {obs=}")
@@ -229,6 +237,8 @@ def main():
229237
e.fifo_w.write(truncated.tobytes())
230238
e.fifo_w.flush()
231239

240+
241+
232242

233243
for i in range(n_stupid_vehicles):
234244
tr_field = supervisor.getFromDef(f"TT02_{n_vehicles + i}").getField("translation")

0 commit comments

Comments
 (0)