当前位置:   article > 正文

关于ROS在一个回调函数中处理两个订阅话题消息(多话题回调、多参数回调问题)_ros 服务 回调函数多参数

ros 服务 回调函数多参数

最近遇到需要在一个回调函数中处理多话题消息的问题,特此记录下在一个回调函数中处理多话题、多参数的方法。

举例:

  1. #include <iostream>
  2. #include <boost/bind.hpp>
  3. #include <ros/ros.h>
  4. #include <sensor_msgs/Image.h>
  5. #include "detectmsg/SpatialDetection.h"
  6. #include "detectmsg/SpatialDetectionArray.h"
  7. #include "detectmsg/pose2D.h"
  8. #include "detectmsg/multi_pedestrian_pose.h"
  9. ros::Publisher obj_pub_;
  10. ros::Subscriber obj_sub_;
  11. ros::Subscriber image_sub_;
  12. int Class_ID;
  13. std::string pub_topic;
  14. std::string sub_topic;
  15. void Callback(const detectmsg::SpatialDetectionArray::ConstPtr& detect, const sensor_msgs::Image::ConstPtr& image)
  16. {
  17. ROS_INFO("ok!");
  18. }
  19. int main(int argc, char **argv)
  20. {
  21. ros::init(argc, argv, "obj_publish_node");
  22. ros::NodeHandle nh;
  23. int ErrorParam=0;
  24. ErrorParam += !nh.getParam("objdetectnode/Class_ID_", Class_ID);
  25. ErrorParam += !nh.getParam("objdetectnode/pub_topic_", pub_topic);
  26. ErrorParam += !nh.getParam("objdetectnode/sub_topic_", sub_topic);
  27. if(ErrorParam>0){
  28. ROS_INFO("Error Param Load! Error Number:%d",ErrorParam);
  29. return 1;
  30. }
  31. ROS_INFO("SUCCESSFULLY STARTED");
  32. obj_pub_ = nh.advertise<detectmsg::multi_pedestrian_pose>(pub_topic, 1);
  33. obj_sub_ = nh.subscribe<detectmsg::SpatialDetectionArray>(sub_topic, 1, boost::bind(&Callback, _1 , sensor_msgs::Image::ConstPtr()));
  34. image_sub_ = nh.subscribe<sensor_msgs::Image>("/yolov4_publisher/color/image", 1, boost::bind(&Callback, detectmsg::SpatialDetectionArray::ConstPtr(), _1));
  35. ros::spin();
  36. return 0;
  37. }

这是一段没写完的代码,主要用来测试一下多参数回调问题,最关键的部分在订阅话题的两端代码上,其他的代码不用看,需要注意到地方会在下文提到。

1、要实现多话题回调需要包含头文件,对应代码:#include <boost/bind.hpp>

2、要实现两个话题传向一个回调函数,首先先分别写出两个订阅话题的代码,最常用的订阅话题的代码段格式为:

obj_sub_ = nh.subscribe<detectmsg::SpatialDetectionArray>(sub_topic, 1, &Callback);

要实现多话题回调需要使用到函数boost::bind(),这里可以参考上文的代码(以obj_sub_为例),将原来回调函数的位置替换为该函数,函数内有三个参数,第一个是这两个话题进行绑定后共同传入的回调函数名称,第二个参数是一个占位符,为下面的话题订阅占位,最后一位为传入当前语句订阅的话题,注意结尾的::ConstPtr()。

image_sub_的语句是类似的,唯一的区别是占位的时候需要注意占位符位置的变化以及消息格式已经变成了上一个话题的消息格式。这里我特意选择了两个不同的消息格式防止混淆,相信各位一对比就能很清楚的明白其中的规律。

最后就是注意下一下在回调函数的参数部分,两个参数的顺序不能颠倒,同时注意const。

---------------------------------------------------------------------------------------------------------------------------------

上面代码经过测试是正常运行的,但是我尝试进行三个话题的同步回调时遇到了问题,按照上述写法,在回调函数中每次只能读取到一个话题的消息,不知道是因为时间同步问题导致还是代码里逻辑有问题,总之运行是不正确的,运行有问题的代码如下:

  1. // obj_sub_ = nh.subscribe<yolov5_ros_msgs::BoundingBoxes>("/yolov5/BoundingBoxes", 1,
  2. // boost::bind(&Callback, _1 , sensor_msgs::Image::ConstPtr(),sensor_msgs::CameraInfo::ConstPtr()));
  3. // image_sub_ = nh.subscribe<sensor_msgs::Image>("/stereo_publisher/stereo/depth", 1,
  4. // boost::bind(&Callback, yolov5_ros_msgs::BoundingBoxes::ConstPtr(), _1,sensor_msgs::CameraInfo::ConstPtr()));
  5. // caminfo_sub_ = nh.subscribe<sensor_msgs::CameraInfo>("/stereo_publisher/stereo/camera_info", 1,
  6. // boost::bind(&Callback, yolov5_ros_msgs::BoundingBoxes::ConstPtr(), sensor_msgs::Image::ConstPtr(),_1));

这段代码语法上是没有问题的,是可以编译通过的,我因为该代码的不正确运行,报的段错误,找到错误花了些时间,有一定迷惑性。

找到错误后更换了订阅话题方法,新方法用到了message_filters这个库,没有安装的话用下面的代码安装即可:

  1. sudo apt-get install ros-melodic-message-filters
  2. //中间部分换成自己的版本

使用时包含头文件并并在cmakelists文件中find package部分进行添加

  1. //头文件
  2. #include<message_filters/subscriber.h>
  3. #include<message_filters/time_synchronizer.h>
  4. ----------------------------------------------------
  5. //CMakelists.txt
  6. find_package(catkin REQUIRED COMPONENTS
  7. ...
  8. message_filters
  9. )
  10. ...
  11. target_link_libraries(your_executable_name
  12. ...
  13. ${catkin_LIBRARIES}
  14. )

cpp中使用方法,还是上面的例子:

  1. message_filters::Subscriber<yolov5_ros_msgs::BoundingBoxes> detect_sub(nh, "/yolov5/BoundingBoxes", 1);
  2. message_filters::Subscriber<sensor_msgs::Image> image_sub(nh, "/stereo_publisher/stereo/depth", 1);
  3. message_filters::Subscriber<sensor_msgs::CameraInfo> caminfo_sub(nh, "/stereo_publisher/stereo/camera_info", 1);
  4. message_filters::TimeSynchronizer<yolov5_ros_msgs::BoundingBoxes, sensor_msgs::Image, sensor_msgs::CameraInfo> sync(detect_sub, image_sub, caminfo_sub, 10);
  5. sync.registerCallback(boost::bind(&Callback, _1, _2, _3));
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号