Home » ROS2入门教程 » ROS2入门教程-创建launch文件

ROS2入门教程-创建launch文件

ROS2入门教程-创建launch文件

说明:

  • 介绍如何创建launch文件

步骤:

  • 新建目录
mkdir ~/luanch 
  • 新建文件

    cd ~/luanch
    touch turtlesim_mimic_launch.py

  • 内容如下:

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'),
            ]
        )
    ])
  • 运行:
cd ~/luanch 
ros2 luanch turtlesim_mimic_launch.py

[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]
  • 发布话题
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}}"
  • 效果:

请输入图片描述

  • rqt_graph查看节点调用

请输入图片描述

代码解析

  • 导入launch和node包
from launch import LaunchDescription
from launch_ros.actions import Node
  • 定义launch描述函数
def generate_launch_description():
   return LaunchDescription([

   ])
  • 调用两个节点打开两个turtlesim
Node(
    package='turtlesim',
    namespace='turtlesim1',
    executable='turtlesim_node',
    name='sim'
),
Node(
    package='turtlesim',
    namespace='turtlesim2',
    executable='turtlesim_node',
    name='sim'
),
  • 调用第三个节点,turtlesim2跟随turtlesim1的位置变化而变化
Node(
    package='turtlesim',
    executable='mimic',
    name='mimic',
    remappings=[
      ('/input/pose', '/turtlesim1/turtle1/pose'),
      ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
    ]
)

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros2入门教程