Ask Your Question

Subscriber callback not being called in C++

asked 2013-07-15 05:11:52 -0500

bja gravatar image

updated 2014-01-28 17:17:15 -0500

ngrennan gravatar image

I am trying to subscribe to a topic and trigger a callback function. Following the wikipage on subscribers, I wrote the following lines (since I am dealing with C++ namespaces):

ros::Subscriber ekf_sub = nh_.subscribe("/ardrone/predictedPose", queue_size_, &VisualOdometry::ekf_callback, this);

void VisualOdometry::ekf_callback(const tum_ardrone::filter_state::ConstPtr& ekf_msg){
std::cout<<"EKF callback function reached "<<std::endl;}

I have used "rostopic list" and rqt to verify the topic "/ardrone/predictedPose" is being published while my program is running. I have used "rostopic info" to verify that the topic is of type "tum_ardrone/filter_state".

In my subscriber constructor, "nh_" is a public nodehandle that is successfully used in the class ("VisualOdometry") for other subscribers. "queue_size_" is simply an int set to 5, and "this" is the pointer to the instance of the class "VisualOdometry" (since that's where the code is located).

I have further verified that the subscriber constructor line is executed, but my callback function is never called. Any help or tips on how to debug is much appreciated since I'm unsure of what to do. I am using ROS Groovy.

edit retag flag offensive close merge delete


Under what context are you running this? Is this a thread?

allenh1 gravatar image allenh1  ( 2013-07-15 05:23:50 -0500 )edit

The class "VisualOdometry" is being run as a node, and is currently subscribing to rgb and depth images from a Kinect. I'm trying to make it also subscribe to the above topic.

bja gravatar image bja  ( 2013-07-15 05:33:06 -0500 )edit

Everything looks right, assuming it's a typo that the topic is of type "tum_ardrone/filter/state" and is actually of type "tum_ardrone/filter_state." Are you spinning?

thebyohazard gravatar image thebyohazard  ( 2013-07-15 05:42:50 -0500 )edit

Yes that was a typo, I changed it in my description, thanks. And yes within the node code, after the "VisualOdometry" object is created, "ros::spin()" is executed.

bja gravatar image bja  ( 2013-07-15 05:53:06 -0500 )edit

Everything seems legit. Just check once that you are continuously polling for message by using spinning appropriately.

ayush_dewan gravatar image ayush_dewan  ( 2013-07-15 05:56:54 -0500 )edit

Is this node run by a launch file or a rosrun command? Because with roslaunch the default isn't for output to show on the screen. You have to change output="screen" in the node tag. link

thebyohazard gravatar image thebyohazard  ( 2013-07-15 06:34:04 -0500 )edit

Are messages being published on the topic? You can check with rostopic echo.

Bill Smart gravatar image Bill Smart  ( 2013-07-15 06:35:16 -0500 )edit

I am using a launch file and output is already set to "screen". I verified again that by doing "rostopic echo /ardrone/predictedPose" the topic is publishing messages (I also observe this in rqt, that rostopic is subscribed to the topic).

bja gravatar image bja  ( 2013-07-15 06:55:15 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-07-15 07:42:17 -0500

Bill Smart gravatar image

What scope is the variable ekf_sub at? If it's automatic, and goes away at the end of the function it's in, then the subscriber will be shut down when it goes out of scope. To make it persistent, it should be a member variable of your class.

edit flag offensive delete link more


That was exactly my problem, I wasn't declaring "ekf_sub" in my header file and it was thus going out of scope. My callback function is now reached; thank you very much!

bja gravatar image bja  ( 2013-07-15 08:29:16 -0500 )edit

You save my dayyyy!!!, thank youu

GPrathap gravatar image GPrathap  ( 2018-10-17 01:41:35 -0500 )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


Asked: 2013-07-15 05:11:52 -0500

Seen: 8,808 times

Last updated: Jul 15 '13