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

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【红灯】。
功能实现。
这是一个十字路口的信息
—————————————————————————

现在还想要知道,现在信号灯相位持续的时间,以及搜索指定信号灯下一个状态的相位是什么
查找到函数

  • 获取到下个信号灯相位的时间(s)

    getNextSwitch(string) -> double

Returns the absolute simulation time at which the traffic light is schedule to switch to the next phase (in seconds).

  • 获取该信号灯相位已经持续的时间
    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.

  • 设置当前信号灯持续的时间
    setPhaseDuration(string, double) -> None

Set the remaining phase duration of the current phase in seconds. This value has no effect on subsquent repetitions of this phase.

我们来看一看另一个traci案例的代码。
下面展示 有行人过街的runner.py

def run():
    """execute the TraCI control loop"""
    #track the duration for which the green phase of the vehicles has been
    # 设置一个考察绿灯持续时间的变量
    greenTimeSoFar = 0

    # 看一下行人是否按下过街的按钮
    activeRequest = False

    # main loop. do something every simulation step until no more vehicles are
    # 开始运行仿真
    while traci.simulation.getMinExpectedNumber() > 0:
        traci.simulationStep()

        # 看是否路口有等待的行人,当此时主路口为绿灯时考虑转换
        # phase for the vehicles exceeds its minimum duration
        if not activeRequest:
        #要是没人过街(按钮没按下),就检索现在排队的人数
            activeRequest = checkWaitingPersons()
        if traci.trafficlight.getPhase(TLSID) == VEHICLE_GREEN_PHASE:
            greenTimeSoFar += 1
        #如果现在持续的绿灯时间,已经超最小绿灯时间,那么如果有行人按下过街按钮,就赶紧把主路口的时间设置为红灯。
            if greenTimeSoFar > MIN_GREEN_TIME:
                # check whether someone has pushed the button
                if activeRequest:
                    # switch to the next phase
                    traci.trafficlight.setPhase(
                        TLSID, VEHICLE_GREEN_PHASE + 1)
                    # 安排了一次过街后,重置按钮,并且重置持续绿灯时间。
                    activeRequest = False
                    greenTimeSoFar = 0

    sys.stdout.flush()
    traci.close()

对行人过街的代码进行分析(如代码片展示),还是使用setphase和getphase两个函数。通过判断行人,增加功能。
展示

二、访问感应线圈的函数

1)第一个代码片中,函数traci.inductionloop.getLastStepVehicleNumber(“0”)

定义:
getLastStepVehicleNumber(string) -> integer:最近一次仿真步里,指定线圈上通过车辆的数量
Returns the number of vehicles that were on the named induction loop within the last simulation step.

2)相应的,可以获取通过感应线圈的车辆ID

getLastStepVehicleIDs(string) -> list(string)

Returns the list of ids of vehicles that were on the named induction loop in the last simulation step.
3)还可以获取车辆平均行驶速度。

getLastStepMeanSpeed(string) -> double

Returns the mean speed in m/s of vehicles that were on the named induction loop within the last simulation step.

三、访问仿真模拟步骤的函数

在这两个代码中,都出现了仿真模拟的函数。其来自于traci的simulation库中的子函数。

1)traci.simulation.getMinExpectedNumber()

可以理解为系统仿真中车辆数。如果车辆数为0,说明所有车辆已经离开路网。仿真可以停止了。
getMinExpectedNumber() -> integer

Returnsthe number of vehicles which are in the net plus the ones still waiting to start. This number may be smaller than the actual number of vehicles still to come because of delayed route file parsing. If the number is 0 however, it is guaranteed that all route files have been parsed completely and all vehicles have left the network.

2)traci.simulationStep()

没有找到这个函数,结合代码,使用在仿真开始时,可以执行仿真步骤。

四、访问车辆动作的函数

链接: traci_change vehicle state.

链接: traci_vehicle.

如果想要利用强化学习对系统进行控制,那么必须能够在仿真时,改变车辆的行为。首先对traci下面的vehicle类进行检索和学习,看一下具体可以实现哪些功能:

  1. changeTarget(string, string) -> None重新规划目的地道路

  2. getAccel(string) -> double获取车辆加速度

  3. getPosition(string) -> (double, double) 获取车辆位置

  4. isStopped(string) -> bool检测车辆是否停止
    Return whether the vehicle is stopped

  5. setAccel(string, double) -> None 设置车辆最大加速度

  6. setMaxSpeed(string, double) -> None设置车辆最大速度

  7. setStop(string, string, double, integer, double, integer, double, double) -> None 还可以设置停车时间

接下来对traci额外提供的车辆接口进行检索和学习,看一下是否还有有价值的函数。
其实两个文档的本质是一样的:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、访问行人动作的函数

在行人过街的案例中,有对行人进行检索的文件。下面进行解析:
下面展示 检索行人的函数

def checkWaitingPersons():
    """确定是否有行人过街"""

    # 确认交叉口的路侧两边
    for edge in WALKINGAREAS:
        peds = traci.edge.getLastStepPersonIDs(edge)
        # check who is waiting at the crossing
        # we assume that pedestrians push the button upon
        # standing still for 1s
        for ped in peds:
            if (traci.person.getWaitingTime(ped) == 1 and
                    traci.person.getNextEdge(ped) in CROSSINGS):
                print("%s pushes the button" % ped)
                return True
    return False

函数解析:
1)traci.edge.getLastStepPersonIDs(edge) 获取该edge上行人的ID

getLastStepPersonIDs(string) -> list(string)

Returns the ids of the persons on the given edge during the last time step.

此外,edge类里面的函数还能够实现对egde上的各对象各功能进行分析,还能对edge上的排放进行分析。

2)traci.person.getWaitingTime(ped) 获取指定行人的等待时间(s)

getWaitingTime() -> double
The waiting time of a person is defined as the time (in seconds) spent with a speed below 0.1m/s since the last time it was faster than 0.1m/s. (basically, the waiting time of a person is reset to 0 every time it moves).

此外,traci.person库下面的函数功能也很齐全。
链接: traci_person.

猜你喜欢

转载自blog.csdn.net/dzy_csu/article/details/106799721