diff --git a/include/message_filters/subscriber.h b/include/message_filters/subscriber.h index 5adebf5..3764ebd 100644 --- a/include/message_filters/subscriber.h +++ b/include/message_filters/subscriber.h @@ -35,6 +35,8 @@ #ifndef MESSAGE_FILTERS__SUBSCRIBER_H_ #define MESSAGE_FILTERS__SUBSCRIBER_H_ +#include + #include #include "message_filters/connection.h" @@ -68,6 +70,49 @@ class SubscriberBase * \param qos (optional) The rmw qos profile to use to subscribe */ virtual void subscribe(rclcpp::Node * node, const std::string& topic, const rmw_qos_profile_t qos = rmw_qos_profile_default) = 0; + + /** + * \brief Subscribe to a topic. + * + * If this Subscriber is already subscribed to a topic, this function will first unsubscribe. + * This override allows SubscriptionOptions to be passed into the class without changing API. + * + * \param node The rclcpp::Node::SharedPtr to use to subscribe. + * \param topic The topic to subscribe to. + * \param qos (optional) The rmw qos profile to use to subscribe. + * \param options The subscription options to use to subscribe. + */ + virtual void subscribe( + rclcpp::Node::SharedPtr node, + const std::string& topic, + const rmw_qos_profile_t qos, + rclcpp::SubscriptionOptions options) + { + this->subscribe(node.get(), topic, qos, options); + }; + + /** + * \brief Subscribe to a topic. + * + * If this Subscriber is already subscribed to a topic, this function will first unsubscribe. + * + * \param node The rclcpp::Node to use to subscribe. + * \param topic The topic to subscribe to. + * \param qos (optional) The rmw qos profile to use to subscribe + */ + virtual void subscribe( + rclcpp::Node * node, + const std::string& topic, + const rmw_qos_profile_t qos, + rclcpp::SubscriptionOptions options) + { + (void) options; + RCLCPP_ERROR( + node->get_logger(), + "SubscriberBase::subscribe with four arguments has not been overridden"); + this->subscribe(node, topic, qos); + } + /** * \brief Re-subscribe to a topic. Only works if this subscriber has previously been subscribed to a topic. */ @@ -123,6 +168,34 @@ class Subscriber : public SubscriberBase, public SimpleFilter subscribe(node, topic, qos); } + /** + * \brief Constructor + * + * See the rclcpp::Node::subscribe() variants for more information on the parameters + * + * \param node The rclcpp::Node::SharedPtr to use to subscribe. + * \param topic The topic to subscribe to. + * \param qos The rmw qos profile to use to subscribe. + * \param options The subscription options to use to subscribe. + */ + Subscriber( + rclcpp::Node::SharedPtr node, + const std::string& topic, + const rmw_qos_profile_t qos, + rclcpp::SubscriptionOptions options) + { + subscribe(node, topic, qos, options); + } + + Subscriber( + rclcpp::Node* node, + const std::string& topic, + const rmw_qos_profile_t qos, + rclcpp::SubscriptionOptions options) + { + subscribe(node, topic, qos, options); + } + /** * \brief Empty constructor, use subscribe() to subscribe to a topic */ @@ -144,9 +217,7 @@ class Subscriber : public SubscriberBase, public SimpleFilter */ void subscribe(rclcpp::Node::SharedPtr node, const std::string& topic, const rmw_qos_profile_t qos = rmw_qos_profile_default) { - subscribe(node.get(), topic, qos); - node_raw_ = nullptr; - node_shared_ = node; + subscribe(node, topic, qos, rclcpp::SubscriptionOptions()); } /** @@ -160,6 +231,47 @@ class Subscriber : public SubscriberBase, public SimpleFilter */ // TODO(wjwwood): deprecate in favor of API's that use `rclcpp::QoS` instead. void subscribe(rclcpp::Node * node, const std::string& topic, const rmw_qos_profile_t qos = rmw_qos_profile_default) + { + subscribe(node, topic, qos, rclcpp::SubscriptionOptions()); + } + + /** + * \brief Subscribe to a topic. + * + * If this Subscriber is already subscribed to a topic, this function will first unsubscribe. + * + * \param node The rclcpp::Node::SharedPtr to use to subscribe. + * \param topic The topic to subscribe to. + * \param qos The rmw qos profile to use to subscribe. + * \param options The subscription options to use to subscribe. + */ + void subscribe( + rclcpp::Node::SharedPtr node, + const std::string& topic, + const rmw_qos_profile_t qos, + rclcpp::SubscriptionOptions options) + { + subscribe(node.get(), topic, qos, options); + node_raw_ = nullptr; + node_shared_ = node; + } + + /** + * \brief Subscribe to a topic. + * + * If this Subscriber is already subscribed to a topic, this function will first unsubscribe. + * + * \param node The rclcpp::Node to use to subscribe. + * \param topic The topic to subscribe to. + * \param qos The rmw qos profile to use to subscribe + * \param options The subscription options to use to subscribe. + */ + // TODO(wjwwood): deprecate in favor of API's that use `rclcpp::QoS` instead. + void subscribe( + rclcpp::Node * node, + const std::string& topic, + const rmw_qos_profile_t qos, + rclcpp::SubscriptionOptions options) { unsubscribe(); @@ -169,10 +281,11 @@ class Subscriber : public SubscriberBase, public SimpleFilter rclcpp::QoS rclcpp_qos(rclcpp::QoSInitialization::from_rmw(qos)); rclcpp_qos.get_rmw_qos_profile() = qos; qos_ = qos; + options_ = options; sub_ = node->create_subscription(topic, rclcpp_qos, [this](std::shared_ptr msg) { this->cb(EventType(msg)); - }); + }, options); node_raw_ = node; } @@ -186,9 +299,9 @@ class Subscriber : public SubscriberBase, public SimpleFilter if (!topic_.empty()) { if (node_raw_ != nullptr) { - subscribe(node_raw_, topic_, qos_); + subscribe(node_raw_, topic_, qos_, options_); } else if (node_shared_ != nullptr) { - subscribe(node_shared_, topic_, qos_); + subscribe(node_shared_, topic_, qos_, options_); } } } @@ -242,6 +355,7 @@ class Subscriber : public SubscriberBase, public SimpleFilter std::string topic_; rmw_qos_profile_t qos_; + rclcpp::SubscriptionOptions options_; }; } // namespace message_filters