Robotics StackExchange | Archived questions

[Ros2] load a .yaml file in cpp program

In ROS2, Is there a way to load a .yaml file in cpp program to set the parameters into node.

Currentlly (in ROS2) we can load .yaml file either commandLine i.e. ros2 run pkg_name nodename __params:=:<path>/test.yaml Or through python launch pkg as below.

yaml file test.yaml:

nodename:
  ros__parameters:
    name: aaa
    type: bbb
    params:
      param1: 10.0
      param2: xyz

.launch.py file test.launch.py:

def generate_launch_description():
    parameters_file_dir = pathlib.Path(__file__).resolve().parent
    parameters_file_path = parameters_file_dir / 'test.yaml'
    os.environ['FILE_PATH'] = str(parameters_file_dir)

    test_node = launch_ros.actions.Node(
            package='pkg_name', node_executable='file_executable_name', node_name='nodename,
            output='screen',
            parameters=[
                parameters_file_path,
                str(parameters_file_path),
                [EnvironmentVariable(name='FILE_PATH'), os.sep, 'test.yaml'],
            ],
        )

    return LaunchDescription([
        test_node,
        launch.actions.RegisterEventHandler(
            event_handler=launch.event_handlers.OnProcessExit(
                target_action=test_node,
                on_exit=[launch.actions.EmitEvent(event=launch.events.Shutdown())],
            )),
]) 

I am looking similar way to load .yaml in cpp program to get/set perameters in a particular node?

Asked by rohit_agrawal1 on 2019-01-28 03:56:38 UTC

Comments

Answers

This git issue addresses this feature.

There's no direct solution but you can use the rcl parser and map function to read the yaml in and reset the parameters. For an example code the ZED2 ROS2 node uses this method. See the file zed-ros2-wrapper/zed_wrapper/src/zed_wrapper_container.cpp. I haven't fully gone through that code, but they definitely use the rcl parser to read in parameters from a yaml programatically.

Asked by batty on 2020-07-19 14:28:27 UTC

Comments