Ask Your Question
1

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
8

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 imageJP ( 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 imagemjcarroll ( 2013-12-12 03:05:33 -0600 )edit

Thanks for that!

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

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

This is useful, thanks!

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 29,142 times

Last updated: Dec 09 '13