实验目的:
编写三个launch文件;
第一个实现启动两个乌龟模拟器节点;
第二个实现启动发布者节点和订阅者节点;
第三个实现启动前两个launch文件。
1、C++工作空间的launch文件
本文C++工作空间基于前文所创建的工作空间:
ROS2 话题通信——自定义消息类型(C++)-CSDN博客
ROS2 服务通信——自定义服务接口(C++)-CSDN博客
ROS2 动作通信——自定义动作接口(C++)-CSDN博客
在该工作空间下src目录中创建launch_pkg功能包
ros2 pkg create launch_pkg --build-type ament_cmake --dependencies rclcpp
编辑CMakeLists.txt文件,添加以下内容
install(DIRECTORY launch DESTINATION share/${PROJECT_NAME})
在launch_pkg功能包目录中创建一个luanch文件夹,存放三个launch文件,分别新建三个launch文件_launch.py、node_launch.py、include_launch.py(这三个luanch文件名后缀最好以_launch.py结尾)
在_launch.py中实现启动两个乌龟模拟器节点
from launch import LaunchDescription
from launch_ros.actions import Node
#封装终端指令相关类
#from launch.actions import ExecuteProcess
#from launch.substitutions import FindExecutable
#参数声明与获取
#from launch.actions import DeclareLaunchArgument
#from launch.substitutions import LaunchConfiguration
#文件包含相关
#from launch.actions import IncludeLaunchDescription
#from launch.launch_description_sources import PythonLaunchDescriptionSource
#分组相关
#from launch_ros.actions import PushRosNamespace
#from launch.actions import GroupAction"
#事件相关
#from launch.event_handlers import OnProcessStart, OnProcessExit
#from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
#获取功能包下share目录路径
#from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
turtle1 = Node(package="turtlesim", executable="turtlesim_node", name="t1")
turtle2 = Node(package="turtlesim", executable="turtlesim_node", name="t2")
return LaunchDescription([turtle1, turtle2])
在node_launch.py中实现启动发布者节点和订阅者节点
'''
Node里面的参数:
executable: 可执行程序
package: 被执行程序所属的功能包
name: 节点名称
namespace: 设置命名空间
exec_name: 设置程序标签
parameters: 设置参数
remappings: 实现话题重映射
arguments: 为节点传参
ros_arguments: 为节点传参
'''
from launch import LaunchDescription
from launch_ros.actions import Node
#封装终端指令相关类
#from launch.actions import ExecuteProcess
#from launch.substitutions import FindExecutable
#参数声明与获取
#from launch.actions import DeclareLaunchArgument
#from launch.substitutions import LaunchConfiguration
#文件包含相关
#from launch.actions import IncludeLaunchDescription
#from launch.launch_description_sources import PythonLaunchDescriptionSource
#分组相关
#from launch_ros.actions import PushRosNamespace
#from launch.actions import GroupAction"
#事件相关
#from launch.event_handlers import OnProcessStart, OnProcessExit
#from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
#获取功能包下share目录路径
#from ament_index_python.packages import get_package_share_directory
'''
功能:启动多个节点
'''
def generate_launch_description():
turtle1 = Node(package="topic_pkg", #功能包名称
executable="topic_interface_pub", #节点的可执行程序
name="t1") #命名节点名字
turtle2 = Node(package="topic_pkg", #功能包名称
executable="topic_interface_sub", #节点的可执行程序
name="t2") #命名节点名字
return LaunchDescription([turtle1, turtle2]) #自动生成launch文件的函数
在include_launch.py中实现启动前两个launch文件
from launch import LaunchDescription
from launch_ros.actions import Node
#封装终端指令相关类
#from launch.actions import ExecuteProcess
#from launch.substitutions import FindExecutable
#参数声明与获取
#from launch.actions import DeclareLaunchArgument
#from launch.substitutions import LaunchConfiguration
#文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
#分组相关
#from launch_ros.actions import PushRosNamespace
#from launch.actions import GroupAction"
#事件相关
#from launch.event_handlers import OnProcessStart, OnProcessExit
#from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
#获取功能包下share目录路径
#from ament_index_python.packages import get_package_share_directory
'''
功能: 当前launch文件包含其他launch文件
'''
from ament_index_python import get_package_share_directory
import os
def generate_launch_description():
include_launch1 = IncludeLaunchDescription(
launch_description_source= PythonLaunchDescriptionSource(
launch_file_path=os.path.join(
get_package_share_directory("launch_pkg"),#功能包名称
"launch/",#需要运行的launch文件的目录
"_launch.py"#需要运行的launch文件
)
)
)
include_launch2 = IncludeLaunchDescription(
launch_description_source= PythonLaunchDescriptionSource(
launch_file_path=os.path.join(
get_package_share_directory("launch_pkg"),#功能包名称
"launch/",#需要运行的launch文件的目录
"node_launch.py"#需要运行的launch文件
)
)
)
return LaunchDescription([include_launch1,include_launch2])
编译一下launch_pkg功能包,可以看到在install目录中看到编译生成的对应文件,在include_launch.py中由于要实现启动前两个launch文件,所以需要特别注意蓝色框中的地址一定要对应上
colcon build --packages-select launch_pkg
source install/setup.bash
运行_launch.py文件
ros2 launch launch_pkg _launch.py
运行node_launch.py文件
ros2 launch launch_pkg node_launch.py
运行include_launch.py文件
ros2 launch launch_pkg include_launch.py
通过测试,实验成功。
2、Python工作空间的launch文件
本文C++工作空间基于前文所创建的工作空间:
ROS2 话题通信——自定义消息类型(Python)-CSDN博客
ROS2 服务通信——自定义服务接口(Python)-CSDN博客
ROS2 动作通信——自定义动作接口(Python)-CSDN博客
在该工作空间下src目录中创建launch_pkg功能包
ros2 pkg create launch_pkg --build-type ament_python --dependencies rclpy
编辑setup.py文件
from setuptools import find_packages, setup
from glob import glob
package_name = 'launch_pkg'
setup(
name=package_name,
version='0.0.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
#launch文件相关配置路径下所有文件
('share/' + package_name, glob("launch/*_launch.py"))
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='saw',
maintainer_email='saw@todo.todo',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
],
},
)
在launch_pkg功能包目录中创建一个luanch文件夹,存放三个launch文件,分别新建三个launch文件_launch.py、node_launch.py、include_launch.py(这三个luanch文件名后缀最好以_launch.py结尾)
在_launch.py中实现启动两个乌龟模拟器节点
from launch import LaunchDescription
from launch_ros.actions import Node
#封装终端指令相关类
#from launch.actions import ExecuteProcess
#from launch.substitutions import FindExecutable
#参数声明与获取
#from launch.actions import DeclareLaunchArgument
#from launch.substitutions import LaunchConfiguration
#文件包含相关
#from launch.actions import IncludeLaunchDescription
#from launch.launch_description_sources import PythonLaunchDescriptionSource
#分组相关
#from launch_ros.actions import PushRosNamespace
#from launch.actions import GroupAction"
#事件相关
#from launch.event_handlers import OnProcessStart, OnProcessExit
#from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
#获取功能包下share目录路径
#from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
turtle1 = Node(package="turtlesim", executable="turtlesim_node", name="t1")
turtle2 = Node(package="turtlesim", executable="turtlesim_node", name="t2")
return LaunchDescription([turtle1, turtle2])
在node_launch.py中实现启动发布者节点和订阅者节点
'''
Node里面的参数:
executable: 可执行程序
package: 被执行程序所属的功能包
name: 节点名称
namespace: 设置命名空间
exec_name: 设置程序标签
parameters: 设置参数
remappings: 实现话题重映射
arguments: 为节点传参
ros_arguments: 为节点传参
'''
from launch import LaunchDescription
from launch_ros.actions import Node
#封装终端指令相关类
#from launch.actions import ExecuteProcess
#from launch.substitutions import FindExecutable
#参数声明与获取
#from launch.actions import DeclareLaunchArgument
#from launch.substitutions import LaunchConfiguration
#文件包含相关
#from launch.actions import IncludeLaunchDescription
#from launch.launch_description_sources import PythonLaunchDescriptionSource
#分组相关
#from launch_ros.actions import PushRosNamespace
#from launch.actions import GroupAction"
#事件相关
#from launch.event_handlers import OnProcessStart, OnProcessExit
#from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
#获取功能包下share目录路径
#from ament_index_python.packages import get_package_share_directory
'''
功能:启动多个节点
'''
def generate_launch_description():
turtle1 = Node(package="topic_pkg", #功能包名称
executable="topic_interface_pub", #节点的可执行程序
name="t1") #命名节点名字
turtle2 = Node(package="topic_pkg", #功能包名称
executable="topic_interface_sub", #节点的可执行程序
name="t2") #命名节点名字
return LaunchDescription([turtle1, turtle2]) #自动生成launch文件的函数
在include_launch.py中实现启动前两个launch文件
from launch import LaunchDescription
from launch_ros.actions import Node
#封装终端指令相关类
#from launch.actions import ExecuteProcess
#from launch.substitutions import FindExecutable
#参数声明与获取
#from launch.actions import DeclareLaunchArgument
#from launch.substitutions import LaunchConfiguration
#文件包含相关
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
#分组相关
#from launch_ros.actions import PushRosNamespace
#from launch.actions import GroupAction"
#事件相关
#from launch.event_handlers import OnProcessStart, OnProcessExit
#from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
#获取功能包下share目录路径
#from ament_index_python.packages import get_package_share_directory
'''
功能: 当前launch文件包含其他launch文件
'''
from ament_index_python import get_package_share_directory
import os
def generate_launch_description():
include_launch1 = IncludeLaunchDescription(
launch_description_source= PythonLaunchDescriptionSource(
launch_file_path=os.path.join(
get_package_share_directory("launch_pkg"),#功能包名称
"./",#需要运行的launch文件的目录
"_launch.py"#需要运行的launch文件
)
)
)
include_launch2 = IncludeLaunchDescription(
launch_description_source= PythonLaunchDescriptionSource(
launch_file_path=os.path.join(
get_package_share_directory("launch_pkg"),#功能包名称
"./",#需要运行的launch文件的目录
"node_launch.py"#需要运行的launch文件
)
)
)
return LaunchDescription([include_launch1,include_launch2])
编译一下launch_pkg功能包,可以看到在install目录中看到编译生成的对应文件,在include_launch.py中由于要实现启动前两个launch文件,所以需要特别注意蓝色框中的地址一定要对应上
colcon build --packages-select launch_pkg
source install/setup.bash
运行_launch.py文件
ros2 launch launch_pkg _launch.py
运行node_launch.py文件
ros2 launch launch_pkg node_launch.py
运行include_launch.py文件
ros2 launch launch_pkg include_launch.py
通过测试,实验成功。