ROS中C++ boost编程,类内回调函数

14 篇文章 0 订阅

首先熟悉boost::bind()

定义如下函数:

int f(int a, int b)
{
    return a + b;
}
 
int g(int a, int b, int c)
{
    return a + b + c;
}

boost::bind(f, 1, 2)可以产生一个无参函数对象,返回f(1, 2);类似的,boost::bind(g, 1, 2, 3)相当于g(1, 2, 3)

其中boost::bind()中使用较为频繁的还有占位符:

如:boost::bind(f, _1, 5) (x) 中,_1就是一个占位符,其位于f函数形参的第一形参 int a 的位置,5位于第二形参 int b 的位置;

_1 表示(x)参数列表的第一个参数;所以,boost::bind(f, _1, 5) (x) 相当于 f(x ,5)。再看下面这些例子:

boost::bind(f, _2, _1)(x, y); //相当于f(y,x),即_2表示后面参数列表的第二个位置:y
boost::bind(g, _1, 9, _1)(x); //相当于g(x, 9, x)
boost::bind(g, _3, _3, _3)(x, y, z); //相当于g(z, z, z)

ROS中的使用
ROS编程过程中,有许多需要给回调函数传递多个参数的情况,下面结合实例进行简单总结一下:
回调函数仅单个参数

#include <ros/ros.h>
#include <turtlesim/Pose.h>

void callback(const turtlesim::PoseConstPtr& msg)  //单个参数为:消息类型为turtlesim::Pose的常量指针msg
{
    float pose_x;
    pose_x = msg->x;
    ROS_INFO("x = [%f]",pose_x);  //打印出小乌龟所在的x坐标
}

回调函数含多个参数

#include <ros/ros.h>
#include <turtlesim/Pose.h>

void callback(const turtlesim::PoseConstPtr& msg, int x, int y)  //三个参数:常量指针msg、x、y
{
    float pose_x;
    pose_x = msg->x;
    ROS_INFO("x = [%f]",pose_x);  //打印出小乌龟所在的x坐标
    ROS_INFO("input_x = [%i] ; input_y = [%i]", x, y);  //打印出输入的参数x、y
}

主函数

int input_x = 1;
int input_y = 2;

int main(int argc, char** argv)
{
    ros::init(argc, argv, "test");
    ros::NodeHandle n;
    
    ros::Subscriber pose_sub = n.subscribe<turtlesim::Pose>("/turtle1/pose", 10, callback);
    //回调函数为单个参数时,这里直接使用callback,传递的单个参数为:接收话题名为/turtle1/pose中的内容;
    
    ros::Subscriber pose_sub = n.subscribe<turtlesim::Pose>("/turtle1/pose", 10, boost::bind(&callback, _1, input_x, input_y));    
    //这里回调函数为三个参数时,使用boost::bind(&callback, _1, input_x, input_y),这里_1即为占位符,为subscriber接收到的/turtle1/pose话题中的内容占位;相当于callback(turtlesim::PoseConst& msg, input_x, input_y)
    
    ros::Rate loop_rate(1);
    int i=1;
    while(i<=3)
    {
        ros::spinOnce();
        loop_rate.sleep();
        i++;
    }
    return 0;
}

可以这样理解 _1 因为我们要传入inpu_x y 但是直接写就把msg 的位置给占了,所以添加一个占位符为msg占位。

ROS 类内如何使用成员函数作为subscribe的回调函数(this指针)
模仿move_base中subscribe函数的形式,使用boost::bind()函数

odom_sub = private_nh.subscribe<nav_msgs::Odometry>("odom", 1, boost::bind(&PatrolRobot::odomCB, this, _1));

this表示指向当前类的指针,用boost函数传入,_1表示占位符,可以参考Subscribe的第四个参数用法

注意:以此形式写回调函数时,形参要写成constptr形式:

void odomCB(const nav_msgs::Odometry::ConstPtr &odom);

https://blog.csdn.net/m0_38089090/article/details/81195776?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&utm_relevant_index=1

ROS与C++入门教程-使用类方法作为回调函数

说明:

大多数教程使用函数的例子,而不是类方法。这是因为使用函数比较简单,不是因为不支持类方法。
本教程将向您展示如何使用类方法订阅服务回调。

订阅

假设你有简单的类,Listener参考代码:

class Listener
{
public:
void callback(const std_msgs::String::ConstPtr& msg);
};

NodeHandle::subscribe()使用函数作为回调,看起来如下:(参考代码)

ros::Subscriber sub = n.subscribe(“chatter”, 1000, chatterCallback);

使用类方法作为回调函数,看起来如下:(参考代码)

Listener listener;
ros::Subscriber sub = n.subscribe(“chatter”, 1000, &Listener::callback, &listener);

如果订阅在Listener内部,你替换最后的参数为关键词this,它意味着订阅会引用类的一部分。

服务端

假设你有简单类,AddTwo,参考代码
代码片段如下:

class AddTwo
{
public:
bool add(roscpp_tutorials::TwoInts::Request& req,
roscpp_tutorials::TwoInts::Response& res);
};

旧方法:NodeHandle::advertiseService()调用如下:(参考代码)
代码片段如下:

ros::ServiceServer service = n.advertiseService(“add_two_ints”, add);

类的版本如下:(参考代码)
代码片段如下:

AddTwo a;
ros::ServiceServer ss = n.advertiseService(“add_two_ints”, &AddTwo::add, &a);

https://www.ncnynl.com/archives/201701/1283.html

Subscribe的第四个参数用法

https://www.cnblogs.com/feixiao5566/p/4791990.html

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值