Skip to content

Commit 5dd68d7

Browse files
committed
Refactor in_range and count_stopped functions
Signed-off-by: Mengting Yang <[email protected]> Signed-off-by: lotusymt <[email protected]>
1 parent 04191ae commit 5dd68d7

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

nav2_collision_monitor/src/costmap.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ bool CostmapSource::getData(
7979
tf2::Transform tf_transform; tf_transform.setIdentity();
8080
const std::string src = data_->header.frame_id;
8181

82-
// This branch is for malformed /local_costmap/costmap in tests or bags.
83-
// It is not expected in the happy path, so we exclude it from coverage.
8482
if (src != base_frame_id_) {
8583
if (!getTransform(curr_time, data_->header, tf_transform)) {
8684
RCLCPP_WARN(logger_, "[%s] TF %s->%s unavailable at t=%.3f",

nav2_collision_monitor/test/collision_monitor_node_bag.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class MetricsCatcher : public rclcpp::Node {
6161
stop_thresh_ = this->declare_parameter<double>("stop_thresh", 0.02); // |vx| <= stop
6262
resume_thresh_ = this->declare_parameter<double>(
6363
"resume_thresh", 0.10); // vx >= resume
64-
6564
debounce_n_ = this->declare_parameter<int>(
6665
"debounce_n", 3); // need K in arow
6766

@@ -144,13 +143,18 @@ class MetricsCatcher : public rclcpp::Node {
144143

145144
if (!R.have_data) {return R;}
146145

147-
// helper: pick samples in a time range
148-
auto in_range = [&](double t0, double t1){
149-
std::vector<Sample> out; out.reserve(samples_.size());
150-
for (const auto & s : samples_) {if (s.t >= t0 && s.t <= t1) {out.push_back(s);}}
146+
auto in_range = [&](double t0, double t1) { // helper: pick samples in [t0, t1]
147+
std::vector<Sample> out;
148+
out.reserve(samples_.size());
149+
for (const auto & s : samples_) {
150+
if (s.t >= t0 && s.t <= t1) {
151+
out.push_back(s);
152+
}
153+
}
151154
return out;
152155
};
153156

157+
154158
// Segments:
155159
// 0..(3 - 0.2) → must be clean (no stop) (guard band)
156160
const auto stop_seg = in_range(stop_window_start_, stop_window_end_);
@@ -193,9 +197,12 @@ class MetricsCatcher : public rclcpp::Node {
193197
// 4) false-stop%: outside the window we should mostly be moving
194198
const size_t clean_total = pre_seg.size() + post_seg.size();
195199
if (clean_total > 0) {
196-
auto count_stopped = [&](const std::vector<Sample> & seg){
197-
return std::count_if(seg.begin(), seg.end(),
198-
[&](const Sample & s){return std::fabs(s.vx) <= stop_thresh_;});
200+
auto count_stopped = [&](const std::vector<Sample> & seg) {
201+
return std::count_if(
202+
seg.begin(), seg.end(),
203+
[&](const Sample & s) {
204+
return std::fabs(s.vx) <= stop_thresh_;
205+
});
199206
};
200207
const size_t clean_stopped = count_stopped(pre_seg) + count_stopped(post_seg);
201208
R.false_stop_pct = static_cast<double>(clean_stopped) / static_cast<double>(clean_total);

0 commit comments

Comments
 (0)