ros_tutorials
  • 简介
  • 1 安装并配置ROS环境
    • 1.1 安装ROS
    • 1.2 管理环境变量
    • 1.3 创建ROS工作空间
  • 2 ROS文件系统介绍
    • 2.1 快速了解文件系统概念
    • 2.2 文件系统工具
    • 2.3 回顾
  • 3 创建ROS程序包(Packages)
    • 3.1 catkin程序包由什么组成?
    • 3.2 在catkin工作空间中的程序包
    • 3.3 创建一个catkin程序包
    • 3.4 编译程序包
    • 3.5 程序包依赖关系
    • 3.6 自定义你的程序包
  • 4 编译ROS程序包(packages)
    • 4.1 使用 catkin_make
    • 4.2 编译你的程序包Package
  • 5 理解ROS节点(nodes)
    • 5.1 图(Graph)概念
    • 5.2 节点(Nodes)
    • 5.3 客户端库(Client Libraries)
    • 5.4 roscore
    • 5.5 使用rosnode
    • 5.6 使用rosrun
    • 5.7 回顾
  • 6 理解ROS话题(topics)
    • 6.1 开始
    • 6.2 ros topics
    • 6.3 ROS Messages
    • 6.4 继续学习 rostopic
    • 6.5 使用 rqt_plot
  • 7 理解ROS服务和参数
    • 7.1 ROS Services
    • 7.2 使用rosservice
    • 7.3 使用 rosparam
  • 8 使用 rqt_console 和 roslaunch
    • 8.1 预先安装rqt和turtlesim程序包
    • 8.2 使用rqt_console和rqt_logger_level
  • 9 使用rosed编辑ROS中的文件
    • 9.1 使用 rosed
    • 9.2 使用Tab键补全文件名
    • 9.3编辑器
  • 10 创建ROS消息和ROS服务
    • 10.1 消息(msg)和服务(srv)介绍
    • 10.2 使用 msg
    • 10.3 使用 srv
    • 10.4 msg和srv都需要的步骤
    • 10.5 获得帮助
    • 10.6 回顾
  • 11 编写简单的Publisher和Subscriber(C++)
    • 11.1 编写Publisher Node
    • 11.2 编写Subscriber Node
    • 11.3 编译nodes
  • 12 编写简单的Publisher和Subscriber(Python)
    • 12.1 编写Publisher Node
    • 12.2 编写Subscriber Node
    • 12.3 编译nodes
  • 13 测试消息Publisher和Subscriber
    • 13.1 启动发布器(Publisher)
    • 13.2 启动订阅器(Subscriber)
  • 14 编写简单的Service和Client (C++)
    • 14.1 编写Service节点
    • 14.2 编写Client节点
    • 14.3 编译nodes
  • 15 编写简单的Service和Client (Python)
    • 15.1 编写Service Node
    • 15.2 编写Client节点
    • 15.3 编译nodes
    • 15.4 试试看
  • 16 测试简单的Service和Client
    • 16.1 运行Service
    • 16.2 运行Client
    • 16.3 关于Service和Client节点的更多例子
  • 17 录制与回放数据
    • 17.1 录制数据(通过创建一个bag文件)
    • 17.2 检查并回放bag文件
    • 17.3 录制数据子集
    • 17.4 rosbag record/play 命令的局限性
  • 18 roswtf入门
    • 18.1 安装检查
    • 18.2 运行时检查(在有ROS节点运行时)
    • 18.3 错误报告
  • 19 探索ROS维基
    • 19.1 基础
    • 19.2 高级
  • 20 接下来做什么
    • 20.1 使用模拟器
    • 20.2 使用 RViz
    • 20.3 理解 TF
    • 20.4 更进一步
Powered by GitBook
On this page
  • 15.2.1 The Code
  • 15.2.2 The Code Explained
  1. 15 编写简单的Service和Client (Python)

15.2 编写Client节点

15.2.1 The Code

在beginner_tutorials包中创建scripts / add_two_ints_client.py文件,并在其中粘贴以下内容:

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

不要忘记增加其可执行权限

$ chmod +x scripts/add_two_ints_client.py

15.2.2 The Code Explained

现在,让我们分解代码。调用服务的客户端代码也很简单。对于客户端,您不必调用init_node()。我们首先呼吁:

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块。

Previous15.1 编写Service NodeNext15.3 编译nodes

Last updated 7 years ago