Example #1
0
    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:
            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()
        CarlaActorPool.set_client(self.client)
        CarlaActorPool.set_world(self.world)
        CarlaDataProvider.set_world(self.world)

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

        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 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
Example #2
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
Example #3
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
Example #4
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_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()
Example #6
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)
Example #7
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
        """
        # The sensors dict is important to make videos after
        self._sensors_dict = sensors
        # 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
            CarlaActorPool.set_client(client)
            CarlaActorPool.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, True)
            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
    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()