0
$\begingroup$

I have tried all the solutions I've seen on forums but none seems to work, I've spent already several days on this and really hope someone can spot my mistake rapidly.

I have the following launch file (reduced to the minimum) my_launch_file.launch.py:

import os
from launch import LaunchDescription
from launch.substitutions import LaunchConfiguration
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import Node

def generate_launch_description():
    param_file = os.path.join(get_package_share_directory('my_pkg'), 'config/my_params.yaml')

    return LaunchDescription([
        Node(
            package="my_pkg",
            executable="my_node",
            parameters=[LaunchConfiguration(param_file)],
        )
    ])

And then I have a parameters file named my_params.yaml:

my_node:
  ros__parameters:
    some_int: 42
    some_bool: true

I built my package, verified that the launch file and node is built correctly, and that the my_params.yaml file is at the correct location. However, when I launch my file, I get:

user@user-PC:~$ ros2 launch my_pkg my_launch_file.launch.py 
[INFO] [launch]: All log files can be found below /home/user/.ros/log/2023-09-18-15-07-27-467850-user-PC
[INFO] [launch]: Default logging verbosity is set to INFO
[ERROR] [launch]: Caught exception in launch (see debug for traceback): launch configuration '/home/user/ros2_ws/install/my_pkg/share/my_pkg/config/my_params.yaml' does not exist

I confirm again that the my_params.yaml file is there, I verified every single character of the path. I also confirm that I sourced the setup.bash from /opt/ros/humble, and the one from ~/ros2_ws/install. Also, the node works and launches correctly if I don't add the parameters file.

Can't find why this is happening, please help! Thanks in advance!

Setup: ROS2 Humble, Ubuntu 22.04


EDIT: I modified the launch file by removing the LaunchConfiguration. Now it looks like this:

import os
from launch import LaunchDescription
from launch.substitutions import LaunchConfiguration
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import Node

def generate_launch_description():
    param_file = os.path.join(get_package_share_directory('my_pkg'), 'config/my_params.yaml')

    return LaunchDescription([

        Node(
            package="my_pkg",
            executable="my_node",
            parameters=[param_file],
        )
    ])

This way, there is no more error in the terminal, but when I do ros2 param list /my_node, I get no parameters, so it still doesn't work.

$\endgroup$

1 Answer 1

2
$\begingroup$

So answering my own question,

To make the parameters available to the node, they first need to be declared inside the node.

Hence, the node should be something like:

class MyNodeClass(Node):
    """
    A node example
    """

    def __init__(self):
        super().__init__("my_node")
        self.declare_parameters(
        namespace='',
        parameters=[
            ('some_int', 18),
            ('some_bool', False),
        ])
        ...
        ...
        ...

Once they get declared here in the node, then the launch file takes the parameters in the yaml file into account. Now when I launch my launch file, and then in another terminal do ros2 param get /my_node some_int, I get the answer Integer value is: 42, Which shows the yaml file value overwrote the one in the node.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.