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

how to print name of elements of ros message

asked 2016-06-02 07:17:43 -0600

JanOr gravatar image

updated 2016-06-02 07:35:08 -0600

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!

edit retag flag offensive close merge delete

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.

NEngelhard gravatar image NEngelhard  ( 2016-06-02 15:08:28 -0600 )edit

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.

JanOr gravatar image JanOr  ( 2016-06-03 10:10:56 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-06-03 10:29:01 -0600

kramer gravatar image

updated 2016-06-10 09:18:42 -0600

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.

edit flag offensive delete link more

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.

JanOr gravatar image JanOr  ( 2016-06-06 03:14:57 -0600 )edit

Edited answer with a(n old) code demo snippet. Hope that helps.

kramer gravatar image kramer  ( 2016-06-10 08:01:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-02 07:17:43 -0600

Seen: 6,163 times

Last updated: Jun 10 '16