[ROS2][rclpy] Load parameters from file does not work when using namespaces

asked 2020-03-24 09:42:00 -0500

S. Hoose gravatar image

As the title says, I'm currently trying to load some parameters from a parameters.yaml file which is formatted as shown below:

nodename:
    ros__parameters:
        val1: 1
        val2: 2

This parameter file is loaded to the node using a nodename.launch.py file as shown below:

    return LaunchDescription([
        Node(
            package='nodename',
            node_namespace='/',
            node_executable='nodename',
            node_name='nodename',
            output='screen',
            emulate_tty=True,
            parameters=[
                path_to_parameters,
            ],
        )
    ])

The node itself is started by setting allow_undeclared_parameters=true and automatically_declare_parameters_from_overrides=true while my code inside of the constructor of the Node loads the parameters as displayed here:

    # load parameters
    val1_default = rclpy.parameter.Parameter(
        "default_behavior",
        type_=rclpy.parameter.Parameter.Type.INTEGER,
        value=1
    )
    val1 = self.get_parameter_or("val1", val1_default).value

All this works fine as long as I don't change the nodes namespace in the launch file by e.g. setting node_namespace='robot1'. I have already played around with the name of my parameter where it is loaded in the code by e.g. applying the namespace name as a prefix. I also tried to changed the .yaml file according to the parameter file description documentation by e.g. including the namespaces name in the parameter file as described here. Unfortunately nothing of this works for me so I would be very thankful if someone could give me some advice.

Edit: I forgot to mention that I'm using ROS2 Eloquent Elusor on Mint/Ubuntu

edit retag flag offensive close merge delete

Comments

Hey, did you solve it? I am having the same problem, just with c++.

klaxalk gravatar image klaxalk  ( 2021-01-14 09:10:53 -0500 )edit

Unfortunately not, instead I kept using the "workaround" to simply define the default parameter inside the code as shown here:

# load parameters val1_default = rclpy.parameter.Parameter( "default_behavior", type_=rclpy.parameter.Parameter.Type.INTEGER, value=1 ) val1 = self.get_parameter_or("val1", val1_default).value

I'm very unhappy with this solution. If you find a better one that does not include to write an own parameterloader I would be very happy if you could share it.

S. Hoose gravatar image S. Hoose  ( 2021-01-15 03:05:35 -0500 )edit

Hey, so I was able to replicate the "normal" ROS1 behavior, meaning, parameters work for me even when the node is namespaced and when the node name changes (I hope I understood your question well and this is what you asked about). Strangely, to achieve this I do exactly the opposite of what the tutorials say --- I do not put the node_name and ros__parameters into the yaml file. Here is my "hello world": https://github.com/ctu-mrs/ros2_uav_example.

klaxalk gravatar image klaxalk  ( 2021-01-15 04:00:34 -0500 )edit

Hey, I tried your purposed solution. In python (still eloquent) it results in an instant process has died [pid 14645, exit code -6 error. But I haven't tried an other parameter loading method (e.g. using declare_parameter) than showed in my question above like you used. Nonetheless, it seems to be a problem of the reading of the YAML file since the node crashes before it is actually started (or at least before parameters are loaded).

S. Hoose gravatar image S. Hoose  ( 2021-01-19 06:42:02 -0500 )edit

I am also trying to find a way to load parameters from a .yaml file in a way that is namespace-agnostic, but not node name-agnostic. It's unfortunate this doesn't seem to be possible. The /**: wildcard mentioned in other answers like here only works if there aren't parameters for different nodes in the same .yaml file.

marshallpt gravatar image marshallpt  ( 2021-07-30 10:31:48 -0500 )edit