任务:客户端节点,运行向服务器发送两个数字,服务器端节点接收两个数字求和并将结果响应回客户端
客户端:提交两个整数,并处理响应结果
1、包含头文件;
2、初始化ROS节点;
3、创建节点句柄;
4、创建一个客户端对象;
5、提交请求并处理响应;
#include "ros/ros.h"
#include "plumbing_server_client/AddInts.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"dabao");
ros::NodeHandle nh;
ros::ServiceClient client=nh.serviceClient<plumbing_server_client::AddInts>("addInts");
plumbing_server_client::AddInts ai;
ai.request.num1 = 100;
ai.request.num2 = 200;
bool flag = client.call(ai);
if (flag)
{
ROS_INFO("响应成功!");
ROS_INFO("响应结果=%d",ai.response.sum);
}else{
ROS_INFO("处理失败!");
}
return 0;
}