SUMO1.9.2 TraCI python 简单使用
Posted zolty
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SUMO1.9.2 TraCI python 简单使用相关的知识,希望对你有一定的参考价值。
1.exa.sumocfg
<configuration>
<input>
<net-file value="bilibili.net.xml"/>
<route-files value="routes.rou.xml"/>
</input>
<!-- sim.py's configuration -->
<time>
<begin value="0"/>
<end value="10000"/>
<step-length value="0.1"/>
</time>
<report>
<no-step-log value="true"/>
</report>
<gui_only>
<start value="true"/>
<quit-on-end value="true"/>
</gui_only>
</configuration>
2.sim.py
import optparse
import sys
import time
import traci
from sumolib import checkBinary
import os
if 'SUMO_HOME' in os.environ:
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
else:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# gui options
# if you want nogui, please cmd -> python sim.py --nogui
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_true",
default=False, help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
# sim loop
# until scenario has no car
def run():
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
time.sleep(0.1)
ElapsedTime = traci.simulation.getTime()
all_vehicle_id = traci.vehicle.getIDList()
print("ElapsedTime", ElapsedTime, "car_list", all_vehicle_id)
traci.close()
sys.stdout.flush()
if __name__ == "__main__":
options = get_options()
if options.nogui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
sumo_file = "F:\\\\workSpace\\\\sumo\\\\scenario_test\\\\exa.sumocfg"
traci.start([sumoBinary, "-c", sumo_file])
run()
3.效果图
以上是关于SUMO1.9.2 TraCI python 简单使用的主要内容,如果未能解决你的问题,请参考以下文章
SUMO + OpenCV + Python =及时模拟?可能吗