You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ROS2 Humble的C++代码中为message_filters订阅者配置QoS

ROS2 Humble中为message_filters添加QoS配置的方法

针对你的需求,以下是给message_filters订阅者添加QoS配置的具体实现,同时为发布者配置匹配的QoS(传感器类话题推荐使用ROS2预设的SensorDataQoS):

修改后的完整代码

#include <memory>
#include <string>
#include <cstring>

#include "rclcpp/rclcpp.hpp"
#include "geometry_msgs/msg/quaternion.hpp"
#include "sensor_msgs/msg/imu.hpp"
#include "geometry_msgs/msg/vector3.hpp"
#include "message_filters/subscriber.h"
#include "message_filters/time_synchronizer.h"
#include "geometry_msgs/msg/quaternion_stamped.hpp"
#include "geometry_msgs/msg/vector3_stamped.hpp"

#include "rclcpp/qos.hpp"

using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;


class ExactTimeSubscriber : public rclcpp::Node
{
public:
  ExactTimeSubscriber()
      : Node("exact_time_subscriber")
  {
      // 配置QoS:使用传感器数据专用的预设QoS(适配IMU类高频实时数据)
      auto qos_profile = rclcpp::SensorDataQoS();
      // 也可自定义QoS参数,示例:
      // qos_profile.keep_last(10); // 设置队列深度
      // qos_profile.reliability(RMW_QOS_POLICY_RELIABILITY_RELIABLE); // 可靠传输
      // qos_profile.durability(RMW_QOS_POLICY_DURABILITY_VOLATILE); // 非持久化

      // 创建带QoS配置的发布者
      imu_pub_ = this->create_publisher<sensor_msgs::msg::Imu>("/robot/imu", qos_profile);
      
      // 为每个message_filters订阅者传入QoS配置
      subscription_imu_1_.subscribe(this, "/robot/attitude", qos_profile);
      subscription_imu_2_.subscribe(this, "/robot/angular_velocity", qos_profile);
      subscription_imu_3_.subscribe(this, "/robot/linear_acceleration", qos_profile);

      sync_ = std::make_shared<message_filters::TimeSynchronizer<geometry_msgs::msg::QuaternionStamped, geometry_msgs::msg::Vector3Stamped, geometry_msgs::msg::Vector3Stamped>>(subscription_imu_1_, subscription_imu_2_, subscription_imu_3_, 3);
      sync_->registerCallback(std::bind(&ExactTimeSubscriber::topic_callback, this,  _1, _2, _3));
  }

private:  
  void topic_callback(const geometry_msgs::msg::QuaternionStamped::ConstSharedPtr& msg1, const geometry_msgs::msg::Vector3Stamped::ConstSharedPtr& msg2, const geometry_msgs::msg::Vector3Stamped::ConstSharedPtr& msg3) const
  {
      // 创建IMU消息并填充字段
      sensor_msgs::msg::Imu imu_msg;
      imu_msg.header = msg1->header;
      imu_msg.orientation = msg1->quaternion;
      imu_msg.linear_acceleration = msg2->vector;
      imu_msg.angular_velocity = msg3->vector;

      // 发布合并后的IMU消息
      RCLCPP_INFO(this->get_logger(), "发布合并后的IMU数据");
      imu_pub_->publish(imu_msg);
  }
  
  rclcpp::Publisher<sensor_msgs::msg::Imu>::SharedPtr imu_pub_;
  message_filters::Subscriber<geometry_msgs::msg::QuaternionStamped> subscription_imu_1_;
  message_filters::Subscriber<geometry_msgs::msg::Vector3Stamped> subscription_imu_2_;
  message_filters::Subscriber<geometry_msgs::msg::Vector3Stamped> subscription_imu_3_;

  std::shared_ptr<message_filters::TimeSynchronizer<geometry_msgs::msg::QuaternionStamped  , geometry_msgs::msg::Vector3Stamped, geometry_msgs::msg::Vector3Stamped>> sync_;
};

int main(int argc, char *argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<ExactTimeSubscriber>());
  rclcpp::shutdown();

  return 0;
}

关键配置说明

  • 预设QoS使用:rclcpp::SensorDataQoS()是ROS2专为传感器类话题设计的预设配置,默认包含keep_last(10)队列深度、RELIABLE可靠传输、VOLATILE非持久化,完全适配IMU这类高频实时数据场景。
  • 自定义QoS:若需调整参数,可在创建QoS对象后修改对应属性,比如切换为尽力而为传输模式、调整队列大小等。
  • message_filters订阅配置:message_filters::Subscriber的subscribe方法支持传入QoS配置对象,作为第三个参数即可完成订阅端的QoS设置。
  • 发布者QoS匹配:发布者的QoS建议和订阅端保持一致,避免因QoS不兼容导致消息无法正常收发。

内容的提问来源于stack exchange,提问作者Macedon971

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 22:26:02