0

I am trying to get vehicle id as follow:

mobility = TraCIMobilityAccess().get(getParentModule());
assert(mobility);
traci = mobility->getCommandInterface();
traciVehicle = mobility->getVehicleCommandInterface();
cout<< mobility->getExternalId();

But it returns an invalid vehicle id. What is wrong?

Please help me to solve this problem. Thanks.

Jerzy D.
  • 6,707
  • 2
  • 16
  • 22
Baram
  • 19
  • 4

2 Answers2

1

What do you mean by an invalid vehicle id? The way you are getting the identifier is the one used by sumo. If that is the case, can you specify what do you expect as an identifier? (that of omnet which starts from [1]?) As the id of SUMO and that one apparent in omnet are not the same (the order of creation), you may add the following to get your own id (that matches the one of omnet) : in the ".h" file of your TraCIDemo11p, add your id:

protected:
    int your_id;//added

in the ".c" file of your TraCIDemo11p, affect the index in your id:

if (stage == 0) {
...
your_id = getParentModule()->getIndex();//added
...

next, in the place you want to verify a statement, add this:

EV << "My SUMO id = " << mobility->getExternalId() << endl;
EV << "My VEINS id = " << your_id /*or just : getParentModule()->getIndex()*/<< endl;

I hope this helps.

MB_7
  • 103
  • 6
  • I applied a shortest path algorithm on the vehicle 18. I am trying to get the vehicle number to ensure if the command getExternalId() works correctly or not, but it returns number 20. And The same for mobility->getRoadId() command. I am trying to get the current vehicle edge number but it also does not work correctly. – Baram Sep 30 '19 at 18:26
  • Alright, as expected. Find in my original post the edited version. Let me know if it does not work. Best. – MB_7 Oct 01 '19 at 12:26
  • I tried the above code, and the output is: My SUMO id = 24 My VEINS id = 18 – Baram Oct 02 '19 at 15:46
  • At first, you saw the id (that you said in the earlier question, 20) but getExternalid() showed you something else right? and that what let you confused. getParentModule()->getIndex() mostly will match "20". Once again, what do you want? (sorry for not understanding your real question) – MB_7 Oct 03 '19 at 17:44
  • Thanks for your response. I want to get the current edge number for the vehicle 18, but it gives an invalid edge number. I tried the command getExternalid() to ensure if the vehicle id is 18 or not, but it also gives an invalid id. I don not know what is wrong. – Baram Oct 05 '19 at 13:21
  • You are welcome, how did you know that it is the vehicle "18"? from where you know that number from the beginning (depending on that, we may find what is really wrong) – MB_7 Oct 05 '19 at 22:44
  • I know from the sumo gui. I applied the algorithm on the vehicle 18 by adding *.node[18].route=true according to my code. I am sure, because it was working before. – Baram Oct 07 '19 at 12:46
  • "*.node[18]" means the 18th vehicle but in the perspective of omnet not sumo. The order of generated vehicles determines their "i"th rank you should, thus, use its real id gotten from the "getParentModule()->getIndex()" instead of sumo. If it was working for your case before, maybe the problem is somewhere else...Best, – MB_7 Oct 09 '19 at 16:58
1

You can try the FindModlue::findHost() in DemoBaseApplLayer::initialize(int stage) in DemoBaseApplLayer.cc:

EV << FindModule<BaseMobility*>::findHost(getParentModule())->getId() << endl;

It will first return the host module and then use the getId() function to get its id.


For better understanding:

Firstly, you can run the simulation to see the indexing of the whole simulation and it would be like this: Simulation information in veins

As read from the figure, each objects are assigned to a number, e.g. node[0]has the id 7, besides that, each sub-modules are also assigned with id numbers, e.g.

  • node[0] id = 7
    • appl id = 8
    • nic id = 9
    • veinsmobility id = 10

All of this ids (7,8,9,10) point to the node[0], which means you can use thoese ids to identify a specific car.

In the default DemoBaseApplLayer.cc, you can find

mac = FindModule<DemoBaseApplLayerToMac1609_4Interface*>::findSubModule(getParentModule());

and

myId = mac->getMACAddress();

in the initialization function void DemoBaseApplLayer::initialize(int stage).

Therefore, you can already use the myId as the vehicle id.


By the way, the reason that you get the 18 and 20 for vehicle id, is that the returned module might just be the host module and the sub-module, e.g. 18 is for the node[*] and the 20 is for its nic sub-module.

Anjie Qiu
  • 11
  • 4