sumo-traci函数解析
为了实现对交通信号灯值的检索、修改;实现对周围车辆状态的检索;实现对控制车辆的速度进行修改;需要对traci接口的部分功能函数进行解析。
下面通过对traci_tls提供的源码进行解析,总结常用的函数。
一、访问信号灯的函数
下面展示十字路口tutorial里面 runner.py
的访问信号灯和感应线圈的部分。
def run():
"""execute the TraCI control loop"""
step = 0
# we start with phase 2 where EW has green
traci.trafficlight.setPhase("0", 2)
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
if traci.trafficlight.getPhase("0") == 2:
# we are not already switching
if traci.inductionloop.getLastStepVehicleNumber("0") > 0:
# there is a vehicle from the north, switch
traci.trafficlight.setPhase("0", 3)
else:
# otherwise try to keep green for EW
traci.trafficlight.setPhase("0", 2)
step += 1
traci.close()
sys.stdout.flush()
- traci.trafficlight.setPhase:被定义为Switches to the phase with the given index in the list of all phases for the current program.,把信号灯设定为指定相位。
phase在这里被指定为2,那phase在哪里定义呢?待会儿注意一下。
用法:setPhase(string, integer) -> None
- traci.trafficlight.getPhase:返回所选信号灯相位。
用法:getPhase(string) -> integer
也就是说,这里的含义是设定“0”号信号灯相位为2,即为绿灯。仿真开始后,一旦发现设置的感应线圈上有车通过【这是在另一个方上设置的】,也就是有车过来,立刻将信号灯转换为相位3【红灯】。
功能实现。