zoukankan      html  css  js  c++  java
  • ROS入门(五) C++使用boost::bind 订阅消息中的返回函数传入多个参数

    最近读别人的代码,看到了一个有意思的东西。

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

    例如:

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

      

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

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

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

    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 
    ",x);
      printf("%d 
    ",y);
    }
    void  multiprint(const std_msgs::Int8::ConstPtr& msg)
    {
      printf("%d 
    ",*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 
    ",*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 
    ",x);
      printf("%d 
    ",y);
    }
    void  multiprint(const std_msgs::Int8::ConstPtr& msg)
    {
      printf("%d 
    ",*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 
    ",x);
      printf("%d 
    ",y);
    }

    其中

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

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

    其返回结果应该是

    12
    
    0
    
    0

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

    参考:

    Boost::bind工作原理 : https://www.cnblogs.com/blueoverflow/p/4740093.html

    参考笔记A0220180206

  • 相关阅读:
    关于学习c++服务端的进度,一边更新,一边学习
    Ubuntu20.04 用git上传GitHub文件
    省赛菜鸡打铜记
    Filename too long unable to checkout working tree warning: Clone succeeded, but checkout failed.
    java 对象转Json字符串 --- 报declares multiple JSON fields named XXX 解决方法
    配置win10电脑右下角任务栏时间显示时分秒
    HTTP与HTTPS
    为人三会 口才三绝 修心三不
    1093
    java项目clone后不显示maven
  • 原文地址:https://www.cnblogs.com/TIANHUAHUA/p/8418818.html
Copyright © 2011-2022 走看看