agent_wrapper.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # Copyright (c) 2019 Intel Corporation
  3. #
  4. # This work is licensed under the terms of the MIT license.
  5. # For a copy, see <https://opensource.org/licenses/MIT>.
  6. """
  7. Wrapper for autonomous agents required for tracking and checking of used sensors
  8. """
  9. from __future__ import print_function
  10. import carla
  11. from srunner.autoagents.sensor_interface import CallBack
  12. from srunner.scenariomanager.carla_data_provider import CarlaDataProvider
  13. class AgentWrapper(object):
  14. """
  15. Wrapper for autonomous agents required for tracking and checking of used sensors
  16. """
  17. _agent = None
  18. _sensors_list = []
  19. def __init__(self, agent):
  20. """
  21. Set the autonomous agent
  22. """
  23. self._agent = agent
  24. def __call__(self):
  25. """
  26. Pass the call directly to the agent
  27. """
  28. return self._agent()
  29. def setup_sensors(self, vehicle, debug_mode=False):
  30. """
  31. Create the sensors defined by the user and attach them to the ego-vehicle
  32. :param vehicle: ego vehicle
  33. :return:
  34. """
  35. bp_library = CarlaDataProvider.get_world().get_blueprint_library()
  36. for sensor_spec in self._agent.sensors():
  37. # These are the sensors spawned on the carla world
  38. bp = bp_library.find(str(sensor_spec['type']))
  39. if sensor_spec['type'].startswith('sensor.camera'):
  40. bp.set_attribute('image_size_x', str(sensor_spec['width']))
  41. bp.set_attribute('image_size_y', str(sensor_spec['height']))
  42. bp.set_attribute('fov', str(sensor_spec['fov']))
  43. sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],
  44. z=sensor_spec['z'])
  45. sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],
  46. roll=sensor_spec['roll'],
  47. yaw=sensor_spec['yaw'])
  48. elif sensor_spec['type'].startswith('sensor.lidar'):
  49. bp.set_attribute('range', str(sensor_spec['range']))
  50. bp.set_attribute('rotation_frequency', str(sensor_spec['rotation_frequency']))
  51. bp.set_attribute('channels', str(sensor_spec['channels']))
  52. bp.set_attribute('upper_fov', str(sensor_spec['upper_fov']))
  53. bp.set_attribute('lower_fov', str(sensor_spec['lower_fov']))
  54. bp.set_attribute('points_per_second', str(sensor_spec['points_per_second']))
  55. sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],
  56. z=sensor_spec['z'])
  57. sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],
  58. roll=sensor_spec['roll'],
  59. yaw=sensor_spec['yaw'])
  60. elif sensor_spec['type'].startswith('sensor.other.gnss'):
  61. sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],
  62. z=sensor_spec['z'])
  63. sensor_rotation = carla.Rotation()
  64. # create sensor
  65. sensor_transform = carla.Transform(sensor_location, sensor_rotation)
  66. sensor = CarlaDataProvider.get_world().spawn_actor(bp, sensor_transform, vehicle)
  67. # setup callback
  68. sensor.listen(CallBack(sensor_spec['id'], sensor, self._agent.sensor_interface))
  69. self._sensors_list.append(sensor)
  70. # Tick once to spawn the sensors
  71. CarlaDataProvider.get_world().tick()
  72. def cleanup(self):
  73. """
  74. Remove and destroy all sensors
  75. """
  76. for i, _ in enumerate(self._sensors_list):
  77. if self._sensors_list[i] is not None:
  78. self._sensors_list[i].stop()
  79. self._sensors_list[i].destroy()
  80. self._sensors_list[i] = None
  81. self._sensors_list = []