2017-08-26 412 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