MCPcopy Create free account
hub / github.com/RustCV/RustCV / correct

Method correct

rustcv-core/src/time.rs:47–81  ·  view source on GitHub ↗

输入一帧的原始硬件时间戳,返回矫正后的系统时间 `hw_ns`: 驱动提供的硬件时间戳 (纳秒) `arrival_time`: 帧到达用户态的系统时刻

(&mut self, hw_ns: u64, arrival_time: Instant)

Source from the content-addressed store, hash-verified

45 /// * `hw_ns`: 驱动提供的硬件时间戳 (纳秒)
46 /// * `arrival_time`: 帧到达用户态的系统时刻
47 pub fn correct(&mut self, hw_ns: u64, arrival_time: Instant) -> Duration {
48 // 1. 记录数据点
49 if self.history.len() >= self.window_size {
50 self.history.pop_front();
51 }
52 self.history.push_back((hw_ns, arrival_time));
53
54 // 2. 如果数据不够,直接返回到达时间作为降级方案
55 if self.history.len() < 5 {
56 // 在初始化阶段,假设无漂移,直接对齐到第一帧
57 if let Some(start_time) = self.history.front() {
58 // 简单偏移计算
59 let elapsed_hw = hw_ns.saturating_sub(start_time.0);
60 return start_time.1.elapsed() + Duration::from_nanos(elapsed_hw);
61 // 粗略估算
62 }
63 // 兜底:直接返回当前系统时间 (不推荐,但作为 fallback)
64 // 注意:这里需要计算相对于 System Boot 的 duration
65 return self.instant_to_duration(arrival_time);
66 }
67
68 // 3. 计算线性回归 (y = kx + b)
69 // x = hw_timestamp (relative to first point in window)
70 // y = system_time (relative to first point in window)
71 // 我们需要预测当前 x 对应的 y
72 self.recalculate_regression();
73
74 // 4. 应用矫正
75 let (base_hw, base_sys) = self.history.front().unwrap();
76 let dx = (hw_ns as f64) - (*base_hw as f64);
77 let predicted_dy_ns = self.estimated_slope * dx + self.estimated_offset;
78
79 let base_sys_dur = self.instant_to_duration(*base_sys);
80 base_sys_dur + Duration::from_nanos(predicted_dy_ns as u64)
81 }
82
83 /// 简单的最小二乘法实现
84 fn recalculate_regression(&mut self) {

Callers 2

next_frameMethod · 0.80
next_frameMethod · 0.80

Calls 2

instant_to_durationMethod · 0.80

Tested by

no test coverage detected