2017-08-26 417 views
1

我试图订阅ROS中的不同主题(每个弹出的汽车都使用一个),使用相同的回调函数。这个想法是boost::bind将通过主题名称作为额外的参数,所以我知道我应该在回调中访问哪个车辆。ROS订阅回调 - 对成员函数使用boost :: bind

问题是,即使我已经经历了关于该主题的多个问题,但没有任何解决方案似乎可行。

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

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以来应该都是相同的),有和没有...::ConstPtr的ROS在回调函数的参数消息(以及subscribe<acl_msgs::ViconState::ConstPtr>等,所以我只是有部分解决方案,在这里他们的排列杂耍......

任何线索?

+0

对不起,为什么使用绑定,而不是摆在首位拉姆达? –

回答

0

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

但是,上次我帮助某人使用ROS订阅和类型扣除时,处理程序显然应该将shared_ptr传递给消息。这可能会帮助你开始看到一个解决方案:

+0

嗯[文档](http://docs.ros.org/indigo/api/roscpp/html/classros_1_1NodeHandle.html#a317fe4c05919e0bf3fb5162ccb2f7c28)也显示非共享ptr重载。我稍后会试着看看。同时,请阅读其他答案,它可能会给你一个线索。 – sehe

+0

谢谢,我曾尝试使用'shared_ptr',但出于某种原因,它以前没有解决过。现在我重写了整个事情,它的工作! – Pronex