1

I have needed to calculate the distance between the current vehicle and the preceding vehicle,for each vehicle in a flow, running on an edge.

While going through TraCI, I came across getLeader method which is supposed to return the Id of the leader and the distance I need.

But I could not find a implemented method with this name, or any of the other method listed under Overview Extended Variables Retrieval written in C++ in TraCI.

I really appreciate if someone can help me with this.


I was successful in implementing getLastStepVehicleIDs to retrieve values from induction loops as advised here. I followed the already implemented methods of the same type (e.g. getJunctionIds), but in this case no such already implemented methods can be found.

user8561039
  • 115
  • 10
  • Yes.It is for Python API. I have already gone through the C++ command interface of TraCI, but could not find what I was looking for. – user8561039 Jan 14 '18 at 12:36
  • Interesting question. I was thinking get position from received packets but that only works in a scenario with two vehicles. I cannot really think of an easy way really, unless there's an RSU and some how get that to accomplish what you are trying to do, but again that's just an idea and might and might not work. – LionsFan Jan 15 '18 at 01:53
  • Thanks for the comment. This could be a way, but identifying leader might be a problem in a complex scenario, to calculate the distance. I guess Michael's answer is more reliable. – user8561039 Jan 15 '18 at 07:53

1 Answers1

7

There are plenty of functions to borrow from in TraCICommandInterface.cc. Without testing an implementation could look like this

std::pair<std:string, double> TraCICommandInterface::getLeader(const double lookahead) {
    TraCIBuffer request;
    request << static_cast<uint8_t>(VAR_LEADER) << nodeId
                    << static_cast<uint8_t>(TYPE_DOUBLE) << lookahead;
    TraCIBuffer response = connection.query(CMD_GET_VEHICLE_VARIABLE, request);

    uint8_t cmdLength; response >> cmdLength;
    if (cmdLength == 0) {
            uint32_t cmdLengthX;
            response >> cmdLengthX;
    }
    uint8_t responseId; response >> responseId;
    ASSERT(responseId == RESPONSE_GET_VEHICLE_VARIABLE);
    uint8_t variable; response >> variable;
    ASSERT(variable == VAR_LEADER);
    std::string id; response >> id;
    uint8_t typeId_r; response >> typeId_r; ASSERT(typeId_r == TYPE_COMPOUND);
    uint32_t compoundLength; response >> compoundLength; ASSERT(compoundLength == 2);
    uint8_t typeId_resp; response >> typeId_resp; ASSERT(typeId_resp == TYPE_STRING);
    std::string leader; response >> leader;
    uint8_t typeId_resp2; response >> typeId_resp2; ASSERT(typeId_resp2 == TYPE_DOUBLE);
    double dist; response >> dist;

    return std::make_pair(leader, dist);
}
Michael
  • 3,510
  • 1
  • 11
  • 23
  • Perfect! Thanks a lot Michael. I guess you saved my day. I will give it a try and let you know how it works (Badly wanted to up vote the answer, but could not due to my low reputation). – user8561039 Jan 15 '18 at 08:04
  • Michael, I just tested the code and encountered this error, "ASSERT: Condition 'typeId_resp == TYPE_STRING' does not hold in function 'getLeader'". Any thoughts on this? – user8561039 Jan 21 '18 at 06:11
  • 1
    I forgot the compound, I edited the answer to respect that, please try again. – Michael Jan 21 '18 at 09:27
  • Tested again and worked fine without errors. I guess the problem has been sorted out. I will let you know if any issue is encountered. Thanks a lot again! – user8561039 Jan 21 '18 at 11:23