【问题标题】:ros python publisher/subscriberros python 发布者/订阅者
【发布时间】:2014-11-24 14:01:43
【问题描述】:

我正在尝试通过 python 订阅 ROS 中的 4 个不同的发布者。我使用以下代码:

def callback(data):
    rospy.loginfo (" Publisher1 Value %s ", custom_msg1.custom_string1)
    rospy.loginfo (" Publisher2 Value %s ", custom_msg2.custom_string2)
    rospy.loginfo (" Publisher3 Value %s ", custom_msg3.custom_string3)
    rospy.loginfo (" Publisher4 Value %s ", custom_msg4.custom_string4)
    rospy.loginfo (" float_publisher value %f ", data.Float64)

def python_code():
    rospy.init_node("python_code")
    rospy.Subscriber("float_publisher",Float64,callback)
    rospy.Subscriber("publisher1", custom_msg1,callback)
    rospy.Subscriber("publisher2", custom_msg2,callback)
    rospy.Subscriber("publisher3", custom_msg3,callback)
    rospy.Subscriber("publisher4", custom_msg4,callback)
    rospy.loginfo(" Test: start spinning!")
    rospy.spin()
    rospy.loginfo("node has shutdown!")

其中 custom_msg1.msg 包含定义为字符串的 custom_string1,其方式与 custom_msg2.msgcustom_msg3.msg 相同strong> 和 custom_msg4.msg

我想知道我是否正确使用了 float 消息以及 custom messages。 输出如下:

Publisher1 Value <member 'custom_string1' of 'custom_msg1' objects>
Publisher2 Value <member 'custom_string2' of 'custom_msg2' objects>
Publisher3 Value <member 'custom_string3' of 'custom_msg3' objects>
Publisher4 Value <member 'custom_string4' of 'custom_msg4' objects>

以错误结束:

rospy.loginfo (" float_publisher value %f ", data.Float64)
AttributeError: 'Custom_msg4' object has no attribute 'Float64'

我不知道这里出了什么问题

【问题讨论】:

  • @jonrsharpe 我得到一个输出,这是错误的,也是一个错误消息。
  • 您的回溯与您发布的代码不匹配。另外,请注意callback 的最后一行与前四行不太一样。
  • 我更改了代码。我使用导入函数来导入浮点数并且错过了在我发布的代码中更新它。我会更新的。

标签: python ros rospy


【解决方案1】:

如果你想用五种不同的消息类型订阅五个不同的主题,最好的方法是使用五个不同的回调函数,一个用于发布 custom_msg1 的发布者1,发布 custom_msg2 的发布者2,发布 custom_msg3 的发布者3,发布 custom_msg3 的发布者4发布 custom_msg4 和一个用于发布 float64 消息的 float_publisher。

在回调函数中,参数数据基本上是一个结构,其中包含在消息头中声明的所有组件。您得到的错误是因为使用 custom_msg4 类型的数据调用回调函数,并且它正在其中寻找一个名为 Float64 的组件。

def callback1(data):
    rospy.loginfo (" Publisher1 Value %s ", data.custom_string1)

def callback2(data):
    rospy.loginfo (" Publisher2 Value %s ", data.custom_string2)

def callback3(data):
    rospy.loginfo (" Publisher3 Value %s ", data.custom_string3)

def callback4(data):
    rospy.loginfo (" Publisher4 Value %s ", data.custom_string4)

def callback5(data):
    rospy.loginfo (" float_publisher value %f ", data.Float64)

def python_code():
    rospy.init_node("python_code")
    rospy.Subscriber("float_publisher",Float64,callback5)
    rospy.Subscriber("publisher1", custom_msg1,callback1)
    rospy.Subscriber("publisher2", custom_msg2,callback2)
    rospy.Subscriber("publisher3", custom_msg3,callback3)
    rospy.Subscriber("publisher4", custom_msg4,callback4)
    rospy.loginfo(" Test: start spinning!")
    rospy.spin()
    rospy.loginfo("node has shutdown!")

【讨论】:

    【解决方案2】:

    如果你想使用一个函数,你必须使用 if else 来

    区分不同的订阅者。

    def callback(data,who):
        if who == 5:
          rospy.loginfo (" float_publisher value %f ", data.data)
        else:
          rospy.loginfo (" Publisher%d Value %s ",who, data.data)
    
    
    def python_code():
        rospy.init_node("python_code")
        rospy.Subscriber("float_publisher",Float64,callback,5)
        rospy.Subscriber("publisher1", String,callback,1)
        rospy.Subscriber("publisher2", String,callback,2)
        rospy.Subscriber("publisher3", String,callback,3)
        rospy.Subscriber("publisher4", String,callback,4)
        rospy.loginfo(" Test: start spinning!")
        rospy.spin()
        rospy.loginfo("node has shutdown!")
    

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 2016-02-07
      • 1970-01-01
      相关资源
      最近更新 更多