车路协同-使用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

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
探索全栈前端技术的魅力:HTML+CSS+JS+JQ+Bootstrap网站源码深度解析 在这个数字化时代,构建一个既美观又功能强大的网站成为了许多开发者和企业追逐的目标。本份资源精心汇集了一套完整网站源码,融合了HTML的骨架搭建、CSS的视觉美化、JavaScript的交互逻辑、jQuery的高效操作以及Bootstrap的响应式设计,全方位揭秘了现代网页开发的精髓。 HTML,作为网页的基础,它构建了信息的框架;CSS则赋予网页生动的外观,让设计创意跃然屏上;JavaScript的加入,使网站拥有了灵动的交互体验;jQuery,作为JavaScript的强力辅助,简化了DOM操作与事件处理,让编码更为高效;而Bootstrap的融入,则确保了网站在不同设备上的完美呈现,响应式设计让访问无界限。 通过这份源码,你将: 学习如何高效组织HTML结构,提升页面加载速度与SEO友好度; 掌握CSS高级技巧,如Flexbox与Grid布局,打造适应各种屏幕的视觉盛宴; 理解JavaScript核心概念,动手实现动画、表单验证等动态效果; 利用jQuery插件快速增强用户体验,实现滑动效果、Ajax请求等; 深入Bootstrap框架,掌握移动优先的开发策略,响应式设计信手拈来。 无论是前端开发新手渴望系统学习,还是资深开发者寻求灵感与实用技巧,这份资源都是不可多得的宝藏。立即深入了解,开启你的全栈前端探索之旅,让每一个网页都成为技术与艺术的完美融合!
使用SUMO交通仿真软件基于Python进行二次开发实现ALINEA匝道控制的步骤如下: 1. 安装SUMO:首先,你需要安装SUMO软件,并且配置好SUMO的环境变量。 2. 导入SUMO模块:在Python代码中,导入SUMO的相关模块,例如`import traci`和`from sumolib import checkBinary` 3. 启动SUMO仿真:使用Python代码启动SUMO仿真器,可以使用以下代码: ```python sumoBinary = checkBinary('sumo') sumoCmd = [sumoBinary, "-c", "path/to/your/sumo_config_file.sumocfg"] traci.start(sumoCmd) ``` 4. 实现ALINEA算法:根据ALINEA算法的原理,编写Python代码实现ALINEA匝道控制的逻辑。你需要考虑车辆的流量、速度、密度等因素,来决定匝道信号的开启和关闭时间。 5. 控制信号使用SUMO提供的API来控制信号。你可以使用`traci.trafficlight.setRedYellowGreenState()`函数来设置信号的状态。根据ALINEA算法的结果,将对应的信号状态传递给这个函数。 6. 更新仿真状态:在每个仿真步骤中,更新仿真状态,例如车辆位置、速度等。你可以使用`traci.simulationStep()`函数来更新仿真状态。 7. 结束仿真:在代码的适当位置,使用`traci.close()`函数来结束仿真。 需要注意的是,ALINEA算法的具体实现涉及到一些复杂的交通控制逻辑和参数调节,需要根据实际情况进行调整和优化。此外,还需要注意SUMO软件和Python版本的兼容性,以及SUMO的API文档和示例代码的使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值