0
$\begingroup$

Rosanswers logo

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.


Originally posted by bja on ROS Answers with karma: 63 on 2013-07-15

Post score: 6


Original comments

Comment by allenh1 on 2013-07-15:
Under what context are you running this? Is this a thread?

Comment by bja on 2013-07-15:
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.

Comment by thebyohazard on 2013-07-15:
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?

Comment by bja on 2013-07-15:
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.

Comment by ayush_dewan on 2013-07-15:
Everything seems legit. Just check once that you are continuously polling for message by using spinning appropriately.

Comment by thebyohazard on 2013-07-15:
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

Comment by Bill Smart on 2013-07-15:
Are messages being published on the topic? You can check with rostopic echo.

Comment by bja on 2013-07-15:
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).

Comment by ayush_dewan on 2013-07-15:
Are you using a bag file to publish this data??

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Bill Smart with karma: 1263 on 2013-07-15

This answer was ACCEPTED on the original site

Post score: 12


Original comments

Comment by bja on 2013-07-15:
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!

Comment by GPrathap on 2018-10-17:
You save my dayyyy!!!, thank youu

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.