title: getNeighbors()探索
author: Tang Hui
date: 2020-05-13 16:14:00 +0800
categories: [Blogging, SUMO]
tags: [sumo, traci]
traci.vehicle.getNeighbors()
def getNeighbors(self, vehID, mode):
""" byte -> list(pair(string, double))
The parameter mode is a bitset (UBYTE), specifying the following:
bit 1: query lateral direction (left:0, right:1)
bit 2: query longitudinal direction (followers:0, leaders:1)
bit 3: blocking (return all:0, return only blockers:1)
The returned list contains pairs (ID, dist) for all lane change relevant neighboring leaders, resp. followers,
along with their longitudinal distance to the ego vehicle (egoFront - egoMinGap to leaderBack, resp.
followerFront - followerMinGap to egoBack. The value can be negative for overlapping neighs).
For the non-sublane case, the lists will contain at most one entry.
Note: The exact set of blockers in case blocking==1 is not determined for the sublane model,
but either all neighboring vehicles are returned (in case LCA_BLOCKED) or
none is returned (in case !LCA_BLOCKED).
"""
self._connection._beginMessage(tc.CMD_GET_VEHICLE_VARIABLE, tc.VAR_NEIGHBORS, vehID, 2)
self._connection._string += struct.pack("!BB", tc.TYPE_UBYTE, mode)
return _readNeighbors(self._connection._checkResult(tc.CMD_GET_VEHICLE_VARIABLE, tc.VAR_NEIGHBORS, vehID))
调用getNeighbors()疑难
调用该函数所设置的第二个参数mode是个难点,根据函数的定义,mode是一个位集,但是设置方式有多种。通过谷歌搜索和github的sumo issue,参考得来一些经验。
探索进程
目前通过设置mode参数,能够获得主车的前方车辆ID和距离,后方车辆ID和距离。、
mode设置方法
提到位集,首先想到的是二进制0b,八进制0o,十六进制0x。
八进制和十六进制设置mode参数
因为要保证位集的范围是”0 <= UBYTE <= 255",所以先从八进制和十六进制开始设置mode参数。
设置十六进制0x00000010能够获得主车正后方车辆。设置0x00000000和0x00000000都没有结果。
设置八进制0o00000010,0o00000100和0o00000110,都获取主车正后方车辆。
二进制设置mode参数
因为二进制较小,八位每位都能够被设置1和0。
当设置二进制mode为0b10000000,0b01000000,0b00100000,0b00010000和0b00001000时,都获取主车正后方车辆。
看似很无聊,实则很无聊。但是为了探索出getNeighbors()函数的功能使用,管他那么多。
再设置为0b00000010时,输出的几个车辆ID和距离,可能发现“新大陆”了。
经过验证,mode参数设置为0b00000010时,获取主车的正前方车辆ID和距离。
为了确认方便,将主车能够获取的车辆ID设置为不同的颜色,sumo-gui运行视频链接。
结尾
目前探索getNeighbors(),获得的进展如是。
接下来会继续探索该函数和其他比较有用有意思的函数和功能。