ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to build a persistent hash?

asked 2018-02-10 16:52:27 -0500

pitosalas gravatar image

updated 2018-02-11 08:11:31 -0500

I want to have a node that subscribes to some messages and builds from that a data structure (which I would think of as a hash or dictionary). I would like the available keys not to be hardwired into the message definitions.

So calling this node the notekeeper, who subscribes to notes, I would want something like this:

note "name", "robot1" note "mood", "happy" note "emotion", 12

And then subsequently I would like other nodes using the notekeeper as a service (perhaps) asking

get_note "mood" and have it return "happy"

Do you follow? What's a good ROSish way of doing this?


My question comes from the fact that I am not sure of the process model that is involved and what kind of variable, object instance, global, survives while waiting for a subscription callback. If you store it in a dict as you recommend, where should that go, as an instance of a class created in the "main" program, in a "global" or where? Obviously it will not survive a crash of the node (or the robot).

edit retag flag offensive close merge delete


Followup - I usually use a python class like in , the dict would be created in the __init__ and the subscribe and service callbacks would be class methods. It's not more 'rosish' than any other approach but it works.

lucasw gravatar image lucasw  ( 2018-02-11 08:50:47 -0500 )edit

If you want to save and reload the data the easiest way is probably putting it into a yaml file

lucasw gravatar image lucasw  ( 2018-02-11 08:53:50 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-02-10 21:20:59 -0500

lucasw gravatar image

updated 2018-02-10 21:25:15 -0500

You can define your own key-pair message/s and a corresponding service with a key in the request and the value in the response. The simplest thing would be to store initially non-string values into a string and have the msg and srv be each two strings, the 12 from your example would be a "12".

The node would subscribe to the StringString message type and be a String service provider, it could have a python dict or C++ std::map<std::string, std::string> for the data structure.

Or just use rosparams?

edit flag offensive delete link more


Thanks! See followup in original question?

pitosalas gravatar image pitosalas  ( 2018-02-11 08:11:39 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2018-02-10 16:52:27 -0500

Seen: 321 times

Last updated: Feb 11 '18