Search code examples
rosros2

How to launch a node with a parameter in ROS2?


Migrating ros1 package to ros2 and couldn't figure how to launch with a paramter in ros2.

With ros1 I have a launch file that refers to a config file and from cpp code I use node.getParam

launch file:

<launch>
    <arg name="node_name" default="collector" />
    <arg name="config_file" default="" />
    <node name="$(arg node_name)" pkg="collector" type="collector" respawn="true">
      <rosparam if="$(eval config_file!='')" command="load" file="$(arg config_file)"/>
    </node>
</launch>

config file:

my_param: 5

cpp code:

double my_param = 0;
n.getParam("my_param", my_param);

My question is how would that translate to ROS2?


Solution

  • For ROS2, please see this link and this link.

    For reference, as of this post, ROS2 has had three releases. While Crystal Clemmens supports Python nodes, the previous release, Bouncy Bolson, does not. Similarly, future releases may add additional support for how/where to input config files.

    The ROS2 Wiki references using a .yaml config file to store parameters and, when running the ROS2 node, passing the config file as a command line parameter.

    ROS2 Wiki: Node Arguments

    Example demo_params.yaml file:

    talker:
        ros__parameters:
            some_int: 42
            a_string: "Hello world"
            some_lists:
                some_integers: [1, 2, 3, 4]
                some_doubles : [3.14, 2.718]`
    

    Then run the following:

    ros2 run demo_nodes_cpp talker __params:=demo_params.yaml