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.
Asked by Redhwan on 2022-05-12 01:04:14 UTC
Answers
You want to use a list of tuples of ints, not a float32 list - the message type is incorrect. You should create a custom message for this.
Relevant questions with some hints about use of tuples and lists in ROS messages:
Asked by ljaniec on 2022-05-12 01:51:58 UTC
Comments
A Float32MultiArray
is a ros message that wraps a multi-dimensional array. In python, given what your publisher sent, your subscriber callback msg contains a two-dimensional numpy
array. If you want the values organized in some other fashion, you need to write code to read the array and create whatever it is you want.
Asked by Mike Scheutzow on 2022-05-12 10:33:08 UTC
Comments
First of all, thank you so much for your answer. I know what you meant but I think that ROS is not designed to publish and subscribe to everything in Python for example dictionary. I am not sure about my case in this question. So, I posted my problem here asking to help.
Asked by Redhwan on 2022-05-12 19:40:01 UTC
Comments