此前说的publisher/subscriber都是广播式的,subscriber被动地接收消息,二者没有request/response这种交互.

  • Service Node
  • Client Node

Service Node

#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h" //由.srv文件生成的头文件 //service函数.提供add功能,入参为srv文件里定义好的request/response类型
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("sending back 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("Ready to add two ints."); ros::spin(); return 0;
}

总结一下

  • 初始化
  • 创建服务
  • ros::spin() 简单说就是运行一个loop,避免程序退掉.具体详细解释看https://answers.ros.org/question/257361/what-is-the-actual-meaning-of-rosspin/

Client Node

#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");
return 1;
} ros::NodeHandle n; //创建client
ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints"); //定义request
beginner_tutorials::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]); //发送request
if (client.call(srv))
{
ROS_INFO("Sum: %ld", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
} return 0;
}

编译

修改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)

编译

# In your catkin workspace
cd ~/catkin_ws
catkin_make

target_link_libraries与add_dependencies区别.https://www.cnblogs.com/wpcockroach/p/6625699.html

简单说一说前两天学习使用CMake解决链接问题时遇到的一个问题。

对于编译时遇到的依赖问题,很多时候我们只需要一句target_link_libraries就可以搞定。

但是CMake还有另外一个command,add_dependencies。这个什么时候用呢?

一般来说用不到。用到的情况就是两个targets有依赖关系(通过target_link_libraries解决)并且依赖库也是通过编译源码产生的。这时候一句add_dependencies可以在直接编译上层target时,自动检查下层依赖库是否已经生成。没有的话先编译下层依赖库,然后再编译上层target,最后link depend target。

Writing a Simple Service and Client (C++)的更多相关文章

  1. Creating a Simple Web Service and Client with JAX-WS

    Creating a Simple Web Service and Client with JAX-WS 发布服务 package cn.zno.service.impl; import javax. ...

  2. Learning WCF Chapter1 Generating a Service and Client Proxy

    In the previous lab,you created a service and client from scratch without leveraging the tools avail ...

  3. Writing a simple Lexer in PHP/C++/Java

    catalog . Comparison of parser generators . Writing a simple lexer in PHP . phc . JLexPHP: A PHP Lex ...

  4. 用C#基于WCF创建TCP的Service供Client端调用

    本文将详细讲解用C#基于WCF创建TCP的Service供Client端调用的详细过程 1):首先创建一个Windows Service的工程 2):生成的代码工程结构如下所示 3):我们将Servi ...

  5. SLAM+语音机器人DIY系列:(二)ROS入门——6.编写简单的service和client

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

  6. ROS学习(十三)—— 编写简单的Service和Client (C++)

    一.编写Service节点 1.节点功能: 我们将创建一个简单的service节点("add_two_ints_server"),该节点将接收到两个整形数字,并返回它们的和. 2. ...

  7. Writing Text Files On The Client in Oracle Forms 10g

    Below is the example to write file on client in Oracle Forms 10g with webutil library package.Note:  ...

  8. java的myeclipse生成webservice的service和client

    前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作. 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 ...

  9. C++编写简单的Service和Client

    在上一步的基础上 编写Service节点 这里,我们将创建一个简单的service节点("add_two_ints_server"),该节点将接收到两个整形数字,并返回它们的和. ...

随机推荐

  1. appium+python+eclipse 自动化测试框架构建!

    经过几天的慢慢研究,现将所需用的自动化框架进行了构建,在后期的代码编写中,直接在框架中套用编写对应的module.case等即可,以此来简化测试方式,提高代码的编写效率与规范 基本的架构设计流程图,如 ...

  2. Description Resource Path Location Type Cannot change version of project facet Dynamic Web Module to 2.3.

    报错信息:Description Resource Path Location Type Cannot change version of project facet Dynamic Web Modu ...

  3. 浅谈URL跳转与Webview安全

    学习信息安全技术的过程中,用开阔的眼光看待安全问题会得到不同的结论. 在一次测试中我用Burpsuite搜索了关键词url找到了某处url,测试一下发现waf拦截了指向外域的请求,于是开始尝试绕过.第 ...

  4. vue+axios访问本地json数据踩坑点

    当我们想在vue项目中模拟后台接口访问json数据时,我们发现无论如何也访问不到本地的json数据. 注意:1.在vue-cli项目中,我们静态资源只能放在static文件夹中,axios使用get请 ...

  5. Django admin自定制功能

    一:基础设置 1.应用注册 1)方式一 若要把app应用显示在后台管理中,需要在admin.py中注册.打开admin.py文件,如下代码: from django.contrib import ad ...

  6. 干货,一文带你超详细了解 Filter 的原理及应用

    提出问题 1.我们在访问后台很多页面时都需要登录,只有登录的用户才能查看这些页面,我们需要   在每次请求的时候都检查用户是否登陆,这样做很麻烦,有没有一种方法可以在我们请求之   前就帮我们做这些事 ...

  7. 优雅地 `async/await`

    async/await 虽然取代了回调,使用类似同步的代码组织方式让代码更加简洁美观,但错误处理时需要加 try/catch. 比如下面这样,一个简单的 Node.js 中使用 async/await ...

  8. [译]PEP 342--增强型生成器:协程

    PEP原文 : https://www.python.org/dev/peps/pep-0342/ PEP标题: Coroutines via Enhanced Generators PEP作者: G ...

  9. java基础(七)-----深入剖析Java中的装箱和拆箱

    本文主要介绍Java中的自动拆箱与自动装箱的有关知识. 基本数据类型 基本类型,或者叫做内置类型,是Java中不同于类(Class)的特殊类型.它们是我们编程中使用最频繁的类型. Java是一种强类型 ...

  10. C# 接口的使用(工厂模式)

    接口(interface)与抽象类(abstract)的区别: 相同点: 1.都不能被直接实例化,都可以通过继承实现其抽象方法. 2.都是面向抽象编程的技术基础,实现诸多模式 不同点: 1.接口可以多 ...