Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added base class for user-defined scenarios
User-defined scenarios shall now be derived from BasicScenario defined in basic_scenario.py Change-Id: I24b5b90498ecba906b3330b7df6bbf218d805e11
- Loading branch information
1 parent
faaa70b
commit 0c069ab
Showing
3 changed files
with
115 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
ScenarioManager/*.pyc | ||
ScenarioManager/__pycache__/ | ||
Scenarios/*.pyc | ||
Scenarios/__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/usr/bin/env python | ||
|
||
# | ||
# This work is licensed under the terms of the MIT license. | ||
# For a copy, see <https://opensource.org/licenses/MIT>. | ||
|
||
""" | ||
This module provide the basic class for all user-defined scenarios. | ||
""" | ||
|
||
import random | ||
import sys | ||
|
||
import py_trees | ||
import carla | ||
|
||
from ScenarioManager.scenario_manager import Scenario | ||
|
||
|
||
def setup_vehicle(world, model, spawn_point, hero=False): | ||
""" | ||
Function to setup the most relevant vehicle parameters, | ||
incl. spawn point and vehicle model. | ||
""" | ||
blueprint_library = world.get_blueprint_library() | ||
|
||
# Get vehicle by model | ||
blueprint = random.choice(blueprint_library.filter(model)) | ||
if hero: | ||
blueprint.set_attribute('role_name', 'hero') | ||
else: | ||
blueprint.set_attribute('role_name', 'scenario') | ||
|
||
vehicle = world.try_spawn_actor(blueprint, spawn_point) | ||
|
||
if vehicle is None: | ||
sys.exit( | ||
"Error: Unable to spawn vehicle {} at {}".format(model, spawn_point)) | ||
|
||
# Let's put the vehicle to drive around | ||
vehicle.set_autopilot(False) | ||
|
||
return vehicle | ||
|
||
|
||
class BasicScenario(object): | ||
|
||
""" | ||
Base class for user-defined scenario | ||
""" | ||
|
||
name = None | ||
criteria_list = [] # List of evaluation criteria | ||
timeout = 60 # Timeout of scenario in seconds | ||
scenario = None | ||
|
||
ego_vehicle = None | ||
other_vehicles = [] | ||
|
||
def __init__(self, name, debug_mode=False): | ||
""" | ||
Setup all relevant parameters and create scenario | ||
and instantiate scenario manager | ||
""" | ||
|
||
self.name = name | ||
|
||
# Setup scenario | ||
if debug_mode: | ||
py_trees.logging.level = py_trees.logging.Level.DEBUG | ||
|
||
behavior = self.create_behavior() | ||
criteria = self.create_test_criteria() | ||
self.scenario = Scenario( | ||
behavior, criteria, self.name, self.timeout) | ||
|
||
def create_behavior(self): | ||
""" | ||
Pure virtual function to setup user-defined scenario behavior | ||
""" | ||
|
||
raise NotImplementedError( | ||
"This function is re-implemented by all scenarios" | ||
"If this error becomes visible the class hierarchy is somehow broken") | ||
|
||
def create_test_criteria(self): | ||
""" | ||
Pure virtual function to setup user-defined evaluation criteria for the | ||
scenario | ||
""" | ||
|
||
raise NotImplementedError( | ||
"This function is re-implemented by all scenarios" | ||
"If this error becomes visible the class hierarchy is somehow broken") | ||
|
||
def __del__(self): | ||
""" | ||
Cleanup. | ||
- Removal of the vehicles | ||
""" | ||
actors = [self.ego_vehicle] + self.other_vehicles | ||
for actor in actors: | ||
actor.destroy() | ||
actor = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters