Skip to content

Commit af68d0d

Browse files
Adding logging for matched events and dropped messages into pub/sub of new nav2_ros_common package. Also adding QoS overrides default ON (#5302)
* Adding logging for matched events and dropped messages Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> * toggle on Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> * apply for smac 2D Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> * Update interface_factories.hpp Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> --------- Signed-off-by: Steve Macenski <stevenmacenski@gmail.com>
1 parent a9fcc5b commit af68d0d

File tree

5 files changed

+91
-29
lines changed

5 files changed

+91
-29
lines changed

nav2_behavior_tree/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ target_link_libraries(${library_name} PUBLIC
4444
${sensor_msgs_TARGETS}
4545
tf2::tf2
4646
tf2_ros::tf2_ros
47+
nav2_ros_common::nav2_ros_common
4748
)
4849

4950
add_library(nav2_compute_path_to_pose_action_bt_node SHARED plugins/action/compute_path_to_pose_action.cpp)
@@ -264,6 +265,7 @@ foreach(bt_plugin ${plugin_libs})
264265
tf2_ros::tf2_ros
265266
${std_msgs_TARGETS}
266267
${std_srvs_TARGETS}
268+
nav2_ros_common::nav2_ros_common
267269
)
268270
target_compile_definitions(${bt_plugin} PRIVATE BT_PLUGIN_EXPORT)
269271
endforeach()

nav2_behavior_tree/include/nav2_behavior_tree/bt_cancel_action_node.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <chrono>
2121

2222
#include "behaviortree_cpp/action_node.h"
23-
#include "nav2_ros_common/node_utils.hpp"
23+
#include "nav2_ros_common/lifecycle_node.hpp"
2424
#include "rclcpp_action/rclcpp_action.hpp"
2525
#include "nav2_behavior_tree/bt_utils.hpp"
2626

nav2_ros_common/include/nav2_ros_common/interface_factories.hpp

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace interfaces
3636

3737
/**
3838
* @brief Create a SubscriptionOptions object with Nav2's QoS profiles and options
39+
* @param topic_name Name of topic
3940
* @param allow_parameter_qos_overrides Whether to allow QoS overrides for this subscription
4041
* @param callback_group_ptr Pointer to the callback group to use for this subscription
4142
* @param qos_message_lost_callback Callback for when a QoS message is lost
@@ -47,6 +48,7 @@ namespace interfaces
4748
* @return A nav2::SubscriptionOptions object with the specified configurations
4849
*/
4950
inline rclcpp::SubscriptionOptions createSubscriptionOptions(
51+
const std::string & topic_name,
5052
const bool allow_parameter_qos_overrides = true,
5153
const rclcpp::CallbackGroup::SharedPtr callback_group_ptr = nullptr,
5254
rclcpp::QOSMessageLostCallbackType qos_message_lost_callback = nullptr,
@@ -67,19 +69,56 @@ inline rclcpp::SubscriptionOptions createSubscriptionOptions(
6769
// Set the callback group to use for this subscription, if given
6870
options.callback_group = callback_group_ptr;
6971

70-
// Set the event callbacks
72+
// ROS 2 default logs this already
73+
options.event_callbacks.incompatible_qos_callback = requested_incompatible_qos_callback;
74+
options.event_callbacks.incompatible_type_callback = incompatible_qos_type_callback;
75+
76+
// Set the event callbacks if given, else log
77+
if (qos_message_lost_callback) {
78+
options.event_callbacks.message_lost_callback =
79+
qos_message_lost_callback;
80+
} else {
81+
options.event_callbacks.message_lost_callback =
82+
[topic_name](rclcpp::QOSMessageLostInfo & info) {
83+
RCLCPP_WARN(
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,
87+
topic_name.c_str());
88+
};
89+
}
90+
91+
if (subscription_matched_callback) {
92+
options.event_callbacks.matched_callback = subscription_matched_callback;
93+
} else {
94+
options.event_callbacks.matched_callback =
95+
[topic_name](rclcpp::MatchedInfo & status) {
96+
if (status.current_count_change > 0) {
97+
RCLCPP_DEBUG(
98+
rclcpp::get_logger("nav2::interfaces"),
99+
"Connected: %d new publisher(s) to [%s]. Total active: %zu.",
100+
status.current_count_change,
101+
topic_name.c_str(),
102+
status.current_count);
103+
} else if (status.current_count_change < 0) {
104+
RCLCPP_DEBUG(
105+
rclcpp::get_logger("nav2::interfaces"),
106+
"Disconnected: %d publisher(s) from [%s]. Total active: %zu.",
107+
-status.current_count_change,
108+
topic_name.c_str(),
109+
status.current_count);
110+
}
111+
};
112+
}
113+
71114
options.event_callbacks.deadline_callback = qos_deadline_requested_callback;
72115
options.event_callbacks.liveliness_callback = qos_liveliness_changed_callback;
73-
options.event_callbacks.incompatible_qos_callback =
74-
requested_incompatible_qos_callback;
75-
options.event_callbacks.message_lost_callback = qos_message_lost_callback;
76-
options.event_callbacks.incompatible_type_callback = incompatible_qos_type_callback;
77-
options.event_callbacks.matched_callback = subscription_matched_callback;
78116
return options;
79117
}
80118

81119
/**
82120
* @brief Create a PublisherOptions object with Nav2's QoS profiles and options
121+
* @param topic_name Name of topic
83122
* @param allow_parameter_qos_overrides Whether to allow QoS overrides for this publisher
84123
* @param callback_group_ptr Pointer to the callback group to use for this publisher
85124
* @param publisher_matched_callback Callback when a publisher is matched with a subscriber
@@ -90,6 +129,7 @@ inline rclcpp::SubscriptionOptions createSubscriptionOptions(
90129
* @return A rclcpp::PublisherOptions object with the specified configurations
91130
*/
92131
inline rclcpp::PublisherOptions createPublisherOptions(
132+
const std::string & topic_name,
93133
const bool allow_parameter_qos_overrides = true,
94134
const rclcpp::CallbackGroup::SharedPtr callback_group_ptr = nullptr,
95135
rclcpp::PublisherMatchedCallbackType publisher_matched_callback = nullptr,
@@ -109,12 +149,36 @@ inline rclcpp::PublisherOptions createPublisherOptions(
109149
// Set the callback group to use for this publisher, if given
110150
options.callback_group = callback_group_ptr;
111151

112-
// Set the event callbacks
113-
options.event_callbacks.deadline_callback = qos_deadline_offered_callback;
114-
options.event_callbacks.liveliness_callback = qos_liveliness_lost_callback;
152+
// ROS 2 default logs this already
115153
options.event_callbacks.incompatible_qos_callback = offered_incompatible_qos_cb;
116154
options.event_callbacks.incompatible_type_callback = incompatible_qos_type_callback;
117-
options.event_callbacks.matched_callback = publisher_matched_callback;
155+
156+
// Set the event callbacks, else log
157+
if (publisher_matched_callback) {
158+
options.event_callbacks.matched_callback = publisher_matched_callback;
159+
} else {
160+
options.event_callbacks.matched_callback =
161+
[topic_name](rclcpp::MatchedInfo & status) {
162+
if (status.current_count_change > 0) {
163+
RCLCPP_DEBUG(
164+
rclcpp::get_logger("nav2::interfaces"),
165+
"Connected: %d new subscriber(s) to [%s]. Total active: %zu.",
166+
status.current_count_change,
167+
topic_name.c_str(),
168+
status.current_count);
169+
} else if (status.current_count_change < 0) {
170+
RCLCPP_DEBUG(
171+
rclcpp::get_logger("nav2::interfaces"),
172+
"Disconnected: %d subscriber(s) from [%s]. Total active: %zu.",
173+
-status.current_count_change,
174+
topic_name.c_str(),
175+
status.current_count);
176+
}
177+
};
178+
}
179+
180+
options.event_callbacks.deadline_callback = qos_deadline_offered_callback;
181+
options.event_callbacks.liveliness_callback = qos_liveliness_lost_callback;
118182
return options;
119183
}
120184

@@ -136,7 +200,7 @@ typename nav2::Subscription<MessageT>::SharedPtr create_subscription(
136200
const rclcpp::CallbackGroup::SharedPtr & callback_group = nullptr)
137201
{
138202
bool allow_parameter_qos_overrides = nav2::declare_or_get_parameter(
139-
node, "allow_parameter_qos_overrides", false);
203+
node, "allow_parameter_qos_overrides", true);
140204

141205
auto params_interface = node->get_node_parameters_interface();
142206
auto topics_interface = node->get_node_topics_interface();
@@ -146,7 +210,7 @@ typename nav2::Subscription<MessageT>::SharedPtr create_subscription(
146210
topic_name,
147211
qos,
148212
std::forward<CallbackT>(callback),
149-
createSubscriptionOptions(allow_parameter_qos_overrides, callback_group));
213+
createSubscriptionOptions(topic_name, allow_parameter_qos_overrides, callback_group));
150214
}
151215

152216
/**
@@ -165,13 +229,13 @@ typename nav2::Publisher<MessageT>::SharedPtr create_publisher(
165229
const rclcpp::CallbackGroup::SharedPtr & callback_group = nullptr)
166230
{
167231
bool allow_parameter_qos_overrides = nav2::declare_or_get_parameter(
168-
node, "allow_parameter_qos_overrides", false);
232+
node, "allow_parameter_qos_overrides", true);
169233
using PublisherT = nav2::Publisher<MessageT>;
170234
auto pub = rclcpp::create_publisher<MessageT, std::allocator<void>, PublisherT>(
171235
*node,
172236
topic_name,
173237
qos,
174-
createPublisherOptions(allow_parameter_qos_overrides, callback_group));
238+
createPublisherOptions(topic_name, allow_parameter_qos_overrides, callback_group));
175239
return pub;
176240
}
177241

nav2_smac_planner/src/smac_planner_2d.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,10 @@ void SmacPlanner2D::configure(
140140
_smoother->initialize(1e-50 /*No valid minimum turning radius for 2D*/);
141141

142142
// Initialize costmap downsampler
143-
if (_downsample_costmap && _downsampling_factor > 1) {
144-
std::string topic_name = "downsampled_costmap";
145-
_costmap_downsampler = std::make_unique<CostmapDownsampler>();
146-
_costmap_downsampler->on_configure(
147-
node, _global_frame, topic_name, _costmap, _downsampling_factor);
148-
}
143+
std::string topic_name = "downsampled_costmap";
144+
_costmap_downsampler = std::make_unique<CostmapDownsampler>();
145+
_costmap_downsampler->on_configure(
146+
node, _global_frame, topic_name, _costmap, _downsampling_factor);
149147

150148
_raw_plan_publisher = node->create_publisher<nav_msgs::msg::Path>("unsmoothed_plan");
151149

@@ -215,7 +213,7 @@ nav_msgs::msg::Path SmacPlanner2D::createPlan(
215213

216214
// Downsample costmap, if required
217215
nav2_costmap_2d::Costmap2D * costmap = _costmap;
218-
if (_costmap_downsampler) {
216+
if (_downsample_costmap && _downsampling_factor > 1) {
219217
costmap = _costmap_downsampler->downsample(_downsampling_factor);
220218
_collision_checker.setCostmap(costmap);
221219
}

nav2_smac_planner/src/smac_planner_hybrid.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,10 @@ void SmacPlannerHybrid::configure(
281281
}
282282

283283
// Initialize costmap downsampler
284-
if (_downsample_costmap && _downsampling_factor > 1) {
285-
_costmap_downsampler = std::make_unique<CostmapDownsampler>();
286-
std::string topic_name = "downsampled_costmap";
287-
_costmap_downsampler->on_configure(
288-
node, _global_frame, topic_name, _costmap, _downsampling_factor);
289-
}
284+
_costmap_downsampler = std::make_unique<CostmapDownsampler>();
285+
std::string topic_name = "downsampled_costmap";
286+
_costmap_downsampler->on_configure(
287+
node, _global_frame, topic_name, _costmap, _downsampling_factor);
290288

291289
_raw_plan_publisher = node->create_publisher<nav_msgs::msg::Path>("unsmoothed_plan");
292290

@@ -391,7 +389,7 @@ nav_msgs::msg::Path SmacPlannerHybrid::createPlan(
391389

392390
// Downsample costmap, if required
393391
nav2_costmap_2d::Costmap2D * costmap = _costmap;
394-
if (_costmap_downsampler) {
392+
if (_downsample_costmap && _downsampling_factor > 1) {
395393
costmap = _costmap_downsampler->downsample(_downsampling_factor);
396394
_collision_checker.setCostmap(costmap);
397395
}

0 commit comments

Comments
 (0)