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

How does ROS knows which node is publishing?

asked 2020-02-26 10:09:38 -0600

John999991 gravatar image

I was looking at the beginner's Publisher node boilerplate code from the wiki and I was wondering:

How does ROS knows which node is the publisher of a message?

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

I see the creation of a chatter Topic and talker node, so, how does pub.publish knows which topic is publishing and if pub.publish doesn't need to know, how does rosmaster knows?

edit retag flag offensive close merge delete

Comments

What exactly do you want to know?

Your first question "how does ROS knows which node is the publisher" is about the client library. In this case this is rospy. Here the node is registered and also the publisher with the topic is registered. Eventually the master handles setting up the communication with nodes that have subscriptions on this topic.

Your second question "how does pub.publish know to which topic it is publishing" is more straightforward. When the publisher is created, the topic is given as an argument and publisher object is stored in the variable pub. When you call pub.publish later on, the object still knows what its topic is.

Wilco Bonestroo gravatar image Wilco Bonestroo  ( 2020-02-26 14:29:11 -0600 )edit
1

Dear Bonestroo,

If I was to create 2 nodes in the same py script:

 rospy.init_node('talker1', anonymous=True)
 rospy.init_node('talker2', anonymous=True)

with all other the same, which one would be the node publishing data?

John999991 gravatar image John999991  ( 2020-02-26 17:15:47 -0600 )edit

Hi, Now I see your question. There can be only one node per python process. According to the documentation "You can only have one node in a rospy process, so you can only call rospy.init_node() once."

Wilco Bonestroo gravatar image Wilco Bonestroo  ( 2020-02-27 03:43:19 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-02-26 10:26:02 -0600

bob-ROS gravatar image

updated 2020-02-26 10:26:29 -0600

Because you use rospy.init_node() before sending the actual message, this will communicate with the roscore of the node's existence. The publisher must have an initialized node before being able to to publish. Try removing the "rospy.init_node('talker', anonymous=True)" and it will fail to publish.

edit flag offensive delete link more

Comments

So, I cannot initialize 2 nodes from the same .py file?

John999991 gravatar image John999991  ( 2020-02-26 11:06:33 -0600 )edit

It is not intended to have multiple nodes in one rospy program https://wiki.ros.org/rospy/Overview/I...

bob-ROS gravatar image bob-ROS  ( 2020-02-26 12:41:29 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2020-02-26 10:09:38 -0600

Seen: 442 times

Last updated: Feb 26 '20