ROS - 如何发布消息并立即获取订阅的回调 [英] ROS - How do I publish a message and get the subscribed callback immediately

查看:90
本文介绍了ROS - 如何发布消息并立即获取订阅的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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

字符串请求---字符串响应

<小时>

服务器输出:

请求:你听到我说话了吗?"

客户端:

客户:你听到了吗?服务器:您好,客户端,收到您的消息.

<小时><块引用>

在 ros-wiki 中了解更多信息

<小时>

[更新]

  • 如果您正在寻找快速通信,TCP-ROS 通信不是您的目的,因为它比 ZeroMQ 等无代理通信器慢(它具有低延迟和高吞吐量):

    1. ZeroMQ 中等效的 ROS 服务模式是 REQ/REP(客户端/服务器)
    2. ZeroMQ 中等效的 ROS 发布者/订阅者模式是 发布/订阅
    3. ROS 发布者/订阅者与 waitformessage 在 ZeroMQ 中等效是 推/拉

    <块引用>

    ZeroMQ 支持 Python 和 C++

  • 此外,为了传输大量数据(即 pointcloud),ROS 中有一种机制称为 nodelet 仅在 C++ 中受支持.这种通信基于机器上的共享内存而不是 TCP-ROS 套接字.

    <块引用>

    究竟什么是nodelet?​​

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.


Learn more in ros-wiki


[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):

    1. ROS-Service pattern equivalent in ZeroMQ is REQ/REP (client/server)
    2. ROS publisher/subscriber pattern equivalent in ZeroMQ is PUB/SUB
    3. ROS publisher/subscriber with waitformessage equivalent in ZeroMQ is PUSH/PULL

    ZeroMQ is available in both Python and C++

  • Also, to transfer huge amounts of data (i.e. pointcloud), there is a mechanism in ROS called nodelet which is supported only in C++. This communication is based on shared memory on a machine instead of TCP-ROS socket.

    What exactly is a nodelet?

这篇关于ROS - 如何发布消息并立即获取订阅的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆