Ask Your Question

Acquiring new data/message from a subscribed topic

asked 2014-03-29 16:06:45 -0500

Bharadwaj gravatar image

updated 2014-03-30 02:50:37 -0500

I have subscribed to the following topic


I have a callback function defined as the following in a c++ file :

ros::Subscriber sub = nh.subscribe ( "/camera/depth_registered/points", 1, process_cloud );

1) I want to obtain the latest data from the above topic when I want it. Is there a way to acquire the latest data from the topic whenever I want inside my callback function ?

I also tried a workaround using the ros::spinOnce() function. But sometimes my callback function does not get called.

2) When and why will this happen ? - the callback function not being called.

3) Is there a way to check if the callback got called when using the ros::spinOnce() ?

  • proccess_cloud is the only callback function i have in my program
  • I am using ros - groovy on a Ubuntu-12.04 machine

I appreciate any help anyone can provide. Please post if you require anymore information.

___ UPDATE ____

Essentially this is what I am trying to do :

1) I have a UDP communication setup in the above program, between the computer that runs the above code that I am having an issue with, I will call this PC 1

2) PC2 sends a request to PC1 over UDP communication - at which time i want to acquire latest data from my Prime Sense Carmine 1.09 sensor.

3) Thus in my above code, in PC1, I want to wait for this UDP request and then acquire a data from the prime Sense sensor and process the data.

I have tried two versions of code to accomplish the above problem

a) 1st version - I use the UDP - recvfrom function inside the callback function : thus the code essentially gets held at this line waiting to request from PC2(which how the recvfrom function works). And when it recieves the request from PC2 rest of the processing occurs - thus working on old data. Which led to my question here as to how to obtain new data in my callback function.

b) To work around the above problem I came up with the 2nd version : Here I use the spinOnce function , thus in my main function I wait for the request from PC2 using the recvfrom function. And then I use spinOnce function - to run my callback function. All this essentially inside a while loop. - But in this case sometimes the callback does not get called sometime. - Also in this case is it ensured that the latest available data from my topic used ?

And my 1st line in the callback function a print to the command line saying I was called ( using std::cout ). Which is how I figure out that the callback does not get called( in my 2nd case).

I am using the following command to obtain data from the prime sense sensor.

roslaunch openni_launch openni.launch

Also is this the best way to obtain data from the prime sense sensor

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-03-29 20:41:16 -0500

McMurdo gravatar image

updated 2014-03-29 20:42:18 -0500

  1. Whenever a callback is called, it is called with the latest data. But, if your callback takes too much time, and another message arrives when you are processing your current callback, then you will most likely lose the message. Too much processing is discouraged in callback functions. The reason for this should be obvious if you try thinking of callbacks as interrupts. It is essentially an interrupt handler in the context of ROS.
  2. Please paste the entire code. The work of spinOnce can be thought of like this - enable interrupts and call the interrupt handler for one interrupt (or in ROS language - look at the callback queue and fetch the fellow at the start). This means that spinOnce should be used inside a while/for loop for it to be called periodically. There is a possibility - if your program has a spinOnce() function that is standing alone, it might never trigger callback at all. Please read more on callback queues if you want a deeper understanding. Also, if there is no spinOnce or spin, the callback will never happen.
  3. Simple - put a print statement inside the callback that says, "I was called" or what have you.

Also, what launch are you using to obtain the depth_registered image? Are you running a custom launch which has the parameter depth_registration set to true? If it is set to false, you will see the topic but depth registration won't happen and you can't get a callback at all. Please be sure that depth registration is actually happening.

Please see this: might be useful -

I have tried my best to answer you. Please tell me if that solves it.

edit flag offensive delete link more


Dear Mr.McMurdo, I appreciate ur detailed reply. I have update my question based on your reply. It does help me get a better understanding but does not solve my issue. Can u pls give me the right link to study about spinOnce function standing alone. I think this might be my issue.

Bharadwaj gravatar image Bharadwaj  ( 2014-03-30 02:57:03 -0500 )edit

answered 2014-03-31 18:24:36 -0500

Bharadwaj gravatar image

I had my problem fixed. I used the second method that I discussed in my question( the one that says b).

From my understand the issue could be that no more messages/data was being available on that topic. Thus there was no data for the subscriber to trigger the callback.

Thus I simply check if the callback was called or not using a variable(global) before calling spinOnce and change the variable value inside the callback function, and after my spinOnce function i check the value of this variable.

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

1 follower


Asked: 2014-03-29 16:06:45 -0500

Seen: 1,428 times

Last updated: Mar 31 '14