About the need for the first slash in the subscriber name and publisher name

Do you need the first slash?

When creating a ROS publisher or subscriber If you're a publisher, "What name do you want to publish?" If you're a subscriber, you're bound to decide what you want to subscribe to.

In code

pub = rospy.Publisher("pub_name", Image, queue_size=1)
sub = rospy.Subscriber("sub_name", Image, img_cb)

It is the part corresponding to "pub_name" and "sub_name" of.

At this time, the publisher name and subscriber name may or may not have a slash at the beginning, what is the meaning? I was thinking. "/pub_name" "pub_name" "/sub_name" "sub_name" It looks like this I didn't know what it was for because the behavior doesn't change with or without it, but it turned out just recently.

Conclusion

When calling and launching with launch, it is possible to add a namespace to the publisher name and subscriber name, but I was asked whether to reflect this or not. If you add a slash, the namespace will not be applied. Conversely, the namespace is reflected only if you do not add a slash.

Concrete example

To explain with a concrete example,

hoge.py


#!/usr/bin/env python
#coding: utf-8
import rospy
from sensor_msgs.msg import Image

def img1_cb(a):
	print "1"
def img2_cb(b):
	print "2"

if __name__ == '__main__':
	rospy.init_node('hoge')	#node creation

	pub1 = rospy.Publisher('/pub_name1', Image, queue_size=1)
	pub2 = rospy.Publisher('pub_name2', Image, queue_size=1)
	sub1 = rospy.Subscriber("/sub_name1", Image, img1_cb)
	sub2 = rospy.Subscriber("sub_name2", Image, img2_cb)

	while not rospy.is_shutdown():	
		rospy.spin()

Create two publishers and two subscribers.

And in the launch file

hoge.launch


<launch>
	<group ns="namespace">
		<node pkg="tttest" name="new_name" type="hoge.py" />
	</group>
</launch>

Describe what was Start with namespace as namespace and node name as new_name. And this

roslaunch test hoge.launch

Start with. To see what name you are publishing and subscribing to

rosnode info /namespace/new_name

And check it,

Node [/name_space/new_name]
Publications: 
 * /pub_name1 [sensor_msgs/Image]
 * /rosout [rosgraph_msgs/Log]
 * /name_space/pub_name2 [sensor_msgs/Image]

Subscriptions: 
 * /name_space/sub_name2 [unknown type]
 * 
 * /sub_name1 [unknown type]

(Partial excerpt). The one with the slash is the same as the name set in hoge.py. If you don't add a slash, you can see that the namespace set in hoge.launch is in the head.

It was refreshing to understand what I had been wondering for a long time. I hope the questions of those who are similarly wondering will be cleared.

Recommended Posts

About the need for the first slash in the subscriber name and publisher name
MongoDB for the first time in Python
12. Save the first column in col1.txt and the second column in col2.txt
About the difference between "==" and "is" in python
Register a task in cron for the first time
For the first time, I learned about Unix (Linux).
Summary of stumbling blocks in Django for the first time
Need for __name__ == "__main__" as seen in circular import problems
Search for variables in pandas.DataFrame and get the corresponding row.
Kaggle for the first time (kaggle ①)
Kaguru for the first time
About "for _ in range ():" in python
The story of returning to the front line for the first time in 5 years and refactoring Python Django
Search the file name including the specified word and extension in the directory
About the procedure for linking Visual Studio Code for Windows and WSL
Format the Git log and get the committed file name in csv format
Need for random_state for train_test_split in sklearn
About dtypes in Python and Cython
[For self-learning] Go2 for the first time
See python for the first time
About the average option in sklearn.metrics.f1_score
Start Django for the first time
Get the host name in Python
The first step in Python Matplotlib
Replace the directory name and the file name in the directory together with a Linux command.
[Python] Sweet Is it sweet? About suites and expressions in the official documentation
python> array> Determine the number and initialize> mylist = [idx for idx in range (10)] / mylist = [0 for idx in range (10)] >> mylist = [0] * 10
The file name was bad in Python and I was addicted to import
Impressions and memorandums when working with VS code for the first time
For the first time in Numpy, I will update it from time to time
What are the "pipeline" and "{...}" in the Jenkins Pipeline pipeline {...} (for Groovy beginners, for experienced languages)
A useful note when using Python for the first time in a while