如何获取VEINS中每辆车的坐标?
How to get Coordinates of each vehicle in VEINS?
我正在使用 Veins 4.6、Sumo 0.25 和 Omnet++ 5.2。我需要在给定时间获取两辆车(节点)的坐标,以计算它们之间的距离。
我试图在函数 handlePositionUpdate() 中修改 TraCIDemo11p.cc 文件。问题是当 veh0 returns 它的坐标同时有 veh1 发送的坐标非常小。
我怎样才能得到两辆车在给定时间的位置并找到它们之间的距离?
void TraCIDemo11p :: handlePositionUpdate(cObject* obj) {
BaseWaveApplLayer::handlePositionUpdate(obj);
// Get vehicle ID
std::string vehID = mobility->getExternalId().c_str();
// Get coordinates of first vehicle
if (vehID == "veh0"){
firstVehX = mobility->getCurrentPosition().x;
firstVehY = mobility->getCurrentPosition().y;
firstVehZ = mobility->getCurrentPosition().z;
calculateDistance(vehID, firstVehX, firstVehY,firstVehZ);
}
//Get coordinates of second vehicle
if (vehID == "veh1"){
secondVehX = mobility->getCurrentPosition().x;
secondVehY = mobility->getCurrentPosition().y;
secondVehZ = mobility->getCurrentPosition().z;
calculateDistance(vehID, secondVehX, secondVehY, secondVehZ);
}
}
据我了解,您想计算此代码为 运行 的车辆与其他车辆的距离。但是,我不确定这另一辆车是什么。例如是 firstVeh
吗?
如果是这种情况,使用此代码您将无法实现您想要的(正如您已经想到的那样)。此代码在模拟中的每辆车上运行,但独立于所有其他车辆。因此,mobility
仅指向此代码 运行 上的当前车辆的移动模块。因此,mobility->getCurrentPosition()
始终只为您提供这辆车的确切位置。
例如,为了计算到 firstVeh
的距离,您需要它的坐标。但是,通常情况下,您对模拟中的任意其他车辆一无所知,除非您收到来自它们的包含其位置的消息(参见 )。
如果您真的需要计算与其他任意车辆(即不是上述消息发送者)的距离,您可以从 TraCIScenarioManager (see 获得指向该车辆的指针。然而,在我看来,这是一种不好的做法,因为在现实中你不会知道场景中的任何其他汽车,除了消息的发送者之外。
在 sink 节点上,您可以获得模拟中所有模块的列表,访问它们的坐标,然后使用 TraCIDemo11p.cc 的 handlePositionUpdate 方法中的以下代码片段找到它们之间的距离:
//Get current position of the node which is going to send message
Coord senderPosition = mobility->getCurrentPosition();
//Get all available nodes in simulation
std::map<std::string, cModule*> availableCars = mobility->getManager()->getManagedHosts();
//Iterate through collection and find distance,
std::map<std::string, cModule*>::iterator it;
for(it = availableCars.begin(); it != availableCars.end(); it++)
{
TraCIMobility* mobility1 = TraCIMobilityAccess().get(it->second);
Coord receiverPosition = mobility1->getCurrentPosition();
//returns distance in meters
senderPosition.distance(receiverPosition)
}
参考:
我正在使用 Veins 4.6、Sumo 0.25 和 Omnet++ 5.2。我需要在给定时间获取两辆车(节点)的坐标,以计算它们之间的距离。
我试图在函数 handlePositionUpdate() 中修改 TraCIDemo11p.cc 文件。问题是当 veh0 returns 它的坐标同时有 veh1 发送的坐标非常小。
我怎样才能得到两辆车在给定时间的位置并找到它们之间的距离?
void TraCIDemo11p :: handlePositionUpdate(cObject* obj) {
BaseWaveApplLayer::handlePositionUpdate(obj);
// Get vehicle ID
std::string vehID = mobility->getExternalId().c_str();
// Get coordinates of first vehicle
if (vehID == "veh0"){
firstVehX = mobility->getCurrentPosition().x;
firstVehY = mobility->getCurrentPosition().y;
firstVehZ = mobility->getCurrentPosition().z;
calculateDistance(vehID, firstVehX, firstVehY,firstVehZ);
}
//Get coordinates of second vehicle
if (vehID == "veh1"){
secondVehX = mobility->getCurrentPosition().x;
secondVehY = mobility->getCurrentPosition().y;
secondVehZ = mobility->getCurrentPosition().z;
calculateDistance(vehID, secondVehX, secondVehY, secondVehZ);
}
}
据我了解,您想计算此代码为 运行 的车辆与其他车辆的距离。但是,我不确定这另一辆车是什么。例如是 firstVeh
吗?
如果是这种情况,使用此代码您将无法实现您想要的(正如您已经想到的那样)。此代码在模拟中的每辆车上运行,但独立于所有其他车辆。因此,mobility
仅指向此代码 运行 上的当前车辆的移动模块。因此,mobility->getCurrentPosition()
始终只为您提供这辆车的确切位置。
例如,为了计算到 firstVeh
的距离,您需要它的坐标。但是,通常情况下,您对模拟中的任意其他车辆一无所知,除非您收到来自它们的包含其位置的消息(参见
如果您真的需要计算与其他任意车辆(即不是上述消息发送者)的距离,您可以从 TraCIScenarioManager (see
在 sink 节点上,您可以获得模拟中所有模块的列表,访问它们的坐标,然后使用 TraCIDemo11p.cc 的 handlePositionUpdate 方法中的以下代码片段找到它们之间的距离:
//Get current position of the node which is going to send message
Coord senderPosition = mobility->getCurrentPosition();
//Get all available nodes in simulation
std::map<std::string, cModule*> availableCars = mobility->getManager()->getManagedHosts();
//Iterate through collection and find distance,
std::map<std::string, cModule*>::iterator it;
for(it = availableCars.begin(); it != availableCars.end(); it++)
{
TraCIMobility* mobility1 = TraCIMobilityAccess().get(it->second);
Coord receiverPosition = mobility1->getCurrentPosition();
//returns distance in meters
senderPosition.distance(receiverPosition)
}
参考: