How to make callback function called by several subscriber?
Hello, I want to make multi-subscriber that calls same function with different argument.
For example,
std::vector<ros::Subscriber> sub;
std::vector<sensor_msgs::JointState> position;
position.resize(2);
sub.resize(2);
for(int i=0; i < sub.size(); i++)
{
char sub_name[20];
sprintf(sub_name, "sub_%d", i);
sub[i] = node.subscribe(sub_name, 1, &Myclass::callback, this);
}
Myclass::callback(const sensor_msgs::JointStateConstPtr &msg)
{
this->position[i] = *msg;
}
So my questions are:
How to sent array index i from main function to Myclass::callback?
Can several subscriber call same callback function like that?
Thank you for your answer. This is additional question.
I modified source code as follows:
position.resize(2);
sub.resize(2);
for(int i=0; i < sub.size(); i++)
{
char sub_name[20];
sprintf(sub_name, "sub_%d", i);
sub[i] = node.subscribe(sub_name, 1, boost::bind(&Myclass::callback, this, _1, i));
}
Myclass::callback(const sensor_msgs::JointStateConstPtr &msg, int i)
{
this->position[i] = *msg;
}
And this is error messages I got.
error: no matching function for call to ‘ros::NodeHandle::subscribe(char [20], int, boost::_bi::bind_t<void, boost::_mfi::mf2<void, Myclass, const boost::shared_ptr<const sensor_msgs::JointState_<std::allocator<void> > >&, int>, boost::_bi::list3<boost::_bi::value<Myclass*>, boost::arg<1>, boost::_bi::value<int> > >)’
and candidates are:
/opt/ros/groovy/include/ros/node_handle.h:379:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M), T*, const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:390:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M)const, T*, const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:438:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(const boost::shared_ptr<const MReq>&), T*, const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:448:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(const boost::shared_ptr<const MReq>&)const, T*, const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:498:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M), const boost::shared_ptr<U>&, const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:509:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M)const, const boost::shared_ptr<U>&, const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:559:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(const boost::shared_ptr<const MReq>&), const boost::shared_ptr<U>&, const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:570:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(const boost::shared_ptr<const MReq>&)const, const boost::shared_ptr<U>&, const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:618:14: note: template<class M> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (*)(M), const ros::TransportHints&)
/opt/ros/groovy/include/ros/node_handle.h:663:14: note: template<class M> ros::Subscriber ros::NodeHandle ...