ros2 rclpy Parameters are not being passed from launch file / yaml into Node

asked 2020-05-20 05:45:13 -0500

CraigH92 gravatar image

updated 2020-05-20 05:52:53 -0500

I want to pass a parameter into a Node.

My launch file looks like this:

from launch import LaunchDescription
from launch_ros.actions import Node
import launch
import pathlib

cwd = str(pathlib.Path(__file__).parents[0])
parameters_file_path = cwd + "/parameters.yaml"
print(parameters_file_path)


def generate_launch_description():
    return LaunchDescription([

        Node(
            package='my_package',
            node_namespace='test',
            node_executable='my_node',
            emulate_tty=True,
            node_name='my_node',
            parameters = [parameters_file_path]
        )
])

My yaml looks like this:

my_node:
    ros__parameters:
        other: 41

And my node:

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

class MyNode(Node):
    def __init__(self):
        super().__init__("my_node", allow_undeclared_parameters = True, automatically_declare_parameters_from_overrides = True)
        param_list = [parameter for parameter in self._parameters.values()]
        print("parameters:")
        for p in param_list:
            print("\t" + p.name + " : " + str(p.value))

def main(args = None):

    rclpy.init(args = args)


    node = MyNode()
    node.get_logger().info("Hello from my_node")

    try:
        rclpy.spin(node) 
    except:
        pass

    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

But when launched with ros2 launch -a launch/my_launch.py, the other parameter is not passed into the node. How di I do this?

This is the current output:

[INFO] [launch]: Default logging verbosity is set to INFO
launch/parameters.yaml
[INFO] [my_node-1]: process started with pid [26444]
[my_node-1] parameters:
[my_node-1]     use_sim_time : False
[my_node-1] [INFO] [test.my_node]: Hello from my_node

If this is not the propper way to do this, how should I do it? All of the tutorials online show how to pass parameters in, but never how to access them in the python ros nodes, or from the entry point def main(args = None).

How do I set / access parameters/arguments when I am launching the node from a launch file?

Thanks

Edit

I have now managed to access the parameters with the above code, but I have to launch the node using:

ros2 run my_package my_node --ros-args -p other:=5

How can I do this using the launch file instead?

edit retag flag offensive close merge delete