ROS 订阅回调 - 使用 boost::绑定成员函数

ROS subscribe callback - using boost::bind with member functions

本文关键字:绑定 成员 函数 boost 使用 回调 ROS      更新时间:2023-10-16

我正在尝试在 ROS 中订阅不同的主题(每个弹出的车辆一个),对所有主题使用相同的回调。这个想法是boost::bind会将主题名称作为附加参数传递,以便我知道我应该在回调中访问哪个车辆。

问题是,即使我已经经历了关于这个主题的多个问题,似乎没有一个解决方案有效。

基本上,我有以下类VOBase包含一个带有成员函数的std::map<std::string, VOAgent*> agents_,如下所示:

void VOBase::callback_agentState(const custom_msgs::VState::ConstPtr& vStateMsg,
std::string topic) {
// [...] regex to find agent name from topic string
std::string agent_name = match.str();
// [...] Check if agent name exists, else throw exception
// Process message
agents_[agent_name]->pos_ = vStateMsg->pose.position;  // etc.
}

我通过此订阅调用:

void VOBase::callback_agentList(const custom_msgs::VehicleList& vehListMsg) {
// [...] New agent/vehicle found: process vehListMsg and get agent_name string
// Subscribe to VState
topic_name = agent_name + "/state_estimate";
subscribers_[topic_name] = nodeHandlePtr->subscribe<custom_msgs::VState>(topic_name, 1,
std::bind(&VOBase::callback_agentState, this, _1, topic_name));
}

但是,我得到了所有候选人的template argument deduction/substitution failed和此错误:

mismatched types ‘std::reference_wrapper<_Tp>’ and ‘VO::VOBase*’
typename add_cv<_Functor>::type&>::type>()(

我已经测试了许多解决方案,例如使用std::ref(this)来获取std::reference_wrapper<_Tp>而不是VO::VOBase*(尽管引用无法生存:use of deleted function),使用boost::bind而不是std::bind(但自 C++11 以来应该都是一样的),在回调函数参数(和subscribe<acl_msgs::ViconState::ConstPtr>中)有和没有 ROS 消息的...::ConstPtr, 等。所以我只是在这里玩弄部分解决方案及其排列......

有什么线索吗?

我还没有研究你显示的代码的细节(很难弄清楚未显示的信息并推断出需要什么)。

但是,上次我帮助某人进行 ROS 订阅和类型推断时,很明显处理程序应该对消息进行shared_ptr。这可能有助于您开始查看解决方案:

  • 使用 boost::bind 进行订阅回调时出错