ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
A dictionary is a concept supported by Python but not by C++ or ROS so you cannot add a dictionary directly to a message. However you can use a standard data definition language such as JSON and transport that data using a std_msgs/String message. This would have the advantage of being human readable if you need to debug the messages in transit too.
In python you could pack or unpack the JSON as below:
import json
encoded_data_string = json.dumps({... your dictionary ...})
loaded_dictionary = json.loads(encoded_data_string)
In a C++ node you could use the boost JSON module to read, store and write the data. The example below fills a boost::property tree object with JSON from a file, but could be adapted to read JSON from a std_msgs/String message.
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
// Short alias for this namespace
namespace pt = boost::property_tree;
// Create a root
pt::ptree root;
// Load the json file in this ptree
pt::read_json("filename.json", root);
Hope this helps.