only rely on history if clock_offset is stable

This commit is contained in:
galister authored and xytovl committed 2024-09-03 18:00:28 +02:00
1 parent 9689cbf822
commit 3f25fed6df
5 files changed
+27 -26

No files matched your search

+12 -14
View File
@@ -58,7 +58,7 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
}
else
{
sample_interval = std::chrono::seconds(1);
sample_interval = std::chrono::milliseconds(100);
int64_t latency = 0;
for (const auto & s: samples)
latency += s.received - s.query;
@@ -88,12 +88,11 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
x0 += (s.query + s.received) * 0.5;
y0 += s.response;
}
x0 /= n;
y0 /= n;
x0 *= inv_n;
y0 *= inv_n;
if (samples.size() < num_samples)
{
offset.a = 1;
offset.b = y0 - x0;
return;
}
@@ -120,15 +119,14 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
double mean_x = sum_x * inv_n;
double mean_y = sum_y * inv_n;
// y = ax + b
double cov = inv_n * sum_xy - mean_x * mean_y;
double v = inv_n * sum_x2 - mean_x * mean_x;
double a = cov / v;
double b = mean_y - a * mean_x;
offset.a = a;
offset.b = y0 + b - int64_t(a * x0);
U_LOG_D("clock relations: headset = a*x+b where a=%f b=%ldµs", offset.a, offset.b / 1000);
double b = y0 + (mean_y - mean_x) - x0;
// b changed less than 20ms
offset.stable = std::abs((b - (double)offset.b)) < 20'000'000;
offset.b = b;
U_LOG_D("clock relations: headset = x+b where b=%ldµs", offset.b / 1000);
}
clock_offset clock_offset_estimator::get_offset()
@@ -139,10 +137,10 @@ clock_offset clock_offset_estimator::get_offset()
XrTime clock_offset::from_headset(XrTime ts) const
{
return (ts - b) / a;
return ts - b;
}
XrTime clock_offset::to_headset(XrTime timestamp_ns) const
{
return a * timestamp_ns + b;
return timestamp_ns + b;
}
+3 -3
View File
@@ -32,13 +32,13 @@ struct clock_offset
{
// y: headset time
// x: server time
// y = ax+b
// y = x+b
int64_t b = 0;
double a = 1;
bool stable = false;
operator bool() const
{
return b != 0;
return stable;
}
XrTime from_headset(XrTime) const;
+10
View File
@@ -20,6 +20,7 @@
#pragma once
#include "clock_offset.h"
#include "util/u_logging.h"
#include <algorithm>
#include <cstddef>
#include <list>
@@ -47,6 +48,15 @@ protected:
// Discard outdated data, packets could be reordered
if (not data.empty())
{
// keep only one sample if the clock_offset is unreliable
if (not offset)
{
U_LOG_D("not using history: clock_offset not stable");
data.clear();
data.emplace_back(sample, produced, t);
return;
}
if (data.back().produced_timestamp > produced)
return;
}
+1 -1
View File
@@ -68,7 +68,7 @@ void wivrn_pacer::on_feedback(const xrt::drivers::wivrn::from_headset::feedback
feedback.displayed and
feedback.frame_index == last.frame_index + 1)
{
frame_duration_ns = std::lerp(frame_duration_ns, (feedback.displayed - last.displayed) / offset.a, 0.1);
frame_duration_ns = std::lerp(frame_duration_ns, (feedback.displayed - last.displayed), 0.1);
}
auto blitted = feedback.blitted;
+1 -8
View File
@@ -261,8 +261,6 @@ void wivrn_session::operator()(from_headset::tracking && tracking)
}
auto offset = offset_est.get_offset();
if (not offset)
return;
hmd->update_tracking(tracking, offset);
left_hand->update_tracking(tracking, offset);
@@ -276,8 +274,6 @@ void wivrn_session::operator()(from_headset::tracking && tracking)
void wivrn_session::operator()(from_headset::hand_tracking && hand_tracking)
{
auto offset = offset_est.get_offset();
if (not offset)
return;
left_hand->update_hand_tracking(hand_tracking, offset);
right_hand->update_hand_tracking(hand_tracking, offset);
@@ -288,16 +284,13 @@ void wivrn_session::operator()(from_headset::fb_face2 && fb_face2)
return;
auto offset = offset_est.get_offset();
if (not offset)
return;
fb_face2_tracker->update_tracking(fb_face2, offset);
}
void wivrn_session::operator()(from_headset::inputs && inputs)
{
auto offset = get_offset();
if (not offset)
return;
left_hand->set_inputs(inputs, offset);
right_hand->set_inputs(inputs, offset);
}