ROS2 dashing launch 文件的编写与执行

获取包的位置:
from ament_index_python.packages import get_package_share_directory
get_package_share_directory('bag')

获取包中文件路径:
import os
os.path.join(
            get_package_share_directory('bag'),
            'map',
            'map.yaml'))
 
定义参数1:
  from launch import LaunchDescription
  use_sim_time = LaunchConfiguration('use_sim_time', default='false')
  
  定义参数2:(可以不要)
  from launch.actions import DeclareLaunchArgument
  需要写在 return LaunchDescription()中
  DeclareLaunchArgument(
            'map',
            default_value=map_dir,
            description='Full path to map file to load'),

包含其它launch文件并设置launch文件中的参数,需要有定义参数1的设置:
from launch.launch_description_sources import PythonLaunchDescriptionSource
IncludeLaunchDescription(
            PythonLaunchDescriptionSource([nav2_launch_file_dir, '/nav2_bringup_launch.py']),
            launch_arguments={
                'map': map_dir,
                'use_sim_time': use_sim_time,
                'params': param_dir}.items(),
        ),
        
Node的使用:
from launch_ros.actions import Node
from launch.conditions import IfCondition(可以控制Node是否开启)
 Node(
            package='rviz2',
            node_executable='rviz2',
            node_name='rviz2',
            arguments=['-d', rviz_config_dir],
            parameters=[{'use_sim_time': use_sim_time,
            					'num': 10	}],
            condition=IfCondition(LaunchConfiguration('userviz')),
            output='screen'),


写完launch文件后需要在CMakeLists文件中添加:
```yaml
install(DIRECTORY launch
        DESTINATION share/${PROJECT_NAME})

每次更改launch后需要colcon build一下。


你可能感兴趣的:(ros2)