ros2中Qos的C++配置方法
1. dds_debug.hpp
#ifndef DDS_DEBUG__DDS_DEBUG_HPP_
#define DDS_DEBUG__DDS_DEBUG_HPP_
#include <rclcpp/rclcpp.hpp>
#include <rclcpp/qos.hpp>
#include <rmw/types.h>
#include <sensor_msgs/msg/imu.hpp>
const rmw_qos_profile_t my_custom_qos_profile =
{
RMW_QOS_POLICY_HISTORY_KEEP_LAST,
10,
RMW_QOS_POLICY_RELIABILITY_RELIABLE,
RMW_QOS_POLICY_DURABILITY_VOLATILE,
RMW_DURATION_INFINITE,
RMW_DURATION_INFINITE,
RMW_QOS_POLICY_LIVELINESS_AUTOMATIC,
RMW_DURATION_INFINITE,
false
};
class DDS_debug : public rclcpp::Node
{
public:
explicit DDS_debug();
~DDS_debug();
protected:
void on_timer();
private:
unsigned int cnt;
rclcpp::Publisher<sensor_msgs::msg::Imu>::SharedPtr imu_pub_;
rclcpp::TimerBase::SharedPtr timer_;
};
#endif
2. dds_debug.cpp
#include "dds_debug/dds_debug.hpp"
DDS_debug::DDS_debug()
: Node("dds_debug")
, cnt(0)
{
auto qos = rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(my_custom_qos_profile), my_custom_qos_profile);
imu_pub_ = create_publisher<sensor_msgs::msg::Imu>("/sensing/imu/tamagawa/imu_raw", qos);
constexpr double period_s = 0.1;
constexpr std::chrono::nanoseconds period_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double>(period_s));
timer_ = rclcpp::create_timer(this, get_clock(), period_ns, std::bind(&DDS_debug::on_timer, this));
}
DDS_debug::~DDS_debug() {}
void DDS_debug::on_timer() {
cnt++;
if (cnt <= 50) {
sensor_msgs::msg::Imu imu_msg;
imu_pub_->publish(imu_msg);
}
}
3. dds_debug_node.cpp
#include "dds_debug/dds_debug.hpp"
int main(int argc, char** argv) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<DDS_debug>());
rclcpp::shutdown();
return 0;
}

浙公网安备 33010602011771号