Skip to content

Commit 68125d6

Browse files
refactor(image_projection_based_fusion): rename cache-related variables' name (#5709)
* refactor(image_projection_based_fusion): rename sub_std_map_ to cached_msg_ Signed-off-by: kminoda <koji.minoda@tier4.jp> * style(pre-commit): autofix * rename other cache-related variables Signed-off-by: kminoda <koji.minoda@tier4.jp> * style(pre-commit): autofix --------- Signed-off-by: kminoda <koji.minoda@tier4.jp> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e6709ee commit 68125d6

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

perception/image_projection_based_fusion/include/image_projection_based_fusion/fusion_node.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ class FusionNode : public rclcpp::Node
118118

119119
// cache for fusion
120120
std::vector<bool> is_fused_;
121-
std::pair<int64_t, typename Msg::SharedPtr> sub_std_pair_;
122-
std::vector<std::map<int64_t, DetectedObjectsWithFeature::ConstSharedPtr>> roi_stdmap_;
123-
std::mutex mutex_;
121+
std::pair<int64_t, typename Msg::SharedPtr>
122+
cached_msg_; // first element is the timestamp in nanoseconds, second element is the message
123+
std::vector<std::map<int64_t, DetectedObjectsWithFeature::ConstSharedPtr>> cached_roi_msgs_;
124+
std::mutex mutex_cached_msgs_;
124125

125126
// output publisher
126127
typename rclcpp::Publisher<Msg>::SharedPtr pub_ptr_;

perception/image_projection_based_fusion/src/fusion_node.cpp

+31-31
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ FusionNode<Msg, ObjType>::FusionNode(
9696

9797
// sub rois
9898
rois_subs_.resize(rois_number_);
99-
roi_stdmap_.resize(rois_number_);
99+
cached_roi_msgs_.resize(rois_number_);
100100
is_fused_.resize(rois_number_, false);
101101
for (std::size_t roi_i = 0; roi_i < rois_number_; ++roi_i) {
102102
std::function<void(const DetectedObjectsWithFeature::ConstSharedPtr msg)> roi_callback =
@@ -163,12 +163,12 @@ void FusionNode<Msg, Obj>::preprocess(Msg & ouput_msg __attribute__((unused)))
163163
template <class Msg, class Obj>
164164
void FusionNode<Msg, Obj>::subCallback(const typename Msg::ConstSharedPtr input_msg)
165165
{
166-
if (sub_std_pair_.second != nullptr) {
166+
if (cached_msg_.second != nullptr) {
167167
stop_watch_ptr_->toc("processing_time", true);
168168
timer_->cancel();
169-
postprocess(*(sub_std_pair_.second));
170-
publish(*(sub_std_pair_.second));
171-
sub_std_pair_.second = nullptr;
169+
postprocess(*(cached_msg_.second));
170+
publish(*(cached_msg_.second));
171+
cached_msg_.second = nullptr;
172172
std::fill(is_fused_.begin(), is_fused_.end(), false);
173173

174174
// add processing time for debug
@@ -183,7 +183,7 @@ void FusionNode<Msg, Obj>::subCallback(const typename Msg::ConstSharedPtr input_
183183
}
184184
}
185185

186-
std::lock_guard<std::mutex> lock(mutex_);
186+
std::lock_guard<std::mutex> lock(mutex_cached_msgs_);
187187
auto period = std::chrono::duration_cast<std::chrono::nanoseconds>(
188188
std::chrono::duration<double, std::milli>(timeout_ms_));
189189
try {
@@ -211,12 +211,12 @@ void FusionNode<Msg, Obj>::subCallback(const typename Msg::ConstSharedPtr input_
211211
continue;
212212
}
213213

214-
if ((roi_stdmap_.at(roi_i)).size() > 0) {
214+
if ((cached_roi_msgs_.at(roi_i)).size() > 0) {
215215
int64_t min_interval = 1e9;
216216
int64_t matched_stamp = -1;
217217
std::list<int64_t> outdate_stamps;
218218

219-
for (const auto & [k, v] : roi_stdmap_.at(roi_i)) {
219+
for (const auto & [k, v] : cached_roi_msgs_.at(roi_i)) {
220220
int64_t new_stamp = timestamp_nsec + input_offset_ms_.at(roi_i) * (int64_t)1e6;
221221
int64_t interval = abs(int64_t(k) - new_stamp);
222222

@@ -230,7 +230,7 @@ void FusionNode<Msg, Obj>::subCallback(const typename Msg::ConstSharedPtr input_
230230

231231
// remove outdated stamps
232232
for (auto stamp : outdate_stamps) {
233-
(roi_stdmap_.at(roi_i)).erase(stamp);
233+
(cached_roi_msgs_.at(roi_i)).erase(stamp);
234234
}
235235

236236
// fuseOnSingle
@@ -240,9 +240,9 @@ void FusionNode<Msg, Obj>::subCallback(const typename Msg::ConstSharedPtr input_
240240
}
241241

242242
fuseOnSingleImage(
243-
*input_msg, roi_i, *((roi_stdmap_.at(roi_i))[matched_stamp]), camera_info_map_.at(roi_i),
244-
*output_msg);
245-
(roi_stdmap_.at(roi_i)).erase(matched_stamp);
243+
*input_msg, roi_i, *((cached_roi_msgs_.at(roi_i))[matched_stamp]),
244+
camera_info_map_.at(roi_i), *output_msg);
245+
(cached_roi_msgs_.at(roi_i)).erase(matched_stamp);
246246
is_fused_.at(roi_i) = true;
247247

248248
// add timestamp interval for debug
@@ -265,7 +265,7 @@ void FusionNode<Msg, Obj>::subCallback(const typename Msg::ConstSharedPtr input_
265265
postprocess(*output_msg);
266266
publish(*output_msg);
267267
std::fill(is_fused_.begin(), is_fused_.end(), false);
268-
sub_std_pair_.second = nullptr;
268+
cached_msg_.second = nullptr;
269269

270270
// add processing time for debug
271271
if (debug_publisher_) {
@@ -278,8 +278,8 @@ void FusionNode<Msg, Obj>::subCallback(const typename Msg::ConstSharedPtr input_
278278
processing_time_ms = 0;
279279
}
280280
} else {
281-
sub_std_pair_.first = int64_t(timestamp_nsec);
282-
sub_std_pair_.second = output_msg;
281+
cached_msg_.first = int64_t(timestamp_nsec);
282+
cached_msg_.second = output_msg;
283283
processing_time_ms = stop_watch_ptr_->toc("processing_time", true);
284284
}
285285
}
@@ -294,28 +294,28 @@ void FusionNode<Msg, Obj>::roiCallback(
294294
(*input_roi_msg).header.stamp.sec * (int64_t)1e9 + (*input_roi_msg).header.stamp.nanosec;
295295

296296
// if cached Msg exist, try to match
297-
if (sub_std_pair_.second != nullptr) {
298-
int64_t new_stamp = sub_std_pair_.first + input_offset_ms_.at(roi_i) * (int64_t)1e6;
297+
if (cached_msg_.second != nullptr) {
298+
int64_t new_stamp = cached_msg_.first + input_offset_ms_.at(roi_i) * (int64_t)1e6;
299299
int64_t interval = abs(timestamp_nsec - new_stamp);
300300

301301
if (interval < match_threshold_ms_ * (int64_t)1e6 && is_fused_.at(roi_i) == false) {
302302
if (camera_info_map_.find(roi_i) == camera_info_map_.end()) {
303303
RCLCPP_WARN_THROTTLE(
304304
this->get_logger(), *this->get_clock(), 5000, "no camera info. id is %zu", roi_i);
305-
(roi_stdmap_.at(roi_i))[timestamp_nsec] = input_roi_msg;
305+
(cached_roi_msgs_.at(roi_i))[timestamp_nsec] = input_roi_msg;
306306
return;
307307
}
308308
if (debugger_) {
309309
debugger_->clear();
310310
}
311311

312312
fuseOnSingleImage(
313-
*(sub_std_pair_.second), roi_i, *input_roi_msg, camera_info_map_.at(roi_i),
314-
*(sub_std_pair_.second));
313+
*(cached_msg_.second), roi_i, *input_roi_msg, camera_info_map_.at(roi_i),
314+
*(cached_msg_.second));
315315
is_fused_.at(roi_i) = true;
316316

317317
if (debug_publisher_) {
318-
double timestamp_interval_ms = (timestamp_nsec - sub_std_pair_.first) / 1e6;
318+
double timestamp_interval_ms = (timestamp_nsec - cached_msg_.first) / 1e6;
319319
debug_publisher_->publish<tier4_debug_msgs::msg::Float64Stamped>(
320320
"debug/roi" + std::to_string(roi_i) + "/timestamp_interval_ms", timestamp_interval_ms);
321321
debug_publisher_->publish<tier4_debug_msgs::msg::Float64Stamped>(
@@ -325,10 +325,10 @@ void FusionNode<Msg, Obj>::roiCallback(
325325

326326
if (std::count(is_fused_.begin(), is_fused_.end(), true) == static_cast<int>(rois_number_)) {
327327
timer_->cancel();
328-
postprocess(*(sub_std_pair_.second));
329-
publish(*(sub_std_pair_.second));
328+
postprocess(*(cached_msg_.second));
329+
publish(*(cached_msg_.second));
330330
std::fill(is_fused_.begin(), is_fused_.end(), false);
331-
sub_std_pair_.second = nullptr;
331+
cached_msg_.second = nullptr;
332332

333333
// add processing time for debug
334334
if (debug_publisher_) {
@@ -346,7 +346,7 @@ void FusionNode<Msg, Obj>::roiCallback(
346346
}
347347
}
348348
// store roi msg if not matched
349-
(roi_stdmap_.at(roi_i))[timestamp_nsec] = input_roi_msg;
349+
(cached_roi_msgs_.at(roi_i))[timestamp_nsec] = input_roi_msg;
350350
}
351351

352352
template <class Msg, class Obj>
@@ -360,13 +360,13 @@ void FusionNode<Msg, Obj>::timer_callback()
360360
{
361361
using std::chrono_literals::operator""ms;
362362
timer_->cancel();
363-
if (mutex_.try_lock()) {
363+
if (mutex_cached_msgs_.try_lock()) {
364364
// timeout, postprocess cached msg
365-
if (sub_std_pair_.second != nullptr) {
365+
if (cached_msg_.second != nullptr) {
366366
stop_watch_ptr_->toc("processing_time", true);
367367

368-
postprocess(*(sub_std_pair_.second));
369-
publish(*(sub_std_pair_.second));
368+
postprocess(*(cached_msg_.second));
369+
publish(*(cached_msg_.second));
370370

371371
// add processing time for debug
372372
if (debug_publisher_) {
@@ -380,9 +380,9 @@ void FusionNode<Msg, Obj>::timer_callback()
380380
}
381381
}
382382
std::fill(is_fused_.begin(), is_fused_.end(), false);
383-
sub_std_pair_.second = nullptr;
383+
cached_msg_.second = nullptr;
384384

385-
mutex_.unlock();
385+
mutex_cached_msgs_.unlock();
386386
} else {
387387
try {
388388
std::chrono::nanoseconds period = 10ms;

0 commit comments

Comments
 (0)