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))
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_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 CarlaDataProvider.set_traffic_manager_port( int(self._args.trafficManagerPort)) tm = self.client.get_trafficmanager(int(self._args.trafficManagerPort)) tm.set_random_device_seed(int(self._args.trafficManagerSeed)) if self._args.sync: tm.set_synchronous_mode(True) # 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
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()) vehicle = world.spawn_actor(bp, transform)