ROS学习之带有用户自定义参数的回调函数
来源:互联网 发布:七月算法 邹博退出 编辑:程序博客网 时间:2024/05/20 03:41
#include "ros/ros.h"#include "std_msgs/String.h"/** * This tutorial demonstrates a simple use of Boost.Bind to pass arbitrary data into a subscription * callback. For more information on Boost.Bind see the documentation on the boost homepage, * http://www.boost.org/ */class Listener{public: ros::NodeHandle node_handle_; ros::V_Subscriber subs_; //std::vector<Subscriber> 向量容器 Listener(const ros::NodeHandle& node_handle) : node_handle_(node_handle) //冒号的含义是使用参数node_handle对类的成员node_handle_进行初始化 { } void init() //Listener类的init()方法 { // std::vector<Subscriber>.push_back()在容器尾部加入一个Subscriber对象(节点句柄的subscribe方法返回的) , // boost::bind方法将一个函数转化成另一个函数 subs_.push_back(node_handle_.subscribe<std_msgs::String>("chatter", 1000, boost::bind(&Listener::chatterCallback, this, _1, "User 1"))); subs_.push_back(node_handle_.subscribe<std_msgs::String>("chatter", 1000, boost::bind(&Listener::chatterCallback, this, _1, "User 2"))); subs_.push_back(node_handle_.subscribe<std_msgs::String>("chatter", 1000, boost::bind(&Listener::chatterCallback, this, _1, "User 3"))); } void chatterCallback(const std_msgs::String::ConstPtr& msg, std::string user_string) //被boost::bind()转化之前的消息回调函数 { ROS_INFO("I heard: [%s] with user string [%s]", msg->data.c_str(), user_string.c_str()); }};int main(int argc, char **argv){ ros::init(argc, argv, "listener_with_userdata"); ros::NodeHandle n; //创建节点句柄 Listener l(n); //创建Listener类 l.init(); //调用Listener类的init()方法,创建3个话题订阅者,并压入容器 ros::spin(); //调用spin()方法,统一调用消息回调函数 return 0;}
boost::bind()将第一个参数(函数对象)转化为另一个函数对象,转化之前的函数参数由_x占位符代替
这里,在消息处理时,消息被传入boost::bind(),它将该消息传入chatterCallback()函数的第一个参数,第二个参数使用“User 1”等。
参考链接: http://blog.csdn.net/hopingwhite/article/details/6278472
0 0
- ROS学习之带有用户自定义参数的回调函数
- ROS学习之 cpp回调函数和轮转(spin)
- 对于js定时器中带有参数的回调函数没有延迟效果的解决方法
- 在C#里面调用带有回调函数和自定义结构体的DLL的例程
- 在C#里面调用带有回调函数和自定义结构体的DLL的例程
- CodeIgniter 表单验证之回调函数的参数问题
- c#调用c++带有回调函数方法的实现
- c# 调用c++带有回调函数方法的实现
- 在C#里面调用带有回调函数和自定义结构体的DLL的例程——转载
- 回调函数的学习
- 自定义回调函数
- 自定义回调函数
- 关于回调函数的参数
- android 开发零起步学习笔记(十七):自定义android用户控件,使用回调函数实现自定义事件
- 自定义回调函数原型的方法
- 自定义View的回调函数
- 自定义view的回调函数
- cocos2dx3.0自定义回调函数,传递自己想要的参数
- 优化php效率,提高php性能的一些方法
- HDU 1059 Dividing
- neural-networks-and-deep-learning false_minimum.py
- 剑指offer——左旋转字符串
- nodejs代码封装基础
- ROS学习之带有用户自定义参数的回调函数
- Java-HashMap详解
- POJ2366
- python文件转换为exe可执行文件的方法
- Linux-tail命令使用方法演示例子
- 在MAC下配置mysql 5.6.31 及以上的 数据库的默认编码问题
- 换行符
- WinDbg配置和使用基础
- POJ 3150 循环矩阵的应用