車路協同-使用SUMO軟件實現智能交通燈控制(一)

這裏,SUMO的軟件安裝就貼教程了,其實網上有很多教程,簡單的說一下:

sudo add-apt-repository ppa:sumo/stable
sudo apt-get update
sudo apt-get install sumo sumo-tools sumo-doc

使用上面三行即可安裝好SUMO,特別是使用深度學習環境的同學,安裝完這個就可以跑代碼了,是不是很快。如果運行SUMO提示:

 Warning: Environment variable SUMO_HOME is not set, schema resolution will use slow website lookups.

不要着急,設置環境變量即可。

首先:

vi ~/.bashrc

在最後一行增加:

 export SUMO_HOME=/usr/share/sumo

然後使用“:wq”保持退出,最後

source ~/.bashrc

到這裏,SUMO環境就安裝完了。

這裏,給大家介紹快速上手SUMO的5步,就可以仿真:

第一步:設置node點文件:

如我建立hello.node.xml,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<nodes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/nodes_file.xsd">
   <node id="0" x="0.0" y="0.0"  type="traffic_light"/>

   <node id="1" x="-500.0" y="0.0" type="priority"/>
   <node id="2" x="+500.0" y="0.0" type="priority"/>
   <node id="3" x="0.0" y="-500.0" type="priority"/>
   <node id="4" x="0.0" y="+500.0" type="priority"/>
   
   <node id="51" x="-510.0" y="0.0" type="priority"/>
   <node id="52" x="+510.0" y="0.0" type="priority"/>
   <node id="53" x="0.0" y="-510.0" type="priority"/>
   <node id="54" x="0.0" y="+510.0" type="priority"/>  
</nodes>

第二步:建立edge文件

我建立hello.edge.xml,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<edges xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/edges_file.xsd">
   <edge id="1i" from="1" to="0" priority="78" numLanes="1" speed="19.444" />
   <edge id="1o" from="0" to="1" priority="46" numLanes="1" speed="11.111" />

   <edge id="2i" from="2" to="0" priority="78" numLanes="1" speed="19.444" />
   <edge id="2o" from="0" to="2" priority="46" numLanes="1" speed="11.111" />

   <edge id="3i" from="3" to="0" priority="78" numLanes="1" speed="19.444" />
   <edge id="3o" from="0" to="3" priority="46" numLanes="1" speed="11.111" />

   <edge id="4i" from="4" to="0" priority="78" numLanes="1" speed="19.444" />
   <edge id="4o" from="0" to="4" priority="46" numLanes="1" speed="11.111" />

   <edge id="51i" from="1" to="51" priority="78" numLanes="1" speed="19.444" />
   <edge id="51o" from="51" to="1" priority="46" numLanes="1" speed="11.111" />

   <edge id="52i" from="2" to="52" priority="78" numLanes="1" speed="19.444" />
   <edge id="52o" from="52" to="2" priority="46" numLanes="1" speed="11.111" />

   <edge id="53i" from="3" to="53" priority="78" numLanes="1" speed="19.444" />
   <edge id="53o" from="53" to="3" priority="46" numLanes="1" speed="11.111" />

   <edge id="54i" from="4" to="54" priority="78" numLanes="1" speed="19.444" />
   <edge id="54o" from="54" to="4" priority="46" numLanes="1" speed="11.111" />
</edges>

第三步:把hello.node.xml文件和hello.edge.xml文件轉換爲網絡,可以看見多了hello.net.xml文件

netconvert --node-files=hello.node.xml --edge-files=hello.edge.xml  --output-file=hello.net.xml

第四步:建立路徑與車文件:

我的是hello.route.xml,代碼如下:

<routes>
        <vType id="typeWE" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" guiShape="passenger"/>
        <vType id="typeNS" accel="0.8" decel="4.5" sigma="0.5" length="7" minGap="3" maxSpeed="25" guiShape="bus"/>

        <route id="right" edges="51o 1i 2o 52i" />
        <route id="left" edges="52o 2i 1o 51i" />
        <route id="down" edges="54o 4i 3o 53i" />
    <vehicle id="left_0" type="typeWE" route="left" depart="0" />
    <vehicle id="left_1" type="typeWE" route="left" depart="2" />
</routes>

第五步:建立配置文件,hello.sumocfg,就是負責加載hello.net.xml與hello.route.xml文件,內容如下:

<?xml version="1.0" encoding="UTF-8"?>

<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/sumoConfiguration.xsd">

    <input>
        <net-file value="hello.net.xml"/>
        <route-files value="hello.route.xml"/>
    </input>

    <time>
        <begin value="0"/>
   <end value="1000"/>
    </time>

</configuration>

到這裏,就可以實現仿真了,使用sumo-gui去加載,指令如下:

sumo-gui hello.sumocfg

界面如下:

 

如果,你想寫一個腳本運行,如下,我建立了一個start.py調用這些:

import os, sys


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



if __name__ == "__main__":

    sumoBinary = checkBinary('sumo-gui')

    traci.start([sumoBinary, "-c", "./hello.sumocfg","--tripinfo-output","tripinfo.xml"])


    step = 0
    while step < 1000:
       traci.simulationStep()
  
       step += 1

    traci.close()

到這裏,一個基本的SUMO就可以運行實現仿真了。這裏的交通等是四個相位變化的,下次專門針對紅綠燈的設置展開講解。

完整代碼下載:https://download.csdn.net/download/caokaifa/12306473

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章