车路协同-使用SUMO软件实现智能交通灯控制(一)


这里,SUMO的软件安装就贴教程了,其实网上有很多教程,简单的说一下:

1
2
3
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.

不要着急,设置环境变量即可。

首先:

1
vi ~/.bashrc

在最后一行增加:

1
 export SUMO_HOME=/usr/share/sumo

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

1
source ~/.bashrc

到这里,SUMO环境就安装完了。

这里,给大家介绍快速上手SUMO的5步,就可以仿真:

第一步:设置node点文件:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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,代码如下:

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
<?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,代码如下:

1
2
3
4
5
6
7
8
9
10
<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文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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调用这些:

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
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