def _set_carla_town(self):
        """
        Extract the CARLA town (level) from the RoadNetwork information from OpenSCENARIO

        Note: The specification allows multiple Logics elements within the RoadNetwork element.
              Hence, there can be multiple towns specified. We just use the _last_ one.
        """
        for logic in self.xml_tree.find("RoadNetwork").findall("LogicFile"):
            self.town = logic.attrib.get('filepath', None)

        if self.town is not None and ".xodr" in self.town:
            if not os.path.isabs(self.town):
                self.town = os.path.dirname(os.path.abspath(
                    self.filename)) + "/" + self.town
            if not os.path.exists(self.town):
                raise AttributeError(
                    "The provided RoadNetwork '{}' does not exist".format(
                        self.town))

        # workaround for relative positions during init
        world = self.client.get_world()
        if world is None or world.get_map().name != self.town:
            self.logger.warning(
                " Wrong OpenDRIVE map in use. Forcing reload of CARLA world")
            if ".xodr" in self.town:
                with open(self.town) as od_file:
                    data = od_file.read()
                self.client.generate_opendrive_world(str(data))
            else:
                self.client.load_world(self.town)
            world = self.client.get_world()
            CarlaDataProvider.set_world(world)
            world.wait_for_tick()
        else:
            CarlaDataProvider.set_world(world)
Exemplo n.º 2
0
    def _load_and_wait_for_world(self, args, town, ego_vehicles=None):
        """
        Load a new CARLA world and provide data to CarlaDataProvider
        """

        self.world = self.client.load_world(town)
        settings = self.world.get_settings()
        settings.fixed_delta_seconds = 1.0 / self.frame_rate
        settings.synchronous_mode = True
        self.world.apply_settings(settings)

        self.world.reset_all_traffic_lights()

        CarlaDataProvider.set_client(self.client)
        CarlaDataProvider.set_world(self.world)
        CarlaDataProvider.set_traffic_manager_port(args.traffic_manager_port)

        self.traffic_manager.set_synchronous_mode(True)
        self.traffic_manager.set_random_device_seed(args.traffic_manager_seed)
        self.traffic_manager.set_hybrid_physics_mode(True)

        # Wait for the world to be ready
        if CarlaDataProvider.is_sync_mode():
            self.world.tick()
        else:
            self.world.wait_for_tick()

        if CarlaDataProvider.get_map().name != town:
            raise Exception("The CARLA server uses the wrong map!"
                            "This scenario requires to use map {}".format(town))
Exemplo n.º 3
0
    def _load_and_wait_for_world(self, town, ego_vehicles=None):
        """
        Load a new CARLA world and provide data to CarlaDataProvider
        """

        if self._args.reloadWorld:
            self.world = self.client.load_world(town)
        else:
            # if the world should not be reloaded, wait at least until all ego vehicles are ready
            ego_vehicle_found = False
            if self._args.waitForEgo:
                while not ego_vehicle_found and not self._shutdown_requested:
                    vehicles = self.client.get_world().get_actors().filter(
                        'vehicle.*')
                    for ego_vehicle in ego_vehicles:
                        ego_vehicle_found = False
                        for vehicle in vehicles:
                            if vehicle.attributes[
                                    'role_name'] == ego_vehicle.rolename:
                                ego_vehicle_found = True
                                break
                        if not ego_vehicle_found:
                            print("Not all ego vehicles ready. Waiting ... ")
                            time.sleep(1)
                            break

        self.world = self.client.get_world()

        if self._args.sync:
            settings = self.world.get_settings()
            settings.synchronous_mode = True
            settings.fixed_delta_seconds = 1.0 / self.frame_rate
            self.world.apply_settings(settings)

            self.traffic_manager.set_synchronous_mode(True)
            self.traffic_manager.set_random_device_seed(
                int(self._args.trafficManagerSeed))

        CarlaDataProvider.set_client(self.client)
        CarlaDataProvider.set_world(self.world)
        CarlaDataProvider.set_traffic_manager_port(
            int(self._args.trafficManagerPort))

        # Wait for the world to be ready
        if CarlaDataProvider.is_sync_mode():
            self.world.tick()
        else:
            self.world.wait_for_tick()
        if CarlaDataProvider.get_map(
        ).name != town and CarlaDataProvider.get_map().name != "OpenDriveMap":
            print("The CARLA server uses the wrong map: {}".format(
                CarlaDataProvider.get_map().name))
            print("This scenario requires to use map: {}".format(town))
            return False

        return True
    def _load_and_wait_for_world(self, town, ego_vehicles=None):
        """
        Load a new CARLA world and provide data to CarlaActorPool and CarlaDataProvider
        """

        if self._args.reloadWorld:
            try:
                self.world = self.client.load_world(town)
            except:
                pass
            settings = self.world.get_settings()
            settings.fixed_delta_seconds = 1.0 / self.frame_rate
            self.world.apply_settings(settings)
        else:
            # if the world should not be reloaded, wait at least until all ego vehicles are ready
            ego_vehicle_found = False
            if self._args.waitForEgo:
                while not ego_vehicle_found and not self._shutdown_requested:
                    vehicles = self.client.get_world().get_actors().filter(
                        'vehicle.*')
                    for ego_vehicle in ego_vehicles:
                        ego_vehicle_found = False
                        for vehicle in vehicles:
                            if vehicle.attributes[
                                    'role_name'] == ego_vehicle.rolename:
                                ego_vehicle_found = True
                                break
                        if not ego_vehicle_found:
                            print("Not all ego vehicles ready. Waiting ... ")
                            time.sleep(1)
                            break

        self.world = self.client.get_world()
        CarlaActorPool.set_client(self.client)
        CarlaActorPool.set_world(self.world)
        CarlaDataProvider.set_world(self.world)

        if self._args.agent:
            settings = self.world.get_settings()
            settings.synchronous_mode = True
            self.world.apply_settings(settings)

        # Wait for the world to be ready
        if self.world.get_settings().synchronous_mode:
            CarlaDataProvider.perform_carla_tick()
        else:
            self.world.wait_for_tick()

        if CarlaDataProvider.get_map().name != town:
            print("The CARLA server uses the wrong map!")
            print("This scenario requires to use map {}".format(town))
            return False

        return True
Exemplo n.º 5
0
def retrieve_wold(client):

    world = client.get_world()
    world_id = world.id

    settings = world.get_settings()
    # settings.synchronous_mode = True
    world.apply_settings(settings)

    CarlaActorPool.set_client(client)
    CarlaActorPool.set_world(world)
    CarlaDataProvider.set_world(world)

    return world
Exemplo n.º 6
0
    def load_world(self, town):
        """
        Load a new CARLA world and provide data to CarlaActorPool and CarlaDataProvider
        """
        self.world = self.client.load_world(town)

        # Wait for the world to be ready
        self.world.tick()

        CarlaActorPool.set_client(self.client)
        CarlaActorPool.set_world(self.world)
        CarlaDataProvider.set_world(self.world)

        return True
    def test_route_parser(self):

        args = Arguments()
        client = carla.Client(args.host, int(args.port))
        client.set_timeout(25.0)
        challenge = ChallengeEvaluator(args)

        filename = os.path.join(self.root_route_file_position,
                                'all_towns_traffic_scenarios.json')
        world_annotations = parser.parse_annotations_file(filename)
        # retrieve routes
        # Which type of file is expected ????

        filename = os.path.join(self.root_route_file_position,
                                'routes_training.xml')
        list_route_descriptions = parser.parse_routes_file(filename)

        # For each of the routes to be evaluated.
        for route_description in list_route_descriptions:
            if route_description['town_name'] == 'Town03' or route_description[
                    'town_name'] == 'Town01':
                continue
            print(" TOWN: ", route_description['town_name'])
            challenge.world = client.load_world(route_description['town_name'])
            # Set the actor pool so the scenarios can prepare themselves when needed
            CarlaActorPool.set_world(challenge.world)

            CarlaDataProvider.set_world(challenge.world)
            # find and filter potential scenarios
            # Returns the interpolation in a different format

            challenge.world.wait_for_tick()

            gps_route, route_description[
                'trajectory'] = interpolate_trajectory(
                    challenge.world, route_description['trajectory'])

            potential_scenarios_definitions, existent_triggers = parser.scan_route_for_scenarios(
                route_description, world_annotations)

            for trigger_id, possible_scenarios in potential_scenarios_definitions.items(
            ):

                print("  For trigger ", trigger_id, " --  ",
                      possible_scenarios[0]['trigger_position'])
                for scenario in possible_scenarios:
                    print("      ", scenario['name'])

            challenge.cleanup(ego=True)
Exemplo n.º 8
0
    def _set_carla_town(self):
        """
        Extract the CARLA town (level) from the RoadNetwork information from OpenSCENARIO

        Note: The specification allows multiple Logics elements within the RoadNetwork element.
              Hence, there can be multiple towns specified. We just use the _last_ one.
        """
        for logic in self.xml_tree.find("RoadNetwork").findall("Logics"):
            self.town = logic.attrib.get('filepath', None)

        if self.town is not None and ".xodr" in self.town:
            (_, tail) = os.path.split(self.town)
            self.town = tail[:-5]

        # workaround for relative positions during init
        self.client.load_world(self.town)
        world = self.client.get_world()
        CarlaDataProvider.set_world(world)
        world.wait_for_tick()
Exemplo n.º 9
0
    def load_world(self, args, town):
        """
        Load a new CARLA world and provide data to CarlaActorPool and CarlaDataProvider
        """

        if args.reloadWorld:
            self.world = self.client.load_world(town)
        else:
            if CarlaDataProvider.get_map().name != town:
                print("The CARLA server uses the wrong map!")
                print("This scenario requires to use map {}".format(town))
                return False

        CarlaActorPool.set_client(self.client)
        CarlaActorPool.set_world(self.world)
        CarlaDataProvider.set_world(self.world)

        # Wait for the world to be ready
        self.world.tick()
        return True
    def test_possible_spawns(self):

        args = Arguments()
        client = carla.Client(args.host, int(args.port))
        client.set_timeout(25.0)
        challenge = ChallengeEvaluator(args)

        filename = os.path.join(self.root_route_file_position,
                                'routes_training.xml')
        list_route_descriptions = parser.parse_routes_file(filename)

        # Which type of file is expected ????

        # For each of the routes to be evaluated.
        for route_description in list_route_descriptions:
            challenge.world = client.load_world(route_description['town_name'])

            # Set the actor pool so the scenarios can prepare themselves when needed
            CarlaActorPool.set_world(challenge.world)

            CarlaDataProvider.set_world(challenge.world)
            # find and filter potential scenarios
            # Returns the iterpolation in a different format

            challenge.world.wait_for_tick()

            gps_route, route_description[
                'trajectory'] = interpolate_trajectory(
                    challenge.world, route_description['trajectory'])

            #print (gps_route)
            #print (route_description['trajectory'])

            elevate_transform = route_description['trajectory'][0][0].transform
            elevate_transform.location.z += 0.5
            #print (elevate_transform)
            challenge.prepare_ego_car(elevate_transform)

            challenge.cleanup(ego=True)
Exemplo n.º 11
0
    def __init__(self, args):
        self.filename_traj = args.filename_traj
        """
        Setup CARLA client and world
        Setup ScenarioManager
        """

        # First of all, we need to create the client that will send the requests
        # to the simulator. Here we'll assume the simulator is accepting
        # requests in the localhost at port 2000.
        self.client = carla.Client(args.host, int(args.port))
        self.client.set_timeout(self.client_timeout)

        # Once we have a client we can retrieve the world that is currently
        # running.
        self.world = self.client.get_world()

        settings = self.world.get_settings()
        #settings.synchronous_mode = True
        self.world.apply_settings(settings)

        CarlaActorPool.set_world(self.world)
        CarlaDataProvider.set_world(self.world)
Exemplo n.º 12
0
        # set the world and data_provider
        client = carla.Client(host, port)
        client.set_timeout(2.0)

        world = client.load_world(city_name)
        traffic_manager = client.get_trafficmanager(int(trafficManagerPort))

        settings = world.get_settings()
        settings.synchronous_mode = True
        settings.fixed_delta_seconds = 1.0 / frame_rate
        world.apply_settings(settings)

        traffic_manager.set_synchronous_mode(True)
        traffic_manager.set_random_device_seed(int(trafficManagerSeed))
        CarlaDataProvider.set_client(client)
        CarlaDataProvider.set_world(world)
        CarlaDataProvider.set_traffic_manager_port(int(trafficManagerPort))

        if CarlaDataProvider.is_sync_mode():
            world.tick()
        else:
            world.wait_for_tick()

        # generate ego vehicle
        blueprint_library = world.get_blueprint_library()
        bp = random.choice(blueprint_library.filter('vehicle'))

        if bp.has_attribute('color'):
            color = random.choice(bp.get_attribute('color').recommended_values)
            bp.set_attribute('color', color)
        transform = random.choice(world.get_map().get_spawn_points())
Exemplo n.º 13
0
    def test_build_scenarios(self):

        args = Arguments()
        client = carla.Client(args.host, int(args.port))
        client.set_timeout(25.0)
        challenge = ChallengeEvaluator(args)

        filename = os.path.join(self.root_route_file_position,
                                'all_towns_traffic_scenarios1_3_4.json')
        world_annotations = parser.parse_annotations_file(filename)
        # retrieve routes
        # Which type of file is expected ????

        filename_train = os.path.join(self.root_route_file_position,
                                      'routes_training.xml')
        filename_val = os.path.join(self.root_route_file_position,
                                    'routes_devtest.xml')
        list_route_descriptions = parser.parse_routes_file(
            filename_train) + parser.parse_routes_file(filename_val)
        # For each of the routes to be evaluated.
        for route_description in list_route_descriptions:

            if route_description['town_name'] == 'Town03'\
                    or route_description['town_name'] == 'Town04':
                continue
            #         or route_description['town_name'] == 'Town02':
            #    continue
            print(" TOWN  ", route_description['town_name'])
            challenge.world = client.load_world(route_description['town_name'])
            CarlaActorPool.set_client(client)
            # Set the actor pool so the scenarios can prepare themselves when needed
            CarlaActorPool.set_world(challenge.world)
            CarlaDataProvider.set_world(challenge.world)
            # find and filter potential scenarios
            # Returns the iterpolation in a different format

            challenge.world.wait_for_tick()
            gps_route, route_description[
                'trajectory'] = interpolate_trajectory(
                    challenge.world, route_description['trajectory'])

            potential_scenarios_definitions, existent_triggers = parser.scan_route_for_scenarios(
                route_description, world_annotations)
            # Sample the scenarios
            sampled_scenarios = challenge.scenario_sampling(
                potential_scenarios_definitions)

            # prepare route's trajectory
            elevate_transform = route_description['trajectory'][0][0]
            elevate_transform.location.z += 0.5
            challenge.prepare_ego_car(elevate_transform)

            # build the master scenario based on the route and the target.
            print("loading master")
            master_scenario = challenge.build_master_scenario(
                route_description['trajectory'],
                route_description['town_name'])
            list_scenarios = [master_scenario]
            if args.background:
                background_scenario = challenge.build_background_scenario(
                    route_description['town_name'])
                list_scenarios.append(background_scenario)
            print(" Built the master scenario ")
            # build the instance based on the parsed definitions.
            print(sampled_scenarios)
            # remove scenario 8 and 9
            scenario_removed = []
            for possible_scenario in sampled_scenarios:
                if possible_scenario['name'] == 'Scenario8' or possible_scenario['name'] == 'Scenario7' or \
                        possible_scenario['name'] == 'Scenario9' or possible_scenario['name'] == 'Scenario5':
                    continue
                else:
                    scenario_removed.append(possible_scenario)

            list_scenarios += challenge.build_scenario_instances(
                scenario_removed, route_description['town_name'])
            print(" Scenarios present ", list_scenarios)

            challenge.cleanup(ego=True)
def test_routes(args):
    """
    Run all routes according to provided commandline args
    """

    # Routes are always visible
    args.route_visible = True
    challenge = ChallengeEvaluator(args)
    # retrieve worlds annotations
    world_annotations = parser.parse_annotations_file(args.scenarios)
    # retrieve routes
    route_descriptions_list = parser.parse_routes_file(args.routes)
    # find and filter potential scenarios for each of the evaluated routes
    # For each of the routes and corresponding possible scenarios to be evaluated.

    route_description = route_descriptions_list[args.route_id]
    # setup world and client assuming that the CARLA server is up and running
    client = carla.Client(args.host, int(args.port))
    client.set_timeout(challenge.client_timeout)

    # load the challenge.world variable to be used during the route
    challenge.load_world(client, route_description['town_name'])
    # Set the actor pool so the scenarios can prepare themselves when needed
    CarlaActorPool.set_client(client)
    CarlaActorPool.set_world(challenge.world)
    # Also se the Data provider pool.
    CarlaDataProvider.set_world(challenge.world)
    # tick world so we can start.
    challenge.world.tick()
    # prepare route's trajectory
    gps_route, route_description['trajectory'] = interpolate_trajectory(
        challenge.world, route_description['trajectory'])

    CarlaDataProvider.set_ego_vehicle_route(
        convert_transform_to_location(route_description['trajectory']))

    potential_scenarios_definitions, existent_triggers = parser.scan_route_for_scenarios(
        route_description, world_annotations)
    potential_scenarios_definitions = challenge.filter_scenarios(
        potential_scenarios_definitions, args.removed_scenarios)
    print("WE HAVE This number of posibilities : ",
          len(potential_scenarios_definitions))

    # Sample the scenarios to be used for this route instance.
    sampled_scenarios_definitions = challenge.scenario_sampling(
        potential_scenarios_definitions)
    # create agent
    challenge.agent_instance = getattr(
        challenge.module_agent, challenge.module_agent.__name__)(args.config)
    correct_sensors, error_message = challenge.valid_sensors_configuration(
        challenge.agent_instance, challenge.track)
    if not correct_sensors:
        # the sensor configuration is illegal
        challenge.report_fatal_error(args.filename, args.show_to_participant,
                                     error_message)
        return

    challenge.agent_instance.set_global_plan(gps_route,
                                             route_description['trajectory'])

    # prepare the ego car to run the route.
    # It starts on the first wp of the route

    elevate_transform = route_description['trajectory'][0][0]
    elevate_transform.location.z += 0.5

    challenge.prepare_ego_car(elevate_transform)

    # build the master scenario based on the route and the target.
    challenge.master_scenario = challenge.build_master_scenario(
        route_description['trajectory'], route_description['town_name'])
    list_scenarios = [challenge.master_scenario]

    if args.background:
        background_scenario = challenge.build_background_scenario(
            route_description['town_name'])
        list_scenarios.append(background_scenario)
    # build the instance based on the parsed definitions.
    print("Definition of the scenarios present on the route ")
    pprint(sampled_scenarios_definitions)
    list_scenarios += challenge.build_scenario_instances(
        sampled_scenarios_definitions, route_description['town_name'])
    if args.debug > 0:
        for scenario in sampled_scenarios_definitions:
            loc = carla.Location(
                scenario['trigger_position']['x'],
                scenario['trigger_position']['y'],
                scenario['trigger_position']['z']) + carla.Location(z=2.0)
            challenge.world.debug.draw_point(loc,
                                             size=1.0,
                                             color=carla.Color(255, 0, 0),
                                             life_time=100000)
            challenge.world.debug.draw_string(loc,
                                              scenario['name'],
                                              draw_shadow=False,
                                              color=carla.Color(0, 0, 255),
                                              life_time=100000,
                                              persistent_lines=True)

    # Tick once to start the scenarios.
    print(" Running these scenarios  --- ", list_scenarios)
    for scenario in list_scenarios:
        scenario.scenario.scenario_tree.tick_once()

    challenge.run_route(list_scenarios, route_description['trajectory'])

    # statistics recording
    challenge.record_route_statistics(route_description['id'])

    # clean up
    for scenario in list_scenarios:
        scenario.remove_all_actors()
    challenge.cleanup(ego=True)
    challenge.agent_instance.destroy()
Exemplo n.º 15
0
    def test_possible_spawns(self):

        args = Arguments()
        client = carla.Client(args.host, int(args.port))
        client.set_timeout(25.0)
        challenge = ChallengeEvaluator(args)

        filename = os.path.join(self.root_route_file_position,
                                'all_towns_traffic_scenarios.json')
        world_annotations = parser.parse_annotations_file(filename)
        # retrieve routes
        # Which type of file is expected ????

        # For each of the routes to be evaluated.
        print(" all keys ", world_annotations.keys())
        list_failed = []
        for town_name in world_annotations.keys():
            if town_name == 'Town06':
                continue
            challenge.world = client.load_world(town_name)
            CarlaActorPool.set_world(challenge.world)
            CarlaDataProvider.set_world(challenge.world)
            print("Town Name ", town_name)

            scenarios = world_annotations[town_name]
            for scenario in scenarios:  # For each existent scenario
                print("Scenario ", scenario['scenario_type'])
                for event in scenario["available_event_configurations"]:
                    waypoint = event['transform']

                    convert_waypoint_float(waypoint)
                    print("Spawn ", waypoint)
                    try:
                        challenge.prepare_ego_car(
                            convert_json_to_transform(waypoint))
                    except:
                        traceback.print_exc()
                        list_failed.append((waypoint, town_name))

                    challenge.world.wait_for_tick()
                    if 'other_actors' in event:
                        if 'left' in event['other_actors']:
                            for other_waypoint in event['other_actors'][
                                    'left']:
                                try:
                                    challenge.prepare_ego_car(
                                        convert_json_to_transform(
                                            other_waypoint))
                                except:
                                    traceback.print_exc()
                                    list_failed.append((waypoint, town_name))
                                challenge.world.wait_for_tick()
                                print("Spawn left", other_waypoint)
                        if 'right' in event['other_actors']:
                            for other_waypoint in event['other_actors'][
                                    'right']:
                                try:
                                    challenge.prepare_ego_car(
                                        convert_json_to_transform(
                                            other_waypoint))
                                except:
                                    traceback.print_exc()
                                    list_failed.append((waypoint, town_name))
                                challenge.world.wait_for_tick()
                                print("Spawn right", other_waypoint)
                        if 'front' in event['other_actors']:
                            for other_waypoint in event['other_actors'][
                                    'front']:
                                try:
                                    challenge.prepare_ego_car(
                                        convert_json_to_transform(
                                            other_waypoint))
                                except:
                                    traceback.print_exc()
                                    list_failed.append((waypoint, town_name))
                                challenge.world.wait_for_tick()
                                print("Spawn front", other_waypoint)

                    challenge.cleanup(ego=True)

        print("Failed Scenarios ", list_failed)
Exemplo n.º 16
0
    def _set_carla_town(self):
        """
        Extract the CARLA town (level) from the RoadNetwork information from OpenSCENARIO

        Note: The specification allows multiple Logics elements within the RoadNetwork element.
              Hence, there can be multiple towns specified. We just use the _last_ one.
        """
        for logic in self.xml_tree.find("RoadNetwork").findall("LogicFile"):
            self.town = logic.attrib.get('filepath', None)

        if self.town is not None and ".xodr" in self.town:
            if not os.path.isabs(self.town):
                self.town = os.path.dirname(os.path.abspath(self.filename)) + "/" + self.town
            if not os.path.exists(self.town):
                raise AttributeError("The provided RoadNetwork '{}' does not exist".format(self.town))

        # workaround for relative positions during init
        world = self.client.get_world()
        wmap = None
        if world:
            world.get_settings()
            wmap = world.get_map()

        if world is None or (wmap is not None and wmap.name.split('/')[-1] != self.town):
            if ".xodr" in self.town:
                with open(self.town, 'r', encoding='utf-8') as od_file:
                    data = od_file.read()
                index = data.find('<OpenDRIVE>')
                data = data[index:]

                old_map = ""
                if wmap is not None:
                    old_map = wmap.to_opendrive()
                    index = old_map.find('<OpenDRIVE>')
                    old_map = old_map[index:]

                if data != old_map:
                    self.logger.warning(" Wrong OpenDRIVE map in use. Forcing reload of CARLA world")

                    vertex_distance = 2.0  # in meters
                    wall_height = 1.0      # in meters
                    extra_width = 0.6      # in meters
                    world = self.client.generate_opendrive_world(str(data),
                                                                 carla.OpendriveGenerationParameters(
                                                                 vertex_distance=vertex_distance,
                                                                 wall_height=wall_height,
                                                                 additional_width=extra_width,
                                                                 smooth_junctions=True,
                                                                 enable_mesh_visibility=True))
            else:
                self.logger.warning(" Wrong map in use. Forcing reload of CARLA world")
                self.client.load_world(self.town)
                world = self.client.get_world()

            CarlaDataProvider.set_world(world)
            if CarlaDataProvider.is_sync_mode():
                world.tick()
            else:
                world.wait_for_tick()
        else:
            CarlaDataProvider.set_world(world)
    def __init__(self, args):

        # retrieving scenario_runner root
        scenario_runner_root = os.getenv('ROOT_SCENARIO_RUNNER',
                                         '/workspace/scenario_runner')

        # remaining simulation time available for this time in seconds
        challenge_time_available = int(
            os.getenv('CHALLENGE_TIME_AVAILABLE', '1080000'))
        self.challenge_time_available = challenge_time_available

        if args.spectator:
            self.spectator = args.spectator
        else:
            self.spectator = False

        self.track = args.track

        # repetition times: not use yet
        #self.repetitions = repetitions
        self.debug = args.debug
        self.ego_vehicle = None
        self._system_error = False
        self.actors = []

        # Tunable parameters
        self.client_timeout = 30.0  # in seconds
        self.wait_for_world = 20.0  # in seconds

        # CARLA world and scenario handlers
        self.world = None
        self.agent_instance = None

        self.master_scenario = None
        # self.background_scenario = None
        self.list_scenarios = []

        # first we instantiate the Agent
        if args.agent is not None:
            module_name = os.path.basename(args.agent).split('.')[0]
            sys.path.insert(0, os.path.dirname(args.agent))
            self.module_agent = importlib.import_module(module_name)
        self._sensors_list = []
        self._hop_resolution = 2.0
        self.timestamp = None

        # debugging parameters
        self.route_visible = self.debug > 0

        self.starting_point = args.starting
        self.ending_point = args.ending

        self.rendering = args.rendering

        # setup world and client assuming that the CARLA server is up and running
        client = carla.Client(args.host, int(args.port))
        client.set_timeout(self.client_timeout)

        # For debugging
        self.route_visible = self.debug > 0

        # Try to load the world and start recording
        # If not successful stop recording and continue with next iteration

        # load the self.world variable to be used during the route
        self.load_world(client, args.map)

        # Set the actor pool so the scenarios can prepare themselves when needed
        CarlaActorPool.set_client(client)
        CarlaActorPool.set_world(self.world)
        # Also se the Data provider pool.
        CarlaDataProvider.set_world(self.world)

        # route calculate
        self.gps_route, self.route = self.calculate_route()
        self.route_timeout = self.estimate_route_timeout()
Exemplo n.º 18
0
def test_routes(args):
    """
    Run all routes according to provided commandline args
    """
    # instance a challenge
    challenge = ChallengeEvaluator(args)

    # retrieve worlds annotations
    world_annotations = parser.parse_annotations_file(args.scenarios)
    # retrieve routes

    routes = []
    route_descriptions_list = parser.parse_routes_file(args.routes)
    for route_description in route_descriptions_list:
        if route_description['town_name'] == args.debug_town:
            routes.append(route_description)
    # find and filter potential scenarios for each of the evaluated routes
    # For each of the routes and corresponding possible scenarios to be evaluated.
    list_scenarios_town = []
    if args.debug_town in world_annotations.keys():
        list_scenarios_town = world_annotations[args.debug_town]

    scenarios_current_type = []
    for scenario in list_scenarios_town:
        if args.debug_scenario == scenario['scenario_type']:
            scenarios_current_type = scenario
            break

    for scenario_configuration in scenarios_current_type[
            'available_event_configurations']:
        scenario_conf = create_configuration_scenario(scenario_configuration,
                                                      args.debug_scenario)

        client = carla.Client(args.host, int(args.port))
        client.set_timeout(challenge.client_timeout)

        # load the challenge.world variable to be used during the route
        challenge.load_world(client, args.debug_town)
        # Set the actor pool so the scenarios can prepare themselves when needed
        CarlaActorPool.set_world(challenge.world)
        # Also se the Data provider pool.
        CarlaDataProvider.set_world(challenge.world)
        # tick world so we can start.

        challenge.world.tick()
        # create agent
        challenge.agent_instance = getattr(challenge.module_agent,
                                           challenge.module_agent.__name__)(
                                               args.config)
        correct_sensors, error_message = challenge.valid_sensors_configuration(
            challenge.agent_instance, challenge.track)
        if not correct_sensors:
            # the sensor configuration is illegal
            challenge.report_fatal_error(args.filename,
                                         args.show_to_participant,
                                         error_message)
            return

        waypoint = routes[0]['trajectory'][0]

        location = carla.Location(x=float(waypoint.attrib['x']),
                                  y=float(waypoint.attrib['y']),
                                  z=float(waypoint.attrib['z']))

        rotation = carla.Rotation(pitch=float(waypoint.attrib['pitch']),
                                  yaw=float(waypoint.attrib['yaw']))

        challenge.prepare_ego_car(carla.Transform(location, rotation))
        CarlaDataProvider.register_actor(challenge.ego_vehicle)

        list_scenarios = []
        list_scenarios += challenge.build_scenario_instances([scenario_conf],
                                                             args.debug_town)

        # Tick once to start the scenarios.
        print(" Running these scenarios  --- ", list_scenarios)
        for scenario in list_scenarios:
            scenario.scenario.scenario_tree.tick_once()

        challenge.run_route(list_scenarios, None, True)

        # clean up
        for scenario in list_scenarios:
            del scenario
        challenge.cleanup(ego=True)
        challenge.agent_instance.destroy()
        break

    # final measurements from the challenge
    challenge.report_challenge_statistics(args.filename,
                                          args.show_to_participant)
    del challenge
Exemplo n.º 19
0
    def __init__(self, client, vehicle_model, route, sensors,
                 scenario_definitions, exp_params, agent_name):
        """
        The experience is like a instance of the environment
         contains all the objects (vehicles, sensors) and scenarios of the the current experience
        :param vehicle_model: the model that is going to be used to spawn the ego CAR
        """

        # We save the agent name for data savings
        self._agent_name = agent_name

        # save all the experiment parameters to be used later
        self._exp_params = exp_params
        # carla recorder mode save the full carla logs to do some replays
        if self._exp_params['carla_recording']:
            client.start_recorder('env_{}_number_{}_batch_{:0>4d}.log'.format(
                self._exp_params['env_name'], self._exp_params['env_number'],
                self._exp_params['exp_number']))
        # this parameter sets all the sensor threads and the main thread into saving data
        self._save_data = exp_params['save_data']
        # we can also toogle if we want to save sensors or not.
        self._save_sensors = exp_params['save_sensors']
        # Start objects that are going to be created
        self.world = None
        # You try to reconnect a few times.
        self.MAX_CONNECTION_ATTEMPTS = 7
        # Scenario definitions to perform the scenario building
        self.scenario_definitions = scenario_definitions
        self._ego_actor = None
        self._instanced_sensors = []
        # set the client object connected to the
        self._client = client
        # We also set the town name to be used
        self._town_name = exp_params['town_name']

        self._vehicle_model = vehicle_model
        # if data is being saved we create the writer object

        # if we are going to save, we keep track of a dictionary with all the data
        self._writer = Writer(exp_params['package_name'],
                              exp_params['env_name'],
                              exp_params['env_number'],
                              exp_params['exp_number'],
                              agent_name,
                              other_vehicles=exp_params['save_opponents'],
                              walkers=exp_params['save_walkers'])

        self._environment_data = {
            'exp_measurements':
            None,  # The exp measurements are specific of the experience
            'ego_controls': None,
            'scenario_controls': None
        }
        # identify this exp
        self._exp_id = self._exp_params['exp_number']

        # We try running all the necessary initalization, if we fail we clean the
        try:
            # Sensor interface, a buffer that contains all the read sensors
            self._sensor_interface = SensorInterface(
                number_threads_barrier=len(sensors))
            # Load the world
            self._load_world()
            # Set the actor pool so the scenarios can prepare themselves when needed
            CarlaDataProvider.set_client(client)
            CarlaDataProvider.set_world(self.world)
            # Set the world for the global data provider
            CarlaDataProvider.set_world(self.world)
            # We get the lat lon ref that is important for the route
            self._lat_ref, self._lon_ref = _get_latlon_ref(self.world)
            # We instance the ego actor object
            _, self._route = interpolate_trajectory(self.world, route)
            # elevate the z transform to avoid spawning problems
            elevate_transform = self._route[0][0]
            elevate_transform.location.z += 0.5
            self._spawn_ego_car(elevate_transform)
            # We setup all the instanced sensors
            self._setup_sensors(sensors, self._ego_actor)
            # We set all the traffic lights to green to avoid having this traffic scenario.
            self._reset_map()
            # Data for building the master scenario
            self._timeout = estimate_route_timeout(self._route)
            self._master_scenario = self.build_master_scenario(
                self._route, exp_params['town_name'], self._timeout)
            other_scenarios = self.build_scenario_instances(
                scenario_definitions, self._timeout)
            self._list_scenarios = [self._master_scenario] + other_scenarios
            # Route statistics, when the route is finished there will
            # be route statistics on this object. and nothing else
            self._route_statistics = None
            # We tick the world to have some starting points
            self.tick_world()
        except RuntimeError as r:
            # We clean the dataset if there is any exception on creation
            traceback.print_exc()
            if self._save_data:
                self._clean_bad_dataset()
            # Re raise the exception
            raise r