【问题标题】:ROS subscribe callback - using boost::bind with member functionsROS 订阅回调 - 使用 boost::bind 和成员函数
【发布时间】:2017-08-26 20:13:13
【问题描述】:

我正在尝试为所有主题使用相同的回调订阅 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&lt;_Tp&gt; 而不是VO::VOBase*(尽管引用不存在:use of deleted function),使用boost::bind 而不是std::bind (但它应该都是一样的,因为C + +11),在回调函数参数中(以及在subscribe&lt;acl_msgs::ViconState::ConstPtr&gt;等中)带有和不带有...::ConstPtr的ROS消息。所以我只是在这里处理部分解决方案及其排列......

有什么线索吗?

【问题讨论】:

  • 抱歉,为什么首先使用 bind 而不是 lambda?

标签: c++ c++11 boost ros reference-wrapper


【解决方案1】:

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

但是,上次我帮助某人处理 ROS 订阅和类型推断时,很明显处理程序应该将 shared_ptr 带到消息中。这可能会帮助您开始寻找解决方案:

【讨论】:

  • the docs 也揭示了非共享指针重载。我稍后会尝试看看。同时,请阅读其他答案,它可能会为您提供线索。
  • 谢谢,我曾尝试使用shared_ptr,但由于某种原因,它以前没有成功。现在我重写了整件事,它奏效了!
猜你喜欢
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
相关资源
最近更新 更多