Robotics StackExchange | Archived questions

How to use boost::bind to pass more than one input arguments to callback?

Hi all,

It's a newbie question but I'm stuck to this one and I need a gently push to move forward.

So I need to pass 2 input arguments in a local callback of subscriber node.

My header file looks like:

#ifndef _GAZEBO_NETNORMALFORCECALC_PLUGIN_HH_
#define _GAZEBO_NETNORMALFORCECALC_PLUGIN_HH_

#include <string>
#include <gazebo/gazebo.hh>
#include "gazebo/transport/transport.hh"
#include "ignition/math/Vector3.hh"

#include <ros/ros.h>

namespace gazebo
{
  class NetForceCalcPlugin : public WorldPlugin
  {
    // Constactor
    public: NetForceCalcPlugin();

    // Destructor
    public: virtual ~NetForceCalcPlugin();

    //  Load the sensor plugin.
    // \param[in] _sensor Pointer to the sensor that loaded this plugin.
    // \param[in] _sdf SDF element that describes the plugin.
    public: virtual void Load(physics::WorldPtr _parent, sdf::ElementPtr /*_sdf*/);

    private: transport::NodePtr node;

    private: transport::SubscriberPtr normalSub1;

    private: ignition::math::Vector3d normal_wheel1;

  };
}
#endif

and my cc

#include "net_normalForce_calc.hh"

using namespace gazebo;

NetForceCalcPlugin::NetForceCalcPlugin() : WorldPlugin()
{
}

NetForceCalcPlugin::~NetForceCalcPlugin()
{
}

void cb1(ConstVector3dPtr &_msg, ignition::math::Vector3d normal)
{
  normal = ignition::math::Vector3d(_msg->x(), _msg->y(), _msg->z());
  ROS_INFO("Normal front_left: [%f, %f, %f]", _msg->x(), _msg->y(), _msg->z());
}

void NetForceCalcPlugin::Load(physics::WorldPtr _parent, sdf::ElementPtr /*_sdf*/)
{
  node = transport::NodePtr(new transport::Node());

  // Initiaize the node
  node->Init();

  // Listen to Gazebo world_stats topic
  normalSub1 = node->Subscribe("/gazebo/default/contact-surface/front_left_normal", boost::bind(&cb1, _1, normal_wheel1));
  ROS_INFO("Net normal stage1 completed succesfully");
}


// Register this plugin with the simulator
GZ_REGISTER_WORLD_PLUGIN(NetForceCalcPlugin)

I know that I probably miss many things but I cannot make it work. From my understanding the compiler waits for a different argument in the boost::bind placeholder and I give something else.

So I get back something looking like:

/home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc: In member function ‘virtual void gazebo::NetForceCalcPlugin::Load(gazebo::physics::WorldPtr, sdf::ElementPtr)’:
/home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc:27:121: error: no matching function for call to ‘gazebo::transport::Node::Subscribe(const char [50], boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const gazebo::msgs::Vector3d>&, ignition::math::Vector3<double>), boost::_bi::list2<boost::arg<1>, boost::_bi::value<ignition::math::Vector3<double> > > >)’
 ault/contact-surface/front_left_normal", boost::bind(&cb1, _1, normal_wheel1));
                                                                              ^
In file included from /usr/include/gazebo-7/gazebo/transport/transport.hh:6:0,
                 from /usr/include/gazebo-7/gazebo/gazebo_core.hh:22,
                 from /usr/include/gazebo-7/gazebo/gazebo.hh:20,
                 from /home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.hh:5,
                 from /home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc:1:
/usr/include/gazebo-7/gazebo/transport/Node.hh:177:21: note: candidate: template<class M, class T> gazebo::transport::SubscriberPtr gazebo::transport::Node::Subscribe(const string&, void (T::*)(const boost::shared_ptr<const T>&), T*, bool)
       SubscriberPtr Subscribe(const std::string &_topic,
                     ^
/usr/include/gazebo-7/gazebo/transport/Node.hh:177:21: note:   template argument deduction/substitution failed:
/home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc:27:121: note:   mismatched types ‘void (T::*)(const boost::shared_ptr<const T>&)’ and ‘boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const gazebo::msgs::Vector3d>&, ignition::math::Vector3<double>), boost::_bi::list2<boost::arg<1>, boost::_bi::value<ignition::math::Vector3<double> > > >’
 ault/contact-surface/front_left_normal", boost::bind(&cb1, _1, normal_wheel1));
                                                                              ^
In file included from /usr/include/gazebo-7/gazebo/transport/transport.hh:6:0,
                 from /usr/include/gazebo-7/gazebo/gazebo_core.hh:22,
                 from /usr/include/gazebo-7/gazebo/gazebo.hh:20,
                 from /home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.hh:5,
                 from /home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc:1:
/usr/include/gazebo-7/gazebo/transport/Node.hh:206:21: note: candidate: template<class M> gazebo::transport::SubscriberPtr gazebo::transport::Node::Subscribe(const string&, void (*)(const boost::shared_ptr<const T>&), bool)
       SubscriberPtr Subscribe(const std::string &_topic,
                     ^
/usr/include/gazebo-7/gazebo/transport/Node.hh:206:21: note:   template argument deduction/substitution failed:
/home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc:27:121: note:   mismatched types ‘void (*)(const boost::shared_ptr<const T>&)’ and ‘boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const gazebo::msgs::Vector3d>&, ignition::math::Vector3<double>), boost::_bi::list2<boost::arg<1>, boost::_bi::value<ignition::math::Vector3<double> > > >’
 ault/contact-surface/front_left_normal", boost::bind(&cb1, _1, normal_wheel1));
                                                                              ^
In file included from /usr/include/gazebo-7/gazebo/transport/transport.hh:6:0,
                 from /usr/include/gazebo-7/gazebo/gazebo_core.hh:22,
                 from /usr/include/gazebo-7/gazebo/gazebo.hh:20,
                 from /home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.hh:5,
                 from /home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc:1:
/usr/include/gazebo-7/gazebo/transport/Node.hh:236:21: note: candidate: template<class T> gazebo::transport::SubscriberPtr gazebo::transport::Node::Subscribe(const string&, void (T::*)(const string&), T*, bool)
       SubscriberPtr Subscribe(const std::string &_topic,
                     ^
/usr/include/gazebo-7/gazebo/transport/Node.hh:236:21: note:   template argument deduction/substitution failed:
/home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc:27:121: note:   mismatched types ‘void (T::*)(const string&)’ and ‘boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const gazebo::msgs::Vector3d>&, ignition::math::Vector3<double>), boost::_bi::list2<boost::arg<1>, boost::_bi::value<ignition::math::Vector3<double> > > >’
 ault/contact-surface/front_left_normal", boost::bind(&cb1, _1, normal_wheel1));
                                                                              ^
In file included from /usr/include/gazebo-7/gazebo/transport/transport.hh:6:0,
                 from /usr/include/gazebo-7/gazebo/gazebo_core.hh:22,
                 from /usr/include/gazebo-7/gazebo/gazebo.hh:20,
                 from /home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.hh:5,
                 from /home/angelos/catkin_ws/src/innoclimber/innoclimber_simulation/innoclimber_gazebo/plugins/net_normalForce_calc.cc:1:
/usr/include/gazebo-7/gazebo/transport/Node.hh:265:21: note: candidate: gazebo::transport::SubscriberPtr gazebo::transport::Node::Subscribe(const string&, void (*)(const string&), bool)
       SubscriberPtr Subscribe(const std::string &_topic,
                     ^
/usr/include/gazebo-7/gazebo/transport/Node.hh:265:21: note:   no known conversion for argument 2 from ‘boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const gazebo::msgs::Vector3d>&, ignition::math::Vector3<double>), boost::_bi::list2<boost::arg<1>, boost::_bi::value<ignition::math::Vector3<double> > > >’ to ‘void (*)(const string&) {aka void (*)(const std::__cxx11::basic_string<char>&)}’
innoclimber/innoclimber_simulation/innoclimber_gazebo/CMakeFiles/netNormalForceCalc_plugin.dir/build.make:62: recipe for target 'innoclimber/innoclimber_simulation/innoclimber_gazebo/CMakeFiles/netNormalForceCalc_plugin.dir/plugins/net_normalForce_calc.cc.o' failed
make[2]: *** [innoclimber/innoclimber_simulation/innoclimber_gazebo/CMakeFiles/netNormalForceCalc_plugin.dir/plugins/net_normalForce_calc.cc.o] Error 1
CMakeFiles/Makefile2:1636: recipe for target 'innoclimber/innoclimber_simulation/innoclimber_gazebo/CMakeFiles/netNormalForceCalc_plugin.dir/all' failed
make[1]: *** [innoclimber/innoclimber_simulation/innoclimber_gazebo/CMakeFiles/netNormalForceCalc_plugin.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j3 -l3" failed

Thanks for your time!

Asked by Angelos on 2017-06-30 04:16:29 UTC

Comments

Answers

Try using _2:

 normalSub1 = node->Subscribe("/gazebo/default/contact-surface/front_left_normal", boost::bind(&cb1, _1, _2));

Asked by chapulina on 2017-06-30 11:51:37 UTC

Comments

Unfortunately didn't work. I still trying to solve it. For the time being, I just declare the callback in the class header file and since my second argument is a class variable I can access it without passing it as argument. Anyway, thanks for your help Louise!

Asked by Angelos on 2017-07-02 08:53:40 UTC