ファイル名

ファイル名.launch.py

テンプレート

from ast import arguments
from http.server import executable
import os
from struct import pack
from tkinter.font import names
from matplotlib import container
import yaml
import launch
import datetime
from launch import LaunchDescription
from ament_index_python.packages import get_package_share_directory
from launch.actions import IncludeLaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import ComposableNodeContainer
from launch_ros.actions import Node
from launch_ros.descriptions import ComposableNode

robot_ns = "ER"#ロボットのname space

# dt_now = datetime.datetime.now()
# rosbag_filename = "rosbag/" + dt_now.strftime('%m_%d_%H_%M_%S')

def generate_launch_description():
    pkg_dir = get_package_share_directory("パッケージ名")

    list = [
        #他のlaunchファイルの実行例
         IncludeLaunchDescription(
            PythonLaunchDescriptionSource([os.path.join(
                get_package_share_directory('launchファイルのあるパッケージ名'), 'launch'), '/launch名.launch.py']),
        ),
        #rviz
        Node(
            package='rviz2',
            namespace='',
            executable='rviz2',
            name='rviz2',
            arguments=['-d' + os.path.join(pkg_dir, 'rviz', 'main.rviz')],
        ),

        #ノード例
        Node(
            package='パッケージ名',
            executable='実行ファイル名',
            namespace=robot_ns,
            #各種設定
            output='screen',#ノードの標準出力を表示する場合 デフォルトだと標準出力はコンソールではなく、ログファイルに出力される
            respawn = True,
            prefix='xterm -e',#ノードを起動するときに別のターミナルを立ち上げる(事前にxtermをインストールしておく必要)
            required='true',#該当ノードが終了した場合に、launchファイルにある他の全てのノードを停止させる
            respawn='true',該当ノードが終了した場合に、再起動するようにする
            #yamlを使用する場合以下のように記載
            parameters=[os.path.join(pkg_dir, "config", "パラメータ名_param.yaml")],
            # parameters=None,#パラメータなしの場合
        #LRFのTF例
        #Node(#古い書き方
        #    package = "tf2_ros",
        #    executable = "static_transform_publisher",
        #    arguments = ["0", "0", "0", "1.570796327", "3.141592", "0", "base_link", "laser"]
        #    # [x,y,z,roll,pitch,yaw,親フレーム名,子フレーム名]
        #),
        Node(
             package='tf2_ros',
             executable='static_transform_publisher',
             arguments = ['--x', '0.0', '--y', '-0.3078', '--z', '0.6', '--yaw', '1.570796', '--pitch', '0.0', '--roll', '0.0', '--frame-id', 'base_link', '--child-frame-id', 'laser']
        ),

    ]

    return LaunchDescription(list)

参考サイト

【ROS2】launchファイルを作成して使うまでの流れ【Python】