【问题标题】:Calculating the distance between the current vehicle and the preceding vehicle计算当前车辆与前车的距离
【发布时间】:2018-01-14 12:01:35
【问题描述】:

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

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

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

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


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

【问题讨论】:

  • 是的。它适用于 Python API。我已经浏览过 TraCI 的 C++ 命令界面,但是找不到我要找的东西。
  • 有趣的问题。我在想从收到的数据包中获取位置,但这仅适用于有两辆车的场景。我真的想不出一个简单的方法,除非有一个 RSU 以及如何让它完成你想要做的事情,但这只是一个想法,可能会也可能不会奏效。
  • 感谢您的评论。这可能是一种方法,但在复杂的场景中识别领导者可能是一个问题,以计算距离。我猜迈克尔的答案更可靠。

标签: c++ omnet++ veins sumo


【解决方案1】:

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

【讨论】:

  • 完美!非常感谢迈克尔。我想你拯救了我的一天。我会试一试,让你知道它是如何工作的(非常想对答案进行投票,但由于我的声誉低而无法投票)。
  • Michael,我刚刚测试了代码,遇到了这个错误,“ASSERT: Condition 'typeId_resp == TYPE_STRING' does not hold in function 'getLeader'”。对此有什么想法吗?
  • 我忘记了化合物,我编辑了答案以尊重它,请重试。
  • 再次测试,工作正常,没有错误。我想问题已经解决了。如果遇到任何问题,我会通知您。再次感谢!
猜你喜欢
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 2017-07-16
  • 2022-12-22
相关资源
最近更新 更多