master_scenario.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. Basic CARLA Autonomous Driving training scenario
  7. """
  8. import py_trees
  9. from srunner.scenarioconfigs.route_scenario_configuration import RouteConfiguration
  10. from srunner.scenariomanager.scenarioatomics.atomic_behaviors import Idle
  11. from srunner.scenariomanager.scenarioatomics.atomic_criteria import (CollisionTest,
  12. InRouteTest,
  13. RouteCompletionTest,
  14. OutsideRouteLanesTest,
  15. RunningRedLightTest,
  16. RunningStopTest,
  17. ActorSpeedAboveThresholdTest)
  18. from srunner.scenarios.basic_scenario import BasicScenario
  19. class MasterScenario(BasicScenario):
  20. """
  21. Implementation of a Master scenario that controls the route.
  22. This is a single ego vehicle scenario
  23. """
  24. radius = 10.0 # meters
  25. def __init__(self, world, ego_vehicles, config, randomize=False, debug_mode=False, criteria_enable=True,
  26. timeout=300):
  27. """
  28. Setup all relevant parameters and create scenario
  29. """
  30. self.config = config
  31. self.route = None
  32. # Timeout of scenario in seconds
  33. self.timeout = timeout
  34. if hasattr(self.config, 'route'):
  35. self.route = self.config.route
  36. else:
  37. raise ValueError("Master scenario must have a route")
  38. super(MasterScenario, self).__init__("MasterScenario", ego_vehicles=ego_vehicles, config=config,
  39. world=world, debug_mode=debug_mode,
  40. terminate_on_failure=True, criteria_enable=criteria_enable)
  41. def _create_behavior(self):
  42. """
  43. Basic behavior do nothing, i.e. Idle
  44. """
  45. # Build behavior tree
  46. sequence = py_trees.composites.Sequence("MasterScenario")
  47. idle_behavior = Idle()
  48. sequence.add_child(idle_behavior)
  49. return sequence
  50. def _create_test_criteria(self):
  51. """
  52. A list of all test criteria will be created that is later used
  53. in parallel behavior tree.
  54. """
  55. if isinstance(self.route, RouteConfiguration):
  56. route = self.route.data
  57. else:
  58. route = self.route
  59. collision_criterion = CollisionTest(self.ego_vehicles[0], terminate_on_failure=False)
  60. route_criterion = InRouteTest(self.ego_vehicles[0],
  61. route=route,
  62. offroad_max=30,
  63. terminate_on_failure=True)
  64. completion_criterion = RouteCompletionTest(self.ego_vehicles[0], route=route)
  65. outsidelane_criterion = OutsideRouteLanesTest(self.ego_vehicles[0], route=route)
  66. red_light_criterion = RunningRedLightTest(self.ego_vehicles[0])
  67. stop_criterion = RunningStopTest(self.ego_vehicles[0])
  68. blocked_criterion = ActorSpeedAboveThresholdTest(self.ego_vehicles[0],
  69. speed_threshold=0.1,
  70. below_threshold_max_time=90.0,
  71. terminate_on_failure=True)
  72. parallel_criteria = py_trees.composites.Parallel("group_criteria",
  73. policy=py_trees.common.ParallelPolicy.SUCCESS_ON_ONE)
  74. parallel_criteria.add_child(completion_criterion)
  75. parallel_criteria.add_child(collision_criterion)
  76. parallel_criteria.add_child(route_criterion)
  77. parallel_criteria.add_child(outsidelane_criterion)
  78. parallel_criteria.add_child(red_light_criterion)
  79. parallel_criteria.add_child(stop_criterion)
  80. parallel_criteria.add_child(blocked_criterion)
  81. return parallel_criteria
  82. def __del__(self):
  83. """
  84. Remove all actors upon deletion
  85. """
  86. self.remove_all_actors()