当前位置:网站首页>ros多客户端请求服务
ros多客户端请求服务
2022-08-02 02:20:00 【诺有缸的高飞鸟】
写在前面
1、本文内容
一个节点的多个服务被请求时产生消息阻塞,使用多线程来解决
2、平台
ubuntu1804, ros melodic
3、转载请注明出处:
https://blog.csdn.net/qq_41102371/article/details/125846107
代码
Service.srv
string client_name
---
string result
service_5.cpp
#include <ros/ros.h>
#include "service_5/Service.h"
#include <thread>
bool func_in=false;
// service回调函数,输入参数req,输出参数res
bool serviceCallback1(service_5::Service::Request &req,
service_5::Service::Response &res)
{
// 显示请求数据
ROS_INFO("client: name:%s\n", req.client_name.c_str());
for(int i=0;i<5;++i){
std::cout<<req.client_name<<": "<<i<<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// // sleep(1);
// std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// 设置反馈数据
res.result = "OK";
return true;
}
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "server_5");
// 创建节点句柄
ros::NodeHandle n;
// 创建server,注册回调函数
ros::ServiceServer service1 = n.advertiseService("/my_server1", serviceCallback1);
ros::ServiceServer service2 = n.advertiseService("/my_server2", serviceCallback1);
ros::ServiceServer service3 = n.advertiseService("/my_server3", serviceCallback1);
ros::ServiceServer service4 = n.advertiseService("/my_server4", serviceCallback1);
ros::ServiceServer service5 = n.advertiseService("/my_server5", serviceCallback1);
// // 循环等待回调函数
ROS_INFO("Ready.");
//方法1
// ros::MultiThreadedSpinner s(5);
// s.spin();
// // ros::spin(s);
//方法2
ros::AsyncSpinner spinner(5); // Use4 threads
spinner.start();
ros::waitForShutdown();
//直接spin消息阻塞
//ros::spin();
return 0;
}
使用
在不同命令行窗口请求服务
rosservice call /my_server3 "client_name: 'client3'"
rosservice call /my_server2 "client_name: 'client2'"
直接spin消息阻塞,client2等client3完了才开始
多线程结果(方法2)
参考
ROS多线程订阅消息(ros::asyncspinner) https://blog.csdn.net/weixin_28900531/article/details/79431192
rosspin、rosspinOnce及多线程订阅 https://blog.csdn.net/yaked/article/details/50776224
边栏推荐
- Data transfer at the data link layer
- How to adjust the cross cursor too small, CAD dream drawing calculation skills
- 工程师如何对待开源
- NAS和私有云盘的区别?1篇文章说清楚
- AI目标分割能力,无需绿幕即可实现快速视频抠图
- Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
- ¶ Backtop back to the top is not effective
- nacos启动报错,已配置数据库,单机启动
- 接口测试神器Apifox究竟有多香?
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
猜你喜欢
Unable to log in to the Westward Journey
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
2022-07-30 mysql8 executes slow SQL-Q17 analysis
LeetCode Brushing Diary: 74. Searching 2D Matrix
CASE2023
[LeetCode Daily Question]——654. The largest binary tree
Ringtone 1161. Maximum In-Layer Elements and
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
【web】Understanding Cookie and Session Mechanism
随机推荐
Redis Subscription and Redis Stream
2022河南青训联赛第(三)场
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
LeetCode Brushing Diary: 74. Searching 2D Matrix
The underlying data structure of Redis
swift项目,sqlcipher3 -&gt; 4,无法打开旧版数据库有办法解决吗
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
2022 Henan Youth Training League Game (3)
MySQL8 download, start, configure, verify
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
NIO‘s Sword(牛客多校赛)
MySQL - CRUD operations
Can Youxuan database import wrongly be restored?
GTK RGB图像绘制
极大似然估计
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
messy website
Data transfer at the data link layer
字典常用方法