MCPcopy Create free account
hub / github.com/AdaCompNUS/summit / CarlaSyncMode

Class CarlaSyncMode

PythonAPI/examples/synchronous_mode.py:41–90  ·  view source on GitHub ↗

Context manager to synchronize output from different sensors. Synchronous mode is enabled as long as we are inside this context with CarlaSyncMode(world, sensors) as sync_mode: while True: data = sync_mode.tick(timeout=1.0)

Source from the content-addressed store, hash-verified

39
40
41class CarlaSyncMode(object):
42 """
43 Context manager to synchronize output from different sensors. Synchronous
44 mode is enabled as long as we are inside this context
45
46 with CarlaSyncMode(world, sensors) as sync_mode:
47 while True:
48 data = sync_mode.tick(timeout=1.0)
49
50 """
51
52 def __init__(self, world, *sensors, **kwargs):
53 self.world = world
54 self.sensors = sensors
55 self.frame = None
56 self.delta_seconds = 1.0 / kwargs.get('fps', 20)
57 self._queues = []
58 self._settings = None
59
60 def __enter__(self):
61 self._settings = self.world.get_settings()
62 self.frame = self.world.apply_settings(carla.WorldSettings(
63 no_rendering_mode=False,
64 synchronous_mode=True,
65 fixed_delta_seconds=self.delta_seconds))
66
67 def make_queue(register_event):
68 q = queue.Queue()
69 register_event(q.put)
70 self._queues.append(q)
71
72 make_queue(self.world.on_tick)
73 for sensor in self.sensors:
74 make_queue(sensor.listen)
75 return self
76
77 def tick(self, timeout):
78 self.frame = self.world.tick()
79 data = [self._retrieve_data(q, timeout) for q in self._queues]
80 assert all(x.frame == self.frame for x in data)
81 return data
82
83 def __exit__(self, *args, **kwargs):
84 self.world.apply_settings(self._settings)
85
86 def _retrieve_data(self, sensor_queue, timeout):
87 while True:
88 data = sensor_queue.get(timeout=timeout)
89 if data.frame == self.frame:
90 return data
91
92
93def draw_image(surface, image, blend=False):

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected