c++ - Error using boost::bind for subscribe callback -
we're getting compile error followed many more errors showing attempts match subscribe parameters possible candidate functions when using boost::bind callback subscribe.
error: no matching function call ‘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*> > >)’
our code follows. commented lines show code works when movegroup context (object pointer) 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; }
the handler takes movegroup& pass address of group
.
instead use ref(group)
:
boost::bind(contact_callback,_1,boost::ref(group))
or, indeed
std::bind(contact_callback,std::placeholders::_1,std::ref(group))
update:
your callback not adhere required signature:
void contact_callback(const geometry_msgs::wrenchstamped&, moveit::planning_interface::movegroup & group) {
must be
void contact_callback(const boost::shared_ptr<geometry_msgs::wrenchstamped const>, moveit::planning_interface::movegroup & group) {
at call site must either make 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 explicitly wrap in 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);
live demo
with roscpp/eigen stuff stubbed out:
#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(); }
prints
invoked! manipulator invoked! manipulator
Comments
Post a Comment