Skip to content

Stamp chunks from a scan-count fit; add timestamp_offset_us - #10

Merged
cboulay merged 3 commits into
masterfrom
fix/scan-count-timestamps
Nov 23, 2026
Merged

cboulay merged 3 commits into
masterfrom
fix/scan-count-timestamps

Conversation

@cboulay

@cboulay cboulay commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Closes #9.

Changes

  • Scan-count timestamps (MCCDevice::stampFor): each poll that sees new scans records (scan count, local_clock()). The chunk stamp comes from the line under all points of the last 30 s that is highest at their mean count (lower convex hull edge). This removes the 0–1 ms poll delay and tracks the DAQ/host rate ratio. Reset on scan restart.
  • NTP guard: if the best poll of the last 2 s sits >20 µs above the line (host clock rate changed faster than the window follows), the line is lifted to it.
  • timestamp_offset_us (config file, saved/loaded by the GUI) / --ts-offset-us (CLI): subtracted from every stamp. Default 0.
  • scripts/gpio_latency_test: Raspberry Pi 5 calibration (GPIO edge bracketed in CLOCK_MONOTONIC → DAQ CH1).
  • README timestamp section rewritten.

Measurements (Pi 5, Debian 13, USB-1608FS-Plus, 2 ch @ 50 kHz, ~1200 edges / 300 s, all localhost, inlet without clock sync)

stamp − true (µs)
master (poll-time stamps) mean 627, SD 297
this PR, offset 0 mean 95–97, SD ~7.5
this PR, offset 93 mean 2.8, SD 8.0 (during a live timesyncd correction, host rate drifting ~4 ppm)

SD is mostly the 20 µs sample spacing of the edge measurement.

Window/guard choice from offline replay of dumped poll points (15 min run): a 120 s window wanders ~1.4 µs less on a free-running clock, but under an NTP ramp (0.075 ppm/s, as observed) 30 s stays within ~5 µs vs ~15 µs. Synthetic 200–500 ppm slews: unguarded hull errs 11–15 ms, guarded worst case 300–400 µs. CPU unchanged (1 ms polling, ~5% on the Pi).

macOS (Mac mini, Apple silicon, macOS 27)

DAQ on the Mini, Pi GPIO16 → CH1; Pi CLOCK_MONOTONIC → Pi CLOCK_REALTIME (gPTP grandmaster over a direct cable) → Mini mach_absolute via hardware-timestamped gPTP sync pairs → local_clock.

stamp − true (µs)
offset 0 mean 148.4, SD 10.5 (n 1206)
offset 148 mean 3.1, SD 15.3 (n 1205) → 151.1

Uncertainty: ~1 µs from the reference plus the unknown fixed offset of the Mac's adapter→mach cross-timestamp.

Rate/channel sweep (offset 0, 300 s each): the latency depends on acquisition settings, so the README now says to calibrate at your own settings.

config stamp − true (µs)
1 kHz × 2 ch 85.1 ± 8.2 (SD 285, sample spacing 1 ms)
16384 Hz × 6 ch 160.6 ± 0.7 (SD 22.9)
50 kHz × 2 ch 148.4 / 151.1
100 kHz × 1 ch 150.4 ± 0.3 (SD 10.4)

Evaluated and dropped: DIO loopback calibration

Toggling the DAQ's own DIO0 (looped to an AI) with ulDBitOut bracketed by local_clock(): the write takes 1.07–1.72 ms (full-speed control transfer) and the pin switches 0.71–1.39 ms into it, so truth-free bounds were −156 ≤ λ ≤ 813 µs. The out/in split is a host USB-stack property, so it cannot calibrate λ.

Timestamps were the poll time, 627 us late on average with 297 us SD
(measured on a Pi 5 against a bracketed GPIO edge). Now each chunk is
stamped from a lower-envelope line of scan count vs. poll time over the
last 30 s, which tracks the device/host rate ratio. If recent polls sit
above the line (host clock slewed by NTP), the line is lifted to them.

- timestamp_offset_us (config) / --ts-offset-us (CLI) subtracts the
  remaining fixed transport latency (~93 us on a Pi 5); default 0.
- scripts/gpio_latency_test: Raspberry Pi 5 procedure to measure it.

Result on Pi 5: mean 95-97 us, SD ~7.5 with offset 0; mean 2.8 us with
offset 93 (SD is mostly the test's 20 us sample spacing).
@cboulay
cboulay merged commit 27ddcc8 into master Nov 23, 2026
6 checks passed
@cboulay
cboulay deleted the fix/scan-count-timestamps branch November 23, 2026 02:07
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.

Timestamps lag ~620 µs (SD 305) behind true acquisition time: stamp from scan count, add configurable offset and calibration

1 participant