0

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

    }
}
kritikaTalwar
  • 1,730
  • 1
  • 17
  • 25
Toothless
  • 41
  • 1
  • 9

2 Answers2

3

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.

Julian Heinovski
  • 1,822
  • 3
  • 16
  • 27
  • I am trying to make the node[0] as a sink node. So the program should receive the position of both vehicles and find the distance. Based on this distance sink node can perform an action on it. Is **handlePositionUpdate(cObject* obj)** function or the whole of the program is run on every module? Using the TraCIDemo11p.cc file I cannot send the coordinates of vehicle position as a wsm message. `WaveShortMessage* wsm = new WaveShortMessage();` `populateWSM(wsm);` `wsm->setWsmData(mobility->getRoadId().c_str()); ` – Toothless Apr 18 '18 at 08:31
  • @Toothless how would you obtain the distance in real world? Is this a information a real world car would also have? If not I would also say this is bad practice, as you use information you do not have in real world. – Ventu Apr 23 '18 at 09:01
  • @Toothless sorry, but I do not get your comment. Using `node[0]` as a sink node sounds like having it as a centralized service provider. You can do that, if every car is broadcasting messages (including its position) and `node[0]` is running your logic upon receiving those messages from the other cars. – Julian Heinovski Apr 23 '18 at 14:22
-1

On sink node you can get list of all modules in simulation, access their coordinates and then find the distance between them using following snippet in handlePositionUpdate method of TraCIDemo11p.cc:

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

Ref: How to get count of cars in specific range

Ahmad Ahsan
  • 187
  • 3
  • 18