计算当前车辆与前车之间的距离

时间:2018-01-14 12:01:36

标签: c++ omnet++ veins sumo

我需要计算当前车辆和前车之间的距离,对于流动中的每辆车,在边缘上运行。

在通过TraCI时,我遇到了getLeader方法,它应该返回领导者的身份和我需要的距离。

但我找不到一个使用此名称的实现方法,或者在TraCI中用C ++编写的 Overview Extended Variables Retrieval 下列出的任何其他方法。

如果有人可以帮助我,我真的很感激。

我成功实施了getLastStepVehicleIDs,以便按照建议here从感应循环中检索值。我遵循已经实现的相同类型的方法(例如getJunctionIds),但在这种情况下,找不到这样已经实现的方法。

1 个答案:

答案 0 :(得分:7)

在TraCICommandInterface.cc中有很多可以借用的函数。没有测试,实现可能看起来像这样

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