问题描述
我有一个 ROS 节点,它允许您向它发布"一个数据结构,它通过发布输出进行响应.我发布的内容和它发布的内容的时间戳是匹配的.
是否有一种机制可以阻止我发送/发布和输出,它会一直等到我收到输出?
我认为您需要 ROS_Services(客户端/服务器)模式而不是发布者/订阅者.
这是在 Python 中执行此操作的简单示例:
客户端代码片段:
import rospy从 test_service.srv 导入 MySrvFilerospy.wait_for_service('a_topic')尝试:send_hi = rospy.ServiceProxy('a_topic', MySrvFile)print('客户:你听到我说话了吗?')resp = send_hi('你听到了吗?')打印(服务器:{}".格式(响应))除了 rospy.ServiceException, e:print("服务调用失败:%s"%e)
服务器代码片段:
import rospy从 test_service.srv 导入 MySrvFile, MySrvFileResponsedef callback_function(req):打印(请求)return MySrvFileResponse('您好,客户端,您的消息已收到.')rospy.init_node('服务器')rospy.Service('a_topic', MySrvFile, callback_function)rospy.spin()
MySrvFile.srv
字符串请求---字符串响应
服务器输出:
请求:你听到我说话了吗?"
客户端:
客户:你听到了吗?服务器:您好,客户端,收到您的消息.
- GitHub 上的项目存储库.
[更新]
如果您正在寻找快速通信,TCP-ROS 通信不是您的目的,因为它比 ZeroMQ 等无代理通信器慢(它具有低延迟和高吞吐量):
- ZeroMQ 中等效的 ROS 服务模式是 REQ/REP(客户端/服务器)
- ZeroMQ 中等效的 ROS 发布者/订阅者模式是 发布/订阅
- ROS 发布者/订阅者与
waitformessage
在 ZeroMQ 中等效是 推/拉
ZeroMQ 支持 Python 和 C++
此外,为了传输大量数据(即
pointcloud
),ROS 中有一种机制称为nodelet
仅在 C++ 中受支持.这种通信基于机器上的共享内存而不是 TCP-ROS 套接字.
I have a ROS node that allows you to "publish" a data structure to it, to which it responds by publishing an output. The timestamp of what I published and what it publishes is matched.
Is there a mechanism for a blocking function where I send/publish and output, and it waits until I receive an output?
I think you need the ROS_Services (client/server) pattern instead of the publisher/subscriber.
Here is a simple example to do that in Python:
Client code snippet:
import rospy
from test_service.srv import MySrvFile
rospy.wait_for_service('a_topic')
try:
send_hi = rospy.ServiceProxy('a_topic', MySrvFile)
print('Client: Hi, do you hear me?')
resp = send_hi('Hi, do you hear me?')
print("Server: {}".format(resp.response))
except rospy.ServiceException, e:
print("Service call failed: %s"%e)
Server code snippet:
import rospy
from test_service.srv import MySrvFile, MySrvFileResponse
def callback_function(req):
print(req)
return MySrvFileResponse('Hello client, your message received.')
rospy.init_node('server')
rospy.Service('a_topic', MySrvFile, callback_function)
rospy.spin()
MySrvFile.srv
string request
---
string response
Server out:
request: "Hi, do you hear me?"
Client out:
Client: Hi, do you hear me?
Server: Hello client, your message received.
- Project repo on GitHub.
[UPDATE]
If you are looking for fast communication, TCP-ROS communication is not your purpose because it is slower than a broker-less communicator like ZeroMQ (it has low latency and high throughput):
- ROS-Service pattern equivalent in ZeroMQ is REQ/REP (client/server)
- ROS publisher/subscriber pattern equivalent in ZeroMQ is PUB/SUB
- ROS publisher/subscriber with
waitformessage
equivalent in ZeroMQ is PUSH/PULL
Also, to transfer huge amounts of data (i.e.
pointcloud
), there is a mechanism in ROS callednodelet
which is supported only in C++. This communication is based on shared memory on a machine instead of TCP-ROS socket.
这篇关于ROS - 如何发布消息并立即获取订阅的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!