Search code examples
c++omnet++veinssumo

Calculating the distance between the current vehicle and the preceding vehicle


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.


Solution

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