ros自定义srv记录

自定义srv

ros 版本:kinetic
自定义test包的文件结构如下

|-- test
|   |-- CMakeLists.txt
|   |-- srv
|   |   `-- WordCount.srv
|   |-- package.xml
|   |-- scripts
|   |   `-- sevice_server.py

srvmsg非常相似,有关自定义msg链接

1. 定义srv文件

WordCount.srv #通常大写字母开头;---上方表输入,下方表输出。

string words
---
uint32 count

2. 修改 package.xml

向其中添加如下信息,首先是<build_depend>message_generation</build_depend><exec_depend>message_runtime</exec_depend>;其次,需要使用那些包就添加进去,例如 std_msgsroscpp

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>rospy</build_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_depend>message_generation</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>roscpp</build_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>message_runtime</exec_depend>
  <exec_depend>std_msgs</exec_depend>

3. 修改 CMakeLists.txt

find_package(catkin REQUIRED COMPONENTS
  rospy
  roscpp
  std_msgs
  message_generation #放在末尾
)

添加自定义的srv文件

add_service_files(
  FILES
  WordCount.srv
)
## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  std_msgs  # Or other packages containing msgs
)
catkin_package(
#   INCLUDE_DIRS include
#   LIBRARIES test
   CATKIN_DEPENDS rospy message_runtime std_msgs roscpp
#   DEPENDS system_lib
)
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
catkin_install_python(PROGRAMS
  scripts/sevice_server.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

4. sevice_server.py

#!/usr/bin/env python

import rospy
from test.srv import WordCount, WordCountRequest, WordCountResponse

def count_words(request):
    return WordCountResponse(len(request.words.split()))

rospy.init_node("service_server")
service = rospy.Service("word_count", WordCount, count_words)

rospy.spin()

5. 运行 catkin build

创建service会生成三个类。本例会同时创建:WordCount, WordCountRequest, WordCountResponse 三个类。

测试

roscore
rosrun test sevice_server.py
rosservice info word_count

顺利的话,会出现如下结果。
在这里插入图片描述

使用 (rosservice 命令)

rosservice call word_count 'one two three four'

返回: 4

相关推荐

  1. ROS2】实现定义服务接口

    2024-02-16 08:26:01       47 阅读
  2. ROS学习笔记10——定义源文件调用

    2024-02-16 08:26:01       54 阅读
  3. 记录使用vue3定义指令

    2024-02-16 08:26:01       35 阅读
  4. 定义注解+AOP实现日志记录

    2024-02-16 08:26:01       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-16 08:26:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-16 08:26:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-16 08:26:01       82 阅读
  4. Python语言-面向对象

    2024-02-16 08:26:01       91 阅读

热门阅读

  1. Codeforces Round 925 (Div. 3)

    2024-02-16 08:26:01       62 阅读
  2. Linux常用指令总结

    2024-02-16 08:26:01       54 阅读
  3. Go语言开发小技巧&易错点100例(十二)

    2024-02-16 08:26:01       58 阅读
  4. B3638 T1 三角形面积

    2024-02-16 08:26:01       51 阅读
  5. 蓝桥杯(Web大学组)2022省赛真题:展开你的扇子

    2024-02-16 08:26:01       53 阅读
  6. C语言系列6——指针:C语言的精髓之一

    2024-02-16 08:26:01       52 阅读
  7. C++ STL:list和vector的比较

    2024-02-16 08:26:01       59 阅读
  8. 【动态规划初识】整数划分

    2024-02-16 08:26:01       53 阅读
  9. 【数据统计】A股分红率排行榜2023

    2024-02-16 08:26:01       159 阅读