Skip to content

Commit

Permalink
only rely on history if clock_offset is stable
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed Sep 2, 2024
1 parent 9abe71f commit dfecca8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion server/driver/clock_offset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
double a = cov / v;
double b = mean_y - a * mean_x;

b = y0 + b - int64_t(a * x0);

// a changed less than 1% and b changed less than 200ms
offset.stable = std::abs((a - offset.a) / offset.a) < 0.01 && std::abs((b - (double)offset.b)) < 200'000'000;

offset.a = a;
offset.b = y0 + b - int64_t(a * x0);
offset.b = b;
U_LOG_D("clock relations: headset = a*x+b where a=%f b=%ldµs", offset.a, offset.b / 1000);
}

Expand All @@ -146,3 +151,8 @@ XrTime clock_offset::to_headset(XrTime timestamp_ns) const
{
return a * timestamp_ns + b;
}

bool clock_offset::is_stable() const
{
return stable;
}
3 changes: 3 additions & 0 deletions server/driver/clock_offset.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct clock_offset
// y = ax+b
int64_t b = 0;
double a = 1;
bool stable = false;

operator bool() const
{
Expand All @@ -44,6 +45,8 @@ struct clock_offset
XrTime from_headset(XrTime) const;

XrTime to_headset(XrTime timestamp_ns) const;

bool is_stable() const;
};

class clock_offset_estimator
Expand Down
10 changes: 10 additions & 0 deletions server/driver/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include "clock_offset.h"
#include "util/u_logging.h"
#include <algorithm>
#include <cstddef>
#include <list>
Expand Down Expand Up @@ -47,6 +48,15 @@ class history
// Discard outdated data, packets could be reordered
if (not data.empty())
{
// keep only one sample if the clock_offset is unstable
if (!offset.is_stable())
{
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;
}
Expand Down

0 comments on commit dfecca8

Please sign in to comment.