ROS中launch文件的参数与节点通信的关系

首先定义一个节点,可以发布一个话题

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被成功订阅

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值