aerostack2 / aerostack2

Aerostack2 is a ROS 2 framework developed to create autonomous multi-aerial-robots systems in an easy and powerful way.
https://aerostack2.github.io/
BSD 3-Clause "New" or "Revised" License
133 stars 25 forks source link

[aerostack2] How to use as2_core launch params utils #511

Closed RPS98 closed 1 month ago

RPS98 commented 3 months ago

Feature Description

In Aerostack2, we use YAML configuration files to pass parameters to the nodes. To facilitate parameter configuration, utilities have been developed that allow setting parameters individually.

Adding launch utilities allows users to set parameters via a configuration file and through parameters, following this order:

  1. Default configuration file in the package config folder.
  2. Custom configuration file specified in the launch command (e.g., config_file:=path/to/file.yaml).
  3. Custom parameters specified in the launch command (e.g., param:=100).

So that, the launcher can be called with:

Implementation Considerations

The following code shows how to use the launch utilities in a launch file:

import as2_core.launch_param_utils as as2_utils

# Generate the launch description
[
  # Declare launch arguments
  DeclareLaunchArgument(...),

  # Declare config file argument and parameter in file arguments
  *as2_utils.declare_launch_arguments(
      'config_file_param_name',
      default_value=custom_config_file_path,
      description='Your description'
  ),

  # Declare the node
  Node(
    package='pkg_name',
    executable='pkg_name_node',
    parameters=[
        # Add configuration file parameter to the node
        *as2_utils.launch_configuration('config_file', default_value=custom_config_file),
        {
            # Rest of the parameters
            'param_name': LaunchConfiguration('param_name'),
        }
    ]
  )
]

Example:

import os
from ament_index_python.packages import get_package_share_directory
import as2_core.launch_param_utils as as2
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration

def generate_launch_description():
    """Entrypoint."""
    # Get default platform configuration file
    package_folder = get_package_share_directory('pkg_name')
    custom_config_file = os.path.join(package_folder, 'config/custom_config_file.yaml')
    return LaunchDescription([
        DeclareLaunchArgument('use_sim_time', default_value='false'),
        *as2.declare_launch_arguments(
            'config_file',
            default_value=custom_config_file,
            description='Configuration file'
        ),
        Node(
            package='pkg_name',
            executable='pkg_name_node',
            parameters=[
                *as2.launch_configuration('config_file', default_value=custom_config_file),
                {
                    'use_sim_time': LaunchConfiguration('use_sim_time'),
                }
            ]
        )
    ])

Note: Use os.path.join rather than PathJoinSubstitution to avoid issues with show args. _Note: When declaring a string which content is a number, must use: " 'numbervalue' " Note: When declaring a launch argument, if adding two config files with the same parameter name, the parameter will retain the value from the first declaration.

You can test the launch file with the following command: ros2 launch pkg_name launch_file_name.py -s

The -sargument will show the launch file arguments and their default values. The config file path argument and parameter in file arguments should be shown in the output.

Current implementation in Aerostack2

Current aerostack2 packages that have been migrated to ament_lint_auto:

External packages:

RPS98 commented 1 month ago

Done in https://github.com/aerostack2/aerostack2/pull/611