From d03af8cc9cdd0170c7456344f417deb09199ce50 Mon Sep 17 00:00:00 2001 From: Joel Moriana Date: Wed, 15 Apr 2020 13:56:45 +0200 Subject: [PATCH] avoid freeze carla --- .../PTV-Vissim/run_synchronization.py | 17 ++++++++++---- .../vissim_integration/carla_simulation.py | 6 ----- .../vissim_integration/vissim_simulation.py | 23 +++++++++++-------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Co-Simulation/PTV-Vissim/run_synchronization.py b/Co-Simulation/PTV-Vissim/run_synchronization.py index 87872e2e5..832f7605d 100644 --- a/Co-Simulation/PTV-Vissim/run_synchronization.py +++ b/Co-Simulation/PTV-Vissim/run_synchronization.py @@ -53,11 +53,11 @@ class SimulationSynchronization(object): SimulationSynchronization class is responsible for the synchronization of ptv-vissim and carla simulations. """ - def __init__(self, args): + def __init__(self, vissim_simulation, carla_simulation, args): self.args = args - self.vissim = PTVVissimSimulation(args) - self.carla = CarlaSimulation(args) + self.vissim = vissim_simulation + self.carla = carla_simulation # Mapped actor ids. self.vissim2carla_ids = {} # Contains only actors controlled by vissim. @@ -68,6 +68,12 @@ class SimulationSynchronization(object): with open(os.path.join(dir_path, 'data', 'vtypes.json')) as f: BridgeHelper.vtypes = json.load(f) + # Configuring carla simulation in sync mode. + settings = self.carla.world.get_settings() + settings.synchronous_mode = True + settings.fixed_delta_seconds = args.step_length + self.carla.world.apply_settings(settings) + def tick(self): """ Tick to simulation synchronization @@ -165,8 +171,11 @@ def synchronization_loop(args): """ Entry point for vissim-carla co-simulation. """ + carla_simulation = CarlaSimulation(args) + vissim_simulation = PTVVissimSimulation(args) + try: - synchronization = SimulationSynchronization(args) + synchronization = SimulationSynchronization(vissim_simulation, carla_simulation, args) while True: start = time.time() diff --git a/Co-Simulation/PTV-Vissim/vissim_integration/carla_simulation.py b/Co-Simulation/PTV-Vissim/vissim_integration/carla_simulation.py index 7401601c2..f7f1b1eab 100644 --- a/Co-Simulation/PTV-Vissim/vissim_integration/carla_simulation.py +++ b/Co-Simulation/PTV-Vissim/vissim_integration/carla_simulation.py @@ -37,12 +37,6 @@ class CarlaSimulation(object): self.world = self.client.get_world() self.blueprint_library = self.world.get_blueprint_library() - # Configuring carla simulation in sync mode. - settings = self.world.get_settings() - settings.synchronous_mode = True - settings.fixed_delta_seconds = args.step_length - self.world.apply_settings(settings) - # The following sets contain updated information for the current frame. self._active_actors = set() self.spawned_actors = set() diff --git a/Co-Simulation/PTV-Vissim/vissim_integration/vissim_simulation.py b/Co-Simulation/PTV-Vissim/vissim_integration/vissim_simulation.py index a0c15ee5e..7bfa83dd4 100644 --- a/Co-Simulation/PTV-Vissim/vissim_integration/vissim_simulation.py +++ b/Co-Simulation/PTV-Vissim/vissim_integration/vissim_simulation.py @@ -14,6 +14,7 @@ import enum import logging import math +import os import carla # pylint: disable=import-error from ctypes import * @@ -143,15 +144,19 @@ class PTVVissimSimulation(object): # Connection to vissim simulator. logging.info('Establishing a connection with a GUI version of PTV-Vissim') - self.ds_proxy.VISSIM_Connect(args.vissim_version, args.vissim_network, - int(1. / args.step_length), - c_double(constants.VISSIM_VISIBILITY_RADIUS), - c_ushort(constants.VISSIM_MAX_SIMULATOR_VEH), - c_ushort(constants.VISSIM_MAX_SIMULATOR_PED), - c_ushort(constants.VISSIM_MAX_SIMULATOR_DET), - c_ushort(constants.VISSIM_MAX_VISSIM_VEH), - c_ushort(constants.VISSIM_MAX_VISSIM_PED), - c_ushort(constants.VISSIM_MAX_VISSIM_SIGGRP)) + result = self.ds_proxy.VISSIM_Connect(args.vissim_version, + os.path.abspath(args.vissim_network), + int(1. / args.step_length), + c_double(constants.VISSIM_VISIBILITY_RADIUS), + c_ushort(constants.VISSIM_MAX_SIMULATOR_VEH), + c_ushort(constants.VISSIM_MAX_SIMULATOR_PED), + c_ushort(constants.VISSIM_MAX_SIMULATOR_DET), + c_ushort(constants.VISSIM_MAX_VISSIM_VEH), + c_ushort(constants.VISSIM_MAX_VISSIM_PED), + c_ushort(constants.VISSIM_MAX_VISSIM_SIGGRP)) + + if not result: + raise RuntimeError('There was an error when establishing a connection with PTV-Vissim') # Structures to keep track of the simulation state at each time step. self._vissim_vehicles = {} # vissim_actor_id: VissimVehicle (only vissim traffic)