What does callbacks means?what is its purpose
How it is related to threading?
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
asked 2020-05-01 07:42:36 -0500
Anonymous
How it is related to threading?
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.
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 .
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.
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.
Asked: 2020-05-01 07:42:36 -0500
Seen: 843 times
Last updated: May 01 '20
What is the recommended embedded board for ROS2?
[ROS2, rclpy] sharing global var between nodes
[Nav2] Why use Voxel layer over 2D layer for RGBD sensor?
Frames are moving away from global frame
Where do I initiate comms in a ros2 hardware interface?
xacro processing in ROS 2 YAML launch files (or XML)?
Synchronizing different nodes on different machines