sumo-traci (二)部分函数解析

本文详细解析了SUMO-TRACI接口中与信号灯控制、感应线圈监测以及仿真模拟相关的函数,包括设置和获取信号灯相位、监测感应线圈车辆信息、仿真模拟步骤的处理,以及车辆和行人的行为控制。通过对源码的分析,展示了如何利用这些函数实现交通系统的智能控制。
摘要由CSDN通过智能技术生成

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()
  1. 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

  1. traci.trafficlight.getPhase:返回所选信号灯相位。

用法:getPhase(string) -> integer

也就是说,这里的含义是设定“0”号信号灯相位为2,即为绿灯。仿真开始后,一旦发现设置的感应线圈上有车通过【这是在另一个方上设置的】,也就是有车过来,立刻将信号灯转换为相位3【红灯】。
功能实现。

  • 5
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值