ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to publish and subscribe to the dictionary?

asked 2022-05-11 03:49:45 -0500

Redhwan gravatar image

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)]}

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2022-05-11 04:35:28 -0500

aarsh_t gravatar image

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.

edit flag offensive delete link more
0

answered 2022-05-11 04:36:37 -0500

ljaniec gravatar image

updated 2022-05-11 04:45:53 -0500

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_...

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! :)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-05-11 03:49:45 -0500

Seen: 540 times

Last updated: May 11 '22