使用boost :: bind进行订阅回调时出错 [英] Error using boost::bind for subscribe callback

查看:149
本文介绍了使用boost :: bind进行订阅回调时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们遇到了此编译错误,随后出现了更多错误,这些错误表明尝试将boost :: bind用作订阅回调时,将订阅参数与所有可能的候选函数进行匹配.

We're getting this compile error followed by many more errors showing attempts to match the subscribe parameters to all possible candidate functions when using boost::bind as a callback for 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上下文(对象指针)时有效的代码.

Our code is as follows. The commented lines show the code which works when the MoveGroup context (object pointer) is not passed.

#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;
}

推荐答案

处理程序采用MoveGroup&但您将其传递为group的地址.

The handler takes a MoveGroup& but you pass it the address of group.

代替使用ref(group):

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

或者,实际上

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

更新:

您的回调不遵循必需的签名:

Your callback does not adhere to the required signature:

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) {


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


At the call site you must either make the message type explicit (it's in non-deducible context):

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

OR ,您可以先明确地将其包装在function<>中:

OR you could simply explicitly wrap in a function<> first:

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 Demo

With all roscpp/Eigen stuff stubbed out:

在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

这篇关于使用boost :: bind进行订阅回调时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆