Exemplo n.º 1
0
    def test_all_xosc(self):
        """
        Load all examples OpenSCENARIO files
        """
        all_test_files = glob.glob('**/srunner/examples/*.xosc',
                                   recursive=True)

        for filename in all_test_files:
            client = carla.Client()
            config = OpenScenarioConfiguration(filename, client, {})
            self.assertTrue(config is not None)
            CarlaDataProvider.set_client(client)
            ego_vehicles = []
            for vehicle in config.ego_vehicles:
                ego_vehicles.append(
                    CarlaDataProvider.request_new_actor(
                        vehicle.model,
                        vehicle.transform,
                        vehicle.rolename,
                        color=vehicle.color,
                        actor_category=vehicle.category))

            scenario = OpenScenario(world=client.get_world(),
                                    ego_vehicles=ego_vehicles,
                                    config=config,
                                    config_file=filename,
                                    timeout=100000)
            self.assertTrue(scenario is not None)

            CarlaDataProvider.cleanup()
    def run_openscenario(self, args):
        """
        Run openscenario
        """

        # Load the scenario configurations provided in the config file
        if not os.path.isfile(args.openscenario):
            print("File does not exist")
            self.cleanup()
            return

        config = OpenScenarioConfiguration(args.openscenario)

        if not self.load_world(args, config.town):
            self.cleanup()
            return

        # Create scenario manager
        self.manager = ScenarioManager(self.world, args.debug)

        # Prepare scenario
        print("Preparing scenario: " + config.name)
        try:
            CarlaActorPool.set_world(self.world)
            self.prepare_ego_vehicles(config, args.waitForEgo)
            scenario = OpenScenario(world=self.world,
                                    ego_vehicles=self.ego_vehicles,
                                    config=config,
                                    config_file=args.openscenario,
                                    timeout=100000)
        except Exception as exception:
            print("The scenario cannot be loaded")
            if args.debug:
                traceback.print_exc()
            print(exception)
            self.cleanup()
            return

        self.load_and_run_scenario(args, config, scenario)

        self.cleanup(ego=(not args.waitForEgo))

        print("No more scenarios .... Exiting")
Exemplo n.º 3
0
    def _load_and_run_scenario(self, config):
        """
        Load and run the scenario given by config
        """
        result = False
        if not self._load_and_wait_for_world(config.town, config.ego_vehicles):
            self._cleanup()
            return False

        if self._args.agent:
            agent_class_name = self.module_agent.__name__.title().replace(
                '_', '')
            try:
                self.agent_instance = getattr(self.module_agent,
                                              agent_class_name)(
                                                  self._args.agentConfig)
                config.agent = self.agent_instance
            except Exception as e:  # pylint: disable=broad-except
                traceback.print_exc()
                print("Could not setup required agent due to {}".format(e))
                self._cleanup()
                return False

        # Prepare scenario
        print("Preparing scenario: " + config.name)
        try:
            self._prepare_ego_vehicles(config.ego_vehicles)
            if self._args.openscenario:
                scenario = OpenScenario(world=self.world,
                                        ego_vehicles=self.ego_vehicles,
                                        config=config,
                                        config_file=self._args.openscenario,
                                        timeout=100000)
            elif self._args.route:
                scenario = RouteScenario(world=self.world,
                                         config=config,
                                         debug_mode=self._args.debug)
            else:
                scenario_class = self._get_scenario_class_or_fail(config.type)
                scenario = scenario_class(self.world, self.ego_vehicles,
                                          config, self._args.randomize,
                                          self._args.debug)
        except Exception as exception:  # pylint: disable=broad-except
            print("The scenario cannot be loaded")
            traceback.print_exc()
            print(exception)
            self._cleanup()
            return False

        # Set the appropriate weather conditions
        self.world.set_weather(config.weather)

        # Set the appropriate road friction
        if config.friction is not None:
            friction_bp = self.world.get_blueprint_library().find(
                'static.trigger.friction')
            extent = carla.Location(1000000.0, 1000000.0, 1000000.0)
            friction_bp.set_attribute('friction', str(config.friction))
            friction_bp.set_attribute('extent_x', str(extent.x))
            friction_bp.set_attribute('extent_y', str(extent.y))
            friction_bp.set_attribute('extent_z', str(extent.z))

            # Spawn Trigger Friction
            transform = carla.Transform()
            transform.location = carla.Location(-10000.0, -10000.0, 0.0)
            self.world.spawn_actor(friction_bp, transform)

        try:
            # Load scenario and run it
            if self._args.record:
                self.client.start_recorder("{}/{}.log".format(
                    os.getenv('ROOT_SCENARIO_RUNNER', "./"), config.name))
            self.manager.load_scenario(scenario, self.agent_instance)
            self.manager.run_scenario()

            # Provide outputs if required
            self._analyze_scenario(config)

            # Remove all actors
            scenario.remove_all_actors()

            result = True
        except SensorConfigurationInvalid as e:
            self._cleanup()
            ChallengeStatisticsManager.record_fatal_error(e)
            sys.exit(-1)

        except Exception as e:  # pylint: disable=broad-except
            traceback.print_exc()
            if self._args.challenge:
                ChallengeStatisticsManager.set_error_message(
                    traceback.format_exc())
            print(e)
            result = False

        self._cleanup()
        return result
Exemplo n.º 4
0
    def _load_and_run_scenario(self, config):
        """
        Load and run the scenario given by config
        """
        result = False
        if not self._load_and_wait_for_world(config.town, config.ego_vehicles):
            self._cleanup()
            return False

        if self._args.agent:
            agent_class_name = self.module_agent.__name__.title().replace('_', '')
            try:
                self.agent_instance = getattr(self.module_agent, agent_class_name)(self._args.agentConfig)
                config.agent = self.agent_instance
            except Exception as e:          # pylint: disable=broad-except
                traceback.print_exc()
                print("Could not setup required agent due to {}".format(e))
                self._cleanup()
                return False

        # Prepare scenario
        print("Preparing scenario: " + config.name)
        try:
            self._prepare_ego_vehicles(config.ego_vehicles)
            if self._args.openscenario:
                scenario = OpenScenario(world=self.world,
                                        ego_vehicles=self.ego_vehicles,
                                        config=config,
                                        config_file=self._args.openscenario,
                                        timeout=100000)
            elif self._args.route:
                scenario = RouteScenario(world=self.world,
                                         config=config,
                                         debug_mode=self._args.debug)
            else:
                scenario_class = self._get_scenario_class_or_fail(config.type)
                scenario = scenario_class(self.world,
                                          self.ego_vehicles,
                                          config,
                                          self._args.randomize,
                                          self._args.debug)
        except Exception as exception:                  # pylint: disable=broad-except
            print("The scenario cannot be loaded")
            traceback.print_exc()
            print(exception)
            self._cleanup()
            return False

        try:
            if self._args.record:
                recorder_name = "{}/{}/{}.log".format(
                    os.getenv('SCENARIO_RUNNER_ROOT', "./"), self._args.record, config.name)
                self.client.start_recorder(recorder_name, True)

            # Load scenario and run it
            self.manager.load_scenario(scenario, self.agent_instance)
            self.manager.run_scenario()

            # Provide outputs if required
            self._analyze_scenario(config)

            # Remove all actors, stop the recorder and save all criterias (if needed)
            scenario.remove_all_actors()
            if self._args.record:
                self.client.stop_recorder()
                self._record_criteria(self.manager.scenario.get_criteria(), recorder_name)

            result = True

        except Exception as e:              # pylint: disable=broad-except
            traceback.print_exc()
            print(e)
            result = False

        self._cleanup()
        return result
Exemplo n.º 5
0
    def _load_and_run_scenario(self, args, config):
        """
        Load and run the scenario given by config
        """

        if not self._load_and_wait_for_world(args, config.town,
                                             config.ego_vehicles):
            self._cleanup()
            return

        # Prepare scenario
        print("Preparing scenario: " + config.name)
        try:
            self._prepare_ego_vehicles(config.ego_vehicles, args.waitForEgo)
            if args.openscenario:
                scenario = OpenScenario(world=self.world,
                                        ego_vehicles=self.ego_vehicles,
                                        config=config,
                                        config_file=args.openscenario,
                                        timeout=100000)
            elif args.route:
                scenario = RouteScenario(world=self.world,
                                         config=config,
                                         debug_mode=args.debug)
            else:
                scenario_class = self._get_scenario_class_or_fail(config.type)
                scenario = scenario_class(self.world, self.ego_vehicles,
                                          config, args.randomize, args.debug)
        except Exception as exception:
            print("The scenario cannot be loaded")
            if args.debug:
                traceback.print_exc()
            print(exception)
            self._cleanup()
            return

        # Set the appropriate weather conditions
        weather = carla.WeatherParameters(
            cloudyness=config.weather.cloudyness,
            precipitation=config.weather.precipitation,
            precipitation_deposits=config.weather.precipitation_deposits,
            wind_intensity=config.weather.wind_intensity,
            sun_azimuth_angle=config.weather.sun_azimuth,
            sun_altitude_angle=config.weather.sun_altitude)

        self.world.set_weather(weather)

        # Create scenario manager
        self.manager = ScenarioManager(self.world, args.debug)

        # Load scenario and run it
        self.manager.load_scenario(scenario)
        self.manager.run_scenario()

        # Provide outputs if required
        self._analyze_scenario(args, config)

        # Stop scenario and _cleanup
        self.manager.stop_scenario()
        scenario.remove_all_actors()

        self._cleanup()
Exemplo n.º 6
0
    def _load_and_run_scenario(self, args, config):
        """
        Load and run the scenario given by config
        """

        if not self._load_and_wait_for_world(args, config.town,
                                             config.ego_vehicles):
            self._cleanup()
            return

        if args.agent:
            agent_class_name = self.module_agent.__name__.title().replace(
                '_', '')
            try:
                self.agent_instance = getattr(
                    self.module_agent, agent_class_name)(args.agentConfig)
                config.agent = self.agent_instance
            except Exception as e:
                print("Could not setup required agent due to {}".format(e))
                self._cleanup()
                return

        # Prepare scenario
        print("Preparing scenario: " + config.name)
        try:
            self._prepare_ego_vehicles(config.ego_vehicles, args.waitForEgo)
            if args.openscenario:
                scenario = OpenScenario(world=self.world,
                                        ego_vehicles=self.ego_vehicles,
                                        config=config,
                                        config_file=args.openscenario,
                                        timeout=100000)
            elif args.route:
                scenario = RouteScenario(world=self.world,
                                         config=config,
                                         debug_mode=args.debug)
            else:
                scenario_class = self._get_scenario_class_or_fail(config.type)
                scenario = scenario_class(self.world, self.ego_vehicles,
                                          config, args.randomize, args.debug)
        except Exception as exception:
            print("The scenario cannot be loaded")
            if args.debug:
                traceback.print_exc()
            print(exception)
            self._cleanup()
            return

        # Set the appropriate weather conditions
        weather = carla.WeatherParameters(
            cloudyness=config.weather.cloudyness,
            precipitation=config.weather.precipitation,
            precipitation_deposits=config.weather.precipitation_deposits,
            wind_intensity=config.weather.wind_intensity,
            sun_azimuth_angle=config.weather.sun_azimuth,
            sun_altitude_angle=config.weather.sun_altitude)

        self.world.set_weather(weather)

        try:
            # Load scenario and run it
            if args.record:
                self.client.start_recorder("{}/{}.log".format(
                    os.getenv('ROOT_SCENARIO_RUNNER', "./"), config.name))
            self.manager.load_scenario(scenario, self.agent_instance)
            self.manager.run_scenario()

            # Stop scenario
            self.manager.stop_scenario()

            # Provide outputs if required
            self._analyze_scenario(args, config)

            # Remove all actors
            scenario.remove_all_actors()
        except SensorConfigurationInvalid as e:
            self._cleanup(True)
            ChallengeStatisticsManager.record_fatal_error(e)
            sys.exit(-1)
        except Exception as e:
            if args.debug:
                traceback.print_exc()
            if args.challenge:
                ChallengeStatisticsManager.set_error_message(
                    traceback.format_exc())
            print(e)

        self._cleanup()
    def run_single_scenario(self, config, vel, dist, rep):
        """
        Modified based on original load_and_run_scenario.

        Run single scenario test once.

        Load and run the scenario given by config
        """
        # original flag to identify if the scenario successfully ran
        result = False

        # additional result storage
        result_flags = ['success', 'collision', 'time_exceed']
        single_result = {
            'result': None,
            'duration_time':
            None,  # float, duration time of single scenario test run
        }

        # original method, get prepared
        if not self._load_and_wait_for_world(config.town, config.ego_vehicles):
            self._cleanup()
            return False

        # prepare agent
        if self._args.agent:
            agent_class_name = self.module_agent.__name__.title().replace(
                '_', '')
            try:
                # todo init agent instance with custom args
                self.agent_instance = getattr(self.module_agent,
                                              agent_class_name)(
                                                  self._args.agentConfig)
                config.agent = self.agent_instance
            except Exception as e:  # pylint: disable=broad-except
                traceback.print_exc()
                print("Could not setup required agent due to {}".format(e))
                self._cleanup()
                return False

        # ============================================================
        # --------------------  modifications  --------------------
        # ============================================================

        # -----------------  retrieve scenario info  -----------------
        #
        route_flag_index = {
            'left': -1,
            'straight': 0,
            'right': 1,
            'straight_1': 0,
        }

        route_config = config.name.split('-')
        ego_route_option = route_config[1]
        traffic_route_option = route_config[3]

        # # an exception for straight scenario
        # if ego_route_option == 'straight_1':
        #     ego_route_option = 'straight'

        # get turn flag
        ego_turn_flag = route_flag_index[ego_route_option]
        traffic_turn_flag = route_flag_index[traffic_route_option]

        # -----------------  Get ego route  -----------------
        # get start location of ego vehicle from config
        route_manager = ScenarioRouteManager(
            world=self.world,
            spawn_location=config.ego_vehicles[0].transform.location,
            debug=True,
            verbose=False,
        )
        # get target junction
        junction = route_manager.junction
        # ego route
        # distance to drive after the junction area
        route_distance = 3.
        ego_wp_route, ego_loc_route, ego_trans_route = \
            route_manager.get_route(spawn_location=config.ego_vehicles[0].transform.location,
                                    distance=route_distance,
                                    turning_flag=ego_turn_flag,
                                    )

        tf_distance = 50.  #
        tf_wp_route, tf_loc_route, tf_trans_route = \
            route_manager.get_route(spawn_location=config.other_actors[0].transform.location,
                                    distance=tf_distance,
                                    turning_flag=traffic_turn_flag,
                                    )

        # visualize ego route
        for wp, _ in ego_wp_route:
            draw_waypoint(self.world, wp, color=(green, green))

        # visualize traffic flow route
        for wp, _ in tf_wp_route:
            draw_waypoint(self.world, wp, color=(magenta, magenta))

        # # todo fix check api to check if test agent is loaded correctly
        # if self.agent_instance.__class__.__name__ == 'TestAgent':

        # set additional API for agent instance
        try:
            # set route option for agent instance
            self.agent_instance.load_model(
                route_option=ego_route_option,
                manual=False,  # manually set model path in test_agent.py
                # debug=True,
                debug=False,
            )
            # set carla.Client to init state manager
            self.agent_instance.set_client(self.client,
                                           self._args.trafficManagerPort)
            # set junction for the agent state manager
            self.agent_instance.set_junction(junction)
            # set transform route for ego vehicle
            self.agent_instance.set_route(ego_trans_route)
            # reset buffer at beginning of each episode
            self.agent_instance.reset_buffer()

        except:
            print()
            raise RuntimeError(
                'Fail to assign critical API to agent instance, please check.')

        # Prepare scenario
        print("Preparing scenario: " + config.name)
        try:
            self._prepare_ego_vehicles(config.ego_vehicles)

            # --------------------  modifications  --------------------
            # assign ego vehicle for agent instance, init a controller
            self.agent_instance.set_ego_vehicles(
                self.ego_vehicles[0],
                controller_timestep=1.0 / self.frame_rate,
            )
            # ---------------------------------------------------------

            if self._args.openscenario:
                scenario = OpenScenario(world=self.world,
                                        ego_vehicles=self.ego_vehicles,
                                        config=config,
                                        config_file=self._args.openscenario,
                                        timeout=100000)
            elif self._args.route:
                scenario = RouteScenario(world=self.world,
                                         config=config,
                                         debug_mode=self._args.debug)
            else:
                scenario_class = self._get_scenario_class_or_fail(config.type)
                # if using developed scenario class
                if scenario_class.__name__.startswith(
                        'IntersectionContinuousTraffic'):
                    scenario = scenario_class(
                        world=self.world,
                        ego_vehicles=self.ego_vehicles,
                        config=config,
                        # randomize=self._args.randomize,
                        randomize=
                        False,  # we use this arg to randomize distance gap
                        debug_mode=self._args.debug,
                        criteria_enable=True,
                        # timeout=80,  # todo fix time limit of a episode
                        # =========================
                        # additional args
                        ego_route=ego_loc_route,  # ego route for success check
                        wp_plan=tf_wp_route,
                        target_speed=vel,  # target speed of traffic flow
                        distance_gap=
                        dist,  # distance between traffic flow spawning
                        verbose=True,  # visualize everything
                    )
                else:  # original lines
                    scenario = scenario_class(self.world, self.ego_vehicles,
                                              config, self._args.randomize,
                                              self._args.debug)

            # ================   original lines   ================
            # scenario_class = self._get_scenario_class_or_fail(config.type)
            # # todo fix the api to init self_defined scenarios
            # scenario = scenario_class(self.world,
            #                           self.ego_vehicles,
            #                           config,
            #                           self._args.randomize,
            #                           self._args.debug,
            #                           ego_route=wp_route,
            #                           )

        except Exception as exception:  # pylint: disable=broad-except
            print("The scenario cannot be loaded")
            traceback.print_exc()
            print(exception)
            self._cleanup()
            return False

        try:
            if self._args.record:
                recorder_name = "{}/{}/{}.log".format(
                    os.getenv('SCENARIO_RUNNER_ROOT', "./"), self._args.record,
                    config.name)
                self.client.start_recorder(recorder_name, True)

            # Load scenario and run it
            self.manager.load_scenario(scenario, self.agent_instance)

            # pre run the scenario
            min_waiting_time = 100. / vel * 3.6  # assuming that first vehicle drive 100m to cross the junction
            waiting_time = float(
                random.uniform(min_waiting_time,
                               1.25 * min_waiting_time))  # in seconds
            init_speed = 10.  # in km/h
            self.manager.pre_run_scenario(
                waiting_time=waiting_time,
                ego_init_speed=init_speed,  # in km/h
            )

            # data storage path
            # get route option
            route_config = config.name.split('-')
            ego_route_option = route_config[1]
            traffic_route_option = route_config[3]

            path_name = 'ego_{}_tf_{}'.format(ego_route_option,
                                              traffic_route_option)
            file_path = os.path.join(scenario_runner_path, 'test_outputs',
                                     path_name, self.tag, 'statistics')
            os.makedirs(file_path, exist_ok=True)

            file_name = 'Vel_{:n}_Dist_{}_Rep_{}.jsonl'.format(vel, dist, rep)

            # run_scenario will return the max_acc and min_ttc of this test run
            max_acc, min_ttc = self.manager.run_scenario(
                file_path=os.path.join(file_path, file_name),
                ego_init_speed=init_speed)

            # get running results
            if scenario.scenario.test_criteria[0].collision_time:
                single_result['result'] = 'collision'
                single_result[
                    'duration_time'] = scenario.scenario.test_criteria[
                        0].collision_time
            elif self.manager.scenario_duration_game >= scenario_class.timeout - 1.:  # todo check how timeout passed to scenario class
                single_result['result'] = 'time_exceed'
                single_result['duration_time'] = scenario_class.timeout
            else:
                single_result['result'] = 'success'
                single_result[
                    'duration_time'] = self.manager.scenario_duration_game

            # add max_acc, min_ttc to result storage
            single_result['max_acc'] = max_acc
            single_result['min_ttc'] = min_ttc

            # write data at final line of json file
            with open(os.path.join(file_path, file_name), 'a') as f:
                f.write(
                    json.dumps({
                        'duration': self.manager.scenario_duration_game,
                        'result': single_result['result'],
                        'max_acc': max_acc,
                        'min_ttc': min_ttc,
                    }) + '\n')

            # Provide outputs if required
            self._analyze_scenario(config)

            # Remove all actors, stop the recorder and save all criterias (if needed)
            scenario.remove_all_actors()
            if self._args.record:
                self.client.stop_recorder()
                self._record_criteria(self.manager.scenario.get_criteria(),
                                      recorder_name)

            result = True

        except Exception as e:  # pylint: disable=broad-except
            traceback.print_exc()
            print(e)
            result = False

        self._cleanup()
        return result, single_result
Exemplo n.º 8
0
    def run_openscenario(self, args):
        """
        Run openscenario
        """

        # Load the scenario configurations provided in the config file
        if not os.path.isfile(args.openscenario):
            print("File does not exist")
            self.cleanup()
            return

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

        config = OpenScenarioConfiguration(args.openscenario)

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

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

        # Create scenario manager
        self.manager = ScenarioManager(self.world, args.debug)

        # Prepare scenario
        print("Preparing scenario: " + config.name)
        try:
            CarlaActorPool.set_world(self.world)
            self.prepare_ego_vehicles(config, args.waitForEgo)
            scenario = OpenScenario(world=self.world,
                                    ego_vehicles=self.ego_vehicles,
                                    config=config,
                                    config_file=args.openscenario,
                                    timeout=100000)
        except Exception as exception:
            print("The scenario cannot be loaded")
            if args.debug:
                traceback.print_exc()
            print(exception)
            self.cleanup()
            return

        # Load scenario and run it
        self.manager.load_scenario(scenario)
        self.manager.run_scenario()

        # Provide outputs if required
        self.analyze_scenario(args, config)

        # Stop scenario and cleanup
        self.manager.stop_scenario()
        scenario.remove_all_actors()

        self.cleanup(ego=(not args.waitForEgo))

        print("No more scenarios .... Exiting")