• ROS1创建自定义服务并使用


    1.首先在功能包创建一个srv文件夹

    在这里插入图片描述
    如上图所示,vehicle_control是我的功能包,创建一个srv文件夹

    2.使用touch指令创建服务文件

    touch Ranging.srv
    
    • 1

    在这里插入图片描述

    3.在文件内输入服务数据

    横线代表分割符,上面的是客户端发送的数据,下面是服务器返回的数据
    在这里插入图片描述
    具体使用什么类型参考自己的项目,以及ros支持类型

    4.注册ROS

    修改package.xml
    在package.xml中添加如下代码,注意添加的位置,放在之前。(上一步自定义msg时已完成)

      <build_depend>message_generation</build_depend>
      <exec_depend>message_runtime</exec_depend>
    
    • 1
    • 2

    修改CMakeLists.txt
    在find_package中添加message_generation(已完成)

    find_package(catkin REQUIRED COMPONENTS
       roscpp
       rospy
       std_msgs
       message_generation
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    取消add_service_files的注释,并把刚刚定义的消息文件添加进去

    add_service_files(
      FILES
      AddTwoInts_alone.srv
      IsOpenyuntai.srv
      Ranging.srv
      )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在catkin_package中添加CATKIN_DEPENDS message_runtime

    catkin_package(
    #  INCLUDE_DIRS include
    #  LIBRARIES beginner_tutorials
        CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
    #  DEPENDS system_lib
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    完成以上工作后,编译整个工作空间

    cd ~/car
    catkin_make
    
    • 1
    • 2


    显示已经做好的服务
    rossrv show vehicle_control/Ranging.srv
    在这里插入图片描述
    完成
    5.服务的使用
    客户端

    import rospy
    
    # imports the AddTwoInts service 
    from rospy_tutorials.srv import *
    
    ## add two numbers using the add_two_ints service
    ## @param x int: first number to add
    ## @param y int: second number to add
    def add_two_ints_client(x, y):
    
        # NOTE: you don't have to call rospy.init_node() to make calls against
        # a service. This is because service clients do not have to be
        # nodes.
    
        # block until the add_two_ints service is available
        # you can optionally specify a timeout
        rospy.wait_for_service('add_two_ints')
        
        try:
            # create a handle to the add_two_ints service
            add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
            
            print "Requesting %s+%s"%(x, y)
            
            # simplified style
            resp1 = add_two_ints(x, y)
    
            # formal style
            resp2 = add_two_ints.call(AddTwoIntsRequest(x, y))
    
            if not resp1.sum == (x + y):
                raise Exception("test failure, returned sum was %s"%resp1.sum)
            if not resp2.sum == (x + y):
                raise Exception("test failure, returned sum was %s"%resp2.sum)
            return resp1.sum
        except rospy.ServiceException, e:
            print "Service call failed: %s"%e
    
    def usage():
        return "%s [x y]"%sys.argv[0]
    
    if __name__ == "__main__":
        
        argv = rospy.myargv()
        if len(argv) == 1:
            import random
            x = random.randint(-50000, 50000)
            y = random.randint(-50000, 50000)
        elif len(argv) == 3:
            try:
                x = int(argv[1])
                y = int(argv[2])
            except:
                print usage()
                sys.exit(1)
        else:
            print usage()
            sys.exit(1)
        print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    服务端

    #!/usr/bin/env python
    
    NAME = 'add_two_ints_server'
    
    # import the AddTwoInts service
    from beginner_tutorials.srv import *
    import rospy 
    
    def add_two_ints(req):
        print("Returning [%s + %s = %s]" % (req.a, req.b, (req.a + req.b)))
        sum = req.a + req.b
        return AddTwoIntsResponse(sum)
    
    def add_two_ints_server():
        rospy.init_node(NAME)
        s = rospy.Service('add_two_ints', AddTwoInts, add_two_ints)
        print "Ready to add Two Ints"
        # spin() keeps Python from exiting until node is shutdown
        rospy.spin()
    
    if __name__ == "__main__":
        add_two_ints_server()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    2023年五一杯数学建模B题快递需求分析问题求解全过程论文及程序
    Topsis与熵权法
    typescript17-函数可选参数
    ChatGPT专业术语及有效使用方法概述
    数据结构第三遍补充(图的应用)
    使用SqlBulkCopy,报错“从bcp客户端收到一个对colid 4无效的列长度”
    Flink 流程处理和批处理开发
    [网络篇]TCP SYN Flood Attack(洪范攻击)
    [shell,hive] 在shell脚本中将hiveSQL分离出去
    linux系统调用拦截Centos7.6(三)の下载Centos7.6内核源码
  • 原文地址:https://blog.csdn.net/weixin_43541510/article/details/134558041