在ROS2中launch
文件有三种格式,python
,mxl
,yaml
。其中ROS2官方推荐使用python
来编写launch
文件,原因在于python
是一种编程语言,更加灵活,可以完成许多别的特性。这里主要介绍如何使用python
来编写launch
文件。
在这里我们使用前面教程中(【ROS2笔记五】ROS2服务通信)已经创建好的工作空间colcon_test03_ws
和功能包example_service_rclcpp
作为基础。
首先我们新建一个功能包用于存放launch
文件
cd ~/colcon_test03_ws/src
ros2 pkg create robot_startup --build-type ament_cmake
然后新建一个launch
目录,接着touch
一个launch.py
文件
mkdir -p src/robot_startup/launch
touch src/robot_startup/launch/example_start.launch.py
首先,我们需要import
两个库,分别用于对launch
文件内容进行描述和声明节点的位置
# 导入库
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
"""launch内容描述函数,由ros2 launch 扫描调用"""
node1 = Node(
package="example_service_rclcpp",
executable="service_server_01"
)
node2 = Node(
package="example_service_rclcpp",
executable="service_client_01"
)
# 创建LaunchDescription对象launch_description,用于描述launch文件
launch_description = LaunchDescription(
[node1, node2])
# 返回让ROS2根据launch描述执行节点
return launch_description
注意,这里函数的名字必须为generate_launch_description
,ROS2会对该函数的名字进行识别。
我们需要配置CMakeLists.txt
来让这个功能包robot_startup
中的launch
文件拷贝到工作空间colcon_test03_ws
中的install
目录中,我们使用的是ament_camke
类型的功能包,所以这里直接配置CMakeListst.txt
来进行拷贝。
CMakeLists.txt
...
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
# >>> add these tow lines <<<
install(DIRECTORY launch
DESTINATION share/${PROJECT_NAME})
ament_package()
因为我们这里启动了别的功能包example_service_rclcpp
的节点,所以需要将别的功能包也添加到搜索路径中
CMakeLists.txt
# find dependencies
find_package(ament_cmake REQUIRED)
# >>> add this line <<<
find_package(example_service_rclcpp REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package( REQUIRED)
package.xml
<buildtool_depend>ament_cmakebuildtool_depend>
<depend>example_service_rclcppdepend>
<test_depend>ament_lint_autotest_depend>
<test_depend>ament_lint_commontest_depend>
然后我们就能够进行编译了,
cd ~/colcon_test03_ws
colcon build --packages-select robot_startup
等待编译完成,我们可以在colcon_test03_ws/launch/robot_startup/share
目录下看到拷贝过去的.launch.py
文件,然后我们直接可以启动launch文件了
source install/setup.bash
ros2 launch robot_startup example_startup.launch.py
运行的结果如下:
sjh@sjh:~/colcon_test03_ws$ ros2 launch robot_startup example_start.launch.py
[INFO] [launch]: All log files can be found below /home/sjh/.ros/log/2024-04-23-09-48-15-916564-tclsjh-343849
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [service_server_01-1]: process started with pid [343857]
[INFO] [service_client_01-2]: process started with pid [343859]
[service_server_01-1] [INFO] [1713836896.557137503] [service_server_01]: Node: service_server_01 has been launched
[service_client_01-2] [INFO] [1713836896.557686664] [service_client_01]: Node: service_client_01 has been launched
[service_client_01-2] [INFO] [1713836896.560214629] [service_client_01]: Calculate 5 + 6 + 7
[service_server_01-1] [INFO] [1713836896.561889115] [service_server_01]: Recieve a: 5 b: 6 c: 7
[service_client_01-2] [INFO] [1713836896.562288840] [service_client_01]: Result: 18
可以看到,我们成功启动了两个节点。
[1]d2lros2
[2]Creating a launch file