TraCI4TrafficLightsTutorial
From sumo
This tutorial wants to show how to use the Traffic Control Interface (in short TraCI) on a simple example. TraCI gives the possibility to control a running road traffic simulation. Therefore a TCP-based client/server architecture was established where SUMO acts as a server and the “controller” is the client. Our “contoller” will be a Python-Script which receives informations about the actual state from the server and then sends instructions back.
Contents |
Example description
Our example plays on a simple signalized intersection with four approaches. We only have traffic on the horizontal axe and important vehicles (like trams, trains, fire engines, ...) on the vertical axe from north to south. On the approach in the north we have an induction loop to recognize entering vehicles. While no vehicle enters from the north we give all the time green on the horizontal axe but when a vehicle enters the inductions loop we switch immediately the signal so the vehicle can cross the intersection without a stop.
Data preparation
The net-definition can be found in the files cross.nod.xml, cross.edg.xml, cross.con.xml and cross.det.xml. The route data is generated randomly by the script. The vehicles leave the source according to a Poisson process approximated here by a binomial distribution. So a parameter p=1./30 in the script means that a vehicle is generated every 30 seconds in average.
Code
cross.py
To start the simulation this script has to be executed. It generates the routes, acts with the server and controls the traffic light.
traciControl.py
This script provides methods to act with the server via TraCI. Especially it bundles the commands and data to TCP messages, sends them to the server, receives the response and unbundles it. It can be found at <SUMO>/tools/traci/traciControl.py.
Simulation
The main program is implemented in the script cross.py. There we first generate the routes as described above. Then GUISIM is started with the config file cross.sumo.cfg wherein the server parameter is specified:
<configuration> ... <remote-port>8813</remote-port> </configuration>
In the next step initTraCI the script connects with the server.
Then we start to control the simulation. We let the server simulate one simulation step, read the induction loop and set the state of the traffic light until the end is reached where no vehicle exists on the road anymore. If we find a vehicle on the induction loop the PROGRAM is started where the sequence of states for the TL is specified. At the end we close the connection.
TraCI
We want to run this simulation in SUMO, acting as a server, and control the signal dependent on the actual simulation state. For this task TraCI offers commands which are described in the corresponding article TraCI in detail. For this example we will use only four commands: Simulation Step, Get Induction Loop Variable, Change Traffic Lights State and Close.
The commands are embedded in TCP messages. The methods for sending and receiving these messages, _recvExact() and _sendExact(), respectively, are implemented in the script traciControl.py without any comment. The four commands themselves are implemented in the methods cmdSimulationStep(step), cmdGetInductionLoopVariable_lastStepVehicleNumber(IndLoopID), cmdChangeTrafficLightsVariable_statePBY(TLID, state) and cmdClose(). I will show the composition of a command using the example of cmdChangeTrafficLightsVariable_statePBY.
setPhase
This method sets the state of a traffic light, so it gets the ID of the TL and the state as parameter. The state consists of three strings, the first for green y/n, the second for brake y/n and the third for yellow y/n. The command is now composed as follows:
| Command_head | Command_content | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Length | Command ID | Variable | Traffic Light ID | Type of value | Value | |||||||
| 0xC2 | 0x21 | Length | TLID | 0x0E | 3 | Length | Phase | Length | Brake | Length | Yellow | |
| B | B | B | i | B | i | i | i | i | ||||
Summary
The example shows how a combination of SUMO and an external application which connects to SUMO via TraCI may be used for building a traffic-adaptive traffic light. The complete tutorial code can be found at <SUMO>/docs/tutorial/traci_tls/.

