/
runner.py
executable file
·135 lines (112 loc) · 4.96 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
# Copyright (C) 2009-2019 German Aerospace Center (DLR) and others.
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v20.html
# SPDX-License-Identifier: EPL-2.0
# @file runner.py
# @author Lena Kalleske
# @author Daniel Krajzewicz
# @author Michael Behrisch
# @author Jakob Erdmann
# @author Leonhard Luecken
# @date 2009-03-26
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import optparse
# we need to import python modules from the $SUMO_HOME/tools directory
try:
sys.path.append(os.path.join(os.path.dirname(
__file__), '..', '..', '..', '..', "tools")) # tutorial in tests
sys.path.append(os.path.join(os.environ.get("SUMO_HOME", os.path.join(
os.path.dirname(__file__), "..", "..", "..")), "tools")) # tutorial in docs
from sumolib import checkBinary # noqa
except ImportError:
sys.exit("please declare environment variable 'SUMO_HOME'")
import traci # noqa
ToC_vehicle = "ToC_veh"
timeTillMRM = 30
def run():
"""execute the TraCI control loop"""
step = 0
lastLane = traci.vehicle.getLaneIndex(ToC_vehicle)
t = traci.simulation.getTime()
print("Time %s: Current lane of veh '%s': %s" % (t, ToC_vehicle, lastLane))
traci.simulationStep()
while traci.simulation.getMinExpectedNumber() > 0:
if step == 2:
requestToC(ToC_vehicle, timeTillMRM)
t = traci.simulation.getTime()
print("Requested ToC of veh0 at t=%s (until t=%s)" % (t, t + timeTillMRM))
newLane = traci.vehicle.getLaneIndex(ToC_vehicle)
acceleration = traci.vehicle.getAcceleration(ToC_vehicle)
accel = traci.vehicle.getAccel(ToC_vehicle)
if newLane != lastLane:
t = traci.simulation.getTime()
print("Time %s: veh '%s' changed lanes. Current: %s" % (t, ToC_vehicle, newLane))
lastLane = newLane
print("Acceleration = %s" % acceleration)
print("Accel = %s" % accel)
printToCParams(ToC_vehicle, True)
sys.stdout.flush()
step += 1
traci.simulationStep()
def requestToC(vehID, timeTillMRM):
traci.vehicle.setParameter(vehID, "device.toc.requestToC", str(timeTillMRM))
def printToCParams(vehID, only_dynamic=False):
holder = traci.vehicle.getParameter(vehID, "device.toc.holder")
manualType = traci.vehicle.getParameter(vehID, "device.toc.manualType")
automatedType = traci.vehicle.getParameter(vehID, "device.toc.automatedType")
responseTime = traci.vehicle.getParameter(vehID, "device.toc.responseTime")
recoveryRate = traci.vehicle.getParameter(vehID, "device.toc.recoveryRate")
initialAwareness = traci.vehicle.getParameter(vehID, "device.toc.initialAwareness")
mrmDecel = traci.vehicle.getParameter(vehID, "device.toc.mrmDecel")
currentAwareness = traci.vehicle.getParameter(vehID, "device.toc.currentAwareness")
state = traci.vehicle.getParameter(vehID, "device.toc.state")
speed = traci.vehicle.getSpeed(vehID)
print("time", traci.simulation.getTime())
print("ToC device infos for vehicle '%s'" % vehID)
if not only_dynamic:
print("Static parameters:")
print(" holder = %s" % holder)
print(" manualType = %s" % manualType)
print(" automatedType = %s" % automatedType)
print(" responseTime = %s" % responseTime)
print(" recoveryRate = %s" % recoveryRate)
print(" initialAwareness = %s" % initialAwareness)
print(" mrmDecel = %s" % mrmDecel)
print("Dynamic parameters:")
print(" currentAwareness = %s" % currentAwareness)
print(" currentSpeed = %s" % speed)
print(" state = %s" % state)
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_false",
default=True, help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
# this is the main entry point of this script
if __name__ == "__main__":
options = get_options()
# this script has been called from the command line. It will start sumo as a
# server, then connect and run
if options.nogui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
# this is the normal way of using traci. sumo is started as a
# subprocess and then the python script connects and runs
traci.start([sumoBinary, "-n", "input_net.net.xml", "-r", "input_routes.rou.xml",
"--no-step-log", "true",
"--default.speeddev", "0"])
# Wait until the vehicle enters
while ToC_vehicle not in traci.vehicle.getIDList():
traci.simulationStep()
printToCParams(ToC_vehicle)
run()
traci.close()
sys.stdout.flush()