def _cleanup(self): """ Remove and destroy all actors """ if self.world is not None and self.manager is not None \ and self._args.agent and self.manager.get_running_status(): # Reset to asynchronous mode settings = self.world.get_settings() settings.synchronous_mode = False settings.fixed_delta_seconds = None self.world.apply_settings(settings) self.client.stop_recorder() self.manager.cleanup() CarlaDataProvider.cleanup() CarlaActorPool.cleanup() for i, _ in enumerate(self.ego_vehicles): if self.ego_vehicles[i]: if not self._args.waitForEgo: print("Destroying ego vehicle {}".format( self.ego_vehicles[i].id)) self.ego_vehicles[i].destroy() self.ego_vehicles[i] = None self.ego_vehicles = [] if self.agent_instance: self.agent_instance.destroy() self.agent_instance = None
def _cleanup(self): """ Remove and destroy all actors """ # Simulation still running and in synchronous mode? if self.world is not None and self._args.sync: try: # Reset to asynchronous mode settings = self.world.get_settings() settings.synchronous_mode = False settings.fixed_delta_seconds = None self.world.apply_settings(settings) except RuntimeError: sys.exit(-1) self.manager.cleanup() CarlaDataProvider.cleanup() for i, _ in enumerate(self.ego_vehicles): if self.ego_vehicles[i]: if not self._args.waitForEgo: print("Destroying ego vehicle {}".format(self.ego_vehicles[i].id)) self.ego_vehicles[i].destroy() self.ego_vehicles[i] = None self.ego_vehicles = [] if self.agent_instance: self.agent_instance.destroy() self.agent_instance = None
def _cleanup(self): """ Remove and destroy all actors """ # Simulation still running and in synchronous mode? if self.manager and self.manager.get_running_status() \ and hasattr(self, 'world') and self.world: # Reset to asynchronous mode settings = self.world.get_settings() settings.synchronous_mode = False settings.fixed_delta_seconds = None self.world.apply_settings(settings) self.traffic_manager.set_synchronous_mode(False) if self.manager: self.manager.cleanup() CarlaDataProvider.cleanup() for i, _ in enumerate(self.ego_vehicles): if self.ego_vehicles[i]: self.ego_vehicles[i].destroy() self.ego_vehicles[i] = None self.ego_vehicles = [] if self._agent_watchdog: self._agent_watchdog.stop() if hasattr(self, 'agent_instance') and self.agent_instance: self.agent_instance.destroy() self.agent_instance = None if hasattr(self, 'statistics_manager') and self.statistics_manager: self.statistics_manager.scenario = None
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 cleanup(self, ego=True): """ Remove and destroy all actors """ for scenario in self._list_scenarios: # Reset scenario status for proper cleanup scenario.scenario.terminate() # Do not call del here! Directly enforce the actor removal scenario.remove_all_actors() scenario = None self._client.stop_recorder() # We need enumerate here, otherwise the actors are not properly removed for i, _ in enumerate(self._instanced_sensors): if self._instanced_sensors[i] is not None: self._instanced_sensors[i].stop() self._instanced_sensors[i].destroy() self._instanced_sensors[i] = None self._instanced_sensors = [] self._sensor_interface.destroy() # We stop the sensors first to avoid problems CarlaDataProvider.cleanup() if ego and self._ego_actor is not None: self._ego_actor.destroy() self._ego_actor = None logging.debug("Removed Ego Vehicle") if self.world is not None: self.world = None
def cleanup(self, ego=False): """ Remove and destroy all actors """ # We need enumerate here, otherwise the actors are not properly removed if hasattr(self, '_sensors_list'): for i, _ in enumerate(self._sensors_list): if self._sensors_list[i] is not None: self._sensors_list[i].stop() self._sensors_list[i].destroy() self._sensors_list[i] = None self._sensors_list = [] for i, _ in enumerate(self.actors): if self.actors[i] is not None: self.actors[i].destroy() self.actors[i] = None self.actors = [] CarlaActorPool.cleanup() CarlaDataProvider.cleanup() if ego and self.ego_vehicle is not None: self.ego_vehicle.destroy() self.ego_vehicle = None
def stop_scenario(self): """ This function triggers a proper termination of a scenario """ if self.scenario is not None: self.scenario.terminate() CarlaDataProvider.cleanup()
def cleanup(self): """ This function triggers a proper termination of a scenario """ if self.scenario is not None: self.scenario.terminate() if self._agent is not None: self._agent.cleanup() self._agent = None CarlaDataProvider.cleanup()
def stop_scenario(self): """ This function triggers a proper termination of a scenario """ with self._my_lock: if self._callback_id: CarlaDataProvider.get_world().remove_on_tick(self._callback_id) self._callback_id = None if self.scenario is not None: self.scenario.terminate() if self._agent is not None: self._agent.cleanup() self._agent = None CarlaDataProvider.cleanup()
def cleanup(self, ego=False): """ Remove and destroy all actors """ settings = self.world.get_settings() settings.synchronous_mode = False self.world.apply_settings(settings) CarlaDataProvider.cleanup() CarlaActorPool.cleanup() for i, _ in enumerate(self.ego_vehicles): if self.ego_vehicles[i]: if ego: self.ego_vehicles[i].destroy() self.ego_vehicles[i] = None self.ego_vehicles = []
def clean_up(): global world settings = world.get_settings() settings.synchronous_mode = False settings.fixed_delta_seconds = None world.apply_settings(settings) CarlaDataProvider.cleanup() for actor in actor_list: carla.command.DestroyActor(actor) global work_agent work_agent.cleanup() work_agent = None global a_agent a_agent.destroy() global hic hic.quit_interface()
def _cleanup(self, ego=False): """ Remove and destroy all actors """ self.client.stop_recorder() CarlaDataProvider.cleanup() CarlaActorPool.cleanup() for i, _ in enumerate(self.ego_vehicles): if self.ego_vehicles[i]: if ego: self.ego_vehicles[i].destroy() self.ego_vehicles[i] = None self.ego_vehicles = [] if self.agent_instance: self.agent_instance.destroy() self.agent_instance = None
def cleanup(self, ego=False): """ Remove and destroy all actors """ # TODO: Print this to a file instead # Scenario name, start time, end time, stats print('Start time') print(self._start_time) print('End time') print(self.world.tick()) CarlaDataProvider.cleanup() CarlaActorPool.cleanup() for i, _ in enumerate(self.ego_vehicles): if self.ego_vehicles[i]: if ego: self.ego_vehicles[i].destroy() self.ego_vehicles[i] = None self.ego_vehicles = []
def _cleanup(self): """ Remove and destroy all actors """ self.client.stop_recorder() self.manager.cleanup() CarlaDataProvider.cleanup() CarlaActorPool.cleanup() for i, _ in enumerate(self.ego_vehicles): if self.ego_vehicles[i]: if not self._args.waitForEgo: print("Destroying ego vehicle {}".format( self.ego_vehicles[i].id)) self.ego_vehicles[i].destroy() self.ego_vehicles[i] = None self.ego_vehicles = [] if self.agent_instance: self.agent_instance.destroy() self.agent_instance = None
def run_loop(host='127.0.0.1', port=2000): # Tunable parameters client_timeout = 30.0 # in seconds wait_for_world = 20.0 # in seconds world = None safety_metrics_vector = [] metrics_hist_dict = {} try: # 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. client = carla.Client(host, int(port)) client.set_timeout(client_timeout) # Once we have a client we can retrieve the world that is currently # running. world = retrieve_wold(client) world_id = world.id current_world_id = world_id fig, axs = plt.subplots(6, figsize=(8, 10)) rss_sensor_missing = True rss_sensor = None ego_vehicle_missing = True current_episode = True while True: # get the RssSensor while rss_sensor_missing: sensors = CarlaDataProvider.get_world().get_actors().filter( 'sensor.other.rss') if sensors is not None and len(sensors) > 0: rss_sensor = sensors[0] rss_sensor_missing = False break else: print("waiting for RSS sensor") ### listen to RSS sensor attached to ego_vehicle rss_sensor.listen( lambda event: _on_rss_response(event, safety_metrics_vector)) if len(safety_metrics_vector) > 0: active_situations = save_metrics_history( metrics_hist_dict, safety_metrics_vector) print("active situations size", len(active_situations)) # create a color palette palette = plt.get_cmap('Set1') if len(active_situations) > 0: for ax in axs: ax.cla() # for i in range(0, situations_cnt): for i in active_situations: situation_id = i # print("situation id", situation_id) metrics = metrics_hist_dict[situation_id] hist_cnt = len(metrics) time = [] msdf_lon = [] msdf_latr = [] msdf_latl = [] msdv = [] d_lon = [] d_min_lon = [] pra = [] ttc = [] ttc_v = [] ttc_thr = [] mttc = [] mttc_v = [] mttc_thr = [] for j in range(0, hist_cnt): metrics_hist = metrics[j] # print("------") time.append( metrics_hist.get_timestamp().elapsed_seconds) # print(len(time), metrics_hist.get_timestamp().elapsed_seconds) msdf_lon.append(metrics_hist.get_msdf().msdf_lon) msdf_latr.append( metrics_hist.get_msdf().msdf_lat_r) msdf_latl.append( metrics_hist.get_msdf().msdf_lat_l) # print(len(msdf_lon), metrics_hist.get_msdf().msdf_lon) # print(metrics_hist.get_msdf().msdf_lat_r) # print(metrics_hist.get_msdf().msdf_lat_l) msdv.append(metrics_hist.get_msdv().msd_violation) d_lon.append(metrics_hist.d_lon) d_min_lon.append(metrics_hist.d_min_lon) # print("msdv", metrics_hist.get_msdv().msd_violation) pra.append(metrics_hist.get_pra().pra) # print("pra", metrics_hist.get_pra().pra) ttc.append(metrics_hist.get_ttc().ttc) ttc_v.append(metrics_hist.get_ttc().ttc_violation) ttc_thr.append( metrics_hist.get_ttc().ttc_threshold) # print(metrics_hist.get_ttc().ttc) # print(metrics_hist.get_ttc().ttc_violation) # print(metrics_hist.get_ttc().ttc_threshold) mttc.append(metrics_hist.get_mttc().mttc) mttc_v.append( metrics_hist.get_mttc().mttc_violation) mttc_thr.append( metrics_hist.get_mttc().mttc_threshold) # print(metrics_hist.get_mttc().mttc) # print(metrics_hist.get_mttc().mttc_violation) # print(metrics_hist.get_mttc().mttc_threshold) # print("------") # print("d_min_lon", d_min_lon, "d_lon", d_lon) axs[0].plot(time, msdf_lon, color=palette(situation_id), linewidth=1.9, label=situation_id) axs[0].set(xlabel='time [s]', ylabel='MSDF Lon [m]') axs[0].plot(time, [1] * len(time), '--r') axs[0].legend(loc="upper right") # axs[1].cla() axs[1].plot(time, msdf_latr, color=palette(situation_id), linewidth=1.9, label=situation_id) # axs[1].plot(time, msdf_latl, 'g' , label = situation_id) axs[1].plot(time, [1] * len(time), '--r') axs[1].set(xlabel='time [s]', ylabel='MSDF Lat R [m]') axs[1].legend(loc="upper right") # axs[2].cla() axs[2].plot(time, pra, color=palette(situation_id), linewidth=1.9, label=situation_id) axs[2].set(xlabel='time [s]', ylabel='PRA') axs[2].legend(loc="upper right") # axs[3].cla() axs[3].plot(time, msdv, color=palette(situation_id), linewidth=1.9, label=situation_id) axs[3].plot(time, d_lon, color='g', linewidth=1.9, label="d_lon") axs[3].plot(time, d_min_lon, color='y', linewidth=1.9, label="d_min_lon") axs[3].set(xlabel='time [s]', ylabel='MSDV') axs[3].legend(loc="upper right") # axs[4].cla() axs[4].plot(time, ttc, color=palette(situation_id), linewidth=1.9, label=situation_id) # axs[4].plot(time, ttc_v, 'g' , label = 'TTC Violation') axs[4].plot(time, ttc_thr, '--r') axs[4].set(xlabel='time [s]', ylabel='TTC [s]') axs[4].legend(loc="upper right") # axs[5].cla() axs[5].plot(time, mttc, color=palette(situation_id), linewidth=1.9, label=situation_id) # axs[5].plot(time, mttc_v, 'g' , label = situation_id) axs[5].plot(time, mttc_thr, '--r') axs[5].set(xlabel='time [s]', ylabel='MTTC [s]') axs[5].legend(loc="upper right") # Format plot # plt.subplots_adjust(bottom=0.20) fig.suptitle('Safety Metrics') # plt.tight_layout() plt.pause(0.0001) plt.savefig("safety_metrics.png") # ''' # As the world is re-loaded for every scenario, no ego exists so far # ''' # ego_vehicle_found = False # carla_vehicles = CarlaDataProvider.get_world().get_actors().filter('vehicle.*') # if len(carla_vehicles) > 0 : # for carla_vehicle in carla_vehicles: # if carla_vehicle.attributes['role_name'] == 'hero': # ego_vehicle_found = True # print("vehicle found") # if ego_vehicle_found == False: # print("---------") # print("new episode") # print("--------") # current_episode = False # # break # curr_world = CarlaDataProvider.get_world() # # current_world_id = world.id # if curr_world != world: # print("starting new episode") # print("exiting") # sys.exit(-1) # # break # if CarlaDataProvider.get_world() != world: # print("EXITING") # break finally: # settings = world.get_settings() # settings.synchronous_mode = False # world.apply_settings(settings) CarlaDataProvider.cleanup() CarlaActorPool.cleanup()