Ask Your Question
0

Some functions are not called and ros::spin() issue

asked 2011-12-17 06:12:09 -0600

alfa_80 gravatar image

updated 2011-12-17 06:40:20 -0600

Eric Perko gravatar image

I am having some parts of code that are not called. The code is here. The scanCallback() function is not called at all, I think(I don't know what to be passed in from the main function as it is a callback function). I already use ros::spin() there, but then, I still need to call some of the member functions explicitly.

I've been told that when using ros::spin, I don't need to invoke the member functions explicitly, but, when I tried, it's not so. What is the culprit?

Hopefully, some experienced coders can guide me on this. Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-12-17 06:40:05 -0600

Eric Perko gravatar image

updated 2011-12-17 07:01:31 -0600

When using ros::spin() you don't need to explicitly call your callbacks - any non-callback member functions will not be automatically called.

Looking at the code you linked, you never actually subscribed to a topic and setup the callback, so roscpp will never call anything, even if you do include ros::spin() or ros::spinOnce(). See the beginner tutorials (specifically this one) for how to subscribe to a topic with roscpp.

UPDATE: Even with the new initializations in the linked code, your callback will not be called. In order to have roscpp process your callbacks, you have to yield control using ros::spin() or ros::spinOnce().

The easiest solution would be to add a ros::spinOnce() just before your call to r.sleep(). This will let roscpp process your callbacks once before your function sleeps to maintain the desired rate. See the roscpp docs on Callbacks and Spinning for details.

edit flag offensive delete link more

Comments

@Eric Perko: Thanks for the clarification. Very sorry for forgetting to include in the previous post some initializations there..could you go back to the link, if possible to see if it's ok.
alfa_80 gravatar imagealfa_80 ( 2011-12-17 06:55:36 -0600 )edit
@Eric Perko: "any non-callback member functions will not be automatically called.", regarding this statement, that means, any non-callback member function I must invoke it explicitly, right? just to double check my understanding..
alfa_80 gravatar imagealfa_80 ( 2011-12-17 07:00:16 -0600 )edit
Updated my answer to reflect the updated code in the linked post.
Eric Perko gravatar imageEric Perko ( 2011-12-17 07:02:06 -0600 )edit
Yes, you must still explicitly invoke any member functions not registered to roscpp as a callback (either callbacks to Subscribers, Timers, etc).
Eric Perko gravatar imageEric Perko ( 2011-12-17 07:02:44 -0600 )edit
Thanks for the clarification. I've added "ros::spinOnce() just before your call to r.sleep()", but then, it returns some errors "terminate called after throwing an instance of 'tf::ExtrapolationException' what(): Unable to lookup transform, cache is empty, when looking up transform from frame [/"
alfa_80 gravatar imagealfa_80 ( 2011-12-17 07:17:21 -0600 )edit
cont'd: when looking up transform from frame [/laser] to frame [/base_link] [publisher_node-2] process has died [pid 9283, exit code -6]. log files: /home/shah/.ros/log/a54c8b46-28ae-11e1-9b0d-c42c03199b29/publisher_node-2*.log
alfa_80 gravatar imagealfa_80 ( 2011-12-17 07:17:58 -0600 )edit
But then, why use ros::spinOnce() there, instead of using ros::spin()?
alfa_80 gravatar imagealfa_80 ( 2011-12-17 07:21:02 -0600 )edit
You use spinOnce in that situation because spin() would spin indefinitely (until the node is requested to be shutdown), whereas spinOnce() only processes each callback once, and then returns control to the calling function (which is already in a n.ok() loop).
Eric Perko gravatar imageEric Perko ( 2011-12-17 10:58:22 -0600 )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

Stats

Asked: 2011-12-17 06:12:09 -0600

Seen: 790 times

Last updated: Dec 17 '11