gz-sim icon indicating copy to clipboard operation
gz-sim copied to clipboard

Is there a default way to check if loading of an SDF element was (syntactically) successful?

Open jrutgeer opened this issue 10 months ago • 0 comments

This is an example of reading a pose from SDF in a system plugin's configure():

https://github.com/gazebosim/gz-sim/blob/579013fe2841de68c0901192780ab8ee86cc9bb1/src/systems/performer_detector/PerformerDetector.cc#L81-L84

This works, but there is no error checking.

E.g. if you forget one value in the SDF

<pose>1 2 3 4 5</pose>

Then you will see an error message:

Error [Element.hh:1011] The value for //pose[@rotation_format='euler_rpy'] must have 6 values, but 5 were found instead in '1 2 3 4 5'.

But the configure() has no means to know that reading the pose has failed.

(By the way: note that this sdformat error message has different formatting than the other log output from gz-sim.)

I tried the following:

auto pose_element = _sdf->FindElement("pose");
sdf::ParamPtr param = pose_element->GetValue();
gz::math::Pose3d sdf_pose;
bool success = val->Get(sdf_pose);

This seems to work, i.e. if the pose was not correctly read from the SDF, then success is false.

However, consider the same for an std::string:

auto string_element = _sdf->FindElement("string");
sdf::ParamPtr param = string_element->GetValue();
std::string sdf_string;
bool success = val->Get(sdf_string);

This yields a segmentation fault if the string is defined in the SDF, but empty.

Obviously, for strings you check this in a different way (see code below), but I wonder if there is some default way to check correctness for any supported type.

https://github.com/gazebosim/gz-sim/blob/579013fe2841de68c0901192780ab8ee86cc9bb1/src/systems/joint_controller/JointController.cc#L109-L116

jrutgeer avatar Aug 11 '23 11:08 jrutgeer