Skip to content

Commit a01f84f

Browse files
committedDec 27, 2024
revert mutex related changes
Signed-off-by: Taekjin LEE <taekjin.lee@tier4.jp>
1 parent 3c73ba9 commit a01f84f

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed
 

‎perception/autoware_image_projection_based_fusion/include/autoware/image_projection_based_fusion/fusion_node.hpp

+1-16
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ struct Det2dStatus
7676
double input_offset_ms = 0.0;
7777
// cache
7878
std::map<int64_t, typename Msg2D::ConstSharedPtr> cached_det2d_msgs;
79-
std::unique_ptr<std::mutex> mtx_ptr;
8079
};
8180

8281
template <class Msg3D, class Msg2D, class ExportObj>
@@ -108,26 +107,13 @@ class FusionNode : public rclcpp::Node
108107
// 2d detection management methods
109108
bool checkAllDet2dFused()
110109
{
111-
std::lock_guard<std::mutex> lock_det2d_flags(mutex_det2d_flags_);
112110
for (const auto & det2d : det2d_list_) {
113111
if (!det2d.is_fused) {
114112
return false;
115113
}
116114
}
117115
return true;
118116
}
119-
void setDet2dFused(Det2dStatus<Msg2D> & det2d)
120-
{
121-
std::lock_guard<std::mutex> lock_det2d_flags(mutex_det2d_flags_);
122-
det2d.is_fused = true;
123-
}
124-
void clearAllDet2dFlags()
125-
{
126-
std::lock_guard<std::mutex> lock_det2d_flags(mutex_det2d_flags_);
127-
for (auto & det2d : det2d_list_) {
128-
det2d.is_fused = false;
129-
}
130-
}
131117

132118
// Custom process methods
133119
virtual void preprocess(Msg3D & output_msg);
@@ -144,7 +130,6 @@ class FusionNode : public rclcpp::Node
144130

145131
// 2d detection management
146132
std::vector<Det2dStatus<Msg2D>> det2d_list_;
147-
std::mutex mutex_det2d_flags_;
148133

149134
// camera projection
150135
float approx_grid_cell_w_size_;
@@ -163,7 +148,7 @@ class FusionNode : public rclcpp::Node
163148
// cache for fusion
164149
int64_t cached_det3d_msg_timestamp_;
165150
typename Msg3D::SharedPtr cached_det3d_msg_ptr_;
166-
std::mutex mutex_det3d_msg_;
151+
std::mutex mutex_cached_msgs_;
167152

168153
// parameters for out_of_scope filter
169154
float filter_scope_min_x_;

‎perception/autoware_image_projection_based_fusion/src/fusion_node.cpp

+18-15
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ FusionNode<Msg3D, Msg2D, ExportObj>::FusionNode(
148148
// 2d detection status initialization
149149
det2d_list_.resize(rois_number_);
150150
for (std::size_t roi_i = 0; roi_i < rois_number_; ++roi_i) {
151-
det2d_list_.at(roi_i).mtx_ptr = std::make_unique<std::mutex>();
152151
det2d_list_.at(roi_i).id = roi_i;
153152
det2d_list_.at(roi_i).project_to_unrectified_image =
154153
point_project_to_unrectified_image.at(roi_i);
@@ -261,13 +260,16 @@ void FusionNode<Msg3D, Msg2D, ExportObj>::subCallback(
261260
// PROCESS: if the main message is remained (and roi is not collected all) publish the main
262261
// message may processed partially with arrived 2d rois
263262
stop_watch_ptr_->toc("processing_time", true);
264-
std::lock_guard<std::mutex> lock_det3d(mutex_det3d_msg_);
265263
exportProcess();
266264

267265
// reset flags
268-
clearAllDet2dFlags();
266+
for (auto & det2d : det2d_list_) {
267+
det2d.is_fused = false;
268+
}
269269
}
270270

271+
std::lock_guard<std::mutex> lock(mutex_cached_msgs_);
272+
271273
// TIMING: reset timer to the timeout time
272274
auto period = std::chrono::duration_cast<std::chrono::nanoseconds>(
273275
std::chrono::duration<double, std::milli>(timeout_ms_));
@@ -291,7 +293,6 @@ void FusionNode<Msg3D, Msg2D, ExportObj>::subCallback(
291293
// for loop for each roi
292294
for (auto & det2d : det2d_list_) {
293295
const auto roi_i = det2d.id;
294-
std::lock_guard<std::mutex> lock_det2d(*(det2d.mtx_ptr));
295296

296297
// check camera info
297298
if (det2d.camera_projector_ptr == nullptr) {
@@ -333,7 +334,7 @@ void FusionNode<Msg3D, Msg2D, ExportObj>::subCallback(
333334

334335
fuseOnSingleImage(*det3d_msg, det2d, *(det2d_msgs[matched_stamp]), *output_msg);
335336
det2d_msgs.erase(matched_stamp);
336-
setDet2dFused(det2d);
337+
det2d.is_fused = true;
337338

338339
// add timestamp interval for debug
339340
if (debug_publisher_) {
@@ -348,15 +349,16 @@ void FusionNode<Msg3D, Msg2D, ExportObj>::subCallback(
348349
}
349350

350351
// PROCESS: check if the fused message is ready to publish
351-
std::lock_guard<std::mutex> lock_det3d(mutex_det3d_msg_);
352352
cached_det3d_msg_timestamp_ = timestamp_nsec;
353353
cached_det3d_msg_ptr_ = output_msg;
354354
if (checkAllDet2dFused()) {
355355
// if all camera fused, postprocess and publish the main message
356356
exportProcess();
357357

358358
// reset flags
359-
clearAllDet2dFlags();
359+
for (auto & det2d : det2d_list_) {
360+
det2d.is_fused = false;
361+
}
360362
} else {
361363
// if all of rois are not collected, publish the old Msg(if exists) and cache the
362364
// current Msg
@@ -374,14 +376,11 @@ void FusionNode<Msg3D, Msg2D, ExportObj>::roiCallback(
374376
stop_watch_ptr_->toc("processing_time", true);
375377

376378
auto & det2d = det2d_list_.at(roi_i);
377-
std::lock_guard<std::mutex> lock_det2d(*(det2d.mtx_ptr));
378379

379380
int64_t timestamp_nsec =
380381
(*det2d_msg).header.stamp.sec * static_cast<int64_t>(1e9) + (*det2d_msg).header.stamp.nanosec;
381382
// if cached Msg exist, try to match
382383
if (cached_det3d_msg_ptr_ != nullptr) {
383-
std::lock_guard<std::mutex> lock_det3d(mutex_det3d_msg_);
384-
385384
int64_t new_stamp =
386385
cached_det3d_msg_timestamp_ + det2d.input_offset_ms * static_cast<int64_t>(1e6);
387386
int64_t interval = abs(timestamp_nsec - new_stamp);
@@ -401,7 +400,7 @@ void FusionNode<Msg3D, Msg2D, ExportObj>::roiCallback(
401400
}
402401
// PROCESS: fuse the main message with the roi message
403402
fuseOnSingleImage(*(cached_det3d_msg_ptr_), det2d, *det2d_msg, *(cached_det3d_msg_ptr_));
404-
setDet2dFused(det2d);
403+
det2d.is_fused = true;
405404

406405
if (debug_publisher_) {
407406
double timestamp_interval_ms = (timestamp_nsec - cached_det3d_msg_timestamp_) / 1e6;
@@ -416,7 +415,9 @@ void FusionNode<Msg3D, Msg2D, ExportObj>::roiCallback(
416415
if (checkAllDet2dFused()) {
417416
exportProcess();
418417
// reset flags
419-
clearAllDet2dFlags();
418+
for (auto & det2d : det2d_list_) {
419+
det2d.is_fused = false;
420+
}
420421
}
421422
processing_time_ms = processing_time_ms + stop_watch_ptr_->toc("processing_time", true);
422423
return;
@@ -440,17 +441,19 @@ void FusionNode<Msg3D, Msg2D, ExportObj>::timer_callback()
440441

441442
using std::chrono_literals::operator""ms;
442443
timer_->cancel();
443-
if (mutex_det3d_msg_.try_lock()) {
444+
if (mutex_cached_msgs_.try_lock()) {
444445
// PROCESS: if timeout, postprocess cached msg
445446
if (cached_det3d_msg_ptr_ != nullptr) {
446447
stop_watch_ptr_->toc("processing_time", true);
447448
exportProcess();
448449
}
449450

450451
// reset flags whether the message is fused or not
451-
clearAllDet2dFlags();
452+
for (auto & det2d : det2d_list_) {
453+
det2d.is_fused = false;
454+
}
452455

453-
mutex_det3d_msg_.unlock();
456+
mutex_cached_msgs_.unlock();
454457
} else {
455458
// TIMING: retry the process after 10ms
456459
try {

0 commit comments

Comments
 (0)