【ROS2笔记八】ROS2编写Python launch 文件

【ROS2笔记八】ROS2编写Python launch 文件

文章目录

  • 【ROS2笔记八】ROS2编写Python launch 文件
    • 1.创建功能包和launch文件
    • 2.编写Python的launch文件
    • 3.将launch文件拷贝到工作空间下
    • 4.编译与测试
    • Reference

在ROS2中launch文件有三种格式,pythonmxlyaml。其中ROS2官方推荐使用python来编写launch文件,原因在于python是一种编程语言,更加灵活,可以完成许多别的特性。这里主要介绍如何使用python来编写launch文件。

1.创建功能包和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

2.编写Python的launch文件

首先,我们需要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会对该函数的名字进行识别。

3.将launch文件拷贝到工作空间下

我们需要配置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>

4.编译与测试

然后我们就能够进行编译了,

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

可以看到,我们成功启动了两个节点。

Reference

[1]d2lros2

[2]Creating a launch file

你可能感兴趣的:(#,ROS2,笔记,python,开发语言)