fix(tracker): retain slow moving tracks

Add a longer motion estimate with timed hysteresis. Keep recent tracks usable during brief TF waits and delay static learning around confirmed tracks.
This commit is contained in:
2026-10-07 17:51:13 +07:00
parent b8422b172a
commit 6a6bbf2e0c
12 changed files with 194 additions and 64 deletions

View File

@@ -68,7 +68,8 @@ tránh va chạm; tracker không thay thế nguồn phát hiện vật cản t
Profile Olei bật `deskew_enabled`: node tra TF tại tia đầu và tia cuối rồi nội suy pose cho Profile Olei bật `deskew_enabled`: node tra TF tại tia đầu và tia cuối rồi nội suy pose cho
từng tia theo `time_increment`. Tách cụm và nền dùng chung điểm đã biến đổi. Nếu TF của tia cuối từng tia theo `time_increment`. Tách cụm và nền dùng chung điểm đã biến đổi. Nếu TF của tia cuối
chưa có, scan chờ tối đa `tf_wait`; nếu hết hạn, scan bị loại và health báo lỗi. Tia xuất hiện chưa có, scan chờ tối đa `tf_wait`. Trong lúc chờ, scan trước vẫn hợp lệ nếu chưa quá `max_age`;
chỉ scan chờ quá hạn mới bị loại và báo `tf_error`. Tia xuất hiện
trong vùng đã quan sát là trống được giữ ngoài nền trong `background_foreground_hold` giây, trong vùng đã quan sát là trống được giữ ngoài nền trong `background_foreground_hold` giây,
trừ khi nó ổn định đủ lâu để học thành vật tĩnh. trừ khi nó ổn định đủ lâu để học thành vật tĩnh.
Tia sát biên che khuất hoặc chỗ độ sâu nhảy mạnh vẫn có thể tạo detection, nhưng không Tia sát biên che khuất hoặc chỗ độ sâu nhảy mạnh vẫn có thể tạo detection, nhưng không
@@ -78,7 +79,12 @@ Tia sát biên che khuất hoặc chỗ độ sâu nhảy mạnh vẫn có thể
`/tracker/debug_tracks` hiện cả `TENTATIVE`, `COASTING`, `STATIC` và `UNKNOWN` để chẩn đoán. `/tracker/debug_tracks` hiện cả `TENTATIVE`, `COASTING`, `STATIC` và `UNKNOWN` để chẩn đoán.
`/tracker/predictions` chỉ dự báo cho cùng tập `MOVING` hợp lệ. MarkerArray là đầu ra RViz; `/tracker/predictions` chỉ dự báo cho cùng tập `MOVING` hợp lệ. MarkerArray là đầu ra RViz;
consumer dùng `TrackSnapshot` cùng lifecycle, motion, health và `last_observed_stamp`. consumer dùng `TrackSnapshot` cùng lifecycle, motion, health và `last_observed_stamp`.
`/tracker/status` có số scan đã xử lý/bỏ, phân loại tia và độ dời TF trong một scan. `/tracker/status` có số scan đã xử lý/bỏ, phân loại tia, độ dời TF trong một scan và
`tf_waited/timeouts` để phân biệt chờ TF ngắn với scan thực sự bị loại.
Profile Olei xét thêm cửa sổ 1,2 s cho người đi chậm, giữ nhánh 0,55 s cho chuyển động nhanh;
bằng chứng `known_free` và biên che khuất vẫn áp dụng. Một quan sát mơ hồ không lập tức xóa
nhãn `MOVING`; nhãn chỉ được giữ thêm tối đa `motion_exit_hold` khi scan mới vẫn còn hợp lệ.
Ứng viên gần track đã xác nhận được trì hoãn học vào nền tối đa `background_track_hold`.
measurement_clock trả Stamp (int64 nanosecond) cùng clock với scan/TF. measurement_clock trả Stamp (int64 nanosecond) cùng clock với scan/TF.
Có thể bỏ callback để dùng robot::Time::now() khi host đã cấu hình clock này đúng. Có thể bỏ callback để dùng robot::Time::now() khi host đã cấu hình clock này đúng.

View File

@@ -38,10 +38,15 @@ tentative_timeout: 0.3
track_timeout: 0.8 track_timeout: 0.8
max_prediction: 0.5 max_prediction: 0.5
moving_speed: 0.15 moving_speed: 0.15
stationary_speed: 0.08 stationary_speed: 0.05
motion_velocity_sigma: 0.7 motion_velocity_sigma: 0.7
confirmation_min_span: 0.15 # s; track mới cần tồn tại đủ lâu. confirmation_min_span: 0.15 # s; track mới cần tồn tại đủ lâu.
motion_window: 0.55 # s; đo độ dời nhất quán qua nhiều scan. motion_window: 0.55 # s; đo độ dời nhất quán qua nhiều scan.
slow_motion_window: 1.20 # s; cửa sổ thứ hai để thấy người đi chậm.
slow_motion_min_displacement: 0.08 # m; phải đi đủ xa hơn nhiễu vị trí.
slow_moving_speed: 0.085 # m/s; cận dưới tốc độ từ hồi quy quỹ đạo.
motion_enter_hold: 0.15 # s; bằng chứng MOVING phải tồn tại liên tục.
motion_exit_hold: 0.40 # s; tránh mất nhãn bởi một quan sát không rõ.
motion_min_observations: 5 # Số quan sát thật tối thiểu. motion_min_observations: 5 # Số quan sát thật tối thiểu.
motion_known_free_beams: 3 # Tối thiểu 3 tia của cụm vào vùng từng thấy là trống. motion_known_free_beams: 3 # Tối thiểu 3 tia của cụm vào vùng từng thấy là trống.
motion_known_free_observations: 3 # Cần ít nhất 3 scan có bằng chứng vùng trống để gọi MOVING. motion_known_free_observations: 3 # Cần ít nhất 3 scan có bằng chứng vùng trống để gọi MOVING.
@@ -68,5 +73,6 @@ background_retention: 20.0 # s; nền cũ hết hạn để tránh odom trôi k
background_radius: 12.0 # m; ngoài vùng này không xuất track động cho local planner. background_radius: 12.0 # m; ngoài vùng này không xuất track động cho local planner.
background_dynamic_padding: 0.20 # m; không học người đang moving thành nền. background_dynamic_padding: 0.20 # m; không học người đang moving thành nền.
background_foreground_hold: 3.0 # s; vật xuất hiện trong vùng từng trống phải ổn định trước khi học nền. background_foreground_hold: 3.0 # s; vật xuất hiện trong vùng từng trống phải ổn định trước khi học nền.
background_track_hold: 1.5 # s; giữ ứng viên gần track đã xác nhận trước khi học nền.
background_edge_guard_beams: 3 # Tia sát biên che khuất không đủ bằng chứng để gọi MOVING. background_edge_guard_beams: 3 # Tia sát biên che khuất không đủ bằng chứng để gọi MOVING.
background_edge_depth_jump: 0.30 # m; ngưỡng đổi khoảng cách giữa hai tia kề nhau. background_edge_depth_jump: 0.30 # m; ngưỡng đổi khoảng cách giữa hai tia kề nhau.

View File

@@ -69,6 +69,8 @@ private:
bool mature(const Cell& cell) const; bool mature(const Cell& cell) const;
bool knownFree(const Eigen::Vector2d& point, Stamp stamp) const; bool knownFree(const Eigen::Vector2d& point, Stamp stamp) const;
bool nearMovingTrack(const Eigen::Vector2d& point, const std::vector<Track>& tracks) const; bool nearMovingTrack(const Eigen::Vector2d& point, const std::vector<Track>& tracks) const;
bool nearRecentTrack(const Eigen::Vector2d& point, Stamp stamp,
const std::vector<Track>& tracks) const;
bool validRange(const RawScan& scan, float range) const; bool validRange(const RawScan& scan, float range) const;
TrackerConfig config_; TrackerConfig config_;
std::unordered_map<Key, Cell, KeyHash> cells_; std::unordered_map<Key, Cell, KeyHash> cells_;

View File

@@ -37,6 +37,9 @@ struct TrackerConfig
double moving_speed = 0.15, stationary_speed = 0.08; // [m/s]. double moving_speed = 0.15, stationary_speed = 0.08; // [m/s].
double motion_velocity_sigma = 0.7; // Maximum velocity stddev for classification [m/s]. double motion_velocity_sigma = 0.7; // Maximum velocity stddev for classification [m/s].
double confirmation_min_span = 0.15, motion_window = 0.55; // [s]. double confirmation_min_span = 0.15, motion_window = 0.55; // [s].
double slow_motion_window = 0.0; // [s], 0 disables the longer motion estimate.
double slow_motion_min_displacement = 0.08, slow_moving_speed = 0.10; // [m], [m/s].
double motion_enter_hold = 0.0, motion_exit_hold = 0.0; // [s], sustained evidence.
std::size_t motion_min_observations = 5; std::size_t motion_min_observations = 5;
std::size_t motion_known_free_beams = 0, motion_known_free_observations = 0; std::size_t motion_known_free_beams = 0, motion_known_free_observations = 0;
double motion_min_displacement = 0.10, motion_max_residual = 0.12; // [m]. double motion_min_displacement = 0.10, motion_max_residual = 0.12; // [m].
@@ -55,6 +58,7 @@ struct TrackerConfig
double background_free_min_span = 0.20; // [s]. double background_free_min_span = 0.20; // [s].
double background_radius = 12.0, background_dynamic_padding = 0.20; // [m]. double background_radius = 12.0, background_dynamic_padding = 0.20; // [m].
double background_foreground_hold = 3.0; // [s], stable foreground before learning as static. double background_foreground_hold = 3.0; // [s], stable foreground before learning as static.
double background_track_hold = 0.0; // [s], bounded hold for tracked unknown foreground.
std::size_t background_edge_guard_beams = 0; // 0 disables depth-edge evidence guard. std::size_t background_edge_guard_beams = 0; // 0 disables depth-edge evidence guard.
double background_edge_depth_jump = 0.30; // [m], discontinuity between adjacent beams. double background_edge_depth_jump = 0.30; // [m], discontinuity between adjacent beams.
std::size_t background_min_hits = 8, background_clear_scans = 4; std::size_t background_min_hits = 8, background_clear_scans = 4;

View File

@@ -28,6 +28,8 @@ private:
Track track; Track track;
std::deque<Observation> observations; std::deque<Observation> observations;
std::size_t moving_hits = 0, stationary_hits = 0; std::size_t moving_hits = 0, stationary_hits = 0;
Stamp moving_candidate_since = 0, stationary_candidate_since = 0;
Stamp last_moving_evidence_stamp = 0;
}; };
void classify(Record&); void classify(Record&);
void addObservation(Record&, const Detection&); void addObservation(Record&, const Detection&);

View File

@@ -72,7 +72,7 @@ struct DetectionFrame
struct Track struct Track
{ {
std::uint64_t session_id = 0, track_id = 0; std::uint64_t session_id = 0, track_id = 0;
Stamp state_stamp = 0, last_observed_stamp = 0; Stamp state_stamp = 0, last_observed_stamp = 0, first_observed_stamp = 0;
Eigen::Vector4d state = Eigen::Vector4d::Zero(); // x, y, vx, vy in snapshot frame. Eigen::Vector4d state = Eigen::Vector4d::Zero(); // x, y, vx, vy in snapshot frame.
Eigen::Matrix4d covariance = Eigen::Matrix4d::Identity(); Eigen::Matrix4d covariance = Eigen::Matrix4d::Identity();
double radius = 0.1; double radius = 0.1;
@@ -94,6 +94,7 @@ struct SourceHealth
std::size_t last_occlusion_edge_beams = 0; std::size_t last_occlusion_edge_beams = 0;
double last_scan_tf_translation = 0.0, last_scan_tf_yaw = 0.0; double last_scan_tf_translation = 0.0, last_scan_tf_yaw = 0.0;
std::uint64_t deskewed_count = 0; std::uint64_t deskewed_count = 0;
std::uint64_t tf_waited_count = 0, tf_timeout_count = 0;
SteadyTime last_receive_steady_time{}, last_success_steady_time{}; SteadyTime last_receive_steady_time{}, last_success_steady_time{};
std::string reason; std::string reason;
}; };

View File

@@ -315,6 +315,10 @@ private:
std::to_string(snapshot->sources.front().last_scan_tf_translation) + "m/" + std::to_string(snapshot->sources.front().last_scan_tf_translation) + "m/" +
std::to_string(snapshot->sources.front().last_scan_tf_yaw) + "rad" + std::to_string(snapshot->sources.front().last_scan_tf_yaw) + "rad" +
" | deskewed=" + std::to_string(snapshot->sources.front().deskewed_count); " | deskewed=" + std::to_string(snapshot->sources.front().deskewed_count);
if (!snapshot->sources.empty())
status.data += " | tf_waited/timeouts=" +
std::to_string(snapshot->sources.front().tf_waited_count) + "/" +
std::to_string(snapshot->sources.front().tf_timeout_count);
status_pub_.publish(status); status_pub_.publish(status);
ROS_INFO_THROTTLE(2.0, "%s", status.data.c_str()); ROS_INFO_THROTTLE(2.0, "%s", status.data.c_str());
} }

View File

@@ -75,10 +75,28 @@ bool StaticBackgroundModel::nearMovingTrack(
const Eigen::Vector2d& point, const std::vector<Track>& tracks) const const Eigen::Vector2d& point, const std::vector<Track>& tracks) const
{ {
for (const auto& track : tracks) for (const auto& track : tracks)
if (track.motion == MotionClass::kMoving && {
(point - track.state.head<2>()).norm() <= if (track.motion != MotionClass::kMoving) continue;
track.radius + config_.background_dynamic_padding) const double radius = track.radius + config_.background_dynamic_padding;
return true; if ((point - track.state.head<2>()).squaredNorm() <= radius * radius) return true;
}
return false;
}
bool StaticBackgroundModel::nearRecentTrack(
const Eigen::Vector2d& point, Stamp stamp, const std::vector<Track>& tracks) const
{
if (config_.background_track_hold <= 0.0) return false;
for (const auto& track : tracks)
{
if (track.lifecycle != TrackLifecycle::kConfirmed || !track.first_observed_stamp) continue;
const double track_age = secondsBetween(stamp, track.first_observed_stamp);
const double observed_age = secondsBetween(stamp, track.last_observed_stamp);
if (track_age < 0.0 || track_age > config_.background_track_hold ||
observed_age < 0.0 || observed_age > config_.coasting_delay) continue;
const double radius = track.radius + config_.background_dynamic_padding;
if ((point - track.state.head<2>()).squaredNorm() <= radius * radius) return true;
}
return false; return false;
} }
@@ -198,7 +216,10 @@ void StaticBackgroundModel::integrate(const RawScan& scan, const ProjectedScan&
if (!validRange(scan, range) || range > config_.background_radius) continue; if (!validRange(scan, range) || range > config_.background_radius) continue;
const Eigen::Vector2d& point = projected.points[i]; const Eigen::Vector2d& point = projected.points[i];
if (nearMovingTrack(point, tracks)) continue; if (nearMovingTrack(point, tracks)) continue;
if (candidate_beams[i] == 2 || candidate_beams[i] == 3) const bool known_free_foreground = candidate_beams[i] == 2 || candidate_beams[i] == 3;
const bool tracked_foreground = candidate_beams[i] == 1 &&
nearRecentTrack(point, scan.stamp, tracks);
if (known_free_foreground || tracked_foreground)
{ {
auto& foreground = foreground_cells_[keyFor(point)]; auto& foreground = foreground_cells_[keyFor(point)];
if (!foreground.first_seen || if (!foreground.first_seen ||
@@ -206,8 +227,9 @@ void StaticBackgroundModel::integrate(const RawScan& scan, const ProjectedScan&
{ foreground.first_seen = scan.stamp; foreground.hits = 0; } { foreground.first_seen = scan.stamp; foreground.hits = 0; }
foreground.last_seen = scan.stamp; foreground.last_seen = scan.stamp;
++foreground.hits; ++foreground.hits;
if (secondsBetween(scan.stamp, foreground.first_seen) < const double hold = tracked_foreground ? config_.background_track_hold :
config_.background_foreground_hold || config_.background_foreground_hold;
if (secondsBetween(scan.stamp, foreground.first_seen) < hold ||
foreground.hits < config_.background_min_hits) continue; foreground.hits < config_.background_min_hits) continue;
} }
else foreground_cells_.erase(keyFor(point)); else foreground_cells_.erase(keyFor(point));

View File

@@ -57,6 +57,14 @@ bool TrackerConfig::validate(std::string& error) const
{ error = "measurement scales and confirmation_min_span must be finite and >= 0"; return false; } { error = "measurement scales and confirmation_min_span must be finite and >= 0"; return false; }
if (!std::isfinite(extent_padding) || extent_padding < 0.0) if (!std::isfinite(extent_padding) || extent_padding < 0.0)
{ error = "extent_padding must be finite and >= 0"; return false; } { error = "extent_padding must be finite and >= 0"; return false; }
if (!std::isfinite(slow_motion_window) || slow_motion_window < 0.0 ||
!std::isfinite(motion_enter_hold) || motion_enter_hold < 0.0 ||
!std::isfinite(motion_exit_hold) || motion_exit_hold < 0.0 ||
!std::isfinite(background_track_hold) || background_track_hold < 0.0)
{ error = "slow motion and hold durations must be finite and >= 0"; return false; }
if (slow_motion_window > 0.0 &&
(!positive(slow_motion_min_displacement, "slow_motion_min_displacement") ||
!positive(slow_moving_speed, "slow_moving_speed"))) return false;
if (!std::isfinite(association_tentative_penalty) || association_tentative_penalty < 0.0 || if (!std::isfinite(association_tentative_penalty) || association_tentative_penalty < 0.0 ||
!std::isfinite(association_coasting_penalty) || association_coasting_penalty < 0.0 || !std::isfinite(association_coasting_penalty) || association_coasting_penalty < 0.0 ||
association_tentative_penalty > gate_chi2 || association_tentative_penalty > gate_chi2 ||
@@ -75,6 +83,12 @@ bool TrackerConfig::validate(std::string& error) const
max_prediction > track_timeout || fusion_wait > tf_wait || poll_period > tf_wait || max_prediction > track_timeout || fusion_wait > tf_wait || poll_period > tf_wait ||
measurement_max_sigma < measurement_sigma || measurement_max_sigma < measurement_sigma ||
motion_window <= confirmation_min_span || motion_window <= confirmation_min_span ||
(slow_motion_window > 0.0 &&
(slow_motion_window <= motion_window || slow_motion_window > 2.0 ||
slow_motion_min_displacement >= motion_min_displacement ||
slow_moving_speed >= moving_speed ||
slow_moving_speed <= stationary_speed)) ||
motion_enter_hold >= track_timeout || motion_exit_hold >= track_timeout ||
coasting_delay >= track_timeout || coasting_delay >= track_timeout ||
background_match_distance < background_resolution || background_match_distance < background_resolution ||
background_depth_margin > background_match_distance || background_depth_margin > background_match_distance ||
@@ -83,6 +97,7 @@ bool TrackerConfig::validate(std::string& error) const
background_edge_guard_beams > 16 || background_edge_depth_jump > 2.0 || background_edge_guard_beams > 16 || background_edge_depth_jump > 2.0 ||
background_min_span >= background_retention || background_min_span >= background_retention ||
background_foreground_hold >= background_retention || background_foreground_hold >= background_retention ||
background_track_hold >= background_retention ||
background_free_min_span >= background_retention || background_free_min_span >= background_retention ||
background_radius <= background_match_distance) background_radius <= background_match_distance)
{ error = "inconsistent motion thresholds, retention, prediction or wait periods"; return false; } { error = "inconsistent motion thresholds, retention, prediction or wait periods"; return false; }

View File

@@ -35,6 +35,8 @@ bool loadTrackerConfig(const std::string& path, TrackerConfig& out, std::string&
LOAD_FIELD(tentative_timeout) LOAD_FIELD(track_timeout) LOAD_FIELD(max_prediction) LOAD_FIELD(tentative_timeout) LOAD_FIELD(track_timeout) LOAD_FIELD(max_prediction)
LOAD_FIELD(moving_speed) LOAD_FIELD(stationary_speed) LOAD_FIELD(motion_velocity_sigma) LOAD_FIELD(moving_speed) LOAD_FIELD(stationary_speed) LOAD_FIELD(motion_velocity_sigma)
LOAD_FIELD(confirmation_min_span) LOAD_FIELD(motion_window) LOAD_FIELD(confirmation_min_span) LOAD_FIELD(motion_window)
LOAD_FIELD(slow_motion_window) LOAD_FIELD(slow_motion_min_displacement)
LOAD_FIELD(slow_moving_speed) LOAD_FIELD(motion_enter_hold) LOAD_FIELD(motion_exit_hold)
LOAD_FIELD(motion_min_observations) LOAD_FIELD(motion_min_displacement) LOAD_FIELD(motion_min_observations) LOAD_FIELD(motion_min_displacement)
LOAD_FIELD(motion_known_free_beams) LOAD_FIELD(motion_known_free_observations) LOAD_FIELD(motion_known_free_beams) LOAD_FIELD(motion_known_free_observations)
LOAD_FIELD(motion_max_residual) LOAD_FIELD(coasting_delay) LOAD_FIELD(motion_max_residual) LOAD_FIELD(coasting_delay)
@@ -48,6 +50,7 @@ bool loadTrackerConfig(const std::string& path, TrackerConfig& out, std::string&
LOAD_FIELD(background_retention) LOAD_FIELD(background_radius) LOAD_FIELD(background_retention) LOAD_FIELD(background_radius)
LOAD_FIELD(background_dynamic_padding) LOAD_FIELD(background_min_hits) LOAD_FIELD(background_dynamic_padding) LOAD_FIELD(background_min_hits)
LOAD_FIELD(background_foreground_hold) LOAD_FIELD(background_foreground_hold)
LOAD_FIELD(background_track_hold)
LOAD_FIELD(background_edge_guard_beams) LOAD_FIELD(background_edge_depth_jump) LOAD_FIELD(background_edge_guard_beams) LOAD_FIELD(background_edge_depth_jump)
LOAD_FIELD(background_clear_scans) LOAD_FIELD(background_clear_scans)
#undef LOAD_FIELD #undef LOAD_FIELD

View File

@@ -117,63 +117,116 @@ void TrackerCore::expire(Stamp now)
} }
void TrackerCore::classify(Record& r) void TrackerCore::classify(Record& r)
{ {
const Stamp observed = r.observations.empty() ? 0 : r.observations.back().stamp;
const auto retainRecentMotion = [&] {
if (r.track.motion != MotionClass::kMoving || !r.last_moving_evidence_stamp ||
secondsBetween(observed, r.last_moving_evidence_stamp) > config_.motion_exit_hold)
r.track.motion = MotionClass::kUnknown;
};
if (r.track.lifecycle != TrackLifecycle::kConfirmed || if (r.track.lifecycle != TrackLifecycle::kConfirmed ||
r.observations.size() < config_.motion_min_observations || r.observations.size() < config_.motion_min_observations ||
r.track.covariance.bottomRightCorner<2,2>().diagonal().maxCoeff() > r.track.covariance.bottomRightCorner<2,2>().diagonal().maxCoeff() >
config_.motion_velocity_sigma * config_.motion_velocity_sigma) config_.motion_velocity_sigma * config_.motion_velocity_sigma)
{ r.moving_hits = r.stationary_hits = 0; r.track.motion = MotionClass::kUnknown; return; } {
const double span = secondsBetween(r.observations.back().stamp, r.observations.front().stamp); r.moving_hits = r.stationary_hits = 0;
if (span < config_.motion_window * 0.65) r.moving_candidate_since = r.stationary_candidate_since = 0;
{ r.moving_hits = r.stationary_hits = 0; r.track.motion = MotionClass::kUnknown; return; } if (r.track.lifecycle != TrackLifecycle::kConfirmed) r.track.motion = MotionClass::kUnknown;
else retainRecentMotion();
return;
}
double mean_time = 0.0; struct MotionFit
Eigen::Vector2d mean_position = Eigen::Vector2d::Zero();
for (const auto& observation : r.observations)
{ {
mean_time += secondsBetween(observation.stamp, r.observations.front().stamp); bool ready = false;
mean_position += observation.position; double speed = 0.0, slope_sigma = 0.0, residual = 0.0, displacement = 0.0;
} std::size_t free_observations = 0;
mean_time /= static_cast<double>(r.observations.size()); };
mean_position /= static_cast<double>(r.observations.size()); const auto fit = [&](double window) {
double time_variance = 0.0; MotionFit result;
Eigen::Vector2d cross = Eigen::Vector2d::Zero(); auto first = r.observations.begin();
for (const auto& observation : r.observations) while (first != r.observations.end() &&
secondsBetween(observed, first->stamp) > window) ++first;
if (first == r.observations.end() ||
secondsBetween(observed, first->stamp) < window * 0.65) return result;
std::size_t count = 0;
double mean_time = 0.0;
Eigen::Vector2d mean_position = Eigen::Vector2d::Zero();
for (auto it = first; it != r.observations.end(); ++it)
{
++count;
mean_time += secondsBetween(it->stamp, first->stamp);
mean_position += it->position;
if (it->known_free) ++result.free_observations;
}
if (count < config_.motion_min_observations) return result;
mean_time /= static_cast<double>(count);
mean_position /= static_cast<double>(count);
double time_variance = 0.0;
Eigen::Vector2d cross = Eigen::Vector2d::Zero();
for (auto it = first; it != r.observations.end(); ++it)
{
const double dt = secondsBetween(it->stamp, first->stamp) - mean_time;
time_variance += dt * dt;
cross += dt * (it->position - mean_position);
}
if (time_variance <= 1e-9) return result;
const Eigen::Vector2d velocity = cross / time_variance;
double residual2 = 0.0;
for (auto it = first; it != r.observations.end(); ++it)
{
const double dt = secondsBetween(it->stamp, first->stamp) - mean_time;
residual2 += (it->position - mean_position - dt * velocity).squaredNorm();
}
result.speed = velocity.norm();
result.residual = std::sqrt(residual2 / static_cast<double>(count));
result.slope_sigma = std::sqrt(residual2 /
(static_cast<double>(count - 2) * time_variance));
result.displacement = (r.observations.back().position - first->position).norm();
result.ready = std::isfinite(result.speed) && std::isfinite(result.residual) &&
std::isfinite(result.slope_sigma) && std::isfinite(result.displacement);
return result;
};
const auto supportsMoving = [&](const MotionFit& estimate, double displacement, double speed) {
return estimate.ready && estimate.residual <= config_.motion_max_residual &&
estimate.free_observations >= config_.motion_known_free_observations &&
estimate.displacement >= displacement &&
estimate.speed - 0.5 * estimate.slope_sigma >= speed;
};
const MotionFit fast = fit(config_.motion_window);
const bool fast_moving = supportsMoving(fast, config_.motion_min_displacement,
config_.moving_speed);
const bool slow_moving = config_.slow_motion_window > 0.0 &&
supportsMoving(fit(config_.slow_motion_window),
config_.slow_motion_min_displacement, config_.slow_moving_speed);
const bool stationary = fast.ready && fast.residual <= config_.motion_max_residual &&
fast.displacement < config_.motion_min_displacement &&
fast.speed + 0.5 * fast.slope_sigma <= config_.stationary_speed;
if (fast_moving || slow_moving)
{ {
const double dt = secondsBetween(observation.stamp, r.observations.front().stamp) - mean_time; if (!r.moving_hits) r.moving_candidate_since = observed;
time_variance += dt * dt; r.moving_hits = std::min(r.moving_hits + 1, config_.motion_hysteresis_hits);
cross += dt * (observation.position - mean_position); r.stationary_hits = 0; r.stationary_candidate_since = 0;
r.last_moving_evidence_stamp = observed;
if (r.moving_hits >= config_.motion_hysteresis_hits &&
secondsBetween(observed, r.moving_candidate_since) >= config_.motion_enter_hold)
r.track.motion = MotionClass::kMoving;
} }
if (time_variance <= 1e-9) else if (stationary)
{ r.moving_hits = r.stationary_hits = 0; r.track.motion = MotionClass::kUnknown; return; }
const Eigen::Vector2d velocity = cross / time_variance;
const double speed = velocity.norm();
double residual2 = 0.0;
for (const auto& observation : r.observations)
{ {
const double dt = secondsBetween(observation.stamp, r.observations.front().stamp) - mean_time; if (!r.stationary_hits) r.stationary_candidate_since = observed;
residual2 += (observation.position - mean_position - dt * velocity).squaredNorm(); r.stationary_hits = std::min(r.stationary_hits + 1, config_.motion_hysteresis_hits);
r.moving_hits = 0; r.moving_candidate_since = 0;
if (r.stationary_hits >= config_.motion_hysteresis_hits &&
secondsBetween(observed, r.stationary_candidate_since) >= config_.motion_exit_hold)
{ r.track.motion = MotionClass::kStationary; r.last_moving_evidence_stamp = 0; }
else retainRecentMotion();
} }
const double residual = std::sqrt(residual2 / static_cast<double>(r.observations.size()));
const double slope_sigma = std::sqrt(residual2 /
(static_cast<double>(r.observations.size() - 2) * time_variance));
const double displacement = (r.observations.back().position -
r.observations.front().position).norm();
const bool consistent = residual <= config_.motion_max_residual;
const std::size_t free_observations = static_cast<std::size_t>(std::count_if(
r.observations.begin(), r.observations.end(), [](const Observation& observation) {
return observation.known_free;
}));
if (consistent && free_observations >= config_.motion_known_free_observations &&
displacement >= config_.motion_min_displacement &&
speed - 0.5 * slope_sigma >= config_.moving_speed)
{ ++r.moving_hits; r.stationary_hits = 0; }
else if (consistent && displacement < config_.motion_min_displacement &&
speed + 0.5 * slope_sigma <= config_.stationary_speed)
{ ++r.stationary_hits; r.moving_hits = 0; }
else else
{ r.stationary_hits = r.moving_hits = 0; r.track.motion = MotionClass::kUnknown; } {
if (r.moving_hits >= config_.motion_hysteresis_hits) r.track.motion = MotionClass::kMoving; r.moving_hits = r.stationary_hits = 0;
if (r.stationary_hits >= config_.motion_hysteresis_hits) r.track.motion = MotionClass::kStationary; r.moving_candidate_since = r.stationary_candidate_since = 0;
retainRecentMotion();
}
} }
void TrackerCore::addObservation(Record& r, const Detection& detection) void TrackerCore::addObservation(Record& r, const Detection& detection)
@@ -183,8 +236,9 @@ void TrackerCore::addObservation(Record& r, const Detection& detection)
r.observations.push_back({observed, detection.position, r.observations.push_back({observed, detection.position,
config_.motion_known_free_beams == 0 || config_.motion_known_free_beams == 0 ||
detection.known_free_beams >= config_.motion_known_free_beams}); detection.known_free_beams >= config_.motion_known_free_beams});
const double history_window = std::max(config_.motion_window, config_.slow_motion_window);
while (!r.observations.empty() && while (!r.observations.empty() &&
secondsBetween(observed, r.observations.front().stamp) > config_.motion_window) secondsBetween(observed, r.observations.front().stamp) > history_window)
r.observations.pop_front(); r.observations.pop_front();
} }
ProcessingResult TrackerCore::update(Stamp stamp, const std::vector<Detection>& detections, ProcessingResult TrackerCore::update(Stamp stamp, const std::vector<Detection>& detections,
@@ -271,6 +325,7 @@ ProcessingResult TrackerCore::update(Stamp stamp, const std::vector<Detection>&
Eigen::Matrix2d::Identity() * config_.initial_velocity_sigma * config_.initial_velocity_sigma; Eigen::Matrix2d::Identity() * config_.initial_velocity_sigma * config_.initial_velocity_sigma;
r.track.radius = d.radius; r.track.state_stamp = stamp; r.track.radius = d.radius; r.track.state_stamp = stamp;
r.track.last_observed_stamp = d.observed_stamp ? d.observed_stamp : stamp; r.track.last_observed_stamp = d.observed_stamp ? d.observed_stamp : stamp;
r.track.first_observed_stamp = r.track.last_observed_stamp;
r.track.observed_sources = d.sources; r.track.hit_count = 1; r.track.observed_sources = d.sources; r.track.hit_count = 1;
addObservation(r, d); addObservation(r, d);
if (config_.confirmation_hits == 1 && config_.confirmation_min_span == 0) if (config_.confirmation_hits == 1 && config_.confirmation_min_span == 0)

View File

@@ -55,6 +55,7 @@ struct TrackerService::Impl
std::optional<ProjectedScan> ready_projected; std::optional<ProjectedScan> ready_projected;
std::vector<std::uint8_t> ready_candidates; std::vector<std::uint8_t> ready_candidates;
SteadyTime pending_since{}, ready_since{}; SteadyTime pending_since{}, ready_since{};
bool pending_tf_counted = false;
}; };
mutable std::mutex lifecycle_mutex; mutable std::mutex lifecycle_mutex;
std::mutex wait_mutex; std::mutex wait_mutex;
@@ -149,6 +150,7 @@ struct TrackerService::Impl
source.last_seen_stamp = scan.stamp; source.last_seen_stamp = scan.stamp;
source.pending = std::move(scan); source.pending = std::move(scan);
source.pending_since = steady; source.pending_since = steady;
source.pending_tf_counted = false;
} }
if (secondsBetween(now, source.pending->stamp) > source.max_age) if (secondsBetween(now, source.pending->stamp) > source.max_age)
{ {
@@ -157,12 +159,22 @@ struct TrackerService::Impl
} }
PlanarTransform transform; PlanarTransform transform;
std::string error; std::string error;
const auto waitForTf = [&](const std::string& reason) {
if (!source.pending_tf_counted)
{ ++health.tf_waited_count; source.pending_tf_counted = true; }
if (elapsed(steady, source.pending_since) >= config.tf_wait)
{
health.status = Health::kTfError; health.reason = reason;
++health.tf_timeout_count; ++health.dropped_count;
source.pending.reset();
}
else if (health.processed_count == 0 && health.status == Health::kWarmingUp)
health.reason = "waiting for scan TF";
};
if (!dependencies.input->lookupTransform(config.tracking_frame, source.pending->frame, if (!dependencies.input->lookupTransform(config.tracking_frame, source.pending->frame,
source.pending->stamp, transform, error)) source.pending->stamp, transform, error))
{ {
health.status = Health::kTfError; health.reason = error; waitForTf(error);
if (elapsed(steady, source.pending_since) >= config.tf_wait)
{ source.pending.reset(); ++health.dropped_count; }
return; return;
} }
PlanarTransform end_transform = transform; PlanarTransform end_transform = transform;
@@ -184,9 +196,7 @@ struct TrackerService::Impl
if (!dependencies.input->lookupTransform(config.tracking_frame, source.pending->frame, if (!dependencies.input->lookupTransform(config.tracking_frame, source.pending->frame,
end_stamp, end_transform, error)) end_stamp, end_transform, error))
{ {
health.status = Health::kTfError; health.reason = "scan end TF: " + error; waitForTf("scan end TF: " + error);
if (elapsed(steady, source.pending_since) >= config.tf_wait)
{ source.pending.reset(); ++health.dropped_count; }
return; return;
} }
} }