ros2 foxy 设置publisher与subscription的qos

ros2中qos介绍及对应关系

#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/point_cloud2.hpp"

int num=0;
void chatterCallback(const sensor_msgs::msg::PointCloud2::SharedPtr msg)
{
    num++;
    std::cout << "I heard: " <create_publisher("pointcloud_test", rclcpp::QoS(rclcpp::KeepLast(1)).reliable().transient_local());
  //auto m_pointCloudPublic = node->create_publisher("pointcloud_test", 10);
  
  sensor_msgs::msg::PointCloud2  m_pointCloudMsg;

  //Subscription的几种设置方法
  //rclcpp::Subscription::SharedPtr sub;
  // auto sub = node->create_subscription(
  //     "pointcloud_test", 10,chatterCallback);
  
  // auto sub = node->create_subscription(
  //     "pointcloud_test",rclcpp::SystemDefaultsQoS(),chatterCallback);
      
  auto sub = node->create_subscription(
      "pointcloud_test",rclcpp::QoS(rclcpp::KeepLast(1)).reliable().transient_local(),chatterCallback);

  auto period = std::chrono::milliseconds(1000); 
  rclcpp::Rate loop_rate(period);
  while (rclcpp::ok())
  {
        m_pointCloudPublic->publish(m_pointCloudMsg);
      rclcpp::spin_some(node->get_node_base_interface());  
      loop_rate.sleep();
  }
    
   

    rclcpp::shutdown();
  return 0;
}


可以通过reliable()转到定义查看原代码中的几种设置,有:
reliable(),best_effort(),durability_volatile(),transient_local().
需要清楚对应关系.

你可能感兴趣的:(ros2)