1from typing import TYPE_CHECKING
2
3if TYPE_CHECKING:
4 from typing import Literal
5
6import carla
7
8__all__ = ['CustomSensorInterface']
9
10
[docs]
11class CustomSensorInterface:
12 """
13 This is a mixin for classes like the :py:class:`.camera_manager.CameraManager` or the :py:class:`classes.rss_sensor.RssSensor`
14 that either wrap around a :external_py_class:`carla.Sensor` or should have a similar interface.
15
16 Attention:
17 Not to be confused with :py:class:`srunner.autoagents.sensor_interface.SensorInterface`.
18 """
19
20 sensor: carla.Sensor
21
[docs]
22 def destroy(self) -> 'bool | None | Literal["Actor was probably destroyed by the CarlaDataProvider"]':
23 """Stops and destroys the actor of the sensor"""
24 from launch_tools import CarlaDataProvider # pylint: disable=import-outside-toplevel # noqa: PLC0415, avoid circular import
25 if self.sensor is not None:
26 self.stop()
27 if CarlaDataProvider.actor_id_exists(self.sensor.id):
28 # Note after https://github.com/carla-simulator/scenario_runner/pull/1091
29 # x = CarlaDataProvider.remove_actor_by_id(self.sensor.id)
30 CarlaDataProvider.remove_actor_by_id(self.sensor.id)
31 destroyed = "Actor was probably destroyed by the CarlaDataProvider"
32 else:
33 destroyed = self.sensor.destroy()
34 self.sensor = None # type: ignore
35 return destroyed
36 return None
37
[docs]
38 def stop(self) -> None:
39 """
40 Stop the :py:attr:`sensor` if its in listening mode.
41 If it is a :external_py_class:`carla.Sensor`, calls the simulator.
42 """
43 if self.sensor is None: # type: ignore
44 return
45 if isinstance(self.sensor, carla.Sensor):
46 if self.sensor.is_listening():
47 self.sensor.stop() # NOTE: calls simulation
48 else:
49 self.sensor.stop() # type: ignore
50
51 def __del__(self):
52 """
53 Calls :py:meth:`stop` and :py:meth:`destroy` when the object is deleted.
54
55 :meta public:
56 """
57 try:
58 if self.sensor is not None:
59 self.stop()
60 self.destroy()
61 except Exception:
62 pass