Skip to content

Commit f59812a

Browse files
daisukesSteveMacenski
authored andcommitted
Add mutex lock into inflation layer to avoid thread issue in updating footprint (#1952)
Signed-off-by: Daisuke Sato <daisukes@cmu.edu>
1 parent 5e522b8 commit f59812a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

nav2_costmap_2d/include/nav2_costmap_2d/inflation_layer.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#include <map>
4242
#include <vector>
43+
#include <mutex>
4344

4445
#include "rclcpp/rclcpp.hpp"
4546
#include "nav2_costmap_2d/layer.hpp"
@@ -77,7 +78,7 @@ class InflationLayer : public Layer
7778
public:
7879
InflationLayer();
7980

80-
~InflationLayer() override = default;
81+
~InflationLayer();
8182

8283
void onInitialize() override;
8384
void updateBounds(
@@ -115,6 +116,13 @@ class InflationLayer : public Layer
115116
return cost;
116117
}
117118

119+
// Provide a typedef to ease future code maintenance
120+
typedef std::recursive_mutex mutex_t;
121+
mutex_t * getMutex()
122+
{
123+
return access_;
124+
}
125+
118126
protected:
119127
void onFootprintChanged() override;
120128

@@ -184,6 +192,7 @@ class InflationLayer : public Layer
184192

185193
// Indicates that the entire costmap should be reinflated next time around.
186194
bool need_reinflation_;
195+
mutex_t * access_;
187196
};
188197

189198
} // namespace nav2_costmap_2d

nav2_costmap_2d/plugins/inflation_layer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ InflationLayer::InflationLayer()
7171
last_max_x_(std::numeric_limits<double>::max()),
7272
last_max_y_(std::numeric_limits<double>::max())
7373
{
74+
access_ = new mutex_t();
75+
}
76+
77+
InflationLayer::~InflationLayer()
78+
{
79+
delete access_;
7480
}
7581

7682
void
@@ -160,6 +166,7 @@ InflationLayer::updateCosts(
160166
int max_i,
161167
int max_j)
162168
{
169+
std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
163170
if (!enabled_ || (cell_inflation_radius_ == 0)) {
164171
return;
165172
}
@@ -305,6 +312,7 @@ InflationLayer::enqueue(
305312
void
306313
InflationLayer::computeCaches()
307314
{
315+
std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
308316
if (cell_inflation_radius_ == 0) {
309317
return;
310318
}

0 commit comments

Comments
 (0)