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.
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.
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.