other_leading_vehicle.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Other Leading Vehicle scenario:
  7. The scenario realizes a common driving behavior, in which the
  8. user-controlled ego vehicle follows a leading car driving down
  9. a given road. At some point the leading car has to decelerate.
  10. The ego vehicle has to react accordingly by changing lane to avoid a
  11. collision and follow the leading car in other lane. The scenario ends
  12. either via a timeout, or if the ego vehicle drives some distance.
  13. """
  14. import py_trees
  15. import carla
  16. from srunner.scenariomanager.carla_data_provider import CarlaDataProvider
  17. from srunner.scenariomanager.scenarioatomics.atomic_behaviors import (ActorTransformSetter,
  18. WaypointFollower,
  19. ActorDestroy)
  20. from srunner.scenariomanager.scenarioatomics.atomic_criteria import CollisionTest
  21. from srunner.scenariomanager.scenarioatomics.atomic_trigger_conditions import (InTriggerDistanceToVehicle,
  22. DriveDistance)
  23. from srunner.scenarios.basic_scenario import BasicScenario
  24. from srunner.tools.scenario_helper import get_waypoint_in_distance
  25. class OtherLeadingVehicle(BasicScenario):
  26. """
  27. This class holds everything required for a simple "Other Leading Vehicle"
  28. scenario involving a user controlled vehicle and two other actors.
  29. Traffic Scenario 05
  30. This is a single ego vehicle scenario
  31. """
  32. def __init__(self, world, ego_vehicles, config, randomize=False, debug_mode=False, criteria_enable=True,
  33. timeout=80):
  34. """
  35. Setup all relevant parameters and create scenario
  36. """
  37. self._world = world
  38. self._map = CarlaDataProvider.get_map()
  39. self._first_vehicle_location = 35
  40. self._second_vehicle_location = self._first_vehicle_location + 1
  41. self._ego_vehicle_drive_distance = self._first_vehicle_location * 4
  42. self._first_vehicle_speed = 55
  43. self._second_vehicle_speed = 45
  44. self._reference_waypoint = self._map.get_waypoint(config.trigger_points[0].location)
  45. self._other_actor_max_brake = 1.0
  46. self._first_actor_transform = None
  47. self._second_actor_transform = None
  48. # Timeout of scenario in seconds
  49. self.timeout = timeout
  50. super(OtherLeadingVehicle, self).__init__("VehicleDeceleratingInMultiLaneSetUp",
  51. ego_vehicles,
  52. config,
  53. world,
  54. debug_mode,
  55. criteria_enable=criteria_enable)
  56. def _initialize_actors(self, config):
  57. """
  58. Custom initialization
  59. """
  60. first_vehicle_waypoint, _ = get_waypoint_in_distance(self._reference_waypoint, self._first_vehicle_location)
  61. second_vehicle_waypoint, _ = get_waypoint_in_distance(self._reference_waypoint, self._second_vehicle_location)
  62. second_vehicle_waypoint = second_vehicle_waypoint.get_left_lane()
  63. first_vehicle_transform = carla.Transform(first_vehicle_waypoint.transform.location,
  64. first_vehicle_waypoint.transform.rotation)
  65. second_vehicle_transform = carla.Transform(second_vehicle_waypoint.transform.location,
  66. second_vehicle_waypoint.transform.rotation)
  67. first_vehicle = CarlaDataProvider.request_new_actor('vehicle.nissan.patrol', first_vehicle_transform)
  68. second_vehicle = CarlaDataProvider.request_new_actor('vehicle.audi.tt', second_vehicle_transform)
  69. self.other_actors.append(first_vehicle)
  70. self.other_actors.append(second_vehicle)
  71. self._first_actor_transform = first_vehicle_transform
  72. self._second_actor_transform = second_vehicle_transform
  73. def _create_behavior(self):
  74. """
  75. The scenario defined after is a "other leading vehicle" scenario. After
  76. invoking this scenario, the user controlled vehicle has to drive towards the
  77. moving other actors, then make the leading actor to decelerate when user controlled
  78. vehicle is at some close distance. Finally, the user-controlled vehicle has to change
  79. lane to avoid collision and follow other leading actor in other lane to end the scenario.
  80. If this does not happen within 90 seconds, a timeout stops the scenario or the ego vehicle
  81. drives certain distance and stops the scenario.
  82. """
  83. # start condition
  84. driving_in_same_direction = py_trees.composites.Parallel("All actors driving in same direction",
  85. policy=py_trees.common.ParallelPolicy.SUCCESS_ON_ONE)
  86. leading_actor_sequence_behavior = py_trees.composites.Sequence("Decelerating actor sequence behavior")
  87. # both actors moving in same direction
  88. keep_velocity = py_trees.composites.Parallel("Trigger condition for deceleration",
  89. policy=py_trees.common.ParallelPolicy.SUCCESS_ON_ONE)
  90. keep_velocity.add_child(WaypointFollower(self.other_actors[0], self._first_vehicle_speed, avoid_collision=True))
  91. keep_velocity.add_child(InTriggerDistanceToVehicle(self.other_actors[0], self.ego_vehicles[0], 55))
  92. # Decelerating actor sequence behavior
  93. decelerate = self._first_vehicle_speed / 3.2
  94. leading_actor_sequence_behavior.add_child(keep_velocity)
  95. leading_actor_sequence_behavior.add_child(WaypointFollower(self.other_actors[0], decelerate,
  96. avoid_collision=True))
  97. # end condition
  98. ego_drive_distance = DriveDistance(self.ego_vehicles[0], self._ego_vehicle_drive_distance)
  99. # Build behavior tree
  100. sequence = py_trees.composites.Sequence("Scenario behavior")
  101. parallel_root = py_trees.composites.Parallel(policy=py_trees.common.ParallelPolicy.SUCCESS_ON_ONE)
  102. parallel_root.add_child(ego_drive_distance)
  103. parallel_root.add_child(driving_in_same_direction)
  104. driving_in_same_direction.add_child(leading_actor_sequence_behavior)
  105. driving_in_same_direction.add_child(WaypointFollower(self.other_actors[1], self._second_vehicle_speed,
  106. avoid_collision=True))
  107. sequence.add_child(ActorTransformSetter(self.other_actors[0], self._first_actor_transform))
  108. sequence.add_child(ActorTransformSetter(self.other_actors[1], self._second_actor_transform))
  109. sequence.add_child(parallel_root)
  110. sequence.add_child(ActorDestroy(self.other_actors[0]))
  111. sequence.add_child(ActorDestroy(self.other_actors[1]))
  112. return sequence
  113. def _create_test_criteria(self):
  114. """
  115. A list of all test criteria will be created that is later used
  116. in parallel behavior tree.
  117. """
  118. criteria = []
  119. collision_criterion = CollisionTest(self.ego_vehicles[0])
  120. criteria.append(collision_criterion)
  121. return criteria
  122. def __del__(self):
  123. self.remove_all_actors()