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代表南北方向通行,东西方向禁行g[0]g[1]g[2]r[3]r[4]r[5]r[6]按照索引位置进行对应。为了方便讲解,这里以上面给的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
欢迎交流!