ROS下的多参数调用,boost::bind使用

主要是当我们订阅一个消息时候,会调用一个返回函数。

例如:

1

ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("/test", 10, multiprint);

  

这样multiprint函数应该包含一个参数,即

1

void  multiprint(const std_msgs::Int8::ConstPtr& msg){}

但是,如果我们想要多参数传入的话,就需要使用boost库中的bind函数。例如,当我们的回调函数是这样的:

1

void  multiprint(const std_msgs::Int8::ConstPtr& msg, int& x, int& y){}

  

Boost::bind    

 在STL中,我们经常需要使用bind1st,bind2st函数绑定器和fun_ptr,mem_fun等函数适配器,这些函数绑定器和函数适配器使用起来比较麻烦,需要根据是全局函数还是类的成员函数,是一个参数还是多个参数等做出不同的选择,而且有些情况使用STL提供的不能满足要求,所以如果可以我们最好使用boost提供的bind,它提供了统一的接口,提供了更多的支持,比如说它增加了shared_ptr,虚函数,类成员的绑定。

 bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数的个数和要绑定的调用对象的类型,总共有数十种不同的形式,编译器会根据具体的绑定代码制动确定要使用的正确的形式,bind的基本形式如下:

复制代码

1 template<class R,class F> bind(F f);
2 template<class R,class F,class A1> bind(F f,A1 a1);
3 namespace
4 {
5 boost::arg<1> _1;
6 boost::arg<2> _2;
7 boost::arg<3> _3;
8 …..                                     //其他6个占位符
9 };

复制代码

  bind接收的第一个参数必须是一个可调用的对象f,包括函数函数指针函数对象、和成员函数指针,之后bind最多接受9个参数参数数量必须与f的参数数量相等,这些参数被传递给f作为入参。 绑定完成后,bind返回一个函数对象,它内部保存了f的拷贝,具有operator()返回值类型自动推导f的返回类型。在发生调用时这个函数对象将把之前存储的参数转发给f完成调用。例如,有一个函数func,它的形式是:

1 func(a1,a2);

那么,他将等价于一个具有无参operator()的bind函数对象调用:

1 bind(func,a1,a2)();

  这是bind最简单的形式,bind表达式存储了func和a1、a2的拷贝,产生了一个临时函数对象。因为func接收两个参数,而a1和a2的拷贝传递给func完成真正的函数调用。

  bind的真正威力在于它的占位符,它们分别定义为_1,_2,_3,一直到 _9,位于一个匿名的名字空间。占位符可以取代bind参数的位置,在发生调用时接受真正的参数占位符的名字表示它在调用式中的顺序,而在绑定的表达式中没有没有顺序的要求,_1不一定必须第一个出现,也不一定只出现一次,例如:

1 bind(func,_2,_1)(a1,a2);

  返回一个具有两个参数的函数对象,第一个参数将放在func的第二个位置,而第二个参数则放在第一个位置,调用时等价于:

1 func(a2,a1);

  

  占位符的最常见的使用方法就是printf中%f,%d等等这些的使用方法。

 

 

例程    

  下面是一个简单的订阅消息的例程,在只传入单个变量的时候如下:

复制代码

#include <ros/ros.h>
#include <std_msgs/Int8.h>


int index1=0;
int index2=0;
void  multiprint(const std_msgs::Int8::ConstPtr msg, int& x, int& y)
{
  printf("%d",*msg);
  printf("%d \r\n",x);
  printf("%d \r\n",y);
}
void  multiprint(const std_msgs::Int8::ConstPtr& msg)
{
  printf("%d \r\n",*msg);
}

int main(int argc, char **argv)
{

    ros::init(argc, argv, "multi_callback");
    ros::NodeHandle n;
    ros::NodeHandle private_nh("~");

    int  rate;

    private_nh.param("rate",rate, 40);

    // ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("/test", 10, boost::bind(&multiprint, _1, index1, index2));
    ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("test", 10, multiprint);
    ros::Rate r(rate);
    while(n.ok())
     {
       ros::spinOnce();
       r.sleep();
     }

    return 0;
}

复制代码

  当通过rostopic传入消息的时候,

 rostopic pub /test std_msgs/Int8 "data: 12" 

  subscrib函数选择回调,

void  multiprint(const std_msgs::Int8::ConstPtr& msg)
{
  printf("%d \r\n",*msg);
}

  输出的结果就应该是消息的输入,即

12

  

 

  当程序改变为:

复制代码

#include <ros/ros.h>
#include <std_msgs/Int8.h>


int index1=0;
int index2=0;
void  multiprint(const std_msgs::Int8::ConstPtr msg, int& x, int& y)
{
  printf("%d",*msg);
  printf("%d \r\n",x);
  printf("%d \r\n",y);
}
void  multiprint(const std_msgs::Int8::ConstPtr& msg)
{
  printf("%d \r\n",*msg);
}

int main(int argc, char **argv)
{

    ros::init(argc, argv, "multi_callback");
    ros::NodeHandle n;
    ros::NodeHandle private_nh("~");

    int  rate;

    private_nh.param("rate",rate, 40);

    ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("/test", 10, boost::bind(&multiprint, _1, index1, index2));
    // ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("test", 10, multiprint);
    ros::Rate r(rate);
    while(n.ok())
     {
       ros::spinOnce();
       r.sleep();
     }

    return 0;
} 

复制代码

  注意:方程中的消息的参数类型后面一定要跟上::ConstPtr,否则会报错。消息的前面因为是_1占位符传入,所以回调函数里参数调用的msg前不能加&。

  同样通过rostopic传入消息,这样地话运行时回调用

复制代码

void  multiprint(const std_msgs::Int8::ConstPtr& msg, int& x, int& y)
{
  printf("%d",*msg);
  printf("%d \r\n",x);
  printf("%d \r\n",y);
}

复制代码

其中

 boost::bind(&multiprint, _1, index1, index2)

的_1部分,是后面的获得的消息的传入。

其返回结果应该是

12

0

0

这样就可以在回调函数中传入多个参数了。

 

转载自:https://www.cnblogs.com/TIANHUAHUA/p/8418818.html

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值