上一节我们完成了 message & srv 文件的创建和加入编译,这次我们要玩简单的Publisher 和 Subscriber

要玩 Publisher 和 Subscriber, 需要具备的条件有哪些呢?先总结一下:

  1. 创建并生成自己的Package,本次是 beginner_tutorials
  2. 创建并生成ROS message & srv

详细版的手记要看我上传到百度文库的文件了:http://wenku.baidu.com/view/f872755a26fff705cc170ade

这里会写个总结~

  1. Writing a Simple Publisher and Subscriber (C++)

    This tutorial covers how to write a publisher and subscriber node in C++.

ROS学习笔记10 - 编写编译和检验Service Node

百度文库:http://wenku.baidu.com/view/e14d5e18dd88d0d232d46a37

Writing a Service Node

Here we'll create the service ("add_two_ints_server")node which will receive two ints and return the sum.

Change directories to your beginner_tutorialspackage you created in your catkin workspace previous tutorials:

cd ~/catkin_ws/src/beginner_tutorials

Please make sure you have followed the directions in the previous tutorialfor creating the service needed in this tutorial,creating
the AddTwoInts.srv
(be sure to choose the rightversion of build tool you're using at the top of wiki page in the link).

The Code

Create the src/add_two_ints_server.cpp file within the
beginner_tutorialspackage and paste the following inside it:

切换行号显示

#include "ros/ros.h"

#include "beginner_tutorials/AddTwoInts.h"

bool add(beginner_tutorials::AddTwoInts::Request  &req,

beginner_tutorials::AddTwoInts::Response &res)

{

res.sum = req.a + req.b;

ROS_INFO("request:x=%ld, y=%ld", (long int)req.a, (long int)req.b);

ROS_INFO("sendingback response: [%ld]", (long int)res.sum);

return true;

}

int main(int argc, char **argv)

{

ros::init(argc, argv, "add_two_ints_server");

ros::NodeHandle n;

ros::ServiceServer service = n.advertiseService("add_two_ints",add);

ROS_INFO("Readyto add two ints.");

ros::spin();

return 0;

}

The Code Explained

Now, let's break the code down.

切换行号显示

#include "ros/ros.h"

#include "beginner_tutorials/AddTwoInts.h"

beginner_tutorials/AddTwoInts.h is the header file generated fromthe srv file that we created earlier. 位置:~/catkin_ws/include/beginner_tutorials

切换行号显示

bool add(beginner_tutorials::AddTwoInts::Request &req,

beginner_tutorials::AddTwoInts::Response &res)

This function provides the service for adding two ints,it takes in the request and response type defined in the srvfile(AddTwoInts.srv) and returns a boolean.

切换行号显示

{

res.sum = req.a + req.b;

ROS_INFO("request:x=%ld, y=%ld", (long int)req.a, (long int)req.b);

ROS_INFO("sendingback response: [%ld]", (long int)res.sum);

return true;

}

Here the two ints are added and stored in theresponse. Then some information about the request and response are logged.Finally the service returns true when it is complete.

切换行号显示

ros::ServiceServerservice = n.advertiseService("add_two_ints", add);

Here the service is created and advertised over ROS.

对比一下前一节publisher nodes的创建:

ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);  //topic name “chatter”, bufferSize:1000

对比一下后面client相关声明语句:

ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");

This creates a client for the add_two_ints service. The ros::ServiceClient object is used to call the service later on.

 

Writing the Client Node

The Code

Create the src/add_two_ints_client.cpp file within the
beginner_tutorialspackage and paste the following inside it:

切换行号显示

#include "ros/ros.h"

#include "beginner_tutorials/AddTwoInts.h"

#include <cstdlib>

int main(int argc, char **argv)

{

ros::init(argc, argv, "add_two_ints_client");

if (argc != 3)

{

ROS_INFO("usage:add_two_ints_client X Y");

return1;

}

ros::NodeHandle n;

ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");

beginner_tutorials::AddTwoInts srv;

srv.request.a= atoll(argv[1]);

srv.request.b= atoll(argv[2]);

if (client.call(srv))

{

ROS_INFO("Sum:%ld", (long int)srv.response.sum);

}

else

{

ROS_ERROR("Failedto call service add_two_ints");

return1;

}

return 0;

}

The Code Explained

Now, let's break the code down.

切换行号显示

ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");

This creates a client for the add_two_intsservice. The ros::ServiceClient object is used to call the service later on.

切换行号显示

beginner_tutorials::AddTwoInts srv;

srv.request.a= atoll(argv[1]);

srv.request.b= atoll(argv[2]);

Here we instantiate列举 an autogenerated service class, and assignvalues into its request member.Aservice class contains two members,
requestand response.It alsocontains two class definitions,
Requestand Response.

切换行号显示

if (client.call(srv))

This actually calls the service. Since service calls are blocking, it willreturn once the call is done. If the service call succeeded, call()will return true and the value in srv.response willbe valid. If the call did not succeed, call() willreturn
false and the value in srv.response will beinvalid.

Building your nodes

Again edit the beginner_tutorials CMakeLists.txt located at ~/catkin_ws/src/beginner_tutorials/CMakeLists.txt and add the following at the end:

https://raw.github.com/ros/catkin_tutorials/master/create_package_srvclient/catkin_ws/src/beginner_tutorials/CMakeLists.txt

切换行号显示

add_executable(add_two_ints_server src/add_two_ints_server.cpp)

target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})

add_dependencies(add_two_ints_server beginner_tutorials_gencpp)

add_executable(add_two_ints_client src/add_two_ints_client.cpp)

target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})

add_dependencies(add_two_ints_client beginner_tutorials_gencpp)

This will create two executables, add_two_ints_server and add_two_ints_client, which by default will go intopackage directory of yourdevel
space
, located by default at ~/catkin_ws/devel/lib/share/<package name>. You can invoke executablesdirectly or you can use rosrun to invoke调用 them. They are not placed in '<prefix>/bin' because that would pollute thePATH when installing your package to
the system. If you wish for your executableto be on the PATH at installation time, you can setup an install target, see:catkin/CMakeLists.txt

For more detailed discription of the CMakeLists.txt file see: catkin/CMakeLists.txt

Now run catkin_make:

# In your catkin workspace

cd ~/catkin_ws

catkin_make

If your build fails for some reason:

Now that you have written a simple service and client, let's examine the simple service and client.

Examining the Simple Service and Client

Running the Service

Let's start by running the service:

$ rosrunbeginner_tutorials add_two_ints_server     (C++)

$ rosrunbeginner_tutorials add_two_ints_server.py  (Python)

You should see something similar to:

Ready to add two ints.

Running the Client

Now let's run the client with the necessaryarguments:

$ rosrunbeginner_tutorials add_two_ints_client1 3     (C++)

$ rosrunbeginner_tutorials add_two_ints_client.py 1 3  (Python)

You should see something similar to:

Requesting 1+3

1 + 3 = 4

Now that you've successfully run your first serverand client, let's learn how torecord and play back data.

Further examples on Service and Client nodes

If you want to investigate further and get ahands-on example, you can get one here. A simple Client and Servicecombination shows the use of custom message types. The Service node is writtenin C++ while the Client is available in C++, Python and LISP.

ROS学习手记 - 8 编写ROS的Publisher and Subscriber的更多相关文章

  1. ROS学习手记 - 5 理解ROS中的基本概念_Services and Parameters

    上一节完成了对nodes, Topic的理解,再深入一步: Services and Parameters 我不理解为何 ROS wiki 要把service与parameter放在一起介绍, 很想分 ...

  2. ROS学习手记 - 7 创建ROS msg & srv

    至此,我们初步学习了ROS的基本工具,接下来一步步理解ROS的各个工作部件的创建和工作原理. 本文的详细文档:http://wenku.baidu.com/view/623f41b3376baf1ff ...

  3. ROS学习手记 - 6 使用ROS中的工具:rqt_console & roslaunch & rosed

    http://wiki.ros.org/ROS/Tutorials/UsingRqtconsoleRoslaunch Using rqt_console and roslaunch This tuto ...

  4. ROS学习手记 - 2.1: Create and Build ROS Package 生成包(Python)

    ROS学习手记 - 2.1: Create and Build ROS Package 生成包(Python) 时隔1年,再回来总结这个问题,因为它是ros+python开发中,太常用的一个操作,需要 ...

  5. SLAM+语音机器人DIY系列:(二)ROS入门——4.如何编写ROS的第一个程序hello_world

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  6. ROS学习手记 9 -- 阶段性复习

    ROS 阶段性总结 1. 基本概念 ROS 是建立在Linux特别是Ubuntu系统上的一套软件系统,它具有操作系统的特征 ,负责管理各个模块的协同运行.设计初衷主要是面向机器人软硬件开发的特点:多 ...

  7. ROS学习笔记一(ROS的catkin工作空间)

    在安装完成ROS indigo之后,需要查看环境变量是否设置正确,并通过创建一个简单的实例来验证ROS能否正常运行. 1 查看环境变量 在ROS的安装过程中,我们执行了如下命令:(此命令就是向当前用户 ...

  8. ROS学习笔记一:ROS安装与测试

    1 Ubuntu和ROS版本的对应关系 Ubuntu 和 ROS 都存在不同的版本,其对应关系如下: 注:如果Ubuntu版本和ROS版本不对应的话,安装就不会成功了- 笔者安装的是Ubuntu14. ...

  9. ROS学习(一)Ros 中使用kinect

    上的安装说明如下: 官网上明确写了如果安装windows kinect还需要安装一个驱动,但是有些ROS的书上并没有这么做,只提到了使用如下两步进行安装即可使用: sudo apt-get insta ...

随机推荐

  1. Apache Tika

    Tika入门 Tika是一个内容抽取的工具集合(a toolkit for text extracting).它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面.其次,Tika也提 ...

  2. [转] openwrt关闭调试串口

    转自: http://wiki.wrtnode.com/index.php?title=Release_UART/zh-cn 由于mt7620n只有一个UART lite接口,在原生的OpenWrt中 ...

  3. xilinx AXI相关IP核学习

    xilinx AXI相关IP核学习 1.阅读PG044 (1)AXI4‐Stream to Video Out Top‐Level Signaling Interface (2)AXI4‐Stream ...

  4. Debian 7.0(Wheezy) 安装配置笔记

    1. 下载光盘镜像 ftp://debian.ustc.edu.cn/debian-cd/7.1.0/amd64/iso-dvd/ [2012.3.25]  1.1 去官网 http://cdimag ...

  5. OpenEXR的采样机制

    OpenEXR的输出机制是无损的,这样做会受到部分低配置电脑存储及运算带宽的限制. 当前有很多针对图像的压缩算法来解决这个问题,OpenEXR另外提供了WRITE_YC这种存储方式,这种方式会将RGB ...

  6. Python高效编程技巧

    如何在列表,字典,集合中根据条件筛选数据 1.过滤掉列表[-1,-2,-3,4,5,6]中的负数和0 方法1,for循环 data = [-1, -2, -3, 4, 5, 6] res = [] f ...

  7. 为IE内核的WebBrowser控件内存泄漏所烦恼的可以考虑用Cefsharp代替它!

    为IE内核的WebBrowser控件内存泄漏所烦恼的朋友们,可以考虑用Cefsharp代替WebBrowser控件 特意做了一个程序来测试 利用Cefsharp做控件,访问网站.每分钟刷新2次,初始时 ...

  8. windows server 2008 R2 安装

    微软服务器操作系统大致有: server 2000(简称2K),还有server 2003(2K3),server 2008(2K8),server 2000和2003是基于NT内核的,而2008是基 ...

  9. Django简介及Django项目的创建详述

    Django简介 Django是一个开源的Web应用框架,由Python写成.但是,百度百科中讲它采用了MVC框架模式,其实这个解释不准确. 确切的讲,Django的模式是:路由控制+MTV模式.所谓 ...

  10. Jmeter(三)Test-Plan、Thread-Group

    思量再三,还是再记一会,看到技术群里边的讨论,真的是压力山大,学习一刻也不能耽搁.继续来回顾Jmeter的知识吧. Test-Plan,是所有Jmeter脚本的根节点,Test-Plan中包含名称.注 ...