如何在 ROS2 和 C++ 中通过 message_filters 在订阅者中使用 QoS?

问题描述 投票:0回答:1

我使用消息过滤器将 3 个 ROS2 主题合并为一个并发布。我使用 ROS2 和谦虚的 C++。所以在ROS2中订阅主题时需要声明QoS。这里的代码可以工作,但没有 QoS。

#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/rclcpp.hpp"
#include "rmw/types.h"
#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")
  {

    
     // Create publishers for the merged IMU topic`
    imu_pub_ = this->create_publisher<sensor_msgs::msg::Imu>("/robot/imu", 10);
    
    subscription_imu_1_.subscribe(this, "/robot/attitude",);
    subscription_imu_2_.subscribe(this, "/robot/angular_velocity");
    subscription_imu_3_.subscribe(this, "/robot/linear_acceleration");

    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
  {
    // Create the IMU message and populate its fields
    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;

    // Publish the merged IMU message
    RCLCPP_INFO(this->get_logger(), "Mono_Inertial node started");
    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。所以不知道订阅ROS2主题时如何声明。所以我的问题是如何在这段代码中使用它的 QoS?

c++ ros qos
1个回答
0
投票

我重新提出这个问题,因为我有类似的问题。我尝试使用 message_filter (sync_policies/approximate_time) 同步 2 个 ROS2 主题:

    来自 Imu 的
  • a sensor_msgs::msgs::Imu 来自雷达的
  • a
  • sensor_msgs::msgs::PointCloud2,与 QoS rclcpp::SensorDataQoS() 一起发布
默认情况下,每个主题的订阅者都是 Reliable,这与 pointcloud2 的发布者 QoS(尽力而为)不兼容。

我认为保留 SensorDataQoS() 对于来自雷达或激光雷达的数据来说是最好的。那么,如何才能将 pointcloud2 主题与另一个主题同步,同时保持 QoS。有没有办法用 message_filter 指定订阅者的 QoS?或者我应该使用另一种方式?

© www.soinside.com 2019 - 2024. All rights reserved.