Omnet++ 如何获取静脉中每辆车的坐标?

Omnet++ 如何获取静脉中每辆车的坐标?,omnet++,veins,sumo,Omnet++,Veins,Sumo,我使用的是Velse4.6、Sumo0.25和Omnet++5.2。我需要得到两个车辆(节点)在给定时间的坐标,以计算它们之间的距离 我已尝试在函数handlePositionUpdate()中修改TraCIDemo11p.cc文件。问题是当veh0返回其坐标的同时,veh1发送的坐标非常小 如何在给定时间获得两辆车的位置并找到它们之间的距离? void TraCIDemo11p :: handlePositionUpdate(cObject* obj) { BaseWaveApplL

我使用的是Velse4.6、Sumo0.25和Omnet++5.2。我需要得到两个车辆(节点)在给定时间的坐标,以计算它们之间的距离

我已尝试在函数handlePositionUpdate()中修改TraCIDemo11p.cc文件。问题是当veh0返回其坐标的同时,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);

    }
}

据我所知,你想计算从这个代码运行的车辆到其他车辆的距离。但是,我不确定另一辆车是什么。例如,它是第一辆车吗

如果是这种情况,那么使用这段代码您就无法实现您想要的(正如您已经发现的那样)。此代码在模拟中的每辆车上运行,但与所有其他车辆无关。因此,
mobility
仅指向运行此代码的当前车辆的移动模块。因此,
mobility->getCurrentPosition()
始终只提供该车辆的确切位置

例如,要计算到
firstVeh
的距离,需要它的坐标。但是,通常情况下,您不了解模拟中的任意其他车辆,除非您收到来自这些车辆的消息,其中包括其位置(请参见)


如果您确实需要计算到其他任意车辆的距离(即不是前面提到的消息发送者),您可以从中获取指向该车辆的指针(请参阅)。然而,在我看来,这是一种不好的做法,因为事实上,除了某个消息发送者之外,您也不会知道场景中有任何其他车辆。

据我所知,您想要计算此代码运行的车辆与其他车辆之间的距离。但是,我不确定另一辆车是什么。例如,它是第一辆车吗

如果是这种情况,那么使用这段代码您就无法实现您想要的(正如您已经发现的那样)。此代码在模拟中的每辆车上运行,但与所有其他车辆无关。因此,
mobility
仅指向运行此代码的当前车辆的移动模块。因此,
mobility->getCurrentPosition()
始终只提供该车辆的确切位置

例如,要计算到
firstVeh
的距离,需要它的坐标。但是,通常情况下,您不了解模拟中的任意其他车辆,除非您收到来自这些车辆的消息,其中包括其位置(请参见)


如果您确实需要计算到其他任意车辆的距离(即不是前面提到的消息发送者),您可以从中获取指向该车辆的指针(请参阅)。然而,在我看来,这是一种不好的做法,因为事实上,除了某个消息的发送者之外,您在场景中不会意识到任何其他车辆。

在接收器节点上,您可以获得模拟中所有模块的列表,访问它们的坐标,然后使用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)
}
//获取要发送消息的节点的当前位置
Coord senderPosition=mobility->getCurrentPosition();
//获取模拟中的所有可用节点
std::map availableCars=mobility->getManager()->getManagedHosts();
//遍历集合并查找距离,
std::map::it迭代器;
for(it=availableCars.begin();it!=availableCars.end();it++)
{
TraCIMobility*mobility1=TraCIMobilityAccess().get(it->second);
Coord receiverPosition=mobility1->getCurrentPosition();
//返回以米为单位的距离
发送者位置距离(接收者位置)
}

参考:

在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)
}
//获取要发送消息的节点的当前位置
Coord senderPosition=mobility->getCurrentPosition();
//获取模拟中的所有可用节点
std::map availableCars=mobility->getManager()->getManagedHosts();
//遍历集合并查找距离,
std::map::it迭代器;
for(it=availableCars.begin();it!=availableCars.end();it++)
{
TraCIMobility*mobility1=TraCIMobilityAccess().get(it->second);
Coord receiverPosition=mobility1->getCurrentPosition();
//返回以米为单位的距离
发送者位置距离(接收者位置)
}

Ref:

我正在尝试将节点[0]设为接收节点。因此,程序应该接收两辆车的位置并找到距离。基于此距离,汇聚节点可以对其执行操作。handlePositionUpdate(cObject*obj)功能还是整个程序都在每个模块上运行?使用TraCIDemo11p.cc文件,我无法将车辆位置坐标作为wsm消息发送<代码>WaveShortMessage*wsm=新的WaveShortMessage()
populateWSM(wsm)
wsm->setWsmData(mobility->getRoadId().c_str())@Toothless在现实世界中你如何获得距离?这是真实世界的汽车也会有的信息吗?若并没有,我也会说这是一种不好的做法,因为你们使用的是你们在现实世界中并没有的信息。@Tootless抱歉,但我并没有得到你们的评论。将
节点[0]
用作接收器节点听起来像是将其用作集中式服务提供商。如果每辆车都在广播消息(包括其位置),并且
节点[0]
在从其他车接收到这些消息时正在运行您的逻辑,则可以这样做。我正在尝试将节点[0]设为接收节点。因此,程序应该接收两辆车的位置并找到距离。基于此距离,接收节点