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

[ROS2] How do I pass argument to launch + node or node directly

asked 2022-07-18 18:24:33 -0500

Kalmah gravatar image

updated 2022-07-19 19:30:29 -0500

Hi all, I am having trouble understanding how to pass custom arguments to my ROS2 node. Here is my ROS2 node and launch file I am trying to pass a custom parameter custom_param, the LogInfo inside LaunchDescription seems to be printing the correct string to stdout but when passing into the Node, it doesn't override the default value.

test_param.launch.py

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration, TextSubstitution


def generate_launch_description():

    custom_param_arg = DeclareLaunchArgument(
        "custom_param",
        default_value="default_param",
        description="Argument for customising param."
    )

    test_node = Node(
        package='test_param_pkg',
        node_executable='test_node.py',
        parameters=[
            {"custom_param", LaunchConfiguration("custom_param")}, # FIXME Why the passed-in arg not being overrided?
        ],
        output='screen'
    )

    return LaunchDescription(
        [
            custom_param_arg,
            test_node,
            LogInfo(msg=LaunchConfiguration('custom_param')),  # NOTE This works!
        ]
    )

test_node.py

#!/usr/bin/python3

import rclpy
from rclpy.node import Node
from rclpy.parameter import Parameter


class TestNode(Node):

    def __init__(self):
        super().__init__("test_param_node")

        self.declare_parameters(
            namespace='',
            parameters=[
                ("custom_param", "default_param_inside_node"),
            ]
        )

        print("---------------")
        print(self.get_parameter('custom_param').value) # FIXME Why the passed-in arg not being overrided?
        print("---------------")
        exit()

def main(args=None):

    rclpy.init(args=args)

    node = TestNode()
    rclpy.spin(node)

    node.destroy_node()
    rclpy.shutdown()


if __name__ == "__main__":
    main()

Launch from terminal

ros2 launch test_param_pkg test_param.launch.py custom_param:="my_very_custom_param"

Result

[INFO] [launch]: Default logging verbosity is set to INFO
/root/robot/install/test_param_pkg/share/test_param_pkg/launch/test_param.launch.py:15: UserWarning: The parameter 'node_executable' is deprecated, use 'executable' instead
  test_node = Node(
[WARNING] [launch_ros.actions.node]: Parameter file path is not a file: custom_parammy_very_custom_param
[INFO] [launch.user]: my_very_custom_param
[INFO] [test_node.py-1]: process started with pid [9494]
[test_node.py-1] ---------------
[test_node.py-1] default_node_param
[test_node.py-1] ---------------
[INFO] [test_node.py-1]: process has finished cleanly [pid 9494]
"

Didn't work.

Pass directly to ros2 node

ros2 run test_param_pkg test_node.py --ros_args custom_param:="my_very_custom_param"

Result

[WARN] [1658186444.966892251] [rcl]: Found remap rule 'custom_param:=my_very_custom_param'. This syntax is deprecated. Use '--ros-args --remap custom_param:=my_very_custom_param' instead.
---------------
default_node_param
---------------

Didn't work either.

Please help.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-07-23 08:15:50 -0500

ijnek gravatar image

In regards to passing it directly to the ros2 node, you're missing the -p to notify that this is a parameter being passed:

sh ros2 run test_param_pkg test_node.py --ros_args -p custom_param:="my_very_custom_param"

In regards to the launch file failing, the warning Parameter file path is not a file: custom_parammy_very_custom_param suggests that you entered the launch command incorrectly, try and run the same command again:

sh ros2 launch test_param_pkg test_param.launch.py custom_param:="my_very_custom_param"

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-07-18 18:24:33 -0500

Seen: 2,724 times

Last updated: Jul 23 '22