how to print name of elements of ros message
Hey, I would like to print the elements of ros messages dynamically, with a function similar to:
template <typename M>
void printElements()const{
const boost::shared_ptr<M> msgPtr;
/*
Extract element table
cout<< how can I access the name of the elements here?<<endl;
*/
};
How can I extract the message elements so that when I execute
<geometry_msgs::PoseStamped>printElements();
,I get for the example of a "geometry_msgs/PoseStamped" Message:
pose.header.sec
pose.header.nsec
pose.position.x
pose.position.x
pose.position.x
pose.orientation.x
pose.orientation.y
pose.orientation.z
pose.orientation.w
Do you have an idea how I can dynamically access the name, e.g. "pose.position.x","pose.position.y","pose.position.z", etc. For other message type, the printout shall be accordingly. Thank you very much for any ideas of solving this issue!
Asked by JanOr on 2016-06-02 07:17:43 UTC
Answers
You might want to look into classes in the topic tools package. I think that the ShapeShifter class provides the functionality you're looking for.
Edit: adding a sketch of example code
// might need additional TransportHints parameter?
ros::Subscriber g_sub = nh.subscribe("my_topic", 10, &in_cb);
void in_cb(const ros::MessageEvent<ShapeShifter>& ev) {
boost::shared_ptr<ShapeShifter const> const &ssmsg = ev.getConstMessage();
ROS_INFO("publisher: [%s]", ev.getPublisherName().c_str());
ROS_INFO(" md5sum: [%s]", ssmsg->getMD5Sum().c_str());
ROS_INFO(" datatype: [%s]", ssmsg->getDataType().c_str());
ROS_INFO(" defn: [%s]", ssmsg->getMessageDefinition().c_str());
ROS_INFO(" msg size: %u", ssmsg->size());
}
This is a snip-job of old code I was playing with in Hydro, so I'm not sure it's current. However, it should be enough to point you (further) in a useful direction.
Edit2: Probably should've mentioned -- in case it's not clear, the specific thing you'll be concerned with is the getMessageDefinition
method. You'll have to parse the returned string as you see fit.
Asked by kramer on 2016-06-03 10:29:01 UTC
Comments
Hey, thanks for the idea. Could you specify this idea a bit more. As I understood, the shapeshifter class can act as a generic message class. Nevertheless I don't now how to show the message variables (e.g. "pose.position.x") and not the message data (e.g. "1.23"). Thanks a lot for your help.
Asked by JanOr on 2016-06-06 03:14:57 UTC
Edited answer with a(n old) code demo snippet. Hope that helps.
Asked by kramer on 2016-06-10 08:01:35 UTC
Comments
Do you need it in C++? This is probably going to be a bit involved, possibly including parsing the created header files. It would be much easier in python as python objects still contain the variable names.
Asked by NEngelhard on 2016-06-02 15:08:28 UTC
Thanks a lot. Yes unfortunately I need it in c++. That's not good news. I thought that roscpp makes the variable names somewhere accessible. Thank you very much.
Asked by JanOr on 2016-06-03 10:10:56 UTC