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

How is “const std_msgs::String::ConstPtr& msg” transmitted into callback function since there is no parameter in the main function?

asked 2022-03-11 12:29:37 -0600

jealor gravatar image

Hey everybody I have just started to learn CPP and ROS,and I've some confusion about the subscribe function.

When we create a subscriber,we use : ros::Subscriber sub=node.subscribe(turtle_name+"pose",10,&Callback); And here,there is no parameter transmitted into Callback, just the name of Callbackfunction is declared

But in the Callbackfunction,we use: void Callback(const turtlesim::PoseConstPtr& msg) Here the Callback function requires a parameter : & msg

Here it confuses me :Sincere there is no parameter transmitted in the main function when we call the Callback function How is &msg transmitted into Callback when it actuarally execuated?

Many thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2022-03-12 09:43:51 -0600

Mike Scheutzow gravatar image

The ROS devs are being tricky.

When you call the subscribe() method you also specify the ROS topic name. At run-time, the subscribe() method looks up the message type for that topic and then checks to see if the argument type of the callback function you provided is a match. If it doesn't match, the function won't be called when a message arrives.

The roscpp code is smart enough to check for various forms of the callback argument: const, non-const, non-ptr, regular ptr and ::ConstPtr. For big messages, some of these are much more efficient than others.

edit flag offensive delete link more

Comments

So it means that the internal mechanism of the subscribe() and Callback function it's well designed by ROS devs. It's really hard for me,the very first beginner, to understand by myself. Thanks a lot bro,best wishes

jealor gravatar image jealor  ( 2022-03-12 10:01:15 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2022-03-11 12:29:37 -0600

Seen: 93 times

Last updated: Mar 12 '22