ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Is threading necessary in long callback function

asked 2021-01-13 16:11:28 -0500

TheMilkman92 gravatar image

updated 2021-01-13 16:12:45 -0500

Hey,

So I was wondering if within a ROS node, a callback function were to take 3 seconds to execute a task, would the whole node be held up by this? Or is it threaded in some way by default.

For this application I have a bump sensor on the front of my robot, and was hoping to do some obstacle avoidance if something was hit. For example, reverse, turn, try again. And I was going to hard code a sequence like that into my GNC system, but it would be good if the node isn't locked up for that time period. I was also hoping to ramp motor velocity up and down when a change in speed or direction is required, I guess that also ties into this question.

Thanks for any help in advance!

edit retag flag offensive close merge delete

Comments

2

This actually sounds like a good candidate for actions

jayess gravatar image jayess  ( 2021-01-13 22:45:45 -0500 )edit

Yeah wow, that is a great idea. I wish I thought of a fair few hours ago... sometimes I overlook the most obvious solutions, thanks for the input.

TheMilkman92 gravatar image TheMilkman92  ( 2021-01-13 23:19:42 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-01-13 20:20:51 -0500

Rufus gravatar image

updated 2021-01-13 20:22:49 -0500

ros::spinOnce (and by extension ros::spin()) is single threaded by default. Your callbacks are handled one by one when ros::spinOnce is called so if your callback takes 3 seconds, ros::spinOnce will be stuck for 3 seconds before proceeding (which also means it other callbacks cannot run). You can use the MultiThreadedSpinner or AsyncSpinner if you want to avoid that, but that obviously means you'll have to do proper thread synchronization in your callbacks. You may also want to tune your subscriber queue size if your data is arriving faster than your callbacks can process.

More information: http://wiki.ros.org/roscpp/Overview/C...

There are also many questions here and on stackoverflow regarding ROS's AsyncSpinner that you can search for.

edit flag offensive delete link more

Comments

Yeah okay, interesting. I was looking into potentially using asyncSpinner but it didn't seem like there was much in the way of functionality past having threaded callbacks.

Would another solution to this be starting a boost thread that can get interrupted by additional data that might be useful? For example if the IMU notices that robot is being lifted the current thread can be interrupted, the wheels can stop, can start a different sequence or something like that?

Sorry if this seems like a silly question, don't have much experience when it comes to threading etc.

TheMilkman92 gravatar image TheMilkman92  ( 2021-01-13 20:42:50 -0500 )edit
1

I don't think you have much control over the threads that run the callbacks (the ones created by asyncSpinner). Common practice is to have callbacks save / set the latest state (which should be quick and hardly blocking), and have another thread that checks the state and does the required computation / actuation.

Rufus gravatar image Rufus  ( 2021-01-13 22:53:38 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2021-01-13 16:11:28 -0500

Seen: 306 times

Last updated: Jan 13 '21