官网地址:https://sumo.dlr.de/docs/index.html
1.简单仿真入门
需要三个文件:.sumocfg
.net.xml(路网文件)
.rou.xml(路由文件)
输入命令sumo
sumo-gui
打开sumo
打开sumocfg
文件即可看见路网,在Edit
命令下的Edit Visualisation
可设置小车的形状,颜色等信息,点击仿真按钮可进行仿真。
输入命令netedit
,打开netedit
2.python接入traci
# we need to import python modules from the $SUMO_HOME/tools directory
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
from sumolib import checkBinary # noqa
import traci # noqa
3.python代码
import json
import optparse
import time
import sys,os,subprocess
# we need to import python modules from the $SUMO_HOME/tools directory校验'SUMO_HOME'是否在环境变量中
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
from sumolib import checkBinary
#导入traci
#sys.path.append('/home/serinus/sumo-1.9.2/tools') #import traci,sumolib
#sumoBinary = "/home/serinus/sumo-1.9.2" #sumo or sumo-gui connection
import traci
if_show_gui = True #仿真时是否打开gui界面进行可视化展示
if not if_show_gui:
sumoBinary = checkBinary("sumo")
else:
sumoBinary = checkBinary("sumo-gui")
#sumocfg文件路径
#sumocfgfile = "/home/serinus/sumo-1.9.2/files/sumo_toturial_file/tracis"
traci.start([sumoBinary,"-c","data/cross.sumocfg", "--tripinfo-output", "tripinfo.xml"])
#获取API
for step in range(0,200):
time.sleep(1)
traci.simulationStep(step + 1)
simulation_current_time = traci.simulation.getTime()
print("仿真时间是",simulation_current_time