首页 > 代码库 > ROS服务和客户端

ROS服务和客户端

TargetTabletopSrv.srv

string Object
string TargetTabletop
---
bool FeedBackFlag

FeedBackFlag.msg

bool FeedBackFlag

TargetTabletop.msg

string Object
string TargetTabletop

创建msg 和srv

--------------------------------------------------

grasp_object_tabletop_server.cpp

创建服务:

/*
定义一个服务,来处理数据;并且发布反馈消息
*/
#include "ros/ros.h"
#include "beginner_tutorials/TargetTabletop.h"
#include "beginner_tutorials/TargetTabletopSrv.h"
#include <iostream>
#include "beginner_tutorials/FeedBackFlag.h"

using namespace std;
beginner_tutorials::FeedBackFlag flag ;
bool grabaction_feedback(beginner_tutorials::TargetTabletopSrv::Request  &MSG,						beginner_tutorials::TargetTabletopSrv::Response  &msg)//grab_feedback
{
  int num = 0;
  if(MSG.Object == "Object"&& MSG.TargetTabletop == "TargetTabletop" )//判断是否接收到的值为此,可以根据需要进行
	{
	cout<<"receive the message from the client"<<endl;
  msg.FeedBackFlag = true;

  cout<<"succeed to send the Feedback"<<endl;
 num = 1;

  }
  if(num == 1)
  {
        flag.FeedBackFlag = true;
  }

return true;
}

int main( int argc,char *argv[] )
{
      
       ros::init(argc,argv,"grasp_object_tabletop_service");
       ros::NodeHandle n;  
       ros::ServiceServer service = n.advertiseService("grasp_object_tabletop", grabaction_feedback); //定义服务
       ros::Publisher feedback_ = n.advertise<beginner_tutorials::FeedBackFlag>("feedback_grasp_object_tabletop",10);
       ros::Rate loop_rate(20);
	    ROS_INFO("Ready to receive the message from the grasp_object_tabletop.");
      while(ros::ok())
      {       
      if(flag.FeedBackFlag == true)
      {

     feedback_.publish(flag);
     }
  ros::spinOnce();
  loop_rate.sleep();
	}
  return 0;

}

定义头文件grasp.h

/*
定义一个头文件,创建一个类:GraspService,
*/
#include "ros/ros.h"
#include "beginner_tutorials/TargetTabletop.h"
#include "beginner_tutorials/TargetTabletopSrv.h"
#include <iostream>

class GraspService
{
public:
	GraspService();//构造函数
	~GraspService();//析构函数

	ros::Subscriber sub;//接收

	ros::ServiceClient client;//服务的客户端
	void callback(const beginner_tutorials::TargetTabletop  msg);//定义函数

};
grasp.cpp

#include "beginner_tutorials/grasp.h"
#include <iostream>
using namespace std;
GraspService::GraspService(){
	
}

GraspService::~GraspService(){
	
}


void GraspService::callback(const beginner_tutorials::TargetTabletop  msg)//定义一个.msg文件,当接收topic的情况,进入子程序
{
	
	beginner_tutorials::TargetTabletopSrv srv;//创建一个.srv文件,并且定义一个服务srv
  srv.request.Object = msg.Object;
  srv.request.TargetTabletop = msg.TargetTabletop;
  
  if (client.call(srv))//调用服务
  {
    cout<<"succeed to call service"<<endl;
  }
  else
  {
    ROS_ERROR("Failed to call service !!!!!!!!!!!!");
  }  
}

main.cpp

#include "beginner_tutorials/grasp.h"


int main(int argc, char *argv[])
{
	
	ros::init(argc, argv, "grasp_object_tabletop");
	ros::NodeHandle n;

  GraspService *graspService = new GraspService();//

    
	graspService->client = n.serviceClient<beginner_tutorials::TargetTabletopSrv>("grasp_object_tabletop");//client
	graspService->sub  = n.subscribe("goal_grasp_object_tabletop",10, &GraspService::callback, graspService);//msg  
	
    ros::Rate r(10.0);

    while(n.ok())
    {
        ros::spin();
        r.sleep();
    }

    return 0;
}



ROS服务和客户端