Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/trafficlight/crossings/runner.py
169708 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file runner.py
16
# @author Michael Behrisch
17
# @author Daniel Krajzewicz
18
# @date 2011-03-04
19
20
21
from __future__ import print_function
22
from __future__ import absolute_import
23
import os
24
import sys
25
26
if "SUMO_HOME" in os.environ:
27
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
28
import traci # noqa
29
import sumolib # noqa
30
31
traci.start([sumolib.checkBinary('sumo'),
32
"-n", "input_net3.net.xml",
33
"-r", "input_routes.rou.xml",
34
'--no-step-log',
35
]
36
+ sys.argv[1:])
37
38
for step in range(3):
39
print("step", step)
40
traci.simulationStep()
41
print("trafficlight", traci.trafficlight.getIDList())
42
print("trafficlight count", traci.trafficlight.getIDCount())
43
tlsID = "C"
44
45
46
def check():
47
print("examining", tlsID)
48
print("ryg", traci.trafficlight.getRedYellowGreenState(tlsID))
49
print("rygdef", traci.trafficlight.getAllProgramLogics(tlsID))
50
print("lanes", traci.trafficlight.getControlledLanes(tlsID))
51
print("links", traci.trafficlight.getControlledLinks(tlsID))
52
print("program", traci.trafficlight.getProgram(tlsID))
53
print("phase", traci.trafficlight.getPhase(tlsID))
54
print("phaseName '%s'" % traci.trafficlight.getPhaseName(tlsID))
55
print("switch", traci.trafficlight.getNextSwitch(tlsID))
56
print("duration", traci.trafficlight.getPhaseDuration(tlsID))
57
58
59
traci.trafficlight.setPhase(tlsID, 4)
60
traci.trafficlight.setPhaseName(tlsID, "setByTraCI")
61
traci.trafficlight.setPhaseDuration(tlsID, 23)
62
check()
63
defs = traci.trafficlight.getAllProgramLogics(tlsID)
64
print("numDefs=%s numPhases=%s" % (len(defs), list(map(lambda d: len(d.getPhases()), defs))))
65
66
traci.trafficlight.subscribe(tlsID)
67
print(traci.trafficlight.getSubscriptionResults(tlsID))
68
for step in range(3, 6):
69
print("step", step)
70
traci.simulationStep()
71
print(traci.trafficlight.getSubscriptionResults(tlsID))
72
73
for _ in range(80):
74
served = [traci.trafficlight.getServedPersonCount(tlsID, i) for i, _ in enumerate(defs[0].getPhases())]
75
print("%s waitingPerPhase=%s" % (traci.simulation.getTime(), served))
76
traci.simulationStep()
77
78
traci.trafficlight.setLinkState(tlsID, 4, 'u')
79
80
try:
81
traci.trafficlight.setLinkState(tlsID, 16, 'u')
82
except traci.TraCIException as e:
83
print("caught", e)
84
check()
85
print("set ryg", traci.trafficlight.getRedYellowGreenState(tlsID))
86
print("program", traci.trafficlight.getProgram(tlsID))
87
traci.trafficlight.setProgram(tlsID, "off")
88
print("ryg", traci.trafficlight.getRedYellowGreenState(tlsID))
89
90
traci.close()
91
92