SUMO co-simulation

CARLA has developed a co-simulation feature with SUMO. This allows to distribute the tasks at will, and exploit the capabilities of each simulation in favour of the user.


Requisites

First and foremost, it is necessary to install SUMO to run the co-simulation. Building from source is recommended over a simple installation, as there are new features and fixes that will improve the co-simulation.

Once that is done, set the SUMO environment variable.

echo "export SUMO_HOME=/usr/share/sumo" >> ~/.bashrc && source ~/.bashrc

SUMO is ready to run the co-simulations. There are some examples in Co-Simulation/Sumo/examples for Town01, Town04, and Town05. These .sumocfg files describe the configuration of the simulation (e.g., net, routes, vehicle types...). Use one of these to test the co-simulation. The script has different options that are detailed below. For the time being, let's run a simple example for Town04.

Run a CARLA simulation with Town04.

cd ~/carla
./CarlaUE4.sh
cd PythonAPI/util
python config.py --map Town04

Then, run the SUMO co-simulation example.

cd ~/carla/Co-Simulation/Sumo
python run_synchronization.py examples/Town04.sumocfg  --sumo-gui

Important


Run a custom co-simulation

Create carla vtypes

With the script Co-Simulation/Sumo/util/create_sumo_vtypes.py the user can create sumo vtypes, the equivalent to CARLA blueprints, based on the CARLA blueprint library.

  • --carla-host (default: 127.0.0.1) — IP of the carla host server.
  • --carla-port (default: 2000) — TCP port to listen to.
  • --output-file (default: carlavtypes.rou.xml) — The generated file containing the vtypes.

This script uses the information stored in data/vtypes.json to create the SUMO vtypes. These can be modified by editing said file.

Warning

A CARLA simulation must be running to execute the script.

Create the SUMO net

The recommended way to create a SUMO net that synchronizes with CARLA is using the script Co-Simulation/Sumo/util/netconvert_carla.py. This will draw on the netconvert tool provided by SUMO. In order to run the script, some arguments are needed.

  • xodr_file — OpenDRIVE file .xodr.
  • --output' (default:net.net.xml) — output file .net.xml.
  • --guess-tls (default:false) — SUMO can set traffic lights only for specific lanes in a road, but CARLA can't. If set to True, SUMO will not differenciate traffic lights for specific lanes, and these will be in sync with CARLA.

The output of the script will be a .net.xml that can be edited using NETEDIT. Use it to edit the routes, add demand, and eventually, prepare a simulation that can be saved as .sumocfg.

The examples provided may be helpful during this process. Take a look at Co-Simulation/Sumo/examples. For every example.sumocfg there are several related files under the same name. All of them comprise a co-simulation example.

Run the synchronization

Once a simulation is ready and saved as a .sumocfg, it is ready to run. There are some optional parameters to change the settings of the co-simulation.

  • sumo_cfg_file — The SUMO configuration file.
  • --carla-host (default: 127.0.0.1) — IP of the carla host server
  • --carla-port (default: 2000) — TCP port to listen to
  • --sumo-host (default: 127.0.0.1) — IP of the SUMO host server.
  • --sumo-port (default: 8813) — TCP port to listen to.
  • --sumo-gui — Open a window to visualize the gui version of SUMO.
  • --step-length (default: 0.05s) — Set fixed delta seconds for the simulation time-step.
  • --sync-vehicle-lights (default: False) — Synchronize vehicle lights.
  • --sync-vehicle-color (default: False) — Synchronize vehicle color.
  • --sync-vehicle-all (default: False) — Synchronize all vehicle properties.
  • --tls-manager (default: none) — Choose which simulator should manage the traffic lights. The other will update those accordingly. The options are carla, sumo, and none. If none is chosen, traffic lights will not be synchronized. Each vehicle would only obey the traffic lights in the simulator that spawn it.
python run_synchronization.py <SUMOCFG FILE> --tls-manager carla --sumo-gui

Warning

To stop the co-simulation, press Ctrl+C in the terminal that run the script.


Spawn NPCs controlled by SUMO

The co-simulation with SUMO makes for an additional feature. Vehicles can be spawned in CARLA through SUMO, and managed by the later as the Traffi Manager would do.

The script spawn_npc_sumo.py is almost equivalent to the already-known spawn_npc.py. This script automatically generates a SUMO network in a temporal folder, based on the active town in CARLA. The script will create random routes and let the vehicles roam around.

As the script runs a synchronous simulation, and spawns vehicles in it, the arguments are the same that appear in run_synchronization.py and spawn_npc.py.

  • --host (default: 127.0.0.1) — IP of the host server.
  • --port (default: 2000) — TCP port to listen to.
  • -n,--number-of-vehicles (default: 10) — Number of vehicles spawned.
  • --safe — Avoid spawning vehicles prone to accidents.
  • --filterv (default: "vehicle.")* — Filter the blueprint of the vehicles spawned.
  • --sumo-gui — Open a window to visualize SUMO.
  • --step-length (default: 0.05s) — Set fixed delta seconds for the simulation time-step.
  • --sync-vehicle-lights (default: False) — Synchronize vehicle lights state.
  • --sync-vehicle-color (default: False) — Synchronize vehicle color.
  • --sync-vehicle-all (default: False) — Synchronize all vehicle properties.
  • --tls-manager (default: none) — Choose which simulator will change the traffic lights' state. The other will update them accordingly. If none, traffic lights will not be synchronized.
# Spawn 10 vehicles, that will be managed by SUMO instead of Traffic Manager.
# CARLA in charge of traffic lights.
# Open a window for SUMO visualization.
python spawn_sumo_npc.py -n 10 --tls-manager carla --sumo-gui

That is all there is so far, regarding for the SUMO co-simulation with CARLA.

Open CARLA and mess around for a while. If there are any doubts, feel free to post these in the forum.