How to publish and subscribe to the list consisting of many tuples?
I tried to publish and subscribe to the list of tuples. Unfortunately, I can't.
My code for slave or talker is:
#!/usr/bin/env python
import rospy
from std_msgs.msg import Float32MultiArray
def talker():
pub = rospy.Publisher('chatter', Float32MultiArray, queue_size=1)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
hello_str = [(360, 196, 558, 460), (184, 183, 342, 477)]
array = Float32MultiArray(data=hello_str)
# print 'gggggggggggggggggggggggg', type(array)
rospy.loginfo(array)
pub.publish(array)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
The output is :
[INFO] [1652334923.612873]: layout:
dim: []
data_offset: 0
data:
- [360, 196, 558, 460]
- [184, 183, 342, 477]
[INFO] [1652334923.713173]: layout:
dim: []
data_offset: 0
data:
- [360, 196, 558, 460]
- [184, 183, 342, 477]
[INFO] [1652334923.813342]: layout:
dim: []
data_offset: 0
data:
- [360, 196, 558, 460]
- [184, 183, 342, 477]
[INFO] [1652334923.913261]: layout:
dim: []
data_offset: 0
data:
- [360, 196, 558, 460]
- [184, 183, 342, 477]
[INFO] [1652334924.013226]: layout:
dim: []
data_offset: 0
data:
- [360, 196, 558, 460]
- [184, 183, 342, 477]
I need the output will be:
data : [(360, 196, 558, 460), (184, 183, 342, 477)]
The code for slave or listener is:
#!/usr/bin/env python
import rospy
from std_msgs.msg import Float32MultiArray
def callback(data):
# rospy.loginfo(data.data)
a = list (data.data)
print (a)
def listener():
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("chatter", Float32MultiArray, callback)
rospy.spin()
if __name__ == '__main__':
listener()
No output for listener code but when I ran it the talker code stopped and give me this message:
Traceback (most recent call last):
File "/home/redhwan/learn1.py", line 20, in <module>
talker()
File "/home/redhwan/learn1.py", line 15, in talker
pub.publish(array)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 886, in publish
raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: <class 'struct.error'>: 'required argument is not a float' when writing '0'
Thank you in advance for your help.
Note: If the list like [1.1,2,3,4,5]
. It is working fine.