首先定义一个节点,可以发布一个话题
import rospy
from std_msgs.msg import String
def talker():
# 1. pub is defined as a publisher that publishes a topic called "/chatter"
pub = rospy.Publisher('chatter', String, queue_size=10)
# 2. init the node "talker"
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
hello_str = "this is the new beginning!"
pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
定义一个launch文件,里面启动四个节点,包括一个订阅者和三个发布者
<launch>
<node name="listener_with_user_data" pkg="rospy_tutorials" type="listener_with_user_data.py" output="screen" />
<node name="talker_1" pkg="rospy_tutorials" type="talker.py" args="chatter:=chatter1"/>
<node name="talker_2" pkg="rospy_tutorials" type="talker.py" args="chatter:=chatter2"/>
<node name="talker_3" pkg="rospy_tutorials" type="talker.py" args="chatter:=chatter3"/>
</launch>
这时只用之前写的那一个节点便可以得到同样的三个节点
情况一
NAME = 'listener_with_user_data'
import rospy
from std_msgs.msg import *
def callback(data, args):
if args == 1:
print("#1: I heard ")
elif args == 2:
print("#2: I heard ")
elif args == 3:
print("#3: I heard ")
else:
print("I heard [%s] with userdata [%s]" % (data.data, str(args)))
def listener_with_user_data():
# Callback arguments (aka user data) allow you to reuse the same
# callback for different topics, or they can even allow you to use
# the same callback for the same topic, but have it do something
# different based on the arguments.
rospy.Subscriber("chatter1", String, callback, 1)
rospy.Subscriber("chatter2", String, callback, 2)
rospy.Subscriber("chatter3", String, callback, 3)
rospy.init_node(NAME, anonymous=True)
rospy.spin()
if __name__ == '__main__':
try:
listener_with_user_data()
except KeyboardInterrupt:
pass
print("exiting")
节点通信情况如下:
这一个订阅者订阅了三个发布者发布的话题,而这三个话题显然是一样的
情况二
节点不规范订阅
NAME = 'listener_with_user_data'
import rospy
from std_msgs.msg import *
def callback(data, args):
if args == 1:
print("#1: I heard ")
elif args == 2:
print("#2: I heard ")
elif args == 3:
print("#3: I heard ")
else:
print("I heard [%s] with userdata [%s]" % (data.data, str(args)))
def listener_with_user_data():
# Callback arguments (aka user data) allow you to reuse the same
# callback for different topics, or they can even allow you to use
# the same callback for the same topic, but have it do something
# different based on the arguments.
rospy.Subscriber("chatter", String, callback, 1)
rospy.Subscriber("chatter", String, callback, ("chatter2", 2))
rospy.Subscriber("chatter3", String, callback, 3)
rospy.init_node(NAME, anonymous=True)
rospy.spin()
if __name__ == '__main__':
try:
listener_with_user_data()
except KeyboardInterrupt:
pass
print("exiting")
节点通信情况如下:
未能正确订阅talker_1和talker_2发布的话题
情况三
订阅者节点的程序与情况二相同,改变launch文件
<launch>
<node name="listener_with_user_data" pkg="rospy_tutorials" type="listener_with_user_data.py" output="screen" />
<node name="talker_1" pkg="rospy_tutorials" type="talker.py" args="chatter"/>
<node name="talker_2" pkg="rospy_tutorials" type="talker.py" args="chatter:=chatter2"/>
<node name="talker_3" pkg="rospy_tutorials" type="talker.py" args="chatter:=chatter3"/>
</launch>
节点通信情况如下:
话题chatter被成功订阅