Skip to content

Commit

Permalink
Added base class for user-defined scenarios
Browse files Browse the repository at this point in the history
User-defined scenarios shall now be derived from BasicScenario defined
in basic_scenario.py

Change-Id: I24b5b90498ecba906b3330b7df6bbf218d805e11
  • Loading branch information
fabianoboril committed Dec 4, 2018
1 parent faaa70b commit 0c069ab
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1 +1,4 @@
ScenarioManager/*.pyc
ScenarioManager/__pycache__/
Scenarios/*.pyc
Scenarios/__pycache__/
104 changes: 104 additions & 0 deletions Scenarios/basic_scenario.py
@@ -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
81 changes: 8 additions & 73 deletions Scenarios/follow_leading_vehicle.py
Expand Up @@ -25,48 +25,19 @@
from ScenarioManager.atomic_scenario_criteria import *
from ScenarioManager.scenario_manager import Scenario
from ScenarioManager.timer import TimeOut
from Scenarios.basic_scenario import *


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 FollowLeadingVehicle(object):
class FollowLeadingVehicle(BasicScenario):

"""
This class holds everything required for a simple "Follow a leading vehicle"
scenario involving two vehicles.
"""

name = "FollowVehicle"
criteria_list = [] # List of evaluation criteria
timeout = 60 # Timeout of scenario in seconds
scenario = None

# ego vehicle parameters
ego_vehicle = None
ego_vehicle_model = 'vehicle.carlamotors.carlacola'
ego_vehicle_start_x = 107
ego_vehicle_start = carla.Transform(
Expand All @@ -76,7 +47,6 @@ class FollowLeadingVehicle(object):
ego_vehicle_distance_to_other = 50 # Min. driven distance of ego vehicle [m]

# other vehicle
other_vehicles = []
other_vehicle_model = 'vehicle.*'
other_vehicle_start_x = ego_vehicle_start_x + ego_vehicle_distance_to_other
other_vehicle_start = carla.Transform(
Expand All @@ -101,14 +71,8 @@ def __init__(self, world, debug_mode=False):
self.ego_vehicle_start,
hero=True)

# 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)
super(FollowLeadingVehicle, self).__init__(name="FollowVehicle",
debug_mode=debug_mode)

def create_behavior(self):
"""
Expand Down Expand Up @@ -209,31 +173,17 @@ def create_test_criteria(self):

return criteria

def __del__(self):
"""
Cleanup.
- Removal of the vehicles
"""
actors = [self.ego_vehicle] + self.other_vehicles
for actor in actors:
actor.destroy()
actor = None


class FollowLeadingVehicleWithObstacle(object):
class FollowLeadingVehicleWithObstacle(BasicScenario):

"""
This class holds a scenario similar to FollowLeadingVehicle
but there is a (hidden) obstacle in front of the leading vehicle
"""

name = "FollowVehicleWithObstacle"
criteria_list = [] # List of evaluation criteria
timeout = 60 # Timeout of scenario in seconds
scenario = None

# ego vehicle parameters
ego_vehicle = None
ego_vehicle_model = 'vehicle.carlamotors.carlacola'
ego_vehicle_start_x = 107
ego_vehicle_start = carla.Transform(
Expand Down Expand Up @@ -276,14 +226,9 @@ def __init__(self, world, debug_mode=False):
self.ego_vehicle_start,
hero=True)

# 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)
super(
FollowLeadingVehicleWithObstacle, self).__init__(name="FollowLeadingVehicleWithObstacle",
debug_mode=debug_mode)

def create_behavior(self):
"""
Expand Down Expand Up @@ -391,13 +336,3 @@ def create_test_criteria(self):
criteria.append(keep_lane_criterion)

return criteria

def __del__(self):
"""
Cleanup.
- Removal of the vehicles
"""
actors = [self.ego_vehicle] + self.other_vehicles
for actor in actors:
actor.destroy()
actor = None

0 comments on commit 0c069ab

Please sign in to comment.