개발하는 핑구
article thumbnail
Published 2023. 3. 2. 13:37
[ROS2 시작하기 - 7] Launch Robotics/ROS2

ROS2 Launch file

Launch file will allow you to start many nodes from one single file. you will be able to quickly launch your application and to create different launch files for different nodes of you robot.

Create and Install a Launch File

cd ros2_ws/src/
ros2 pkg create my_robot_bringup

cd my_robot_bringup
rm -rf include/ src/
mkdir launch

In CMakeLists.txt, remove some codes and in order to install add some codes like this.

And let’s create launch file

cd launch
touch number_app.launch.py
chmod +x number_app.launch.py

number_app.launch.py

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    ld = LaunchDescription()

    number_publisher_node = Node(
        package="my_py_pkg",
        executable="number_publisher"
    )

    counter_node = Node(
        package="my_py_pkg",
        executable="number_counter"
    )

    ld.add_action(number_publisher_node)
    ld.add_action(counter_node)

    return ld

And add some lines to execute nodes from other package.

Let’s install and launch this file!

cd ~/ros2_ws
colcon build --packages-select my_robot_bringup --symlink-install
ros2 launch my_robot_bringup number_app.launch.py
ros2 node list
"
/number_counter
/number_publisher
"

Configure your Nodes in a Launch File

we can configure name, topics and parameter of node in launch file.

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    ld = LaunchDescription()

    remap_number_topic = ("number", "my_number")

    number_publisher_node = Node(
        package="my_py_pkg",
        executable="number_publisher",
        name="my_number_publisher",
        remappings=[
            remap_number_topic
        ],
        parameters=[
            {"number_to_publish": 4},
            {"publish_frequency": 5.0}
        ]
    )

    counter_node = Node(
        package="my_py_pkg",
        executable="number_counter",
        name="my_number_counter",
        remappings=[
            remap_number_topic,
            ("number_count", "my_number_count")
        ]
    )

    ld.add_action(number_publisher_node)
    ld.add_action(counter_node)

    return ld
ros2 launch my_robot_bringup number_app.launch.py

ros2 node list
"
/my_number_counter
/my_number_publisher
"

ros2 topic list
"
/my_number
/my_number_count
/parameter_events
/rosout
"

ros2 topic hz /my_number_count 
"
average rate: 4.999
	min: 0.184s max: 0.212s std dev: 0.00980s window: 7
average rate: 4.972
	min: 0.184s max: 0.212s std dev: 0.00784s window: 12
average rate: 4.993
	min: 0.184s max: 0.212s std dev: 0.00718s window: 18
"

Template for launch file

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():

    return LaunchDescription([

        Node(
            package='',
            executable='',
            name='',
            output='screen'),
        
    ])

Activity

In this activity you will practice by creating a new launch file, with the nodes that you’ve already created during this course.

Goal:

  • Start 5 “robot_news_station” nodes and 1 smartphone node.
  • Each “robot_news_station” will need a different name, and will publish "Hi, this is <robot_name> from the Robot News Station!"
  • The “smartphone” node gets all the messages from all other nodes.

Here’s the graph you should get:

So, no need to create or modify any node here. You just need to create a single launch file to start your (renamed) nodes with parameters.

Bonus point if you find the book/movie reference for all robot names! (shouldn’t be too hard)

Solution

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    ld = LaunchDescription()

    robot_names = ["Giskard", "BB8", "Daneel", "Jander", "C3PO"]
    robot_news_station_nodes = []

    for name in robot_names:
        robot_news_station_nodes.append(Node(
            package="my_py_pkg",
            executable="robot_news_station",
            name="robot_news_station_" + name.lower(),
            parameters=[{"robot_name": name}]
        ))

    smartphone_node = Node(
        package="my_py_pkg",
        executable="smartphone"
    )

    for node in robot_news_station_nodes:
        ld.add_action(node)
    ld.add_action(smartphone_node)

    return ld

Conclusion

In this section you have discovered Launch Files.

With a launch file, you can start your entire application with only one command line, in one terminal. You can add any number of nodes and fully configure them. That will make your application fully customizable in no time.

Recap:

Setup for launch files:

  • Create a new package <robot_name>_bringup (best practice).
  • Create a launch/ folder at the root of the package.
  • Configure CMakeLists.txt to install files from this launch/ folder.
  • Create any number of files you want inside the launch/ folder, ending with .launch.py.

Run a launch file:

  • After you’ve written your file, use “colcon build” to install the file.
  • Don’t forget to source your environment
  • Start the launch file with “ros2 launch <package> <name_of_the_file>

Now, you have everything you need to create a complete application, and scale it without any problem.

'Robotics > ROS2' 카테고리의 다른 글

[ROS2 시작하기 - 6] Parameter  (0) 2023.03.01
[ROS2 시작하기 - 5] Interface  (0) 2023.02.27
[ROS2 시작하기 - 4] Service  (0) 2023.02.24
[ROS2 시작하기 - 3] Topic  (0) 2023.02.23
[ROS2 시작하기 - 2] Tools  (0) 2023.02.21
profile

개발하는 핑구

@sinq

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!