【问题标题】:How to get Coordinates of each vehicle in VEINS?如何在 VEINS 中获取每辆车的坐标?
【发布时间】:2018-04-17 11:18:04
【问题描述】:

我正在使用 Veins 4.6、Sumo 0.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);

    }
}

【问题讨论】:

    标签: omnet++ veins sumo


    【解决方案1】:

    据我了解,您想计算从运行此代码的车辆到其他车辆的距离。但是,我不确定这辆车是什么。例如是firstVeh 吗?

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

    例如,为了计算到firstVeh 的距离,您需要它的坐标。但是,通常您对模拟中的任意其他车辆一无所知,除非您从他们那里收到包含其位置的消息(请参阅Calculating distance between cars nodes VEINS)。

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

    【讨论】:

    • 我正在尝试将节点 [0] 作为接收节点。所以程序应该接收两辆车的位置并找到距离。基于此距离,sink 节点可以对其执行操作。是 handlePositionUpdate(cObject* obj) 函数还是整个程序在每个模块上运行?使用 TraCIDemo11p.cc 文件,我无法将车辆位置坐标作为 wsm 消息发送。 WaveShortMessage* wsm = new WaveShortMessage();populateWSM(wsm);wsm->setWsmData(mobility->getRoadId().c_str());
    • @Toothless 你如何获得现实世界的距离?这是现实世界的汽车也会有的信息吗?如果不是,我也会说这是不好的做法,因为您使用的是现实世界中没有的信息。
    • @Toothless 对不起,但我没有得到你的评论。使用node[0] 作为接收节点听起来像是将其作为集中式服务提供者。你可以这样做,如果每辆车都在广播消息(包括它的位置)并且node[0] 在接收到来自其他汽车的这些消息时运行你的逻辑。
    【解决方案2】:

    在sink节点上,你可以得到模拟中所有模块的列表,访问它们的坐标,然后使用TraCIDemo11p.cc的handlePositionUpdate方法中的sn-p找到它们之间的距离:

    //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)
    }
    

    参考:How to get count of cars in specific range

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多