缘起:
ros2 节点参数可以通过启动节点添加,也可以通过launch文件添加,还可以通过配置文件.yaml文件添加,或者是几种方式套用添加,给初学者带来很大的困惑具体是哪个生效了呢,我们拿slam_toolbox 包的online_sync_launch.py来剖析。
程序安装参考这里
(SLAM) 建图时导航 — Navigation 2 1.0.0 文档
我们这里采用源码编译安装,方便随时修改代码编译运行看效果
文件目录结构
online_sync_launch.py # 同步建图launch文件源码
import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.conditions import UnlessCondition
from launch.substitutions import LaunchConfiguration, PythonExpression
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
from nav2_common.launch import HasNodeParams
def generate_launch_description():
use_sim_time = LaunchConfiguration('use_sim_time')
params_file = LaunchConfiguration('params_file')
default_params_file = os.path.join(get_package_share_directory("slam_toolbox"),
'config', 'mapper_params_online_sync.yaml')
declare_use_sim_time_argument = DeclareLaunchArgument(
'use_sim_time',
default_value='true',
description='Use simulation/Gazebo clock')
declare_params_file_cmd = DeclareLaunchArgument(
'params_file',
default_value=default_params_file,
description='Full path to the ROS2 parameters file to use for the slam_toolbox node')
# If the provided param file doesn't have slam_toolbox params, we must pass the
# default_params_file instead. This could happen due to automatic propagation of
# LaunchArguments. See:
# https://github.com/ros-planning/navigation2/pull/2243#issuecomment-800479866
has_node_params = HasNodeParams(source_file=params_file,
node_name='slam_toolbox')
actual_params_file = PythonExpression(['"', params_file, '" if ', has_node_params,
' else "', default_params_file, '"'])
log_param_change = LogInfo(msg=['provided params_file ', params_file,
' does not contain slam_toolbox parameters. Using default: ',
default_params_file],
condition=UnlessCondition(has_node_params))
start_sync_slam_toolbox_node = Node(
parameters=[
actual