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

load yaml with code

asked 2014-05-26 06:50:01 -0600

crpizarr gravatar image

updated 2014-05-26 06:51:20 -0600

Is there a way to load a .yaml file in python directly to the parameter server? I want to do something like "rosparam load", but programmatically, as a callback for an event in a GUI.

Thanks in advance

edit retag flag offensive close merge delete


This is duplicate of , there is an answer there.

lucasw gravatar image lucasw  ( 2015-05-26 18:40:47 -0600 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2014-05-26 08:22:37 -0600

ahendrix gravatar image

You should be able to load a YAML file into a python data structure using the pyyaml module.

You can then use rospy.set_param('/prefix', data) to push the entire contents of your data structure to a prefix on the parameter server.

edit flag offensive delete link more


It's not the same as doing "rosparam load" . I've tried what you propose, but it does not make the namespace structure as "rosparam load" does. I mean, with "rosparam load file.yaml" I load all the namespaces in the file, that is, the data I can access is just the leaves of the structure. and all the internal nodes in the file are loaded as namespaces. With rospy.set_param, I just set the "/prefix" with a dictionary, so I can't access the data with rospy.get_param("/prefix/namespace1/namespace2").

crpizarr gravatar image crpizarr  ( 2014-05-26 09:29:48 -0600 )edit

That's a bit surprising. What sort of structure/namespaces do you get on the parameter server after uploading a dict with set_param? It might be worth adding what you've tried to your question, particularly a small piece of python and the resulting `rosparam list /prefix` and `rosparam get /prefix`

ahendrix gravatar image ahendrix  ( 2014-05-26 10:32:40 -0600 )edit

answered 2014-05-27 04:50:22 -0600

crpizarr gravatar image

updated 2014-05-27 05:03:38 -0600

In response to @ahendrix, this an use-case

the data: "asdf.yaml"

    namespace1: {data1: 1, data2: 2}
    namespace2: {data3: 3, data4: 4.5}

the use-case:

> rosparam load asdf.yaml main
> rosparam list
 ...and other ros stuff

> rosparam get main/namespace1/data1

In an IPython console

> data = rosparam.load_file("asdf.yaml")
[({'main': {'namespace1': {'data1': 1, 'data2': 2}, 'namespace2': {'data3': 3, 'data4': 4.5}}},'/')]
# The parameter server is not touched, it just returns a list
> rospy.set_param("main", data)
> rospy.get_param("main")
   [[{'namespace1': {'data1': 1, 'data2': 2}, 'namespace2': {'data3': 3, 'data4': 4.5}}, '/']]

Outside, in another terminal:

> rosparam list
> rosparam get main/namespace1/data1
   ERROR: Parameter [/main/namespace1/data1] is not set

So it doesn't load the namespace structure I think.

edit flag offensive delete link more


Does this work if you load your yaml file using the python yaml module, as I suggested?

ahendrix gravatar image ahendrix  ( 2014-05-27 06:54:10 -0600 )edit

Oh, sorry, i overlooked that. With the yaml module it does work. Thanks. By the way, what is the utility of the rosparam.load_file method, if it is not to load a file into the parameter server.?

crpizarr gravatar image crpizarr  ( 2014-05-27 10:30:29 -0600 )edit

This function does what I expected it to do:

def load_params_from_yaml(complete_file_path):
    from rosparam import upload_params
    from yaml import load
    f = open(full_path, 'r')
    yamlfile = load(f)
    upload_params('/', yamlfile)
awesomebytes gravatar image awesomebytes  ( 2014-10-21 05:02:01 -0600 )edit

answered 2014-05-26 07:10:32 -0600

130s gravatar image

Have you tried rosparam set with -t option?

It's possible from .launch file as well (example).

edit flag offensive delete link more


That is not what I'm looking for. What I want to do is something like this: rosparam.load_something("filename.yaml") and then I want to see the parameters of filename.yaml in the parameter server. Currently the rosparam.load_file method just prints the content of the file in stdout, without touching the parameter server.

crpizarr gravatar image crpizarr  ( 2014-05-26 08:12:09 -0600 )edit

Question Tools

1 follower


Asked: 2014-05-26 06:50:01 -0600

Seen: 11,423 times

Last updated: May 27 '14