从 C++ TRACI 客户端在 SUMO 中转换车辆位置(XY 坐标到纬度和经度)时出错

Error in converting vehicle position ( XY Coordinates to Latitude & Longitude) in SUMO from C++ TRACI Client

我在 TRACI 客户端中编写了一个函数来查询 SUMO(TRACI 服务器)以获取汽车的当前位置,我在 XY 坐标系中得到了正确的位置。现在我想将这个检索到的 XY 位置更改为 Latitude 和 Longitude.I 根据 http://www.sumo.dlr.de/wiki/TraCI/Simulation_Value_Retrieval#Command_0x82:_Position_Conversion 上的文档编码 但我收到错误!!请看代码

TraCITestClient::Position TraCITestClient::getPosition()
{
send_commandGetVariable(0xa4, 0x42, "veh1");

tcpip::Storage inMsg;
try {
    std::string acknowledgement;
    check_resultState(inMsg, 0xa4, false, &acknowledgement);

} catch (tcpip::SocketException& e) {
    pos.x = -1;
    pos.y = -1;
    return pos;
}
check_commandGetResult(inMsg, 0xa4, -1, false);
// report result state
try {
    int variableID = inMsg.readUnsignedByte();
    std::string objectID = inMsg.readString();

    int valueDataType = inMsg.readUnsignedByte();

    pos.x = inMsg.readDouble();
    pos.y = inMsg.readDouble();

} catch (tcpip::SocketException& e) {
    std::stringstream msg;
    msg << "Error while receiving command: " << e.what();
    errorMsg(msg);
    pos.x = -1;
    pos.y = -1;
    return pos;
}

//till here i am getting correct value in pos.x and pos.y

// now i want to convert these XY coordinates to actual Lat Long


    tcpip::Storage* tmp = new tcpip::Storage;


    tmp->writeByte(TYPE_COMPOUND);
    tmp->writeInt(2);


    tmp->writeDouble(pos.x);
    tmp->writeDouble(pos.y);
    tmp->writeByte(TYPE_UBYTE);

    tmp->writeUnsignedByte(POSITION_LON_LAT);

send_commandGetVariable(0x82, 0x58, "veh1",tmp); //**here i am getting error**

tcpip::Storage inMsgX;
try {
    std::string acknowledgement;
    check_resultState(inMsgX, 0x82, false, &acknowledgement);

} catch (tcpip::SocketException& e) {
    return pos;
}
check_commandGetResult(inMsgX, 0x82, -1, false);
// report result state
try {

    int variableID = inMsgX.readUnsignedByte();
    std::string objectID = inMsgX.readString();

    int valueDataType = inMsgX.readUnsignedByte();


    pos.x = inMsgX.readDouble();
    pos.y = inMsgX.readDouble();

} catch (tcpip::SocketException& e) {
    std::stringstream msg;
    msg << "Error while receiving command: " << e.what();
    errorMsg(msg);
    return pos;
}

return pos;
}

所以我在 SUMO 服务器上遇到的错误是:错误:tcpip::Storage::readIsSafe:想从存储中读取 823066624 字节,但只剩下 20 字节 正在退出(出错)。

无需重新发明轮子。 src/utils/traci/TraCIAPI.h 中有一个 TraCI C++ API。 您的第一个电话可以减少到

 TraCIPosition pos = TraCIAPI::VehicleScope::getPosition("veh1");

不幸的是,第二次调用还不是 C++ 的一部分 API,但您可能可以使用

修复它
tcpip::Storage* tmp = new tcpip::Storage;
tmp->writeByte(TYPE_COMPOUND);
tmp->writeInt(2);
tmp->writeByte(POSITION_2D);
tmp->writeDouble(pos.x);
tmp->writeDouble(pos.y);
tmp->writeByte(TYPE_UBYTE);
tmp->writeUnsignedByte(POSITION_LON_LAT);
send_commandGetVariable(CMD_GET_SIM_VARIABLE, POSITION_CONVERSION, "",tmp);

您的版本不包含第一个类型说明符 (POSITION_2D),并且还为命令和变量使用了错误的十六进制代码。在这里使用常量而不是十六进制代码总是一个好主意。