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

本文详细介绍了如何在ROS2中使用Python编写launch文件,包括创建功能包、编写launch.py文件、配置CMakeLists.txt以及编译和测试过程。通过实例展示了如何启动example_service_rclcpp节点并实现服务通信。
摘要由CSDN通过智能技术生成

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

在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(<dependency> REQUIRED)

package.xml

<buildtool_depend>ament_cmake</buildtool_depend>
<!-- >>> add this line <<< -->
<depend>example_service_rclcpp</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_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

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值