ros2+launch命令

Launching nodes

Goal: Use a command line tool to launch multiple nodes at once.

Tutorial Level: Beginner

Time: 5 minutes

In most of the introductory tutorials, you have been opening new terminals for every new node you run. As you create more complex systems with more and more nodes running simultaneously, opening terminals and reentering configuration details becomes tedious.

Launch files allow you to start up and configure a number of executables containing ROS 2 nodes simultaneously.

Running a single launch file with the ros2 launch command will start up your entire system - all nodes and their configurations - at once.

Prerequisites

Before starting these tutorials, install ROS 2 by following the instructions on the ROS 2 Installation page.

The commands used in this tutorial assume you followed the binary packages installation guide for your operating system (Debian packages for Linux). You can still follow along if you built from source, but the path to your setup files will likely be different. You also won’t be able to use the sudo apt install ros-- command (used frequently in the beginner level tutorials) if you install from source.(如果是使用源码编译的方式安装ros2将无法使用sudo apt install ros-命令)

If you are using Linux and are not already familiar with the shell, this tutorial will help.

Tasks

Running a Launch File

Open a new terminal and run:

ros2 launch turtlesim multisim.launch.py        这里跟ros1不一样了,ros1都是.launch结尾

ros2+launch命令_第1张图片

This command will run the following launch file:

# turtlesim/launch/multisim.launch.py

from launch import LaunchDescription
import launch_ros.actions

def generate_launch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            namespace= "turtlesim1", package='turtlesim', executable='turtlesim_node', output='screen'),
        launch_ros.actions.Node(
            namespace= "turtlesim2", package='turtlesim', executable='turtlesim_node', output='screen'),
    ])

ros2+launch命令_第2张图片

Note

The launch file above is written in Python, but you can also use XML and YAML to create launch files. You can see a comparison of these different ROS 2 launch formats in ros2_launch文件教程Using Python, XML, and YAML for ROS 2 Launch Files.

This will run two turtlesim nodes:

ros2+launch命令_第3张图片

For now, don’t worry about the contents of this launch file. You can find more information on ROS 2 launch in the ros2_launch文件教程ROS 2 launch tutorials.

(Optional) Control the Turtlesim Nodes

Now that these nodes are running, you can control them like any other ROS 2 nodes. For example, you can make the turtles drive in opposite directions by opening up two additional terminals and running the following commands:

In the second terminal:

ros2 topic pub  /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}}"

ros2+launch命令_第4张图片

In the third terminal:

ros2 topic pub  /turtlesim2/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}}"

ros2+launch命令_第5张图片

After running these commands, you should see something like the following:

ros2+launch命令_第6张图片

Summary

The significance of what you’ve done so far is that you’ve run two turtlesim nodes with one command. Once you learn to write your own launch files, you’ll be able to run multiple nodes - and set up their configuration - in a similar way, with the ros2 launch command.

For more tutorials on ROS 2 launch files, see the main launch file tutorial page.

Next steps

In the next tutorial, Recording and playing back data, you’ll learn about another helpful tool, ros2 bag.

你可能感兴趣的:(ros2,ros2,ubuntu)