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.
First and foremost, it is necessary to [__install SUMO__](https://sumo.dlr.de/docs/Installing.html) 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.
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](#run-the-synchronization). For the time being, let's run a simple example for __Town04__.
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.
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](https://sumo.dlr.de/docs/NETCONVERT.html) tool provided by SUMO. In order to run the script, some arguments are needed.
*__`--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](https://sumo.dlr.de/docs/NETEDIT.html)__. 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.
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.
*__`--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.
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-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.