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

Why Publisher publish a message but Subscriber callback recieves a pointer of message?

asked 2021-10-15 01:20:35 -0500

Hieu Joestar gravatar image

updated 2021-10-15 02:11:01 -0500

gvdhoorn gravatar image

I wonder why publisher publish a message. EX:

chatter_pub.publish(msg)

But the Subscriber CallBack function has a argument of pointer of msg. EX:

void chatterCallback(const std_msgs::String::ConstPtr& msg)

Link of above code: http://wiki.ros.org/roscpp_tutorials/...

edit retag flag offensive close merge delete

Comments

1

I've changed mess to message, as "a mess" is something completely different and rather confusing.

When I first read your question title I assumed you had a problem with data corruption.

gvdhoorn gravatar image gvdhoorn  ( 2021-10-15 02:10:36 -0500 )edit

oh sorry for my bad english.Message is really the word i want to say. Thank you.

Hieu Joestar gravatar image Hieu Joestar  ( 2021-10-15 08:02:09 -0500 )edit

No problem.

gvdhoorn gravatar image gvdhoorn  ( 2021-10-15 08:05:18 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-10-15 08:54:46 -0500

Mike Scheutzow gravatar image

A ConstPtr is the most efficient way to pass the message to your function, especially if it's a very large message. ros does support passing the message using other prototypes, but there's rarely a good reason to use any of them.

edit flag offensive delete link more

Comments

Hi sir, i know the reason why we use pointer. But why publish() just expect the msg( EX:std_msgs::String msg) instead of std_msgs::String::ConstPtr like a Callback function expect.

Hieu Joestar gravatar image Hieu Joestar  ( 2021-10-16 20:11:24 -0500 )edit

publish() takes a plain std_msgs::String to make writing code more convenient for you. It will also accept a ::Ptr or a ::ConstPtr message if that's what you have.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-10-17 10:05:47 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-10-15 01:20:35 -0500

Seen: 386 times

Last updated: Oct 15 '21