Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

omnet++ - How to get Coordinates of each vehicle in VEINS?

I am using Veins 4.6, Sumo 0.25 and Omnet++ 5.2. I need to get the coordinates of two vehicles (nodes) at a given time, to calculate the distance between them.

I have tried to modify the TraCIDemo11p.cc file in the function handlePositionUpdate(). The Problem is when the veh0 returns its coordinate at the same time there is coordinate sent by veh1 which is very small.

How can I get the position of both the vehicles at the given time and find the distance between them?

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);

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As far as I understood, you want to calculate the distance from the vehicle this code is running on to some other car. However, I am not sure what this other vehicle is. Is it firstVeh for instance?

If this is the case, with this code you can not have achieve what you want (as you figured out already). This code runs on every vehicle in the simulation but is independent from all other vehicles. Therefore, mobility points only to the mobility module of the current vehicle this code is running on. Thus, mobility->getCurrentPosition() always gives you only the position of exactly this vehicle.

For calculating the distance to firstVeh for example, you need its coordinates. Usually, however, you do not have any knowledge about arbitrary other vehicles in the simulation, unless you receive a message from them which includes its position (see Calculating distance between cars nodes VEINS).

If you really need to calculate the distance to an other, arbitrary vehicle (i.e. not an aforementioned sender of a message), you could get a pointer to that vehicle from the TraCIScenarioManager (see How to get count of cars in specific range). This, however, is bad practice in my opinion, since in reality you would not be aware of any other cars in the scenario, other than some sender of a message, either.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...