sumo-traci 与python接口文档翻译(一)

Traci 与python接口

本文介绍traci英文文档(主要是与python的接口)。

第一个接口已有较多参考,故从第二个开始介绍。

Subscriptions

可以将“subscribe”视为用于检索变量的批处理模式。 您可以在每个时间步骤后自动检索感兴趣的值,而不必一遍又一遍地询问相同的变量。 TraCI的subscribe按每个模块进行处理。 也就是说,您可以在每个时间步之后向模块询问所有当前subscribe的结果。 为了subscribe变量,您需要知道它们的变量ID,可以在traci / constants.py文件中查找它们。
下面展示一些 代码

import traci
import traci.constants as tc

traci.start(["sumo", "-c", "my.sumocfg"])
traci.vehicle.subscribe(vehID, (tc.VAR_ROAD_ID, tc.VAR_LANEPOSITION))
print(traci.vehicle.getSubscriptionResults(vehID))
for step in range(3):
   print("step", step)
   traci.simulationStep()
   print(traci.vehicle.getSubscriptionResults(vehID))#每隔3步,输出订阅结果
traci.close()
  1. subscribe是vehicle类下面的函数,定义为to one or more object values for the given interval.,表示在指定间隔内获取多个对象的值。
  2. getSubscriptionResults也是一样。定义为Returns the subscription results for the last time step and the given object.,也就是返回订阅的结果。

Context Subscriptions

TraCI的context subscriptions按每个模块进行处理。 也就是说,可以在每个时间步之后向模块询问所有当前订阅的结果。 为了context subscriptions变量,您需要获取要检索的对象的域ID,以及可以在traci / constants.py文件中查找的变量ID。 域ID的格式始终为CMD_GET_ _VARIABLE。

下面展示一些 检索路口范围(42m)内的所有车速和等待时间(隐式检索车辆ID)

import traci
import traci.constants as tc

traci.start(["sumo", "-c", "my.sumocfg"])
traci.junction.subscribeContext(junctionID, tc.CMD_GET_VEHICLE_VARIABLE, 42, [tc.VAR_SPEED, tc.VAR_WAITING_TIME])
print(traci.junction.getContextSubscriptionResults(junctionID))
for step in range(3):
   print("step", step)
   traci.simulationStep()
   print(traci.junction.getContextSubscriptionResults(junctionID))
traci.close()

和第一节的subscribe的区别在于,context指定所要订阅变量的域(范围)。也就是在上面代码中,指定交叉口junction的ID,指定domain是vehicle,指定范围“42”,就能搜索该范围内的车辆的速度和时间。

其中,subscribeContext的函数表示,Subscribe to objects of the given domain (specified as domain=traci.constants.CMD_GET__VARIABLE),
which are closer than dist to the object specified by objectID.

Context Subscription Filters

下面展示 addSubscriptionFilter<FILTER_ID>()用法。

traci.vehicle.subscribeContext("ego", tc.CMD_GET_VEHICLE_VARIABLE, 0.0, [tc.VAR_SPEED])
#获取以车辆为“ego”为中心,0米范围内所有车辆的速度。
traci.vehicle.addSubscriptionFilterLanes(lanes, noOpposite=True, downstreamDist=100, upstreamDist=50)
#增加lane的过滤器,下游距离100,上游距离50
  • 注意到这里范围设置的是0米,是不是就是没有意义的呢?文档中解释,此处设置为0,是因为下一行有规定:
    【 since it is be overridden by the selective values of downstreamDist and upstreamDist, respectively, given to the call of addSubscriptionFilterLanes() in the second line. 因为它分别被提供给第二行中的addSubscriptionFilterLanes()调用的下游值上游值的选择值覆盖。】
  • 这本质上是一个信息的过滤过程。你可以指定相应的、想要获取的信息。
  • 还有更多的过滤器的信息。
    例如:
  • lanes:Return surrounding vehicles on lanes specified relatively to the reference vehicle返回相对于指定车辆的车道上其他车辆
  • Turn:return conflicting vehicles on upcoming junctions along the vehicle’s route返回该车辆路径上下一个交叉口有冲突的车辆信息。

Adding a StepListener

通常,每次调用traci.simulationStep()时都需要调用一个函数,以使其自动发生,可以添加StepListener对象“ listener”(更确切地说是 traci.StepListener的子类),即:

下面展示一些 代码

 class ExampleListener(traci.StepListener):
    def step(self, t=0):
        # do something at every simulaton step
        print("ExampleListener called at time %s ms." % t)
        # indicate that the step listener should stay active in the next step
        return True
       
 listener = ExampleListener()
 traci.addStepListener(listener)

英文文档源链接

链接: link.

猜你喜欢

转载自blog.csdn.net/dzy_csu/article/details/106793399
今日推荐