Skip to content

ROS2: Templatized publisher in class diagnostic_updater::DiagnosedPublisher #164

Description

@rostest

The ROS2 publisher in class diagnostic_updater::DiagnosedPublisher currently supports messages of type diagnostic_msgs::msg::DiagnosticArray only:

rclcpp::Publisher<diagnostic_msgs::msg::DiagnosticArray>::SharedPtr publisher_; (https://github.com/ros/diagnostics/blob/eloquent/diagnostic_updater/include/diagnostic_updater/publisher.hpp, line 247).

This way, only messages of type diagnostic_msgs::msg::DiagnosticArray can be published by diagnostic_updater::DiagnosedPublisher. With a templetatized publisher this class could monitor messages of all types.

Example for a DiagnosedPublisher with template publisher:

namespace diagnostic_updater
{
  template <class DiagnosedPublisherT> class DiagnosedPublisher : public TopicDiagnostic
  {
  public:
    template <typename PublisherType> DiagnosedPublisher(PublisherType publisher, diagnostic_updater::Updater & diag, 
      const diagnostic_updater::FrequencyStatusParam & freq, const diagnostic_updater::TimeStampStatusParam & stamp)
    : diagnostic_updater::TopicDiagnostic(publisher->get_topic_name(), diag, freq, stamp), publisher_(publisher)
    {
    }
    template <typename MessageType> void publish(const std::shared_ptr<MessageType> & message)
    {
      publish(*message);
    }
    template <typename MessageType> void publish(const MessageType & message)
    {
      tick(message.header.stamp);
      publisher_->publish(message);
    }
  protected:
    DiagnosedPublisherT publisher_;
  };
}   // namespace diagnostic_updater

Usage example:

rclcpp::Node::SharedPtr nh = rclcpp::Node::make_shared("my_node", "");
double min_frequency = 10.0, max_frequency = 20.0;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pointcloudPub = nh->create_publisher<sensor_msgs::msg::PointCloud2>("cloud", rclcpp::SystemDefaultsQoS());
diagnostic_updater::Updater diagnostics(nh);
DiagnosedPublisher<rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr>* diagnosticPub
  = new DiagnosedPublisher<rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr>(
    pointcloudPub, 
    diagnostics,
    diagnostic_updater::FrequencyStatusParam(&min_frequency, &max_frequency),
    diagnostic_updater::TimeStampStatusParam());

I'd like to suggest to make publisher_ in class diagnostic_updater::DiagnosedPublisher a template to support messages of all types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ros2PR tackling a ROS2 branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions