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

How to launch a node with a parameter in ROS2?

asked 2018-12-24 15:23:33 -0500

ezra gravatar image

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?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2019-04-24 16:29:13 -0500

vadbut gravatar image

The way I do it:

from launch import LaunchDescription
from launch.substitutions import EnvironmentVariable
import os
import launch_ros.actions
import pathlib

parameters_file_name = 'default.yaml'

def generate_launch_description():
    parameters_file_path = str(pathlib.Path(__file__).parents[1]) # get current path and go one level up
    parameters_file_path += '/config/' + parameters_file_name
    print(parameters_file_path)
    return LaunchDescription([
        launch_ros.actions.Node(
            package='example_pkg',
            node_executable='example_node',
            output='screen',
            parameters=[
                parameters_file_path
            ],
         ),
    ])

config file in the /config folder:

 example_node:
        ros__parameters:
            some_int: 41
            log_severity: 1 #0-debug, 1-info, 2-Warn, 3-Error, 4-Fatal
            a_string: "Hello world"
            some_lists:
                some_integers: [1, 2, 3, 4]
                some_doubles : [3.14, 2.718]

in the cpp file:

ExampleNode::get_parameter_or("log_severity", severity_level , 0);
edit flag offensive delete link more
0

answered 2020-03-16 08:38:19 -0500

Loy gravatar image

Apparently, specifying a launch file can be a bit easier.

You can use

from ament_index_python.packages import get_package_share_directory
from pathlib import Path

parameters_file_path = Path(get_package_share_directory('the_package'), 'config', 'example_param.yaml')

and pass that to the Node as described above.

edit flag offensive delete link more
1

answered 2020-06-28 23:19:05 -0500

Rufus gravatar image

updated 2020-06-29 04:21:07 -0500

Alternative to parameters=[parameters_file_path], one can specify the parameters directly with

parameters=[
    {"param1": "value1"},
    {"param2": "value2"}
]

as stated here

edit flag offensive delete link more

Question Tools

4 followers

Stats

Asked: 2018-12-24 15:23:33 -0500

Seen: 6,358 times

Last updated: Mar 16 '20