fix(panda): drop stale frame_counter carryover in Mode B raw stream
CI for csaxs_bec / test (push) Failing after 2m51s

Diagnosed against a real scan (S06313): grouping raw interf_st_fzp_x/_y
samples by frame_counter + gate_detector_active reproduces rt_positions'
trusted average_x_st_fzp/_y to R² > 0.9999, confirming that method -- but one
point (167) was off by ~116 nm because PandA's COUNTER1.OUT is not cleared by
Arm(). The first raw samples of an acquisition can briefly carry a stale
frame_counter value left over from the previous acquisition's tail (observed:
36 samples reading a stale 167 before dropping to 0), which silently
contaminates whichever real point later in the same scan happens to share
that counter value.

PandaBoxOMNY now drops raw rows captured before it observes frame_counter ==
0 for the first time in each acquisition -- on every arm, including
on_pre_scan retries, since each Arm() reopens the same stale-value window --
and drops leftover buffered rows from an abandoned attempt at the same point.
Verified against S06313: point 167's residual drops from ~116 nm to ~0.1 nm
after the fix, in line with the rest of the scan.

Write-up, including the diagnostic method and a possible PandA-layout-level
alternative fix, in docs/developer/panda_box_free_running_setup.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NPZ9gzN2rt3wgiMnTKyrug
This commit is contained in:
x01dcandClaude Sonnet 5 committed 2026-09-21 20:21:23 +02:00
1 parent 7fe20ea3d1
commit 3eb6e0355f
3 files changed
+239 -4

No files matched your search

@@ -220,6 +220,70 @@ The BEC-side data schema produced by Mode B (what ends up in the HDF5 async
dataset) is documented separately in
`docs/developer/panda_box_free_running_data_schema.md`.
## Known data-quality caveat: stale `frame_counter` carryover (found + fixed 2026-09-21)
Diagnosed against a real scan (`S06313`): grouping raw `interf_st_fzp_x`/`_y`
samples by `frame_counter` (restricted to `gate_detector_active == 1`) and
averaging per group reproduces `rt_positions`'s trusted
`average_x_st_fzp`/`average_y_st_fzp` to R² > 0.9999 once a single constant
per-axis offset is removed -- an expected reference-zero difference between
the PandA/`INENC` counter and RT's own internal tracking, irrelevant to
reconstruction since it's the same constant for every point in a scan. This
also confirms the grouping/filtering method itself: naively treating every
raw row as its own position (without grouping by `frame_counter` and
filtering to `gate_detector_active == 1`) does not reproduce the true
positions and cannot be fixed by any scale/offset correction, since most raw
rows are samples taken between real exposures, not at them (see "Continuous
gate/arm" above).
One point out of 472 was a clear outlier under that method: its
`frame_counter` group had 38 extra samples (1036 vs. the usual ~998) and its
recomputed position was off by ~116 nm in Y, against ~5 nm typical.
Root cause: PandA's `COUNTER1.OUT` (the block behind `frame_counter`) is not
cleared by `Arm()`. `COUNTER1.ENABLE=PCAP.ACTIVE` only gates *counting*, not
the persisted output value, and `COUNTER1.SET` -- the block's reset-trigger
input -- is wired to the constant `ZERO` in the saved layout (see
`layouts/omny_freerun.ini`), i.e. never pulsed on arm, unlike `ENABLE` which
*is* wired to a live source (`PCAP.ACTIVE`). So the first raw samples of a new
acquisition can briefly carry a stale `frame_counter` value left over from the
previous acquisition's tail -- observed directly in `S06313`'s raw data: 36
samples reading a stale `167` before dropping to `0` and counting correctly
from there. Harmless on its own, except that stale value collided with a real
point index later in the *same* scan (point 167), so grouping by
`frame_counter` silently mixed ~38 stale rows into that one point's average.
Fix, implemented in `panda_box_omny.py` (software-only, no PandA layout
change needed): every time PCAP is armed -- including `on_pre_scan` retries,
since each `Arm()` reopens the same stale-value window -- `PandaBoxOMNY` now
drops any raw rows captured before it observes `frame_counter == 0` for the
first time in that acquisition (`_drop_stale_frame_counter_rows`, called from
`_receive_frame_data`), and drops any leftover buffered rows from an
abandoned previous attempt at the same point (`_reset_raw_stream_state`,
called from `on_stage` and from `_try_arm` right before every `Arm()`). Every
signal's row list is truncated at the same index to keep channels aligned.
Verified against `S06313`: dropping the (37, in that scan) leading stale rows
before grouping brings point 167's residual from ~116 nm down to ~0.1 nm, in
line with the rest of the scan.
A more thorough fix would rewire `COUNTER1.SET` on the PandA layout itself
(from the constant `ZERO` to `PCAP.ACTIVE`'s rising edge, matching how
`ENABLE` is already wired) so `OUT` resets at the source on every arm, rather
than relying on the client to detect and discard the stale prefix. Not
implemented here since it requires a PandA layout change to be tested against
live hardware; the software-side drop is a correct and sufficient fix in the
meantime, and remains a reasonable defense-in-depth even if the layout is
fixed later. If this is revisited: re-verify against a range of scans
(especially ones with `frame_counter` staying below a few hundred, where a
stale-value collision like point 167's is most likely) before removing the
software-side check.
This also means the "gap in an otherwise-monotonic sequence" completeness
check described in the mode-comparison table's "Data completeness
verification" row needs one caveat: a single backward step at the very start
of the raw sequence, from the previous acquisition's terminal value down to
`0`, is this expected (now-filtered) artifact, not a sign of dropped rows.
## Switching between modes
`omny_panda` (Mode A) and `omny_panda_continuous` (Mode B) need different PandA hardware