Skip to content

Add one euro filter to KER streams to stabilize - #22

Merged
kou merged 3 commits into
enactic:mainfrom
euyniy:feat/one-euro-filter
Nov 10, 2026
Merged

kou merged 3 commits into
enactic:mainfrom
euyniy:feat/one-euro-filter

Conversation

@euyniy

@euyniy euyniy commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Prevent irrelevant shakiness

@euyniy
euyniy requested review from stevenaya and tokirobot November 5, 2026 02:54
@kou

kou commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

How about using the timestamp field instead of seq to compute the sample time?

I checked the firmware, and seq counts sent frames, not acquired samples:

  • acquisitionTask stamps each sample with micros() (main.cpp#L139) and writes it into a 1-deep queue with xQueueOverwrite() (L252, L428).
  • streamTask increments seq only for frames it actually sends (L488).

So when streamTask falls behind and a sample gets overwritten, time passes but seq doesn't advance. seq × period then underestimates dt. The 1 kHz period is also an assumption: the acquisition loop runs at roughly 1 kHz (vTaskDelay(1) plus processing time), not exactly.

The firmware already sends the acquisition time as timestamp (uint32 µs from micros()) in every frame (L385). Using it would:

  • give the real dt even when samples are overwritten before sending,
  • make --ker-rate-hz unnecessary.

The change is small. The wraparound and restart handling can stay as it is:

class SampleClock:
    """Sample time in seconds from the device "timestamp" field (micros())."""

    def __init__(self):
        self.timestamp_prev = None
        self.t = 0.0

    def update(self, timestamp_us: int) -> tuple[float, bool]:
        if self.timestamp_prev is None:
            self.timestamp_prev = timestamp_us
            return self.t, False

        # micros() is uint32 and wraps about every 71.6 minutes.
        gap = (timestamp_us - self.timestamp_prev) & 0xFFFFFFFF
        self.timestamp_prev = timestamp_us
        # A huge gap is the timestamp going back (device restart).
        if gap >= 1 << 31:
            return self.t, True

        self.t += gap * 1e-6
        return self.t, False
-            if one_euro is not None and "seq" in data:
-                t, restarted = clock.update(data["seq"])
+            if one_euro is not None and "timestamp" in data:
+                t, restarted = clock.update(data["timestamp"])

What do you think?

seq counts frames the firmware sends, not samples it acquires: when
streamTask falls behind and xQueueOverwrite() drops a sample, time
passes but seq does not advance, so seq * period underestimates dt.
The 1 kHz rate is also only nominal.

The firmware stamps every sample with micros() at acquisition and
sends it as "timestamp", so use that instead. This drops the
--ker-rate-hz option. Wraparound and restart handling stay the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@euyniy

euyniy commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

@kou I made some change to the timestamp detection. What do you think?

@kou
kou merged commit 5635d31 into enactic:main Nov 10, 2026
4 checks passed
@kou

kou commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants