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

Programatically load yaml config file to the Parameter Server in Python

asked 2013-03-21 01:26:41 -0600

updated 2023-06-16 11:58:16 -0600

How should I do that? I've looked up along the roslaunch and dynamic reconfigure code but I don't find it. Do you know any standard function to do it?

By the momment I've done this (but I understand that should exist another way):

config = yaml.load(open(custom_configuration_file))
rospy.loginfo(config)
self.recursive_set_param(config)
...

def recursive_set_param(self, config, key_path=""):
        for k in config.keys():
            data = config[k]

            full_qualified_key = key_path + "/" + k
            if data.__class__ == dict:
                self.recursive_set_param(data, full_qualified_key)
            else:
                rospy.set_param(full_qualified_key, config[k])
                rospy.loginfo("SETTING PARAM %s -> %s", full_qualified_key, data)
edit retag flag offensive close merge delete

Comments

Any reason why you want to do that? There might be simpler ways to achieve your goal.

dornhege gravatar image dornhege  ( 2013-03-21 01:48:58 -0600 )edit

For sure! and that is what I was looking for!

Pablo Iñigo Blasco gravatar image Pablo Iñigo Blasco  ( 2013-03-27 09:48:15 -0600 )edit

3 Answers

Sort by » oldest newest most voted
4

answered 2013-03-27 09:54:00 -0600

Following the guidelines of dornhege, this looks the code that it is needed to load a yaml file dynamically (tested in Fuerte).

I firstly expected to be able to use the rosparam.yamlmain method. But it is not possible because it is programed using as argument sys.args.

The below for loop looks like it could be avoided using some method, but if such method exist I haven't found it.

import roslib
roslib.load_manifest("rosparam")
import rosparam

paramlist=rosparam.load_file("/path/to/myfile",default_namespace="my_namespace")
for params, ns in paramlist:
    rosparam.upload_params(ns,params)
edit flag offensive delete link more
3

answered 2013-03-21 01:48:09 -0600

dornhege gravatar image

If you are doing python, you could just use rosparam and reuse its load functionality.

Just check out the basic script in your distro. You should be able to import the function in your script and thus do exactly the same as rosparam load does.

edit flag offensive delete link more

Comments

I've followed your guidelines and I found how to do it with the rosparam API. I've added another response to this answer with the code. I expected to find a simple method to import the yaml file, but at the end a few lines of code where needed. Thanks! +1

Pablo Iñigo Blasco gravatar image Pablo Iñigo Blasco  ( 2013-03-27 09:56:35 -0600 )edit

By the way. One of the problems of using this dynamic loading approach is that the nodes which uses the dynamic reconfigure server don't notice the change. This is related with this question: http://answers.ros.org/question/58820/dynamic-reconfigure-server-how-explicity-fetch-parameters/

Pablo Iñigo Blasco gravatar image Pablo Iñigo Blasco  ( 2013-03-27 09:58:26 -0600 )edit
1

answered 2013-03-21 02:12:20 -0600

Miquel Massot gravatar image

You can load the yaml file in a launchfile and then, in the node, get the needed params. No parsing needed.

Add to your launchfile a

<rosparam file="path/to/your/file.yaml" />

and in that yamlfile, structure it like this:

node_name1:
  param_name1: value
  param_name2: value

node_name2:
  param_name1: value
  param_name2: value
edit flag offensive delete link more

Comments

yeah but that forces you to load the configuration file through a launch file. In my case, my node wants to load at runtime and proactively a configuration file (without the human intervention).

Pablo Iñigo Blasco gravatar image Pablo Iñigo Blasco  ( 2013-03-27 09:17:17 -0600 )edit

but how can we get the param_name1's value in cpp files?

neil gravatar image neil  ( 2017-08-02 01:25:57 -0600 )edit
knxa gravatar image knxa  ( 2017-09-04 09:06:20 -0600 )edit

Question Tools

Stats

Asked: 2013-03-21 01:26:41 -0600

Seen: 8,091 times

Last updated: Jun 16 '23