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

时间:2016-12-06 13:25:57

标签: sumo

我在TRACI Client中编写了一个函数来查询SUMO(TRACI服务器)以获取汽车的当前位置,我在XY坐标系中得到了正确的结果。现在我想将此检索到的XY位置更改为纬度和经度。我根据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 Server上遇到的错误是:错误:tcpip :: Storage :: readIsSafe:想从存储中读取823066624个字节,但只剩下20个字节 退出(出错)。

1 个答案:

答案 0 :(得分:0)

没有必要重新发明轮子。在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),并且还为命令和变量使用了错误的十六进制代码。在这里使用常量而不是十六进制代码总是一个好主意。

相关问题