ROS 2 tutorial (entry level): create a launch file

Goals Create a launch file to run a complex ROS 2 system.

background knowledge

Until this tutorial, you've tried running new nodes by opening new terminals. As you create more and more complex systems with more and more nodes that need to run simultaneously, it becomes tiresome to open multiple terminals and re-enter settings.

Launch files allow you to start and configure multiple multi-execution ROS 2 nodes simultaneously.

Use ros2 launchthe command to run a launch file to start your entire system at once, including all nodes and their configuration files.

prerequisite

This tutorial will use the rqt_graph and turtlesim packages .

You will also use a text editor.

Best of all, don't forget to source your ROS 2 on each newly opened terminal .

Task

1. Settings

Create a folder to store your launch files:

mkdir launch

turtlesim_mimic_launch.pyCreate a file called launch by entering the following command in the terminal :

touch launch/turtlesim_mimic_launch.py

You can also use the graphical interface to create a new file through the system folder.

Open this new file with your favorite text editor.

2. Write the launch file

Let's turtlesimmake up a ROS 2 launch file with the package and its executable.

Copy all the code below and paste it into turtlesim_mimic_launch.pythe file:

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='turtlesim',
            namespace='turtlesim1',
            executable='turtlesim_node',
            name='sim'
        ),
        Node(
            package='turtlesim',
            namespace='turtlesim2',
            executable='turtlesim_node',
            name='sim'
        ),
        Node(
            package='turtlesim',
            executable='mimic',
            name='mimic',
            remappings=[
                ('/input/pose', '/turtlesim1/turtle1/pose'),
                ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
            ]
        )
    ])

2.1 Check the launch file

These import statements introduce some Python launchmodules.

from launch import LaunchDescription
from launch_ros.actions import Node

What starts next is the description of the launch file.

def generate_launch_description():
   return LaunchDescription([

   ])

Inside LaunchDescriptionis a system composed of three nodes, all of which are from turtlesimthe package. The goal of this system is to start two turtlesim windows and have one turtle mimic the movement of the other.

The first two actions of the launch description file are to start two turtlesim windows:

Node(
    package='turtlesim',
    namespace='turtlesim1',
    executable='turtlesim_node',
    name='sim'
),
Node(
    package='turtlesim',
    namespace='turtlesim2',
    executable='turtlesim_node',
    name='sim'
),

Note that the only difference between the two nodes is their namespace values. Different namespaces allow two simulators to be started in the system, and their node names and topic names will not conflict.

Two turtles in this system receive commands via the same topic and publish their poses via the same topic. Without a unique namespace, there is no way to distinguish which turtle these messages are for.

The last node is also from turtlesimthe package, but it's a different executable: mimic:

Node(
    package='turtlesim',
    executable='mimic',
    name='mimic',
    remappings=[
      ('/input/pose', '/turtlesim1/turtle1/pose'),
      ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
    ]
)

This node has added configuration details through remapping.

mimicThe topic of /input/poseis remapped to /turtlesim1/turtle1/poseand its /output/cmd_veltopic is remapped to /turtlesim2/turtle1/cmd_vel. This means that mimicwill subscribe /turtlesim1/simto the pose topic, and republish it to /turtlesim2/simthe velocity command's subscribed topic. That is, the movement turtlesim2that will be imitated turtlesim1.

3. ros2 launch

To start turtlesim_mimic_launch.py, go to the folder you created earlier and run the following command:

cd launch
ros2 launch turtlesim_mimic_launch.py

Remark

It can run launch directly as we did above, or it can be started by a package. When it is provided by a certain package, it can be executed with the following syntax:

ros2 launch <package_name> <launch_file_name>

You can learn how to create packages with this tutorial .

Two turtlesim windows will open, and at the same time, you will see the following [INFO]message telling you which node has started:

[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [turtlesim_node-1]: process started with pid [11714]
[INFO] [turtlesim_node-2]: process started with pid [11715]
[INFO] [mimic-3]: process started with pid [11716]

To see the system in action, open a new terminal and run this thread ros2 topic pubto /turtlesim1/turtle1/cmd_velget the first turtle moving:

ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"

You will see both turtles moving along the same path.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-v0SjpFfN-1628648460396)(https://docs.ros.org/en/foxy/_images/mimic.png )]

4. Use rqt_graph to view the internal operation of the system

While the system is still running, open a new terminal and run rqt_graphto get a better understanding of the relationships between the nodes in your launch file.

Run the command below:

rqt_graph

insert image description here

A hidden node ( ros2 topic pubgenerated by you running the command) is publishing data to /turtlesim1/turtle1/cmd_velthe topic on the left, which is /turtlesim1/simsubscribed by this node. The rest of the diagram shows what was described before: mimicSubscribing /turtlesim1/simto the Pose topic, then publishing to /turtlesim2/simthe Velocity Command topic.

Summarize

The launch file simplifies running complex systems with multiple nodes and specific configuration details. You can use Python to create startup files and then ros2 launchrun them through commands.

To learn more about ROS 2, you can follow me or subscribe to my column: ROS 2 column .

Guess you like

Origin blog.csdn.net/weixin_42499608/article/details/119596932