创建用户自定义msg和srv文件

目录

创建用户自定义msg和srv文件

一、程序编写

1、创建软件包

 2、创建msg消息和srv服务文件夹

3、定义msg消息示例

 4、定义srv服务示例

 5、修改和添加编译选项

6、软件包设置

二、程序测试

1、编译软件包

2、测试查询信息

 3、测试查询服务


·        上一讲回顾,我们使用C++实现编写简单的服务器和客户端。里面使用消息定义和服务定义都是示例中的定义。本章节讲解我们如何自定义自己专用的消息和服务。废话不多说,开启代码之旅。

#include <std_msgs/msg/string.hpp>
#include <example_interfaces/srv/add_two_ints.hpp>

一、程序编写

1、创建软件包

ros2 pkg create --build-type ament_cmake tutorial_interfaces

 2、创建msg消息和srv服务文件夹

        在软件包中创建两个文件夹:msg消息和srv服务文件夹

mkdir msg srv

3、定义msg消息示例

src/tutorial_interfaces/msg/Num.msg
int64   num

 4、定义srv服务示例

src/tutorial_interfaces/srv/AddThreeInts.srv
int64 a
int64 b
int64 c
---
int64 sum

 5、修改和添加编译选项

cmake_minimum_required(VERSION 3.8)
project(tutorial_interfaces)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
    "msg/Num.msg"
    "srv/AddThreeInts.srv"
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

6、软件包设置

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>tutorial_interfaces</name>
  <version>0.0.0</version>
  <description>defined msg and srv</description>
  <maintainer email="motion_gui@126.com">gmotion</maintainer>
  <license>Apache License 2.0</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <build_depend>rosidl_default_generators</build_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

二、程序测试

1、编译软件包

colcon build --packages-select tutorial_inter

2、测试查询信息

. install/setup.bash
ros2 interface show tutorial_interfaces/msg/Num

 3、测试查询服务

. install/setup.bash
ros2 interface show tutorial_interfaces/srv/AddThreeInts 

4、其他软件包引用问题

        需要在对应的软件包引用此软件包。软件包引用后才能头文件包含目录

<depend>tutorial_interfaces</depend>

         消息和服务正确发布出来,这章节就不做带入代码编程,在下一章节中进行叙述。谢谢大家的阅读,下一章节我们再会。谢谢大家。。。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐