How to publish and subscribe to the dictionary?
I am trying to publish and subscribe to a dictionary similar to a list but I can't.
The list is [1.1,2,3,4,5]
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 = [1.1,2,3,4,5]
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 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()
How can do it with the dictionary?
The dictionary is {u'person': [(360, 196, 558, 460), (184, 183, 342, 477)]}
Asked by Redhwan on 2022-05-11 03:49:45 UTC
Answers
Publishing dictionary is not supported by ROS as it is a Python only concept. But someone has really worked on that and created this package. You can also have look on similar question.
Asked by aarsh_t on 2022-05-11 04:35:28 UTC
Comments
You can use a really nice package called rospy_message_converter
:
Converts between Python dictionaries and JSON to ROS messages.
from there:
https://github.com/uos/rospy_message_converter
From the documentation:
Convert a dictionary to a ROS message
from rospy_message_converter import message_converter from std_msgs.msg import String dictionary = { 'data': 'Howdy' } message = message_converter.convert_dictionary_to_ros_message('std_msgs/String', dictionary)
Convert a ROS message to a dictionary
from rospy_message_converter import message_converter from std_msgs.msg import String message = String(data = 'Howdy') dictionary = message_converter.convert_ros_message_to_dictionary(message)
P.S. The main author is also very active on ROS Answers! :)
Asked by ljaniec on 2022-05-11 04:36:37 UTC
Comments