ros2 跟着官方教学从零开始 创建动作*action*接口
ros2 从零开始15 创建动作action接口前言之前提到过动作action这个概念我们回顾一下。动作是ROS 2中的一种通信类型旨在执行长期任务。 它们由三个部分组成目标、反馈和结果。背景之前学过动作action。与其他通信类型及其接口topics/msg 和 services/srv类似 你也可以自定义软件包中的动作。 这个教程会教你如何定义和构建一个你可以使用的动作。实践1.创建一个包进入工作区的src目录用如下指令创建我们的包ros2 pkg create --build-type ament_cmake action_tutorials_interfacesrootbc2bf85b2e4a:~/ros2_ws# cd src rootbc2bf85b2e4a:~/ros2_ws/src# ros2 pkg create --build-type ament_cmake action_tutorials_interfaces going to create a new package package name: action_tutorials_interfaces destination directory: /root/ros2_ws/src package format: 3 version: 0.0.0 description: TODO: Package description maintainer: [root ********] licenses: [TODO: License declaration] build type: ament_cmake dependencies: [] creating folder ./action_tutorials_interfaces creating ./action_tutorials_interfaces/package.xml creating source and include folder creating folder ./action_tutorials_interfaces/src creating folder ./action_tutorials_interfaces/include/action_tutorials_interfaces creating ./action_tutorials_interfaces/CMakeLists.txt [WARNING]: Unknown license TODO: License declaration. This has been set in the package.xml, but no LICENSE file has been created. It is recommended to use one of the ament license identitifers: Apache-2.0 BSL-1.0 BSD-2.0 BSD-2-Clause BSD-3-Clause GPL-3.0-only LGPL-3.0-only MIT MIT-0action_tutorials_interfaces是本次新包的名称。备注将 .msg、.srv 和 .action文件与使用它们的节点分开的是个好习惯。 这使得在不同软件包间重复使用接口定义变得更容易。2.创建自定义动作action自定义动作action定义在 .action文件中形式如下# Request --- # Result --- # Feedback动作由3部分组成由—分隔请求消息由动作客户端action client发送给动作服务器action server用来开始新目标goal。响应消息是当目标goal完成时动作服务器action server会向动作客户端action client发送的结果消息。反馈消息会定期从动作服务器action server发送到动作客户端action client更新目标goal相关的信息比如进度信息。一个动作的实体通常被称为目标goal。现在我们定义一个动作Fibonacci 用来计算“斐波那契数列”。斐波那契数列:斐波那契数列是典型的一个数列 0, 1, 1, 2, 3, 5, 8, 13… 从第3项开始当前值是前2项之和2.1 创建动作文件进入我们的包action_tutorials_interfaces并且创建action目录。cd action_tutorials_interfaces mkdir action在action目录创建一个文件Fibonacci.action 文件内容如下int32 order --- int32[] sequence --- int32[] partial_sequence解析order是我们需要计算的斐波那契数列也是目标goalsequence是我们最终得到的斐波那契数列的结果partial_sequence 是我们目前计算出来的数列2.2 修改CMakeLists.txt在编译之前我们需要修改CMakeLists.txt 把如下的内容贴在ament_package()行之前find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} action/Fibonacci.action )2.3 修改package.xml同理我们也要把如下信息贴在package.xml里面buildtool_dependrosidl_default_generators/buildtool_depend dependaction_msgs/depend member_of_grouprosidl_interface_packages/member_of_group3. 开始编译进入工作区用colcon build等待编译完成。rootbc2bf85b2e4a:/# cd ~/ros2_ws rootbc2bf85b2e4a:~/ros2_ws# colcon build --packages-select action_tutorials_interfaces Starting action_tutorials_interfaces Finished action_tutorials_interfaces [0.48s] Summary: 1 package finished [0.80s]4. 查看动作action接口编译完成后我们需要安装后才能运行使用source install/setup.sh打开一个终端输入如下命令查看接口rootbc2bf85b2e4a:~/ros2_ws# source install/setup.sh rootbc2bf85b2e4a:~/ros2_ws# ros2 interface show action_tutorials_interfaces/action/Fibonacci int32 order --- int32[] sequence --- int32[] partial_sequence如上显示ros已经识别到我们定义的动作接口Fibonacci了。总结接下来我们将写动作服务器action server和 动作客户端action client