26
26
#include < pcl/search/pcl_search.h>
27
27
#include < pcl_conversions/pcl_conversions.h>
28
28
29
+ #include < atomic>
29
30
#include < map>
30
31
#include < memory>
31
32
#include < string>
@@ -85,7 +86,6 @@ class VoxelGridMapLoader
85
86
{
86
87
protected:
87
88
rclcpp::Logger logger_;
88
- std::mutex * mutex_ptr_;
89
89
double voxel_leaf_size_;
90
90
double voxel_leaf_size_z_{};
91
91
double downsize_ratio_z_axis_;
@@ -98,7 +98,7 @@ class VoxelGridMapLoader
98
98
typedef typename PointCloud::Ptr PointCloudPtr;
99
99
explicit VoxelGridMapLoader (
100
100
rclcpp::Node * node, double leaf_size, double downsize_ratio_z_axis,
101
- std::string * tf_map_input_frame, std::mutex * mutex );
101
+ std::string * tf_map_input_frame);
102
102
103
103
virtual bool is_close_to_map (const pcl::PointXYZ & point, const double distance_threshold) = 0;
104
104
static bool is_close_to_neighbor_voxels (
@@ -121,11 +121,12 @@ class VoxelGridStaticMapLoader : public VoxelGridMapLoader
121
121
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr sub_map_;
122
122
VoxelGridPointXYZ voxel_grid_;
123
123
PointCloudPtr voxel_map_ptr_;
124
+ std::atomic_bool is_initialized_{false };
124
125
125
126
public:
126
127
explicit VoxelGridStaticMapLoader (
127
128
rclcpp::Node * node, double leaf_size, double downsize_ratio_z_axis,
128
- std::string * tf_map_input_frame, std::mutex * mutex );
129
+ std::string * tf_map_input_frame);
129
130
virtual void onMapCallback (const sensor_msgs::msg::PointCloud2::ConstSharedPtr map);
130
131
bool is_close_to_map (const pcl::PointXYZ & point, const double distance_threshold) override ;
131
132
};
@@ -145,6 +146,7 @@ class VoxelGridDynamicMapLoader : public VoxelGridMapLoader
145
146
146
147
/* * \brief Map to hold loaded map grid id and it's voxel filter */
147
148
VoxelGridDict current_voxel_grid_dict_;
149
+ std::mutex dynamic_map_loader_mutex_;
148
150
rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr sub_kinematic_state_;
149
151
150
152
std::optional<geometry_msgs::msg::Point > current_position_ = std::nullopt;
@@ -182,8 +184,7 @@ class VoxelGridDynamicMapLoader : public VoxelGridMapLoader
182
184
public:
183
185
explicit VoxelGridDynamicMapLoader (
184
186
rclcpp::Node * node, double leaf_size, double downsize_ratio_z_axis,
185
- std::string * tf_map_input_frame, std::mutex * mutex,
186
- rclcpp::CallbackGroup::SharedPtr main_callback_group);
187
+ std::string * tf_map_input_frame, rclcpp::CallbackGroup::SharedPtr main_callback_group);
187
188
void onEstimatedPoseCallback (nav_msgs::msg::Odometry::ConstSharedPtr msg);
188
189
189
190
void timer_callback ();
@@ -194,17 +195,19 @@ class VoxelGridDynamicMapLoader : public VoxelGridMapLoader
194
195
bool is_close_to_next_map_grid (
195
196
const pcl::PointXYZ & point, const int current_map_grid_index, const double distance_threshold);
196
197
197
- inline pcl::PointCloud<pcl::PointXYZ> getCurrentDownsampledMapPc () const
198
+ inline pcl::PointCloud<pcl::PointXYZ> getCurrentDownsampledMapPc ()
198
199
{
199
200
pcl::PointCloud<pcl::PointXYZ> output;
201
+ std::lock_guard<std::mutex> lock (dynamic_map_loader_mutex_);
200
202
for (const auto & kv : current_voxel_grid_dict_) {
201
203
output = output + *(kv.second .map_cell_pc_ptr );
202
204
}
203
205
return output;
204
206
}
205
- inline std::vector<std::string> getCurrentMapIDs () const
207
+ inline std::vector<std::string> getCurrentMapIDs ()
206
208
{
207
209
std::vector<std::string> current_map_ids{};
210
+ std::lock_guard<std::mutex> lock (dynamic_map_loader_mutex_);
208
211
for (auto & kv : current_voxel_grid_dict_) {
209
212
current_map_ids.push_back (kv.first );
210
213
}
@@ -243,9 +246,9 @@ class VoxelGridDynamicMapLoader : public VoxelGridMapLoader
243
246
return ;
244
247
}
245
248
246
- (*mutex_ptr_).lock ();
247
249
current_voxel_grid_array_.assign (
248
250
map_grids_x_ * map_grid_size_y_, std::make_shared<MapGridVoxelInfo>());
251
+ std::lock_guard<std::mutex> lock (dynamic_map_loader_mutex_);
249
252
for (const auto & kv : current_voxel_grid_dict_) {
250
253
int index = static_cast <int >(
251
254
std::floor ((kv.second .min_b_x - origin_x_) / map_grid_size_x_) +
@@ -256,14 +259,12 @@ class VoxelGridDynamicMapLoader : public VoxelGridMapLoader
256
259
}
257
260
current_voxel_grid_array_.at (index ) = std::make_shared<MapGridVoxelInfo>(kv.second );
258
261
}
259
- (*mutex_ptr_).unlock ();
260
262
}
261
263
262
264
inline void removeMapCell (const std::string & map_cell_id_to_remove)
263
265
{
264
- (*mutex_ptr_). lock ();
266
+ std::lock_guard<std::mutex> lock (dynamic_map_loader_mutex_ );
265
267
current_voxel_grid_dict_.erase (map_cell_id_to_remove);
266
- (*mutex_ptr_).unlock ();
267
268
}
268
269
269
270
virtual inline void addMapCellAndFilter (
@@ -310,9 +311,8 @@ class VoxelGridDynamicMapLoader : public VoxelGridMapLoader
310
311
current_voxel_grid_list_item.map_cell_pc_ptr .reset (new pcl::PointCloud<pcl::PointXYZ>);
311
312
current_voxel_grid_list_item.map_cell_pc_ptr = std::move (map_cell_downsampled_pc_ptr_tmp);
312
313
// add
313
- (*mutex_ptr_). lock ();
314
+ std::lock_guard<std::mutex> lock (dynamic_map_loader_mutex_ );
314
315
current_voxel_grid_dict_.insert ({map_cell_to_add.cell_id , current_voxel_grid_list_item});
315
- (*mutex_ptr_).unlock ();
316
316
}
317
317
};
318
318
0 commit comments