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

ROS_INFO can't print a ROS message?

asked 2020-08-14 07:52:20 -0500

db gravatar image

updated 2020-08-16 09:37:54 -0500

I just spent around almost 2 hrs trying to fix a problem, that didn't even exist in the first place. All because ROS_INFO was printing the wrong thing (essentially printing garbage) when I tried to do something like

std::string s = "..."
ROS_INFO("result: %s", s)

I had to print an std::string because I was trying to print a ROS message, which I had to parse myself, because I couldn't JUST PRINT THE MESSAGE:

void soemclass::jointPositionsCallback(const some_msgs::JointsPositions::ConstPtr& msg){
    ROS_INFO(msg); // <- COMPILE ERROR

Is there really no way to JUST PRINT THE MESSAGE? Why can't I just print the std::string as well?

I feel this should be so much easier.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-08-14 08:10:36 -0500

fergs gravatar image

updated 2020-08-14 08:11:58 -0500

The non-stream versions take C-style strings (a null terminated character array), so they work with something like this:

ROS_INFO("a c-style string");

If you want to print a std::string you can do it one of two ways:

ROS_INFO(s.c_str());  // this prints a C-style string

For your message, this should work:

edit flag offensive delete link more


Obligatory link to the wiki page: roscpp/Overview/Logging.

gvdhoorn gravatar image gvdhoorn  ( 2020-08-14 08:25:55 -0500 )edit

Ok, I didn't know about the ROS_INFO_STREAM, thank you. Do you know if the ROS message objects have a built-in "to_string()" method?

db gravatar image db  ( 2020-08-16 09:29:29 -0500 )edit

@gvdroorn I saw this documentation. But, as you can see, I just wanted to make a simple logging of an std::string and a ros message. This is a very complicated documentation for such a basic function, and the documentation doesn't even mention the supposedly correct method to do this, which is ROS_INFO_STREAM(). The purpose of this question is also to bring awareness to this issue, these little things make ROS unnecessarily hard to use.

db gravatar image db  ( 2020-08-16 09:33:24 -0500 )edit

Yes, the ROS messages have a "<<" operator, which is how streams work.

fergs gravatar image fergs  ( 2020-08-17 22:52:33 -0500 )edit

Question Tools



Asked: 2020-08-14 07:52:20 -0500

Seen: 1,870 times

Last updated: Aug 16 '20