【ROS2】中级-创建一个动作

发布于:2024-07-13 ⋅ 阅读:(139) ⋅ 点赞:(0)

目标:在 ROS 2 包中定义一个动作。

教程级别:中级

 时间:5 分钟

 目录

  •  背景

  •  先决条件

  •  任务

    • 1. 创建一个接口包

    • 2. 定义一个动作

    • 3. 构建动作

  •  摘要

  •  下一步

  •  相关内容

 背景

您之前已经在了解动作教程中学习了有关动作的内容。就像其他通信类型及其各自的接口(主题/msg 和服务/srv)一样,您也可以在您的包中自定义动作。本教程将向您展示如何定义和构建一个动作,您可以在下一个教程中编写的动作服务器和动作客户端中使用它。

先决条件

您应该安装 ROS 2 和 colcon。

您应该知道如何设置工作区并创建包。

记得首先配置你的 ROS 2 安装环境。

 任务

1. 创建一个接口包

mkdir -p ~/ros2_ws/src # you can reuse an existing workspace with this naming convention
cxy@ubuntu2404-cxy:~$ cd ~/ros2_ws/src
cxy@ubuntu2404-cxy:~/ros2_ws/src$ ros2 pkg create --license Apache-2.0 custom_action_interfaces
going to create a new package
package name: custom_action_interfaces
destination directory: /home/cxy/ros2_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['cxy <cxy@todo.todo>']
licenses: ['Apache-2.0']
build type: ament_cmake
dependencies: []
creating folder ./custom_action_interfaces
creating ./custom_action_interfaces/package.xml
creating source and include folder
creating folder ./custom_action_interfaces/src
creating folder ./custom_action_interfaces/include/custom_action_interfaces
creating ./custom_action_interfaces/CMakeLists.txt

2.定义一个动作

行动定义在 .action 形式的文件中:

# Request
---
# Result
---
# Feedback

一个动作定义由三个消息定义组成,它们之间用 --- 分隔。

  • 请求消息从动作客户端发送到动作服务器,以启动一个新目标。

  • 当目标完成时,操作服务器会向操作客户端发送一个结果消息。

  • 反馈消息会定期从动作服务器发送到动作客户端,以更新关于目标的信息。

一个动作的实例通常被称为一个目标。

假设我们要定义一个新的动作“Fibonacci”,用于计算斐波那契序列。

在我们的 ROS 2 包 custom_action_interfaces 中创建一个 action 目录:

cd custom_action_interfaces
mkdir action
cxy@ubuntu2404-cxy:~/ros2_ws/src/custom_action_interfaces$ cd action
cxy@ubuntu2404-cxy:~/ros2_ws/src/custom_action_interfaces/action$ gedit Fibonacci.action

在 action 目录中,创建一个名为 Fibonacci.action 的文件,内容如下:

int32 order
---
int32[] sequence
---
int32[] partial_sequence

目标请求是我们想要计算的斐波那契序列的 order ,结果是最终的 sequence ,反馈是到目前为止计算的 partial_sequence 。

3. 构建动作

在我们的代码中使用新的斐波那契操作类型之前,我们必须将定义传递给 rosidl 代码生成管道。

这是通过在 ament_package() 行之前,向我们的 CMakeLists.txt 中添加以下几行来实现的:

find_package(rosidl_default_generators REQUIRED)


rosidl_generate_interfaces(${PROJECT_NAME}
  "action/Fibonacci.action"
)
// 定义CMake的最低版本要求为3.8
cmake_minimum_required(VERSION 3.8)
// 定义项目名称为custom_action_interfaces
project(custom_action_interfaces)


// 如果编译器是GNU或Clang,添加编译选项
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()


// 查找依赖项
find_package(ament_cmake REQUIRED)
// 如果需要手动填写其他依赖项,取消以下部分的注释
// find_package(<dependency> REQUIRED)


// 查找rosidl_default_generators包
find_package(rosidl_default_generators REQUIRED)


// 生成接口
rosidl_generate_interfaces(${PROJECT_NAME}
  "action/Fibonacci.action"
)


// 如果构建测试
if(BUILD_TESTING)
  // 查找ament_lint_auto包
  find_package(ament_lint_auto REQUIRED)
  // 下一行跳过检查版权的linter
  // 当所有源文件都添加了版权和许可时,注释掉这行
  set(ament_cmake_copyright_FOUND TRUE)
  // 下一行跳过cpplint(只在git仓库中工作)
  // 当这个包在git仓库中,并且所有源文件都添加了版权和许可时,注释掉这行
  set(ament_cmake_cpplint_FOUND TRUE)
  // 查找测试依赖项
  ament_lint_auto_find_test_dependencies()
endif()


// 打包
ament_package()

我们还应该将所需的依赖项添加到我们的 package.xml 中:

<buildtool_depend>rosidl_default_generators</buildtool_depend>


<member_of_group>rosidl_interface_packages</member_of_group>
<!-- 定义XML版本为1.0 -->
<?xml version="1.0"?>
<!-- 定义XML模型的链接和模式类型 -->
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<!-- 定义包的格式为3 -->
<package format="3">
  <!-- 定义包的名称为custom_action_interfaces -->
  <name>custom_action_interfaces</name>
  <!-- 定义包的版本为0.0.0 -->
  <version>0.0.0</version>
  <!-- 定义包的描述,这里需要填写具体的描述 -->
  <description> Creating an action</description>
  <!-- 定义包的维护者和联系方式 -->
  <maintainer email="cxy@126.com">cxy</maintainer>
  <!-- 定义包的许可证为Apache-2.0 -->
  <license>Apache-2.0</license>


  <!-- 定义构建工具的依赖为ament_cmake -->
  <buildtool_depend>ament_cmake</buildtool_depend>
  
  <!-- 定义构建工具的依赖为rosidl_default_generators -->
  <buildtool_depend>rosidl_default_generators</buildtool_depend>
  <!-- 定义包为rosidl_interface_packages组的成员 -->
  <member_of_group>rosidl_interface_packages</member_of_group>


  <!-- 定义测试依赖为ament_lint_auto -->
  <test_depend>ament_lint_auto</test_depend>
  <!-- 定义测试依赖为ament_lint_common -->
  <test_depend>ament_lint_common</test_depend>


  <!-- 导出构建类型为ament_cmake -->
  <export>
    <build_type>ament_cmake</build_type>
  </export>
<!-- 结束包的定义 -->
</package>

我们现在应该能够构建包含 Fibonacci 操作定义的包:

# Change to the root of the workspace
cd ~/ros2_ws
# Build
cxy@ubuntu2404-cxy:~/ros2_ws$ colcon build --packages-select custom_action_interfaces
Starting >>> custom_action_interfaces
Finished <<< custom_action_interfaces [24.5s]                       


Summary: 1 package finished [34.3s]

我们完成了!

按照惯例,动作类型将以其包名称和单词 action 为前缀。因此,当我们要引用我们的新动作时,它将具有完整名称 custom_action_interfaces/action/Fibonacci 。

我们可以使用命令行工具检查我们的操作是否成功构建。首先导入我们的工作空间:

source install/local_setup.bash

现在检查我们的操作定义是否存在:

cxy@ubuntu2404-cxy:~/ros2_ws$ source install/local_setup.bash
cxy@ubuntu2404-cxy:~/ros2_ws$ ros2 interface show custom_action_interfaces/action/Fibonacci
int32 order
---
int32[] sequence
---
int32[] partial_sequence

‍您应该看到屏幕上打印的斐波那契操作定义。

 摘要

在本教程中,您学习了动作定义的结构。您还学习了如何使用 CMakeLists.txt 和 package.xml 正确构建新的动作接口,以及如何验证构建成功。

 下一步

接下来,让我们通过创建一个动作服务和客户端(在 Python https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Writing-an-Action-Server-Client/Py.html 或 C++ https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Writing-an-Action-Server-Client/Cpp.html 中)来利用您新定义的动作接口。

 相关内容 

有关 ROS 操作的更详细信息,请参阅设计文章https://design.ros2.org/articles/actions.html 。

详细介绍ubuntu 系统 ROS2 colcon build 参数

118637b45195fa35e8d8353fb8eb021d.png

b82a0f44143575e0d524ce8b19411325.png

详细介绍 ROS2中的 话题、服务、动作 文件内容构成

2b35acd2428a059614df57cd9cef0707.png


网站公告

今日签到

点亮在社区的每一天
去签到