🦁

パッケージを作らなくてもlaunchファイルは起動できる

2023/02/23に公開

ROS 2ユーザにとっては常識かもしれんが、タイトルのとおりである。

demo_nodes_cppパッケージを題材に試してみる。
ホスト環境のROS 2バージョンはHumble。

https://github.com/ros2/demos/blob/humble/demo_nodes_cpp/launch/topics/talker_listener.launch.py

まずはパッケージをインストールする。
ホスト環境にパッケージ(ノード)がインストールされていること、これが重要である。

$ sudo apt install ros-humble-demo-nodes-cpp

インストールされたlaunchファイルを実行する。
talkerノードとlistenerノードが起動すればOK。

$ ros2 launch demo_nodes_cpp talker_listener.launch.py
...
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [talker-1]: process started with pid [61015]
[INFO] [listener-2]: process started with pid [61017]
[talker-1] [INFO] [1677163556.582432010] [talker]: Publishing: 'Hello World: 1'
[listener-2] [INFO] [1677163556.583082865] [listener]: I heard: [Hello World: 1]
[talker-1] [INFO] [1677163557.582413378] [talker]: Publishing: 'Hello World: 2'
[listener-2] [INFO] [1677163557.582903464] [listener]: I heard: [Hello World: 2]
[talker-1] [INFO] [1677163558.582424411] [talker]: Publishing: 'Hello World: 3'
[listener-2] [INFO] [1677163558.582930395] [listener]: I heard: [Hello World: 3]
[talker-1] [INFO] [1677163559.582417178] [talker]: Publishing: 'Hello World: 4'
[listener-2] [INFO] [1677163559.582898810] [listener]: I heard: [Hello World: 4]
...

次にlaunchファイルをダウンロードして実行してみる。

# launchファイルをダウンロード
$ wget https://raw.githubusercontent.com/ros2/demos/humble/demo_nodes_cpp/launch/topics/talker_listener.launch.py

# ダウンロードしたlaunchファイルを実行する
$ ros2 launch talker_listener.launch.py 
...
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [talker-1]: process started with pid [61297]
[INFO] [listener-2]: process started with pid [61299]
[talker-1] [INFO] [1677163706.549943015] [talker]: Publishing: 'Hello World: 1'
[listener-2] [INFO] [1677163706.550545398] [listener]: I heard: [Hello World: 1]
[talker-1] [INFO] [1677163707.549904752] [talker]: Publishing: 'Hello World: 2'
[listener-2] [INFO] [1677163707.550312881] [listener]: I heard: [Hello World: 2]
[talker-1] [INFO] [1677163708.549889189] [talker]: Publishing: 'Hello World: 3'
[listener-2] [INFO] [1677163708.550244356] [listener]: I heard: [Hello World: 3]
...

起動した。
せっかくなのでlaunchファイルを編集し、ノード名を変えてみる。

def generate_launch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            package='demo_nodes_cpp', executable='talker', output='screen', name='TALKERRRRRRRRRRRRRRRR'),
        launch_ros.actions.Node(
            package='demo_nodes_cpp', executable='listener', output='screen', name='LISTENERRRRRRRRRRRRR'),
    ])

実行してみよう。

$ ros2 launch talker_listener.launch.py
...
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [talker-1]: process started with pid [61535]
[INFO] [listener-2]: process started with pid [61537]
[talker-1] [INFO] [1677163908.151252825] [TALKERRRRRRRRRRRRRRRR]: Publishing: 'Hello World: 1'
[listener-2] [INFO] [1677163908.151861623] [LISTENERRRRRRRRRRRRR]: I heard: [Hello World: 1]
[talker-1] [INFO] [1677163909.151221814] [TALKERRRRRRRRRRRRRRRR]: Publishing: 'Hello World: 2'
[listener-2] [INFO] [1677163909.151701643] [LISTENERRRRRRRRRRRRR]: I heard: [Hello World: 2]
[talker-1] [INFO] [1677163910.151224149] [TALKERRRRRRRRRRRRRRRR]: Publishing: 'Hello World: 3'
[listener-2] [INFO] [1677163910.151695869] [LISTENERRRRRRRRRRRRR]: I heard: [Hello World: 3]
[talker-1] [INFO] [1677163911.151162914] [TALKERRRRRRRRRRRRRRRR]: Publishing: 'Hello World: 4'
[listener-2] [INFO] [1677163911.151640109] [LISTENERRRRRRRRRRRRR]: I heard: [Hello World: 4]

ノード名が変更されているのでヨシっ。

以上!

Discussion