Subscriber callback not being called in C++
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.
Under what context are you running this? Is this a thread?
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.
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?
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.
Everything seems legit. Just check once that you are continuously polling for message by using spinning appropriately.
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
Are messages being published on the topic? You can check with
rostopic echo
.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).