background_activity.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. #
  3. # This work is licensed under the terms of the MIT license.
  4. # For a copy, see <https://opensource.org/licenses/MIT>.
  5. """
  6. Scenario spawning elements to make the town dynamic and interesting
  7. """
  8. import carla
  9. from srunner.scenariomanager.carla_data_provider import CarlaDataProvider
  10. from srunner.scenarios.basic_scenario import BasicScenario
  11. class BackgroundActivity(BasicScenario):
  12. """
  13. Implementation of a scenario to spawn a set of background actors,
  14. and to remove traffic jams in background traffic
  15. This is a single ego vehicle scenario
  16. """
  17. town_amount = {
  18. 'Town01': 120,
  19. 'Town02': 100,
  20. 'Town03': 120,
  21. 'Town04': 200,
  22. 'Town05': 120,
  23. 'Town06': 150,
  24. 'Town07': 110,
  25. 'Town08': 180,
  26. 'Town09': 300,
  27. 'Town10': 120,
  28. }
  29. def __init__(self, world, ego_vehicles, config, randomize=False, debug_mode=False, timeout=35 * 60):
  30. """
  31. Setup all relevant parameters and create scenario
  32. """
  33. self.config = config
  34. self.debug = debug_mode
  35. self.timeout = timeout # Timeout of scenario in seconds
  36. super(BackgroundActivity, self).__init__("BackgroundActivity",
  37. ego_vehicles,
  38. config,
  39. world,
  40. debug_mode,
  41. terminate_on_failure=True,
  42. criteria_enable=True)
  43. def _initialize_actors(self, config):
  44. town_name = config.town
  45. if town_name in self.town_amount:
  46. amount = self.town_amount[town_name]
  47. else:
  48. amount = 0
  49. new_actors = CarlaDataProvider.request_new_batch_actors('vehicle.*',
  50. amount,
  51. carla.Transform(),
  52. autopilot=True,
  53. random_location=True,
  54. rolename='background')
  55. if new_actors is None:
  56. raise Exception("Error: Unable to add the background activity, all spawn points were occupied")
  57. for _actor in new_actors:
  58. self.other_actors.append(_actor)
  59. def _create_behavior(self):
  60. """
  61. Basic behavior do nothing, i.e. Idle
  62. """
  63. pass
  64. def _create_test_criteria(self):
  65. """
  66. A list of all test criteria will be created that is later used
  67. in parallel behavior tree.
  68. """
  69. pass
  70. def __del__(self):
  71. """
  72. Remove all actors upon deletion
  73. """
  74. self.remove_all_actors()