publisher与subscriber话题的消息类型md5sum不一致问题

[ERROR] [1604235557.591296096]: Client [/range_vision_fusion_01] wants topic /detection/vision_objects to have datatype/md5sum [autoware_msgs/DetectedObjectArray/13415342e539aa36ea9a6f04ffaf39b4], but our version has [autoware_msgs/DetectedObjectArray/c16aecef51c24c6808480a0295e47806]. Dropping connection.

原因:publisher和subscriber虽然话题消息的类型autoware_msgs/DetectedObjectArray,但是由于使用的头文件不同,对同一消息类型的定义版本不同,即md5码不同。
publisher使用的autoware_msgs/DetectedObjectArray/opt/ros/melodic/include/autoware_msgs/DetectedObjectArray.h标准库头文件中定义,md5码如下:
在这里插入图片描述
subscriber使用的autoware_msgs/DetectedObjectArray是在catkin_make编译时在/devel/include/autoware_msgs/DetectedObjectArray.h中定义,且.cpp代码文件包含该.h头文件时用的是双引号include "autoware_msgs/DetectedObjectArray.h"<

### 发布者-订阅者模式的实现 发布者-订阅者(Pub/Sub)模式是一种消息传递机制,在这种模式下,发送方(即发布者)会向特定接收方发送数据;相反,它会将消息广播到主题或类别中,而任何对该主题感兴趣的订阅者都可以接收到这些消息[^3]。 #### 实现细节 为了构建一个基本的 Pub/Sub 系统,可以按照以下方式设计: 1. **定义发布者的接口** 发布者负责注册和注销订阅者,并在事件发生通知所有已注册的订阅者。以下是 C++ 中的一个简单实现示例: ```cpp #include <iostream> #include <vector> #include <functional> class Publisher { public: using Callback = std::function<void(const std::string&)>; int register_subscriber(Callback callback) { static int id_counter = 0; subscribers.emplace_back(id_counter++, callback); return --id_counter; // Return the ID assigned. } void unregister_subscriber(int id) { auto it = subscribers.begin(); while(it != subscribers.end()) { if ((*it).first == id) { it = subscribers.erase(it); } else { ++it; } } } void notify_all(const std::string& message) const { for(auto &[id, cb] : subscribers){ cb(message); } } private: std::vector<std::pair<int, Callback>> subscribers; }; ``` 上述代码展示了如何通过 `register_subscriber` 方法让订阅者加入并分配唯一ID,以及通过 `notify_all` 方法通知所有订阅者关于某个事件的消息[^1]。 2. **创建订阅者类** 订阅者需要绑定到具体的发布实例上,并在其生命周期结束自动取消订阅。这可以通过构造函数和析构函数完成。下面是一个基于前面提到的设计理念的具体例子[^2]: ```cpp class Subscriber { public: explicit Subscriber(Publisher& pub, Publisher::Callback callback) : publisher_(pub), callback_id_(pub.register_subscriber(callback)) {} ~Subscriber() { publisher_.unregister_subscriber(callback_id_); } private: Publisher& publisher_; int callback_id_; // Identifier returned when registering with the publisher. }; // Example usage: int main(){ Publisher pub; // Define callbacks as lambda functions or regular function pointers. auto handle_event_1 = [](const std::string &msg){std::cout << "Event handled by sub1: " << msg << "\n"; }; auto handle_event_2 = [](const std::string &msg){std::cout << "Event handled by sub2: " << msg << "\n"; }; Subscriber s1(pub, handle_event_1); Subscriber s2(pub, handle_event_2); pub.notify_all("This is a test message"); return 0; } ``` 此程序片段显示了两个同的回调处理逻辑被分别附加到了同一个发布者对象之上。当调用 `notify_all()` 函数,这两个订阅都会得到相应的通知。 3. **扩展功能——引入事件处理器(Event Handlers)** 除了简单的消息分发外,实际应用中的订阅者可能还需要执行更复杂的操作,比如触发外部API请求、更新数据库记录或者生成日志文件等。这类行为通常由专门的事件处理器(event handler)来承担[^4]。 例如,如果希望每当有新订单到达就立即给客户发送电子邮件,则可以在订阅者的回调方法内部集成邮件服务调用逻辑。 --- ###
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值