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

Calculating the distance between the current vehicle and the preceding vehicle

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

在使用 TraCI 时,我遇到了 getLeader 方法,该方法应该 return 领导者的 ID 和我需要的距离。

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

如果有人能帮助我,我将不胜感激。


我成功实施了 getLastStepVehicleIDs to retrieve values from induction loops as advised here。我遵循了相同类型的已实现方法(例如 getJunctionIds),但在这种情况下找不到此类已实现的方法。

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