I am trying to subscribe to a 4 different publishers in ROS through python. I use the following code :
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!")
where custom_msg1.msg contains custom_string1 defined as string and in the same way custom_msg2.msg, custom_msg3.msg and custom_msg4.msg
I want know whether I have used the float message and also the custom messages correctly. Output is as follows:
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>
Ending with an error:
rospy.loginfo (" float_publisher value %f ", data.Float64)
AttributeError: 'Custom_msg4' object has no attribute 'Float64'
I don't know what is going wrong here
If you want to subscribe to five different topics with five different message types the best way to do it is to use five different callback functions, e.g.:
publisher1that publishescustom_msg1publisher2that publishescustom_msg2publisher3that publishescustom_msg3publisher4that publishescustom_msg4float_publisherthat publishesfloat64messages.In the call back function the parameter data is basically a structure that has all of the components that are declared in the message header. The error that you are getting is because the call back function is being called with data of type
custom_msg4and it is looking for a component calledFloat64in it.