服务通信
一、理论模型
服务通信自定义srv
1.定义srv文件
2.编辑配置文件
3.翻译
三(A)、服务通信自定义srv调用A(C++)
0.vscode配置
1.服务端
案例代码:
#include "ros/ros.h"
#include "plumbing_server_client/AddInts.h"
bool doNums(plumbing_server_client::AddInts::Request &request,
plumbing_server_client::AddInts::Response &response)
{
int num1 = request.num1;
int num2 = request.num2;
ROS_INFO("收到的请求数据:num1 = %d , num2 = %d ",num1,num2);
int sum = num1 + num2;
response.sum = sum;
ROS_INFO("求和结果:sum= %d",sum);
return true;
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"heiShui");
ros::NodeHandle nh;
ros::ServiceServer server = nh.advertiseService("addInts",doNums);
ROS_INFO("服务器端启动");
ros::spin();
return 0;
}
2.客户端
案例代码:
#include "ros/ros.h"
#include "plumbing_server_client/AddInts.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
if(argc != 3)
{
ROS_INFO("提交的参数个数不对。");
return 1;
}
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 = atoi(argv[1]);
ai.request.num2 = atoi(argv[2]);
client.waitForExistence();
bool flag = client.call(ai);
if(flag)
{
ROS_INFO("响应成功");
ROS_INFO("响应结果 = %d",ai.response.sum);
}
else
{
ROS_INFO("处理失败……");
}
return 0;
}
3.配置CMakeList.txt
4.执行
三(B)、服务通信自定义srv调用B(python)
。。。。。。。。。。。。。。。。。。。
|