Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/vehicle/openGap/runner.py
169689 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file runner.py
15
# @author Leonhard Luecken
16
# @date 2018-11-02
17
18
from __future__ import absolute_import
19
from __future__ import print_function
20
21
import os
22
import sys
23
24
if "SUMO_HOME" in os.environ:
25
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
26
import sumolib # noqa
27
import traci # noqa
28
import traci.constants as tc # noqa
29
sumoBinary = sumolib.checkBinary(sys.argv[1])
30
31
# id of vehicle that is controlled
32
followerID = "follower"
33
# id of vehicle that is followed
34
leaderID = "leader"
35
# time to simulate after gap control is released
36
extraTime = 50.
37
# Offset to trigger phase of keeping enlarged headway
38
POSITIONAL_EPS = 0.1
39
40
41
def runSingle(targetTimeHeadway, targetSpaceHeadway, duration, changeRate, maxDecel, refVehID):
42
step = 0
43
traci.start([sumoBinary, "-c", "sumo.sumocfg"])
44
# traci.init(port=54321)
45
dt = traci.simulation.getDeltaT()
46
47
traci.simulationStep()
48
# print(traci.vehicle.getIDList())
49
print("Fix follower's lane.")
50
traci.vehicle.changeLane(followerID, 0, 500)
51
print("Subscribe to leader.")
52
traci.vehicle.subscribeLeader(followerID, 500)
53
traci.vehicle.subscribe(followerID, [tc.VAR_SPEED])
54
traci.vehicle.subscribe(leaderID, [tc.VAR_SPEED])
55
56
gapControlActive = False
57
targetGapEstablished = False
58
remainingTime = duration
59
testCompleted = False
60
prevLeader = ""
61
leaderArrived = False
62
while not testCompleted:
63
traci.simulationStep()
64
results = traci.vehicle.getSubscriptionResults(followerID)
65
if refVehID == "":
66
leader = results[tc.VAR_LEADER][0]
67
leaderDist = results[tc.VAR_LEADER][1]
68
elif not leaderArrived:
69
leader = refVehID
70
arrived = traci.simulation.getArrivedIDList()
71
# print ("arrived vehicles: %s"%(str(arrived)))
72
if leader in arrived:
73
print("Reference vehicle has been removed.")
74
leaderArrived = True
75
targetGapEstablished = True
76
gapControlActive = False
77
else:
78
traci.vehicle.subscribe(leaderID, [tc.VAR_SPEED])
79
leaderEdgeID = traci.vehicle.getRoadID(leader)
80
leaderPos = traci.vehicle.getLanePosition(leader)
81
leaderDist = traci.vehicle.getDrivingDistance(followerID, leaderEdgeID, leaderPos)
82
followerSpeed = results[tc.VAR_SPEED]
83
leaderSpeed = traci.vehicle.getSubscriptionResults(leaderID)[tc.VAR_SPEED]
84
currentTimeHeadway = leaderDist/followerSpeed if followerSpeed > 0 else 10000
85
currentTime = traci.simulation.getTime()
86
print("Time %s: Gap 'follower'->'%s' = %.3f (headway=%.3f)" %
87
(currentTime, leader, leaderDist, currentTimeHeadway))
88
print("'follower' speed = %s" % followerSpeed)
89
sys.stdout.flush()
90
if (not gapControlActive and not targetGapEstablished and currentTime > 50.):
91
print("## Starting to open gap.")
92
sys.stdout.flush()
93
print("(followerID, targetTimeHeadway, targetSpaceHeadway, duration, changeRate) = %s" %
94
str((followerID, targetTimeHeadway, targetSpaceHeadway, duration, changeRate)))
95
if refVehID == "":
96
if maxDecel == -1:
97
traci.vehicle.openGap(followerID, targetTimeHeadway, targetSpaceHeadway, duration, changeRate)
98
else:
99
print("maxDecel = %s" % maxDecel)
100
traci.vehicle.openGap(followerID, targetTimeHeadway, targetSpaceHeadway, duration, changeRate,
101
maxDecel)
102
else:
103
print("(maxDecel, refVehID) = %s" % str((maxDecel, refVehID)))
104
traci.vehicle.openGap(followerID, targetTimeHeadway, targetSpaceHeadway, duration, changeRate,
105
maxDecel, refVehID)
106
if maxDecel == -1:
107
# For test 'reference_vehicle_removed' set new route for merger
108
traci.vehicle.setRouteID('merger', 'route_merger')
109
110
gapControlActive = True
111
elif gapControlActive:
112
print("Current/target headway: {0:.3f}/{1}".format(currentTimeHeadway, targetTimeHeadway))
113
currentSpacing = currentTimeHeadway * followerSpeed
114
targetSpacing = targetTimeHeadway * followerSpeed
115
minSpacing = max(targetSpacing, targetSpaceHeadway) - POSITIONAL_EPS
116
if not targetGapEstablished and (leader == "" or currentSpacing > minSpacing or prevLeader != leader):
117
if (leader != "" and prevLeader != leader):
118
traci.vehicle.deactivateGapControl(followerID)
119
print("## Deactivating gap control (leader has changed).")
120
remainingTime = 0.0
121
else:
122
print("## Target Headway attained.")
123
targetGapEstablished = True
124
if targetGapEstablished:
125
remainingTime -= dt
126
print("Remaining: %s" % max(0.0, remainingTime))
127
if gapControlActive and remainingTime < 0.:
128
gapControlActive = False
129
print("## Gap control expired.")
130
if remainingTime <= -extraTime:
131
testCompleted = True
132
if leaderSpeed == 0:
133
# Let leader go on slowly, to test space gap
134
traci.vehicle.setSpeed(leaderID, 1.)
135
prevLeader = leader
136
step += 1
137
138
traci.close()
139
sys.stdout.flush()
140
141
142
sys.stdout.flush()
143
targetTimeHeadway = float(sys.argv[2])
144
targetSpaceHeadway = float(sys.argv[3])
145
duration = float(sys.argv[4])
146
changeRate = float(sys.argv[5])
147
if (len(sys.argv) > 6):
148
maxDecel = float(sys.argv[6])
149
if (len(sys.argv) > 7):
150
refVehID = sys.argv[7]
151
else:
152
refVehID = ""
153
else:
154
maxDecel = -1
155
refVehID = ""
156
runSingle(targetTimeHeadway, targetSpaceHeadway, duration, changeRate, maxDecel, refVehID)
157
158