Sumo中Traci.trafficlight详解(上)

Sumo中Traci.trafficlight详解(上)

记录慢慢学习traci的每一天,希望也能帮到你


文章目录

  • Sumo中Traci.trafficlight详解(上)
  • Traci.trafficlight
  • 信号灯参数讲解
  • 1.getAllProgramLogics(self,tlsID)
  • [2.getBlockingVehicles(self, tlsID, linkIndex)](#2.getBlockingVehicles(self, tlsID, linkIndex))
  • [3.getConstraints(self, tlsID, tripId='')](#3.getConstraints(self, tlsID, tripId=''))
  • [4.getConstraintsByFoe(self, foeSignal, foeId='')](#4.getConstraintsByFoe(self, foeSignal, foeId=''))
  • [5.getControlledLanes(self, tlsID)](#5.getControlledLanes(self, tlsID))
  • [6.getControlledLinks(self, tlsID)](#6.getControlledLinks(self, tlsID))
  • [7.getNemaPhaseCalls(self, tlsID)](#7.getNemaPhaseCalls(self, tlsID))
  • [8.getNextSwitch(self, tlsID)](#8.getNextSwitch(self, tlsID))
  • [9.getPhase(self, tlsID) 、getPhaseDuration(self, tlsID)、getPhaseName(self, tlsID)](#9.getPhase(self, tlsID) 、getPhaseDuration(self, tlsID)、getPhaseName(self, tlsID))

Traci.trafficlight

导入库

python 复制代码
import traci
#这里不需要全部导入

信号灯参数讲解

getAllProgramLogics(ID)输入为信号灯ID,其中xml文件中对信号灯的定义方式如下:

python 复制代码
 <tlLogic id="0" type="static" programID="0" offset="0">
        <phase duration="31" state="GrGr"/>
        <phase duration="6" state="yryr"/>
        <phase duration="31" state="rGrG"/>
        <phase duration="6" state="ryry"/>
    </tlLogic>

从上面可以得出,第一个id为信号控制所在node的id,信号灯的programID为'0',当然这个id可以自己给定,数据类型为字符串(str),控制类型为static,即定时信号控制,另外在sumo中还有其它的控制类型:

可以自己多多尝试。duration为当前相位持续时间,单位为s;

state为信号相位,即通行权解释,sumo的信号灯配置比较麻烦,这里详细讲解一下:

每一个state代表一个或者多个流线的放行,如gggrrrrgggrrrr代表南北方向通行,东西方向禁行g0g1g2r3r4r5r6按照索引位置进行对应。为了方便讲解,这里以上面给的xml文件中的参数进行trafficlight的相关用法及返回值的讲解

python 复制代码
 <tlLogic id="0" type="static" programID="0" offset="0">
        <phase duration="31" state="GrGr"/>
        <phase duration="6" state="yryr"/>
        <phase duration="31" state="rGrG"/>
        <phase duration="6" state="ryry"/>
    </tlLogic>

1.getAllProgramLogics(self,tlsID)

复制代码
getAllProgramLogics(self, tlsID)= getCompleteRedYellowGreenDefinition
getAllProgramLogics(string) -> list(Logic)
 返回值为元组数据,功能等同于getCompleteRedYellowGreenDefinition方法
	Returns a list of Logic objects.
	Each Logic encodes a traffic light program for the given tlsID.
返回逻辑对象的列表。每个逻辑为给定的tlsID编码一个红绿灯程序。
python 复制代码
traci.trafficlight.getAllProgramLogics( tlsID='0')
#(Logic(programID='0', type=0, currentPhaseIndex=2, 
#phases=(Phase(duration=31.0, state='GrGr', minDur=31.0, maxDur=31.0, next=()), 
#Phase(duration=6.0, state='yryr', minDur=6.0, maxDur=6.0, next=()), 
#Phase(duration=31.0, state='rGrG', minDur=31.0, maxDur=31.0, next=()), 
#Phase(duration=6.0, state='ryry', minDur=6.0, maxDur=6.0, next=())), subParameter={}),)

通过getAllProgramLogics(self,tlsID)可以获得当前信号灯配时的全部信息。

2.getBlockingVehicles(self, tlsID, linkIndex)

复制代码
输入为信号控制灯ID以及信号灯相位索引
getBlockingVehicles(self, tlsID, linkIndex)
getBlockingVehicles(string, int) -> int
Returns the list of vehicles that are blocking the subsequent block for the given tls-linkIndex
返回在信号交叉口阻塞的车辆ID列表

代码如下(示例):

python 复制代码
traci.trafficlight.getBlockingVehicles( tlsID='0',0)
#返回值为空表示没有阻塞车辆

3.getConstraints(self, tlsID, tripId='')

复制代码
getConstraints(self, tlsID, tripId='')
getConstraint(self, tlsID, tripId='')
getConstraints(string, string) -> list(Constraint)
Returns the list of rail signal constraints for the given rail signal.
If tripId is not "", only constraints with the given tripId are
returned. Otherwise, all constraints are returned
返回给定轨道信号的轨道信号约束列表。
如果tripId不为"",则只有具有给定tripId的约束为返回。否则,将返回所有约束

代码如下(示例):

python 复制代码
traci.trafficlight.getConstraints( tlsID='0',0)
#针对轨道信号,普通交叉口会报错

4.getConstraintsByFoe(self, foeSignal, foeId='')

复制代码
getConstraintByFoe(self, foeSignal, foeId='')
getConstraintsByFoe(string, string) -> list(Constraint)
Returns the list of rail signal constraints that have the given rail signal id as their foeSignal.
If foeId is not "", only constraints with the given foeId are
returned. Otherwise, all constraints are returned
返回具有给定轨道的轨道信号约束的列表信号id作为他们的foeSignal。
如果foeId不是"",则只有具有给定foeId的约束为返回。否则,将返回所有约束
python 复制代码
traci.trafficlight.getConstraints( foeSignal, foeId='')
#针对轨道信号,普通交叉口会报错

5.getControlledLanes(self, tlsID)

复制代码
getControlledLanes(self, tlsID)
getControlled Lanes(self, tlsID)
getControlledLanes(string) -> c
Returns the list of lanes which are controlled by the named traffic light.
返回由命名红绿灯控制的车道列表
python 复制代码
traci.trafficlight.getControlledLanes(tlsID='0')
#('4i_0', '2i_0', '3i_0', '1i_0')返回车道列表id

6.getControlledLinks(self, tlsID)

复制代码
getControlled Links(self, tlsID)
getControlledLinks(string) -> list(list(list(string))) 
Returns the links controlled by the traffic light, sorted by the signal index and described by giving
the incoming, outgoing, and via lane.
返回由红绿灯控制的链接,按信号索引排序,并通过给出传入、传出和通过通道。
python 复制代码
traci.trafficlight.getControlledLinks(tlsID='0')
#[[('4i_0', '3o_0', ':0_0_0')], 
#[('2i_0', '1o_0', ':0_1_0')],
#[('3i_0', '4o_0', ':0_2_0')], 
#[('1i_0', '2o_0', ':0_3_0')]]
#返回车道id,这里的车道指的是经过交叉口的所有车道,包括进口道出口道的路径

7.getNemaPhaseCalls(self, tlsID)

复制代码
getNemaPhaseCalls(self, tlsID)
getNemaPhaseCalls(string) -> list(string)
Get the vehicle calls for the phases.The output is vehicle calls (coming from the detectors) for the phases.
获取车辆对相位的调用。输出为车辆对相位(来自检测器)的调用。

注意:需要是Nema类型的信号灯才可以调用,其它类型调用会报错

8.getNextSwitch(self, tlsID)

复制代码
getNextSwitch(self, tlsID)
getNextSwitch(string) -> double
Return the absolute simulation time at which the traffic light is schedule to switch to the next phase (in seconds).
返回红绿灯计划切换到下一阶段的绝对模拟时间(以秒为单位)
python 复制代码
traci.trafficlight.getNextSwitch('0')
#31.0

9.getPhase(self, tlsID) 、getPhaseDuration(self, tlsID)、getPhaseName(self, tlsID)

这三个方法的用法类似,放在一起讲:

复制代码
getPhase(self, tlsID)获取当前相位信息
getPhase(string) -> integer
Returns the index of the current phase within the list of all phases of the current program.
返回红绿灯计划切换到下一阶段的绝对模拟时间(以秒为单位)。返回当前程序所有阶段列表中当前阶段的索引。
python 复制代码
traci.trafficlight.getPhase('0')
# 返回值为 2
复制代码
getPhaseDuration(string) -> double
 
Returns the total duration of the current phase (in seconds). This value is not affected by the elapsed or remaining duration of the current phase.
返回当前相位的总共时长,不受剩余相位时间影响,相当于返回某个信号周期的某个相位的完整时长
python 复制代码
traci.trafficlight.getPhaseDuration('0')
#返回值为31.0
复制代码
getPhaseName(self, tlsID)
getPhaseName(string) -> string
Returns the name of the current phase.
返回相位名称(自定义,如果没有定义,返回为空字符串)
python 复制代码
traci.trafficlight.getPhaseName('0')

仿真示例代码链接如下:

链接:https://pan.baidu.com/s/1IFs4UJUBPxPM_LUTSrcmSw

**提取码:**Sumo
欢迎交流!

相关推荐
泛联新安2 天前
FPGA功能仿真:VShark正式亮相,换仿真器不用换习惯
fpga开发·编译·仿真·嵌入式软件·代码漏洞扫描
星恒讯工业路由器9 天前
无人驾驶全场景通信方案:车路协同如何实现“车-路-网-云”一体化
网络·物联网·5g·工业路由器·车路协同·5g工业路由器·无人驾驶通信
Evand J9 天前
【MATLAB代码,车联网8】MPC网联车辆节能跟驰控制:速度跟踪、间距约束与能耗优化。MATLAB完整代码订阅专栏后可直接查看
matlab·代码·车联网·mpc·车辆·网联·网联技术
智联物联14 天前
矿车车载通信升级方案,助力矿山无人化、智能化作业
车联网·工业级路由器·智慧矿山·车载路由器·车载网关·5g矿区·矿区通讯
具身智能之心16 天前
Gemini Robotics 2 背后,仿真是机器人持续学习的基础设施
仿真·机器人学习·gemini robotics
Oflycomm17 天前
智能网联汽车渗透率攀升至38.4%,Wi-Fi 7 + 蓝牙6.0如何重塑车载无线连接?
物联网·plc·车联网·wifi7·智能网联汽车·蓝牙6.0
单片机仿真设计19 天前
【proteus仿真】基于 STM32 的智能垃圾桶设计(仿真图+程序)
c语言·stm32·stm32单片机·proteus·毕设·仿真
Evand J22 天前
【MATLAB例程,车联网8】MPC网联车辆节能跟驰控制:速度跟踪、间距约束与能耗优化。附MATLAB完整代码的下载链接
开发语言·matlab·车联网·调度·mpc·节能·车辆控制
Evand J22 天前
【MATLAB例程,车联网12】基于模型预测控制(MPC)的网联车辆交叉口生态驾驶动态优化仿真分析。附完整代码的下载链接
物联网·matlab·车联网·mpc·模型预测控制·车辆·网联车辆
Evand J25 天前
【MATLAB例程,车联网7】CACC网联车辆协同控制:ACC对比、V2X时延丢包与队列稳定性,附例程下载链接
开发语言·matlab·车联网