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

What does callbacks means?what is its purpose

asked 2020-05-01 07:42:36 -0500

anonymous user

Anonymous

How it is related to threading?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-05-01 11:49:18 -0500

billy gravatar image

I'll give you a non-technical answer, but enough info to get a feel for what callbacks are and how they relate to threading. I'm sure you can get many details from google though.

Callbacks are functions that are called when a pre-define event happens in the computer. When you setup the callback you tell the computer what that event is. It could be an electrical signal on an input connector, and counter or clock signal reaching a preset value, or in the case of ROS a common use of callbacks is for receiving messages on a specific topic. In all these cases something has happened and your program needs to respond. So if you have a callback setup for a signal that arrived on a pin indicating a sensor has new data, your callback would likely communicate with that sensor to get the data it has. Deeper in the code you could say the the signal generated an interrupt and that led to the callback being called, but that applies for the case of a signal on a pin from a sensor. Callbacks can be triggered in ways other than interrupts, so I won't go deeper. In ROS, if a message has arrived on a topic, your callback should read the message and process it.

Threads are blocks of code or functions running in parallel with other functions or blocks of code from a program. A single thread program will run it's code through the processor single file. One function, then another, then another, as it works it's way through the program. On a multi-threaded program, it can fork the program into different threads and run multiple functions together, and the leave it up the operating system on how to manage it. This can be beneficial if your program has to do two things that don't need to sync in time - say read data from a temperature sensor and also play a song from memory. Those two things have nothing to do with each other so you may want to do sensor reading in one thread while playing music in another.

Threading and CallBacks - they are related in that they both involve processing blocks of code or functions. Callbacks can be called in the primary thread of a program, interrupting that main program, or they can be called on a different thread so the callback is processed while the main program continues to run. Different programming languages many handle default callbacks in different ways. So if you get to the place where you need to really understand exactly what is happening, a little research will be be required for the setup you're using.

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

edit flag offensive delete link more

Comments

Thank you very much!

anonymous userAnonymous ( 2020-05-03 23:25:51 -0500 )edit

Can I consider callbacks as thread

anonymous userAnonymous ( 2020-05-09 09:41:33 -0500 )edit

All user apps running on a PC are running on a 'thread' so I guess you can say callback run on threads. Something tells me you have a specific reason for this line of questioning. Maybe it helps if you tell us what that reason is.

billy gravatar image billy  ( 2020-05-09 18:22:00 -0500 )edit

Thanks for answering. Actually I'm new to both Operating systems and ROS. So I'm trying to correlate how Ros nodes and it's threads(nodelets) run as a usual user program on Linux OS using default scheduler(CFS) . I found information on callbacks and spinners in Ros wiki but I didn't understand it clearly . That's why I asked whether callbacks are considered as threads .

anonymous userAnonymous ( 2020-05-10 07:04:25 -0500 )edit

My understanding is that ROS is running in purely within user space so is likely being scheduled no differently than anything else. But I have no experience working under the hood with roscore or anything that is generally hidden so claim no level of expertise in this area.

billy gravatar image billy  ( 2020-05-10 13:13:07 -0500 )edit

Well thanks it means a lot

anonymous userAnonymous ( 2020-05-10 13:32:44 -0500 )edit

I gave an answer here which explains at a conceptual level how the ROS2 executor works. https://answers.ros.org/question/3527... The executor is in charge of determining which callback should be executed next. Maybe this can clarify some things for you.

MCornelis gravatar image MCornelis  ( 2020-05-20 03:20:08 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-05-01 07:42:36 -0500

Seen: 843 times

Last updated: May 01 '20