ROS学习笔记-2: 编写服务端与客户端

定义srv服务
roscd beginner_tutorials
mkdir srv
cd srv
touch AddTwoInts.srv
rosed beginner_tutorials AddTwoInts.srv (使用rosed需要安装vim)
srv文件分为请求和响应两部分,由'---‘分隔。在AddTwoInts.srv中输入代码
int64 A
int64 B
---
int64 Sum
打开文件rosed beginner_tutorials package.xml,增加依赖,
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
打开文件rosed beginner_tutorials CMakeLists.txt,增加依赖,
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)
add_service_files(
   FILES
   AddTwoInts.srv
)
generate_messages(
  DEPENDENCIES
  std_msgs
)
cd ~/catkin_ws
catkin_make
rossrv show beginner_tutorials/AddTwoInts (检查服务)

编写服务端节点
roscd beginner_tutorials/scripts
touch add_two_ints_server.py
chmod +x add_two_ints_server.py
rosed beginner_tutorials add_two_ints_server.py

在add_two_ints_server.py输入:

#!/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()


cd ~/catkin_ws/
catkin_make
rosrun beginner_tutorials add_two_ints_server.py (新终端)
rosservice list (列出服务)
rosservice args /add_two_ints  (查看服务类型) (出错)
rosservice call /add_two_ints 1 2 (调用服务) (出错)

编写客户端节点
roscd beginner_tutorials/scripts
touch add_two_ints_client.py
chmod +x add_two_ints_client.py
rosed beginner_tutorials add_two_ints_client.py
在add_two_ints_client.py输入:
#!/usr/bin/env python
import sys
import os
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))


cd ~/catkin_ws/
catkin_make
rosrun beginner_tutorials add_two_ints_client.py 4 5
编写客户端节点
roscd beginner_tutorials/scripts
touch add_two_ints_client.py

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值