我试图通过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.msg、custom_msg3.msg和custom_msg4.msg
我想知道我是否正确使用了float消息以及自定义消息。 输出如下:
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'
我不知道这里出了什么事
如果要订阅具有五种不同消息类型的五个不同主题,最好的方法是使用五个不同的回调函数,一个用于发布自定义消息的publisher1,一个用于发布自定义消息的publisher2,一个用于发布自定义消息的publisher3,一个用于发布自定义消息的publisher4,以及一个用于发布float64消息的float_publisher。
在回调函数中,参数数据基本上是一个包含消息头中声明的所有组件的结构。您得到的错误是因为正在使用custom_msg4类型的数据调用回调函数,它正在其中查找名为Float64的组件。
如果您希望使用一个函数,则必须使用If-else
区分不同的订户。
相关问题 更多 >
编程相关推荐