3

当使用 boost::bind 作为 subscribe 的回调时,我们得到了这个编译错误,随后还有更多错误显示尝试将 subscribe 参数与所有可能的候选函数匹配。

error: no matching function for call to ‘ros::NodeHandle::subscribe(const char [18], int, boost::_bi::bind_t<void, void (*)(const geometry_msgs::WrenchStamped_<std::allocator<void> >&, moveit::planning_interface::MoveGroup&), boost::_bi::list2<boost::arg<1>, boost::_bi::value<moveit::planning_interface::MoveGroup*> > >)’

我们的代码如下。注释行显示了在未传递 MoveGroup 上下文(对象指针)时工作的代码。

#include <stdio.h>
#include <boost/bind.hpp>
#include <geometry_msgs/WrenchStamped.h>
#include <moveit/move_group_interface/move_group.h>

using namespace Eigen;
using namespace std;

//void contact_callback(const geometry_msgs::WrenchStamped& msg) {
void contact_callback(const geometry_msgs::WrenchStamped& msg, moveit::planning_interface::MoveGroup &group){
    //if(msg.wrench.force.z > 5) group.stop();
}

int main(int argc, char **argv) {
    ros::init(argc, argv, "get_stiffness");
    ros::NodeHandle node_handle; 
    ros::AsyncSpinner spinner(1);
    spinner.start();
    moveit::planning_interface::MoveGroup group("manipulator");
    ros::Subscriber contact_sub;
    //contact_sub = node_handle.subscribe("/finger1/nano17ft",1,contact_callback);
    contact_sub = node_handle.subscribe("/finger1/nano17ft",100,boost::bind(contact_callback,_1,&group));
    ros::waitForShutdown();
    return 0;
}
4

1 回答 1

3

处理程序采用 MoveGroup& 但您将其传递给group.

而是使用ref(group)

boost::bind(contact_callback,_1,boost::ref(group))

或者,确实

std::bind(contact_callback,std::placeholders::_1,std::ref(group))

更新

您的回调不符合要求的签名:

void contact_callback(const geometry_msgs::WrenchStamped&, moveit::planning_interface::MoveGroup & group) {

一定是

void contact_callback(const boost::shared_ptr<geometry_msgs::WrenchStamped const>, moveit::planning_interface::MoveGroup & group) {

在呼叫站点,您必须明确消息类型(它在不可推导的上下文中):

contact_sub = node_handle.subscribe<geometry_msgs::WrenchStamped>("/finger1/nano17ft", 100,
        boost::bind(contact_callback, _1, boost::ref(group)));

或者您可以简单地明确包装function<>第一个:

boost::function<void(const boost::shared_ptr<geometry_msgs::WrenchStamped const>&)> callback =
    boost::bind(contact_callback, _1, boost::ref(group));
contact_sub = node_handle.subscribe("/finger1/nano17ft", 100, callback);

现场演示

去掉所有 roscpp/Eigen 的东西:

Live On Coliru

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <iostream>

////////////////// STUBS STUBS STUBS //////
//#include <geometry_msgs/WrenchStamped.h>
namespace Eigen {}
namespace geometry_msgs {
struct WrenchStamped {}; }
namespace moveit { namespace planning_interface { struct MoveGroup { std::string name; MoveGroup(std::string s):name(s){} }; } }

namespace ros {
    void init(...) {}
    void waitForShutdown(...) {}
    struct Subscriber {};
    struct NodeHandle {
        using VoidConstPtr = void const *;
        enum class TransportHints {};
        template <typename M>
        Subscriber subscribe(const std::string &topic, uint32_t queue_size,
                const boost::function<void(const boost::shared_ptr<M const> &)> &callback,
                const VoidConstPtr &tracked_object = VoidConstPtr(),
                const TransportHints &transport_hints = TransportHints())
        { 
            (void)topic, (void)queue_size, void(tracked_object), void(transport_hints);
            callback({});
            return {};
        }
    };
    struct AsyncSpinner {
        AsyncSpinner(int) {}
        void start() {}
    };
};
//#include <moveit/move_group_interface/move_group.h>
////////////////// END STUBS END STUBS END STUBS //////

using namespace Eigen;

void contact_callback(const boost::shared_ptr<geometry_msgs::WrenchStamped const>, moveit::planning_interface::MoveGroup & group) {
    // if(msg.wrench.force.z > 5) group.stop();
    std::cout << "Invoked! " << group.name << "\n";
}

int main(int argc, char **argv) {
    ros::init(argc, argv, "get_stiffness");
    ros::NodeHandle node_handle;
    ros::AsyncSpinner spinner(1);
    spinner.start();
    moveit::planning_interface::MoveGroup group("manipulator");
    ros::Subscriber contact_sub;

    contact_sub = node_handle.subscribe<geometry_msgs::WrenchStamped>("/finger1/nano17ft", 100,
            boost::bind(contact_callback, _1, boost::ref(group)));
    {
        boost::function<void(const boost::shared_ptr<geometry_msgs::WrenchStamped const>&)> callback =
            boost::bind(contact_callback, _1, boost::ref(group));
        contact_sub = node_handle.subscribe("/finger1/nano17ft", 100, callback);
    }
    ros::waitForShutdown();
}

印刷

Invoked! manipulator
Invoked! manipulator
于 2017-07-25T18:39:11.040 回答