Search code examples
omnet++veinssumo

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

    }
}

Solution

  • 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.