ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A
Ask Your Question

PAUSE/STOP the ros subscriber(callback function) and restart it?

asked 2013-05-30 12:24:13 -0500

anthonyli gravatar image

updated 2013-05-30 12:27:09 -0500

According to the tutorial of cv_bridge(here is two lines of code in the tutorial, 1.4 An example ROS node),

(line 24)image_sub_ = it_.subscribe("in", 1, &ImageConverter::imageCb, this);

(line 61)ros::spin();

Base on this code, the ros::spin() will call the callback function and then always stay in the callback function! So if I want to pause the callback function or subscriber for a while, and then restart it, what should I do ?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-05-30 13:18:00 -0500

tfoote gravatar image

updated 2013-05-30 13:18:30 -0500

That's not how spin works. See the roscpp spinning documentation

If you want to stop receiving data you should unsubscribe and resubscribe to start the flow again. See the subscriber overview

edit flag offensive delete link more


Thank you ! I try to find the answer in the link of subscriber overview you given. But it seems nothing related to unsubscribe and resubscribe. Then I try like this, the code show in later answer. it will shutdown the subscriber, but I don't know why it doesn't resubscribe.

anthonyli gravatar imageanthonyli ( 2013-05-30 22:51:12 -0500 )edit

Also, for a temporary pause that needs to happen fairly quickly, you can send a message to your node causing it to start ignoring the original topic until told to resume operation.

joq gravatar imagejoq ( 2013-05-31 05:45:38 -0500 )edit

To unsubscribe shutdown the subscriber or destruct it. And to resubscribe create a new subscriber.

tfoote gravatar imagetfoote ( 2013-05-31 07:37:21 -0500 )edit

Thank you!

anthonyli gravatar imageanthonyli ( 2013-06-01 05:39:52 -0500 )edit

answered 2013-05-30 22:56:48 -0500

anthonyli gravatar image

updated 2013-05-31 00:33:48 -0500

image_transport::Subscriber image_sub_;

while(1) { image_sub_=it_.subscribe("/ardrone/image_raw", 1,imageCallback); ros::spin(); ...... }

and in the end of imageCallback function I add this code, "image_sub_.shutdown()"

The final result seems, the subscriber will shutdown, but never resubscribe.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2013-05-30 12:24:13 -0500

Seen: 7,793 times

Last updated: May 31 '13