ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

access custom message fields roscpp

asked 2013-02-03 17:11:16 -0600

IgorZ gravatar image

Hi, I am building a robot gripper in gazebo. I have created a YAML file where I specify, what part of my message controls which joint of the gripper.

I am asking probably obvious question but how can I get access to particular message field having its name as a string?


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-03-19 03:04:37 -0600

As far as I know, C++ does not support reflection or introspection out-of-the-box. There have been various suggestions for hacks to achieve similar functionality, but I don't know that any of these has wide acceptance. In general, they all seem to revolve around defining some macros that let you manually "register" your class/message structure at compile-time, for later introspection at run-time.

Here's a similar ROS request from a few years ago with a similar (negative) response. In that case, there was also a suggestion to do the introspection on the .msg definition file, and use that info to extract data from the serialized message stream. This would work, but might end up being more trouble than it's worth.

There was even an enhancement request filed 4 years ago for this very feature. But it was closed as "wontFix" at the time. The suggestion was to just use python instead.

For your application, I'd say it feels like the hardcoded switch-style that dornhege suggested would be the easiest method (assuming you have a limited set of messages you're trying to deal with):

if (field_name == "joint1")
  result = msg.joint1;
else if (field_name == "joint2")
  result = msg.joint2;  
edit flag offensive delete link more

answered 2013-02-04 00:44:51 -0600

dornhege gravatar image

There is not nice solution for this as in e.g. python.

If you know the message at compile-time it's probably something like if(field == "val1") value = msg.val1;. I'd be happy to see if someone can come up with something more elegant.

If you don't know it at compile-time it might not be possible to do that at all.

edit flag offensive delete link more

Question Tools


Asked: 2013-02-03 17:11:16 -0600

Seen: 707 times

Last updated: Mar 19 '13