15 #ifndef NAV2_ROS_COMMON__INTERFACE_FACTORIES_HPP_
16 #define NAV2_ROS_COMMON__INTERFACE_FACTORIES_HPP_
21 #include "nav2_ros_common/qos_profiles.hpp"
22 #include "nav2_ros_common/service_client.hpp"
23 #include "nav2_ros_common/service_server.hpp"
24 #include "nav2_ros_common/simple_action_server.hpp"
25 #include "nav2_ros_common/node_utils.hpp"
26 #include "nav2_ros_common/publisher.hpp"
27 #include "nav2_ros_common/subscription.hpp"
28 #include "nav2_ros_common/action_client.hpp"
29 #include "rclcpp_action/client.hpp"
50 inline rclcpp::SubscriptionOptions createSubscriptionOptions(
51 const std::string & topic_name,
52 const bool allow_parameter_qos_overrides =
true,
53 const rclcpp::CallbackGroup::SharedPtr callback_group_ptr =
nullptr,
54 rclcpp::QOSMessageLostCallbackType qos_message_lost_callback =
nullptr,
55 rclcpp::SubscriptionMatchedCallbackType subscription_matched_callback =
nullptr,
56 rclcpp::IncompatibleTypeCallbackType incompatible_qos_type_callback =
nullptr,
57 rclcpp::QOSRequestedIncompatibleQoSCallbackType requested_incompatible_qos_callback =
nullptr,
58 rclcpp::QOSDeadlineRequestedCallbackType qos_deadline_requested_callback =
nullptr,
59 rclcpp::QOSLivelinessChangedCallbackType qos_liveliness_changed_callback =
nullptr)
61 rclcpp::SubscriptionOptions options;
63 if (allow_parameter_qos_overrides) {
64 options.qos_overriding_options = rclcpp::QosOverridingOptions(
65 {rclcpp::QosPolicyKind::Depth, rclcpp::QosPolicyKind::Durability,
66 rclcpp::QosPolicyKind::Reliability, rclcpp::QosPolicyKind::History});
70 options.callback_group = callback_group_ptr;
73 options.event_callbacks.incompatible_qos_callback = requested_incompatible_qos_callback;
74 options.event_callbacks.incompatible_type_callback = incompatible_qos_type_callback;
77 if (qos_message_lost_callback) {
78 options.event_callbacks.message_lost_callback =
79 qos_message_lost_callback;
81 options.event_callbacks.message_lost_callback =
82 [topic_name](rclcpp::QOSMessageLostInfo & info) {
84 rclcpp::get_logger(
"nav2::interfaces"),
85 "[%lu] Message was dropped on topic [%s] due to queue size or network constraints.",
86 info.total_count_change,
91 if (subscription_matched_callback) {
92 options.event_callbacks.matched_callback = subscription_matched_callback;
94 options.event_callbacks.matched_callback =
95 [topic_name](rclcpp::MatchedInfo & status) {
96 if (status.current_count_change > 0) {
98 rclcpp::get_logger(
"nav2::interfaces"),
99 "Connected: %d new publisher(s) to [%s]. Total active: %zu.",
100 status.current_count_change,
102 status.current_count);
103 }
else if (status.current_count_change < 0) {
105 rclcpp::get_logger(
"nav2::interfaces"),
106 "Disconnected: %d publisher(s) from [%s]. Total active: %zu.",
107 -status.current_count_change,
109 status.current_count);
114 options.event_callbacks.deadline_callback = qos_deadline_requested_callback;
115 options.event_callbacks.liveliness_callback = qos_liveliness_changed_callback;
131 inline rclcpp::PublisherOptions createPublisherOptions(
132 const std::string & topic_name,
133 const bool allow_parameter_qos_overrides =
true,
134 const rclcpp::CallbackGroup::SharedPtr callback_group_ptr =
nullptr,
135 rclcpp::PublisherMatchedCallbackType publisher_matched_callback =
nullptr,
136 rclcpp::IncompatibleTypeCallbackType incompatible_qos_type_callback =
nullptr,
137 rclcpp::QOSOfferedIncompatibleQoSCallbackType offered_incompatible_qos_cb =
nullptr,
138 rclcpp::QOSDeadlineOfferedCallbackType qos_deadline_offered_callback =
nullptr,
139 rclcpp::QOSLivelinessLostCallbackType qos_liveliness_lost_callback =
nullptr)
141 rclcpp::PublisherOptions options;
143 if (allow_parameter_qos_overrides) {
144 options.qos_overriding_options = rclcpp::QosOverridingOptions(
145 {rclcpp::QosPolicyKind::Depth, rclcpp::QosPolicyKind::Durability,
146 rclcpp::QosPolicyKind::Reliability, rclcpp::QosPolicyKind::History});
150 options.callback_group = callback_group_ptr;
153 options.event_callbacks.incompatible_qos_callback = offered_incompatible_qos_cb;
154 options.event_callbacks.incompatible_type_callback = incompatible_qos_type_callback;
157 if (publisher_matched_callback) {
158 options.event_callbacks.matched_callback = publisher_matched_callback;
160 options.event_callbacks.matched_callback =
161 [topic_name](rclcpp::MatchedInfo & status) {
162 if (status.current_count_change > 0) {
164 rclcpp::get_logger(
"nav2::interfaces"),
165 "Connected: %d new subscriber(s) to [%s]. Total active: %zu.",
166 status.current_count_change,
168 status.current_count);
169 }
else if (status.current_count_change < 0) {
171 rclcpp::get_logger(
"nav2::interfaces"),
172 "Disconnected: %d subscriber(s) from [%s]. Total active: %zu.",
173 -status.current_count_change,
175 status.current_count);
180 options.event_callbacks.deadline_callback = qos_deadline_offered_callback;
181 options.event_callbacks.liveliness_callback = qos_liveliness_lost_callback;
194 template<
typename MessageT,
typename NodeT,
typename CallbackT>
195 typename nav2::Subscription<MessageT>::SharedPtr create_subscription(
197 const std::string & topic_name,
198 CallbackT && callback,
200 const rclcpp::CallbackGroup::SharedPtr & callback_group =
nullptr)
202 bool allow_parameter_qos_overrides = nav2::declare_or_get_parameter(
203 node,
"allow_parameter_qos_overrides",
true);
205 auto params_interface = node->get_node_parameters_interface();
206 auto topics_interface = node->get_node_topics_interface();
207 return rclcpp::create_subscription<MessageT, CallbackT>(
212 std::forward<CallbackT>(callback),
213 createSubscriptionOptions(topic_name, allow_parameter_qos_overrides, callback_group));
224 template<
typename MessageT,
typename NodeT>
225 typename nav2::Publisher<MessageT>::SharedPtr create_publisher(
227 const std::string & topic_name,
229 const rclcpp::CallbackGroup::SharedPtr & callback_group =
nullptr)
231 bool allow_parameter_qos_overrides = nav2::declare_or_get_parameter(
232 node,
"allow_parameter_qos_overrides",
true);
233 using PublisherT = nav2::Publisher<MessageT>;
234 auto pub = rclcpp::create_publisher<MessageT, std::allocator<void>, PublisherT>(
238 createPublisherOptions(topic_name, allow_parameter_qos_overrides, callback_group));
249 template<
typename SrvT,
typename NodeT>
250 typename nav2::ServiceClient<SrvT>::SharedPtr create_client(
252 const std::string & service_name,
253 bool use_internal_executor =
false)
255 return std::make_shared<nav2::ServiceClient<SrvT>>(
256 service_name, node, use_internal_executor);
267 template<
typename SrvT,
typename NodeT,
typename CallbackT>
268 typename nav2::ServiceServer<SrvT>::SharedPtr create_service(
270 const std::string & service_name,
271 CallbackT && callback,
272 rclcpp::CallbackGroup::SharedPtr callback_group =
nullptr)
274 using Request =
typename SrvT::Request;
275 using Response =
typename SrvT::Response;
276 using CallbackFn = std::function<void(
277 const std::shared_ptr<rmw_request_id_t>,
278 const std::shared_ptr<Request>,
279 std::shared_ptr<Response>)>;
280 CallbackFn cb = std::forward<CallbackT>(callback);
282 return std::make_shared<nav2::ServiceServer<SrvT>>(
283 service_name, node, cb, callback_group);
298 template<
typename ActionT,
typename NodeT>
299 typename nav2::SimpleActionServer<ActionT>::SharedPtr create_action_server(
301 const std::string & action_name,
302 typename nav2::SimpleActionServer<ActionT>::ExecuteCallback execute_callback,
303 typename nav2::SimpleActionServer<ActionT>::CompletionCallback complete_cb =
nullptr,
304 std::chrono::milliseconds server_timeout = std::chrono::milliseconds(500),
305 bool spin_thread =
false,
306 const bool realtime =
false)
308 return std::make_shared<nav2::SimpleActionServer<ActionT>>(
309 node, action_name, execute_callback, complete_cb, server_timeout, spin_thread, realtime);
319 template<
typename ActionT,
typename NodeT>
320 typename nav2::ActionClient<ActionT>::SharedPtr create_action_client(
322 const std::string & action_name,
323 rclcpp::CallbackGroup::SharedPtr callback_group =
nullptr)
325 auto client = rclcpp_action::create_client<ActionT>(node, action_name, callback_group);
326 nav2::setIntrospectionMode(client,
327 node->get_node_parameters_interface(), node->get_clock());
A QoS profile for standard reliable topics with a history of 10 messages.