ros学习笔记(4)编写服务器和客户端

来源:互联网 发布:aptget删除软件 编辑:程序博客网 时间:2024/05/18 01:27

#include<> 和 #include""的区别


http://blog.csdn.net/vegetable_bird_001/article/details/50905244

https://www.cnblogs.com/LeoFeng/p/5346530.html


头文件

beginner_tutorials/AddTwoInts.h是由编译系统自动根据我们先前创建的srv文件生成的对应该srv文件的头文件,这些个文件存放在工作空间的devel/include目录下


另外参考

带注释的程序http://blog.csdn.net/mountzf/article/details/52336714

atoll函数http://www.cplusplus.com/reference/cstdlib/atoll/




ros::NodeHandle成员介绍

http://blog.csdn.net/hookie1990/article/details/52228531





服务器

我们先复习几个命令

rosservice list                   列出所有活动的服务

rosservice type                查看某个服务的类型

rosservice type 服务| rossrv show            查看某个服务类型具体输入什么类型的参数,返回什么类型的参数


首先,这个程序前面有两个头文件,位置和生成方式之前说过了。

在第二个头文件beginner_tutorials/AddTwoInts.h中,有这样一段

namespace beginner_tutorials
{

struct AddTwoInts
{

typedef AddTwoIntsRequest Request;
typedef AddTwoIntsResponse Response;
Request request;
Response response;

typedef Request RequestType;
typedef Response ResponseType;

}; // struct AddTwoInts
} // namespace beginner_tutorials


这段话在命名空间beginner_tutorials定义了一个结构体struct AddTwoInts,而beginner_tutorials/AddTwoInts恰好就是服务/add_two_ints的类型,

我们可以用

rosservice list                   列出所有活动的服务

rosservice type                查看某个服务的类型

得到验证


现在我想看看结构体的两个成员类型AddTwoIntsRequest和AddTwoIntsResponse具体是什么。


这个头文件还依赖于另外两个头文件

#include <beginner_tutorials/AddTwoIntsRequest.h>
#include <beginner_tutorials/AddTwoIntsResponse.h>


在同样的位置找到这两个头文件


AddTwoIntsRequest.h

这个头文件其中有一段

struct AddTwoIntsRequest_
{
  typedef AddTwoIntsRequest_<ContainerAllocator> Type;

  AddTwoIntsRequest_()
    : a(0)
    , b(0)  {
    }
  AddTwoIntsRequest_(const ContainerAllocator& _alloc)
    : a(0)
    , b(0)  {
  (void)_alloc;
    }



   typedef int64_t _a_type;
  _a_type a;

   typedef int64_t _b_type;
  _b_type b;




  typedef boost::shared_ptr< ::beginner_tutorials::AddTwoIntsRequest_<ContainerAllocator> > Ptr;
  typedef boost::shared_ptr< ::beginner_tutorials::AddTwoIntsRequest_<ContainerAllocator> const> ConstPtr;

}; // struct AddTwoIntsRequest_


说明AddTwoInts结构体的成员AddTwoIntsRequest_也是一个结构体,这个结构体有两个很重要的元素int64的a,b。这两个元素就构成了我们服务器的子程序

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;}
中的req.a和req.b。其中req就是结构体AddTwoIntsRequest的具体化



AddTwoIntsResponse.h

这个头文件其中有一段

struct AddTwoIntsResponse_
{
  typedef AddTwoIntsResponse_<ContainerAllocator> Type;

  AddTwoIntsResponse_()
    : sum(0)  {
    }
  AddTwoIntsResponse_(const ContainerAllocator& _alloc)
    : sum(0)  {
  (void)_alloc;
    }



   typedef int64_t _sum_type;
  _sum_type sum;




  typedef boost::shared_ptr< ::beginner_tutorials::AddTwoIntsResponse_<ContainerAllocator> > Ptr;
  typedef boost::shared_ptr< ::beginner_tutorials::AddTwoIntsResponse_<ContainerAllocator> const> ConstPtr;

}; // struct AddTwoIntsResponse_

说明AddTwoInts结构体的成员AddTwoIntsResponse_也是一个结构体,这个结构体有一个很重要的元素int64的sum。这就构成了刚才子程序中的res.sum



由于

bool add(beginner_tutorials::AddTwoInts::Request  &req,         beginner_tutorials::AddTwoInts::Response &res)
中传递的是别名,所以整个操作结束后实参会被直接修改。



接下来主函数中

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;}

服务名称:add_two_ints 由主函数中advertiseService函数给出。

服务类型:rosservice type+服务名称 来查看,结果是头文件中定义的结构体的类型,这个结构体必须包含另外两个结构体:request和response。这两个结构体的成员决定了这个服务类型具体输入什么参数(request成员的数据类型)输出什么参数(response成员的数据类型),可以用rossrv show+服务类型查看。




客户端









原创粉丝点击