Skip to content

Commit f2c896d

Browse files
mlherdCarl Delsey
authored andcommitted
fix for #1363 - use clearMap instead of reset to clear costmaps (#1412)
* -fix for #1363. -created clearMap function for each layer. -use clearMap instead of reset to clear the costmaps * -function name change -infilation layer clearMap change -make resetMap a pure virtual function * Changing clearMaps back to reset * Fix uncrustify error * Fixing lifecycle transitions and tests now that reset doesn't act as a cleanup function * Updating this based on the approach taken in PR #1431 * Moving voxel_grid reset to the resetMaps function. * Missed adding resetMaps back to header. * Adding some comments to help navigate the inheritance hierarchy
1 parent c6d98d9 commit f2c896d

File tree

8 files changed

+28
-36
lines changed

8 files changed

+28
-36
lines changed

nav2_costmap_2d/include/nav2_costmap_2d/inflation_layer.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ class InflationLayer : public Layer
9999

100100
virtual void reset()
101101
{
102-
undeclareAllParameters();
103-
onInitialize();
102+
matchSize();
104103
}
105104

106105
/** @brief Given a distance, compute a cost.

nav2_costmap_2d/include/nav2_costmap_2d/layer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Layer // TODO(mjeronimo): public nav2_util::LifecycleHelperInterface
6767
rclcpp::Node::SharedPtr rclcpp_node);
6868
virtual void deactivate() {} /** @brief Stop publishers. */
6969
virtual void activate() {} /** @brief Restart publishers if they've been stopped. */
70-
virtual void reset() {}
70+
virtual void reset() = 0;
7171

7272
/**
7373
* @brief This is called by the LayeredCostmap to poll this plugin as to how

nav2_costmap_2d/include/nav2_costmap_2d/obstacle_layer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ class ObstacleLayer : public CostmapLayer
8383
virtual void activate();
8484
virtual void deactivate();
8585
virtual void reset();
86+
/**
87+
* @brief triggers the update of observations buffer
88+
*/
89+
void resetBuffersLastUpdated();
8690

8791
/**
8892
* @brief A callback to handle buffering LaserScan messages

nav2_costmap_2d/plugins/obstacle_layer.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,9 @@ ObstacleLayer::activate()
588588
observation_subscribers_[i]->subscribe();
589589
}
590590
}
591-
592-
for (unsigned int i = 0; i < observation_buffers_.size(); ++i) {
593-
if (observation_buffers_[i]) {
594-
observation_buffers_[i]->resetLastUpdated();
595-
}
596-
}
591+
resetBuffersLastUpdated();
597592
}
593+
598594
void
599595
ObstacleLayer::deactivate()
600596
{
@@ -620,11 +616,19 @@ ObstacleLayer::updateRaytraceBounds(
620616
void
621617
ObstacleLayer::reset()
622618
{
623-
deactivate();
624619
resetMaps();
620+
resetBuffersLastUpdated();
625621
current_ = true;
626-
activate();
627-
undeclareAllParameters();
622+
}
623+
624+
void
625+
ObstacleLayer::resetBuffersLastUpdated()
626+
{
627+
for (unsigned int i = 0; i < observation_buffers_.size(); ++i) {
628+
if (observation_buffers_[i]) {
629+
observation_buffers_[i]->resetLastUpdated();
630+
}
631+
}
628632
}
629633

630634
} // namespace nav2_costmap_2d

nav2_costmap_2d/plugins/static_layer.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ StaticLayer::deactivate()
109109
void
110110
StaticLayer::reset()
111111
{
112-
map_sub_.reset();
113-
map_update_sub_.reset();
114-
115-
undeclareAllParameters();
116-
onInitialize();
112+
has_updated_data_ = true;
117113
}
118114

119115
void

nav2_costmap_2d/plugins/voxel_layer.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,18 @@ void VoxelLayer::matchSize()
110110

111111
void VoxelLayer::reset()
112112
{
113-
voxel_pub_->on_deactivate();
114-
deactivate();
113+
// Call the base class method before adding our own functionality
114+
ObstacleLayer::reset();
115115
resetMaps();
116-
activate();
117-
undeclareAllParameters();
118-
voxel_pub_->on_activate();
119116
}
120117

121118
void VoxelLayer::resetMaps()
122119
{
123-
Costmap2D::resetMaps();
120+
// Call the base class method before adding our own functionality
121+
// Note: at the time this was written, ObstacleLayer doesn't implement
122+
// resetMaps so this goes to the next layer down Costmap2DLayer which also
123+
// doesn't implement this, so it actually goes all the way to Costmap2D
124+
ObstacleLayer::resetMaps();
124125
voxel_grid_.reset();
125126
}
126127

nav2_costmap_2d/src/costmap_2d_ros.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ Costmap2DROS::on_cleanup(const rclcpp_lifecycle::State & /*state*/)
245245
{
246246
RCLCPP_INFO(get_logger(), "Cleaning up");
247247

248-
resetLayers();
249248
delete layered_costmap_;
250249
layered_costmap_ = nullptr;
251250

nav2_costmap_2d/test/integration/obstacle_tests.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -357,20 +357,9 @@ TEST_F(TestNode, testRepeatedResets) {
357357
return plugin->hasParameter(layer_param);
358358
}));
359359

360-
// Reset all layers. This will un-declare all params and might re-declare internal ones
361-
// Should run without throwing exceptions
360+
// Reset all layers. Parameters should be declared if not declared, otherwise skipped.
362361
ASSERT_NO_THROW(
363362
for_each(begin(*plugins), end(*plugins), [](const auto & plugin) {
364363
plugin->reset();
365364
}));
366-
367-
// Check for node-level param
368-
ASSERT_TRUE(node_->has_parameter(node_dummy.first));
369-
370-
// Layer-level parameters shouldn't be found
371-
ASSERT_TRUE(
372-
none_of(begin(*plugins), end(*plugins), [&layer_dummy](const auto & plugin) {
373-
string layer_param = layer_dummy.first + "_" + plugin->getName();
374-
return plugin->hasParameter(layer_param);
375-
}));
376365
}

0 commit comments

Comments
 (0)