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