How are topics and nodes connected?

asked 2021-03-11 10:07:38 -0600

Victor_Kash gravatar image

I'm pretty new to ROS programming, This question might seem silly but can someone please clear this one for me? It is really bothering me.

simple_publisher = rospy.Publisher('topic_1', String, queue_size = 10)

the line above creates a publisher object which takes string type messages for transfer under the topic_1 name

rospy.init_node('node_1', anonymous = False)

Initialized the node for publisher

topic1_content = "my first ROS topic"

have my content for transfer ready

while not rospy.is_shutdown():
        simple_publisher.publish(topic1_content)

In this bit how does the program know that the topic1_content that was published using simple_publisher is from the node_1. How is node_1 getting connected to topic_1?

Can we only make 1 node per program i.e. we can only run rospy.init_node only once?

edit retag flag offensive close merge delete

Comments

Yes. A node is a process that performs computation,It is an executable program running inside your application. (Source: ROS Wiki). So yes, strictly speaking a program can only have a node.

Next in your program:

This Line:

rospy.init_node('node_1', anonymous = False)

Specify:

The program is creating and initializing a node call 'node 1'.

This line:

simple_publisher = rospy.Publisher('topic_1', String, queue_size = 10)

Specify:

The program/node will create a communication line known as a topic which is named as 'topic 1'.

this line:

simple_publisher.publish(topic1_content)

Specify:

In the topic there is a message in it. The message is 'topic1_content'.

Hope it clarify a bit.

loguna gravatar image loguna  ( 2021-03-18 23:37:53 -0600 )edit