Writing a Simple Service and Client (C++)

此前说的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++)

上一篇:疯狂抨击ie6下各种扭曲行为


下一篇:【转载】IE浏览器常见的9个css Bug以及解决办法