(不是抄的)ROS中回调函数传入多个参数
查了好久发现都是抄来抄去的,不能解决问题。
问题描述:回调函数中传入多个参数,如何实现subscribe?
先放参考链接:Passing multiple arguments to a callback. (C++)
再看修改完的代码,基本一看就明白了:
#include "ros/ros.h"
#include "sensor_msgs/Image.h"
#include "cv_bridge/cv_bridge.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
void image_callback(const sensor_msgs::ImageConstPtr &img_msg,
std::string &output_path)
{
//...
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "");
if (argc < 3){
std::cerr<< "too few arguments: --topic_name --save_jpg_path\n";
}
if(argc > 3){
std::cerr<< "too many arguments: --topic_name --save_jpg_path\n";
}
std::string topic_name = argv[1]; // "/usb_cam/image_raw"
std::string output_path = argv[2]; // /home/zhang/save_jpg/
std::cout<< topic_name << " " << output_path << std::endl;
ros::init(argc, argv, "bag2img");
ros::NodeHandle nh;
// 主要就是这一行,boost::bind(fun, _1, arg1, arg2), _1 表示第一个参数img_msg
ros::Subscriber sub = nh.subscribe<sensor_msgs::Image>(topic_name, 10,
boost::bind(image_callback, _1, output_path));
ros::spin();
return 0;
}
如果出现error,注意检查 subscribe 实例化的类型是否正确