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

Print out the contents of a string using ROS_INFO

asked 2013-12-09 10:38:59 -0600

JP gravatar image

Hi all,

I have a line such as:

   std_msgs::String s;
s.data = c.c_str(); //same data as above.
pthread_mutex_lock(&send_CS);
tum_ardrone_pub.publish(s);
pthread_mutex_unlock(&send_CS);

What I would like to do is look at the contents of s that is passed into the method publish(). I tried something like:

ROS_INFO("(2) (%s) \n",s.c_str());

But that failed at compile time. Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
13

answered 2013-12-09 10:48:01 -0600

std_msgs::String is not the same thing as std::string and cannot be treated as such; it is a message type that has a std::string member variable called data. Thus, the correct way to output the message string is

ROS_INFO("%s\n", s.data.c_str());

edit flag offensive delete link more

Comments

Thank you!

JP gravatar image JP  ( 2013-12-09 11:51:13 -0600 )edit

For additional clarification, using ROS_INFO_STREAM uses the ostream operator implementation (operator<<) that rosmsg generates for each message type. The ostream operator is similar to the output you would see from rostopic echo.

mjcarroll gravatar image mjcarroll  ( 2013-12-12 03:05:33 -0600 )edit

Thanks for that!

JP gravatar image JP  ( 2013-12-12 09:52:31 -0600 )edit

For some reason c_str() didn't work for me so I used data() instead: ROS_INFO("frame: %s", msg.header.frame_id.data()); or in OP case ROS_INFO("%s\n", s.data());

Combinacijus gravatar image Combinacijus  ( 2022-12-14 08:12:07 -0600 )edit
8

answered 2013-12-09 10:42:32 -0600

mjcarroll gravatar image

I tend to use ROS_INFO_STREAM when I'm doing simple debugging.

You should be able to do:

ROS_INFO_STREAM(s);

Or

ROS_INFO_STREAM("Msg: " << s);

edit flag offensive delete link more

Comments

2

This is useful, thanks!

JP gravatar image JP  ( 2013-12-09 11:51:33 -0600 )edit

I tried this for moveit_msgs/RobotTrajectory.msg and it worked fine. And I think it can print any type of ROS message. Thanks!

askkvn gravatar image askkvn  ( 2021-06-18 02:37:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-12-09 10:38:59 -0600

Seen: 49,908 times

Last updated: Dec 09 '13