Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/simpla/openGap/runner.py
169685 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 Jakob Erdmann
18
# @author Daniel Krajzewicz
19
# @date 2011-03-04
20
21
22
from __future__ import print_function
23
from __future__ import absolute_import
24
import os
25
import sys
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
import simpla # noqa
31
from simpla import SimplaException # noqa
32
33
traci.start([sumolib.checkBinary('sumo'), '-c', 'sumo.sumocfg'])
34
simpla._utils.DEBUG_GAP_CONTROL = True
35
36
37
def step():
38
s = traci.simulation.getTime()
39
traci.simulationStep()
40
return s
41
42
43
def check(vehID):
44
print("examining", vehID)
45
print("speed", traci.vehicle.getSpeed(vehID))
46
print("speed w/o traci", traci.vehicle.getSpeedWithoutTraCI(vehID))
47
print("acceleration", traci.vehicle.getAcceleration(vehID))
48
print("lane", traci.vehicle.getLaneID(vehID))
49
print("lanePos", traci.vehicle.getLanePosition(vehID))
50
51
52
followerID = "follower"
53
leaderID = "leader"
54
55
56
def runSteps(nSteps):
57
for i in range(nSteps):
58
print("\n# step", step())
59
print("# Follower:")
60
check(followerID)
61
print("# Leader:")
62
check(leaderID)
63
leaderInfo = traci.vehicle.getLeader(followerID, 1000)
64
if leaderInfo is not None:
65
print("\n# gap = %s" % leaderInfo[1])
66
speedDiff = traci.vehicle.getSpeed(leaderID) - traci.vehicle.getSpeed(followerID)
67
print("# speedDiff = %s\n" % speedDiff)
68
else:
69
print("\n# No follow-lead relation!\n")
70
71
72
runSteps(100)
73
74
# params for next openGap call
75
gap = 50
76
speedDiff = 1.0
77
maxDecel = 0.1
78
duration = 100
79
80
print("\n# Calling simpla.openGap(%s, %s, %s, %s)\n" % (gap, speedDiff, maxDecel, duration))
81
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration)
82
print("\n")
83
84
runSteps(200)
85
86
# params for next openGap call
87
speedDiff = 10
88
maxDecel = 10. # Don't exceed vehicle's decel
89
duration = 50
90
91
print("\n# Calling simpla.openGap(%s, %s, %s, %s)\n" % (gap, speedDiff, maxDecel, duration))
92
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration)
93
print("\n")
94
95
runSteps(100)
96
97
# params for next openGap call
98
gap = 75
99
speedDiff = 4.0
100
maxDecel = 2.
101
102
print("\n# Calling simpla.openGap(%s, %s, %s, %s)\n" % (gap, speedDiff, maxDecel, duration))
103
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration)
104
print("\n")
105
106
# params for next openGap call
107
gap = -75
108
print("\n# Calling simpla.openGap(%s, %s, %s, %s)\n" % (gap, speedDiff, maxDecel, duration))
109
try:
110
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration)
111
except SimplaException as e:
112
print(e)
113
print("\n")
114
115
gap = 75
116
speedDiff = -4
117
print("\n# Calling simpla.openGap(%s, %s, %s, %s)\n" % (gap, speedDiff, maxDecel, duration))
118
try:
119
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration)
120
except SimplaException as e:
121
print(e)
122
print("\n")
123
124
speedDiff = 4
125
maxDecel = -2
126
print("\n# Calling simpla.openGap(%s, %s, %s, %s)\n" % (gap, speedDiff, maxDecel, duration))
127
try:
128
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration)
129
except SimplaException as e:
130
print(e)
131
print("\n")
132
133
maxDecel = 2
134
duration = -100
135
print("\n# Calling simpla.openGap(%s, %s, %s, %s)\n" % (gap, speedDiff, maxDecel, duration))
136
try:
137
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration)
138
except SimplaException as e:
139
print(e)
140
print("\n")
141
142
duration = 51
143
gap = 10000
144
print("\n# Calling simpla.openGap() various times\n")
145
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration)
146
simpla.openGap(followerID, gap * 3, speedDiff, maxDecel / 2., duration)
147
simpla.openGap(followerID, gap, speedDiff, maxDecel, duration / 2.)
148
print("\n")
149
150
runSteps(100)
151
152
# done
153
traci.close()
154
155