Search code examples
sumo

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


I have written a function in TRACI Client to Query SUMO (TRACI Server) for fetching the current position of a car, which i am getting correct in XY coordinate system. Now i want to change this retrieved XY position to Latitude and Longitude.I coded as per the documentation present at http://www.sumo.dlr.de/wiki/TraCI/Simulation_Value_Retrieval#Command_0x82:_Position_Conversion but i am getting error!!. Please take a look at code

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

So the error i am getting on SUMO Server is :Error: tcpip::Storage::readIsSafe: want to read 823066624 bytes from Storage, but only 20 remaining Quitting (on error).


Solution

  • There is no need to reinvent the wheel. There is a TraCI C++ API available in src/utils/traci/TraCIAPI.h. Your first call can be reduced to

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

    Unfortunately the second call is not part of the C++ API yet but probably you could fix it using

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

    Your version did not contain the first type specifier (POSITION_2D) and also used the wrong hex codes for the command and for the variable. It is always a good idea to use the constants instead of the hex codes here.