0
$\begingroup$

i'm using XmlRpc::XmlRpcValue to receive data from .yaml file, but it seems that the received data order is different from the order in .yaml file.

here is my .yaml file:

wheelsets:
  left_front:
    position: [ 0.585, 0.557 ]  
    <<: &default
      steer_offset: 0.0
      wheel_radius: 0.1175
  right_front:
    position: [ 0.585, -0.557 ]
    <<: *default
  left_back:
    position: [ -0.585, 0.557 ]
    <<: *default
  right_back:
    position: [ -0.585, -0.557 ]
    <<: *default

and here is my code:

XmlRpc::XmlRpcValue wheelsets;
nh_.getParam("wheelsets", wheelsets);
ROS_ASSERT(wheelsets.getType() == XmlRpc::XmlRpcValue::TypeStruct);
for (const auto& wheelset : wheelsets)
{
    ROS_ASSERT(wheelset.second.hasMember("position"));
    ROS_ASSERT(wheelset.second["position"].getType() == XmlRpc::XmlRpcValue::TypeArray);
    ROS_ASSERT(wheelset.second["position"].size() == 2);
    ROS_ASSERT(wheelset.second.hasMember("steer_offset"));
    ROS_ASSERT(wheelset.second["steer_offset"].getType() == XmlRpc::XmlRpcValue::TypeDouble);
    ROS_ASSERT(wheelset.second.hasMember("wheel_radius"));
    ROS_ASSERT(wheelset.second["wheel_radius"].getType() == XmlRpc::XmlRpcValue::TypeDouble);
}

the code can run successfully, but the data i receive is in order of left_back, left_front, right_back, right_front, which i guess is alphabetical order.

Is there any way to receive data in order same as .yaml file?

$\endgroup$

1 Answer 1

0
$\begingroup$

With a hash, there should be no guarantee that the data will be in order.

If order is important, one would normally use a list.

Other options would be

std::vecgtor<std::string>sets = ["left_front", "right_front", "left_back", "right_back"];
for (const auto& s : sets)
{
    ROS_ASSERT(wheelset[s].hasMember("position"));
    ROS_ASSERT(wheelset[s]["position"].getType() == XmlRpc::XmlRpcValue::TypeArray);
    ROS_ASSERT(wheelset[s]["position"].size() == 2);
    ROS_ASSERT(wheelset[s].hasMember("steer_offset"));
    ROS_ASSERT(wheelset[s]["steer_offset"].getType() == XmlRpc::XmlRpcValue::TypeDouble);
    ROS_ASSERT(wheelset[s].hasMember("wheel_radius"));
    ROS_ASSERT(wheelset[s]["wheel_radius"].getType() == XmlRpc::XmlRpcValue::TypeDouble);
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.