ROS入门教程 (编写简单的Service和Client (Python))

编写简单的Service和Client (Python)

Note: This tutorial assumes that you have completed the previous tutorials: 执行你的消息发布器和订阅器http://wiki.ros.org/cn/ROS/Tutorials/ExaminingPublisherSubscriber

Description: 本教程介绍如何用Python编写Service和Client节点。

1. 写入服务节点

在这里,我们将创建服务("add_two_ints_server")节点,该节点将接收两个ints并返回和。将目录更改为在早期教程中创建的beginner_tutorials 包,创建一个包:

$ roscd beginner_tutorials

请确保您已经按照上一个教程中的说明创建了本教程所需的服务,并创建了addtwoints.srv (http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv#Creating_a_srv)(请确保在链接的wiki页面顶部选择正确版本的生成工具)。

1.1 代码

Create the scripts/add_two_ints_server.py file within the beginner_tutorials package and paste the following inside it:

#!/usr/bin/env python

from beginner_tutorials.srv import *
import rospy

def handle_add_two_ints(req):
    print "Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b))
    return AddTwoIntsResponse(req.a + req.b)

def add_two_ints_server():
    rospy.init_node('add_two_ints_server')
    s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
    print "Ready to add two ints."
    rospy.spin()

if __name__ == "__main__":
    add_two_ints_server()

别忘了要让节点executable:

chmod +x scripts/add_two_ints_server.py

1.2 代码解释

现在,让我们把代码分解。

使用Rospy编写服务几乎没有什么用处。我们使用init_node()声明我们的节点,然后声明我们的服务:

s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)

这将声明一个名为 add_two_ints 服务类型的新服务 AddTwoInts 。所有请求都被传递到“handle_add_two_ints“。用AddTwoIntsRequest 实例调用 handle_add_two_ints,并返回 AddTwoIntsResponse 实例。

与订阅服务器示例一样,rospy.spin()在服务关闭之前阻止代码退出。

2. 写入客户端节点

2.1 代码

Create the scripts/add_two_ints_client.py file within the beginner_tutorials package and paste the following inside it:

#!/usr/bin/env python

import sys
import rospy
from beginner_tutorials.srv import *

def add_two_ints_client(x, y):
    rospy.wait_for_service('add_two_ints')
    try:
        add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
        resp1 = add_two_ints(x, y)
        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__":
    if len(sys.argv) == 3:
        x = int(sys.argv[1])
        y = int(sys.argv[2])
    else:
        print usage()
        sys.exit(1)
    print "Requesting %s+%s"%(x, y)
    print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))

别忘了要让节点executable:

$ chmod +x scripts/add_two_ints_client.py

2.2 代码解释

用于调用服务的客户机代码也很简单。对于客户机,您不必调用init_node()。我们先call:

rospy.wait_for_service('add_two_ints')

这是一种方便的方法,在名为add_two_ints的服务可用之前阻止它。接下来,我们创建一个调用服务的句柄:

add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)

我们可以像使用普通函数一样使用这个句柄并调用它:

resp1 = add_two_ints(x, y)
        return resp1.sum

因为我们已经将服务类型声明为 AddTwoInts,所以它可以为您生成 AddTwoIntsRequest 对象(您可以自由传递自己的对象)。返回值是 AddTwoIntsResponse 对象。如果调用失败,则可能会引发rospy.ServiceException,因此应设置适当的try/except模块。 

3. 构建你的节点

我们使用cmake作为构建系统,是的,您甚至必须将其用于Python节点。这是为了确保为消息和服务创建了自动生成的python代码。

Go to your catkin workspace and run catkin_make.

# In your catkin workspace
$ cd ~/catkin_ws
$ catkin_make

4. Try it out!

In a new terminal, run

$ cd ~/catkin_ws
$ . devel/setup.bash
$ rosrun beginner_tutorials add_two_ints_server.py

In a new terminal, run

$ cd ~/catkin_ws
$ . devel/setup.bash
$ rosrun beginner_tutorials add_two_ints_client.py

您将看到打印的使用信息,类似于

/home/user/catkin_ws/src/beginner_tutorials/scripts/add_two_ints_client.py [x y]

Then run

$ rosrun beginner_tutorials add_two_ints_client.py 4 5

And you will get

Requesting 4+5
4 + 5 = 9
  • And the server will print out
Returning [4 + 5 = 9]

现在你已经学会如何编写简单的Service和Client,开始测试简单的Service和Client吧。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值