Ask Your Question
1

Please explain how the Subscriber works

asked 2019-03-20 09:34:44 -0600

Yehor gravatar image

Hello,

Could someone explain to me how the rospy.Subscriber('topic', msg, callback) method works. As I noticed we don't need to loop that method to update and check the topic iteratively. It is doing it by itself, and I can't understand how.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
6

answered 2019-03-20 10:29:02 -0600

updated 2019-03-20 10:39:18 -0600

The ROS python library runs additional threads to handle message subscription and publishing. So when you call rospy.Subscribe in your code, it doesn't check for incoming messages and execute callbacks itself. It simply adds the topic and function to a list. This list is regularly checked in a different thread which executes the callbacks as needed. This is why you don't need any polling in your code to make the message callbacks work.

Hope this makes sense.

edit flag offensive delete link more

Comments

So, whenever ros topic will be updated, the Subscriber will get it and launch the callback function? Without any looping.

Yehor gravatar imageYehor ( 2019-03-20 12:50:11 -0600 )edit

Strictly speaking topics are not updated, because they have no state, they are channels for messages to be passed through. So think of a message being received as opposed to an update. But you are correct, the Subscriber object will make sure the callback is executed when a message is received. You don't need any loops in your code to make this happen, there are loops but they are hidden with rospy so you don't need to worry about them.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2019-03-21 06:53:20 -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

2 followers

Stats

Asked: 2019-03-20 09:34:44 -0600

Seen: 59 times

Last updated: Mar 20