Repository navigation
feat(sequencer): hardware-sequenced Z FIRST, one single-channel burst per channel - #790
hongquanli wants to merge 15 commits into
Conversation
…rst per channel Hongquan, 2026-11-01: "do hardware sequenced acquisition for z first", "one burst per channel". Until now a Z FIRST acquisition fell back to software sequencing (4413314), because the program's only order was channels inner. Per FOV the worker now runs one SEQ_RUN per selected channel, each a plain single-channel z-stack (return_to_start, so the piezo is back at the stack start after every burst). Between the bursts the host does what the software Z FIRST loop does between channels: that channel's exposure, gain and emission filter, and laser AF per stack when asked for. The engine's z-inner order is not used: nothing can move a serial filter wheel inside a burst, and on a stepper the jump back to the first plane at a channel boundary would be an uncompensated reversal. CHANNEL FIRST is unchanged: one channels-inner burst per FOV. - sequenced_acquisition: BurstPlan / burst_plans / build_bursts; the per-burst uniformity rules (one gain, one emission filter position, one set of X-Light iris values) apply to CHANNEL FIRST only - in Z FIRST each burst has one channel and gets that channel's own settings. Per-channel z offsets stay ineligible in both orders. - worker: _sequenced_bursts (plan + program per burst), _ensure_uploaded (the controller holds one program; Z FIRST uploads each later channel's program before its burst, CHANNEL FIRST once per acquisition as before), _set_up_for_burst for the burst's channels, the per-burst retry policy (three attempts, then the user is asked), dispatcher index per burst, the per-z bookkeeping after the first burst only, abort checked between bursts, _re_anchor_before_stack before every burst but the first. - Cost to measure on the bench: one 63-byte program upload (16 acked words + commit) per channel per FOV. If it matters, a SEQ_RUN channel selector in firmware removes it; not in this PR. Tests: burst plans and the order-aware rules; a worker-level test file with the burst runner replaced by a recorder (order, uploads, per-burst exposure and filter, dispatch index, bookkeeping once, retry of one burst alone, three failures ask the user, AF per stack before every burst but the first, abort between bursts); end to end in simulation the sequenced Z FIRST run delivers the software Z FIRST run's image set in its order, and with a simulated wheel each channel's filter position and exposure are in place when its burst starts. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
|
The per-burst program upload noted under "Cost to measure on the bench" is removed by #792 (stacked on this PR): |
…he burst The controller returns the piezo to the stack START after a burst, which for From Center is the stack bottom that prepare_z_stack() moved to. The sequenced path returned there without the climb back that the software stack does in move_z_back_after_stack(), and the next FOV's From Center move started from the cached (bottom) position: with autofocus off, every FOV began half a stack below the previous one (three planes at 1.5 um: FOV 2 started at 17 um instead of 18.5). acquire_at_position() now climbs back to the centre after a sequenced From Center stack, exactly as after a software one. The e2e test compares the per-image piezo z and the final piezo position of a two-FOV From Center acquisition between the sequenced and the software path. Found by an external review of the #656 / #790 / #792 stack. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
…etry After a burst failed, the retry paired its captures with whatever frame arrived first. A frame the controller fired in the failed attempt can still be on its way (a late frame is one of the ways a burst fails), and a frame carries nothing that tells it from the retry's first frame: it was saved under the retry's z = 0 and every later frame shifted by one, while the retry's count and consecutive-id checks still passed. A silent mislabel, the one class of error validate-before- save exists to rule out. _run_sequenced_burst() now goes through _discard_failed_burst() on every failure path, which restarts the camera stream before returning: the drivers lose in-flight frames with the stream (ToupCam Stop(); Hamamatsu cap_stop + buf_release), so the retry starts from a quiet camera. Worker-level tests with a fake camera that holds a frame in flight: the late frame of attempt 1 is not paired with attempt 2, and a burst the controller refused resynchronizes too. Found by an external review of the #656 / #790 / #792 stack. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
Master's ENABLE_TOUPCAM_GLOBAL_RESET (#705) and this branch's HARDWARE_TRIGGER_GLOBAL_RESET each put the sensor in global reset, through two writers with one cache. With only the branch's opt-in on, _apply_global_reset_mode() wrote the option without updating _global_reset_enabled, and on the way back to software trigger _set_global_reset() skipped the write-off because its cache said the mode was already off: the sensor stayed in global reset under software trigger, where the illumination is on through the readout and shades the image. _set_global_reset() is now the only writer and enables the mode for either opt-in (with the stream paused, as #705 does); _apply_global_reset_mode() only verifies (the write was refused, or the read-back is not 1) and raises, as before. Each opt-in keeps its own strobe model until the bench settles which one is right. The merge of master (4a00afc) also left the branch's ToupCam test helper without the #705 attributes, which failed 15 tests in CI (AttributeError on _global_reset_enabled); the helper now models the post-startup state. New test: the mode is written on and written off again across a hardware -> software trigger round trip. Found by an external review of the #656 / #790 / #792 stack. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
…esident The eligibility rule budgeted for two bursts, but the burst dispatcher holds one in flight and queues one behind it (submit() blocks only beyond that), and the worker collects the next burst meanwhile: three 400 MB bursts could occupy 1.2 GB under a 1 GB budget. The rule now requires three bursts to fit, and says so in its reason. Found by an external review of the #656 / #790 / #792 stack. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
…encer-z-first # Conflicts: # software/control/core/multi_point_worker.py
…ders In Z FIRST the controller leaves the piezo at the bottom of the LAST burst; the climb back to the centre after the stack applies the same way. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
…the retry 7c2e772 restarted the camera stream after a failed burst so a late frame of that burst could not be paired with the retry. That covers frames still in the camera or its SDK, not a frame the driver had already read: the Hamamatsu read thread takes a batch out of DCAM's ring under the capture lock and publishes it after releasing the lock, one callback at a time, so a frame held by a callback ahead of the worker's (a display) survives the restart and is delivered during the retry - with an id that continues into the new capture, since _frame_id_base is rebased from the newest frame read. Count and id order pass; the frame is saved under the retry's z = 0. (The driver's constructor comment claimed the lock covered publication; it does not, and now says so.) Every frame now carries the run of the camera stream it was READ in (CameraFrame.stream_generation; AbstractCamera.stream_generation() is the current run, bumped by each start of the capture). The Hamamatsu stamps it under the capture lock, the ToupCam reads it at the top of its frame callback before the pull, the simulated camera in its frame generator; a driver that does not stamp reports 0 for both. The worker's frame callback refuses a frame whose run is not the camera's current one, before any pairing. Tests: the Hamamatsu case end to end over the real read loop and a fake DCAM ring (a display callback holds frame 3 across the restart; the retry gets 4, 5, 6 and frame 3 is logged and dropped); frames carry their capture's run across a restart; the ToupCam callback stamps the run read before the pull and starting the raw stream begins a new run; the worker-level fake camera no longer assumes the restart drops what the driver had read. Found by the external review's second pass of the #656 / #790 / #792 stack. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
Master's ENABLE_TOUPCAM_GLOBAL_RESET (#705) and this branch's HARDWARE_TRIGGER_GLOBAL_RESET each put the sensor in global reset, through two writers with one cache. With only the branch's opt-in on, _apply_global_reset_mode() wrote the option without updating _global_reset_enabled, and on the way back to software trigger _set_global_reset() skipped the write-off because its cache said the mode was already off: the sensor stayed in global reset under software trigger, where the illumination is on through the readout and shades the image. _set_global_reset() is now the only writer and enables the mode for either opt-in (with the stream paused, as #705 does); _apply_global_reset_mode() only verifies (the write was refused, or the read-back is not 1) and raises, as before. Each opt-in keeps its own strobe model until the bench settles which one is right. The merge of master (4a00afc) also left the branch's ToupCam test helper without the #705 attributes, which failed 15 tests in CI (AttributeError on _global_reset_enabled); the helper now models the post-startup state. New test: the mode is written on and written off again across a hardware -> software trigger round trip. Found by an external review of the #656 / #790 / #792 stack. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod (cherry picked from commit 5a1118e)
…encer-z-first # Conflicts: # software/control/core/multi_point_worker.py
…camera stream it comes from eafc652 pinned every batch of pending captures to the run of the camera stream it was queued in, so that a frame callback still on its way to pairing when a hardware-sequenced burst is given up cannot take the retry's captures. That broke the AION/NL5 path: a stalled confocal scan is aborted inside send_trigger(), the camera re-armed (stop + start streaming, a new run) and the scan retried. The retried scan's frame then came from the new run, the ordinary capture's batch refused it, the worker never became ready for the next trigger and the acquisition aborted after the frame timeout (tests/control/test_aion_hw_trigger.py::test_a_stall_during_an_acquisition_does_not_abort_it, red on CI for #656, #790 and #792). Only a batch the worker itself gives up and retries after a stream restart needs the pin, and that is the hardware-sequenced burst. An ordinary capture's batch is now queued for no particular run (expect(..., stream_generation=None)) and follows the camera: the worker never restarts the stream under it, and the confocal unit retries only a scan that had not exposed the camera, so one capture never meets two frames. This is the pairing behaviour master has for ordinary captures. PendingCaptures.stream_generation says which run the queued batch is for, and the worker's unpaired-frame warning uses it to name a stream restart under a queued burst (the camera recovered on its own; the burst times out and is retried) instead of calling the capture given up. Tests: the any-run batch and the queued run on PendingCaptures; worker-level, an ordinary capture pairing its frame after the trigger restarted the stream, and the warning for a restart under a burst. The AION file passes again with the sequenced, retry and Hamamatsu restart sets (123 tests). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
|
Updated with #656 e3f6c46 (master merged, firmware 2.0): clean merge, head 6f570f7. All three firmware images build, 973 tests passed in the controller, sequencer, calibration and worker files, collect-only clean, simulation GUI driver ok. 🤖 Generated with Claude Code |
…into feat/hardware-sequencer-z-first # Conflicts: # software/control/core/multi_point_worker.py # software/tests/control/test_sequenced_acquisition.py
… camera's strobe delay for that burst's exposure Finding of the 2026-11-13 Codex review of the stack, verified against the code. The programs were built once per acquisition from one strobe-delay snapshot, taken before the camera was set up for the first burst's exposure (Z FIRST moved that setup after the snapshot), and every burst got the same snapshot. A camera's trigger latency can depend on the exposure: the ToupCam's does for short exposures, and in global reset that latency is the whole strobe delay. So a Z FIRST burst with another exposure than the snapshot's, or a channel first burst after a live exposure, ran with a stale strobe delay: the illumination started before the sensor integrated, and nothing noticed. Hamamatsu's strobe delay does not depend on the exposure, so it was unaffected. Now the worker keeps the burst plans, the resolved channels and the stack parameters, and builds a burst's program when it is about to upload it, after the camera is set up for that burst, reading the strobe delay then. The controller holds one program, so a program equal to the one held (programs are values) is not uploaded again: CHANNEL FIRST still uploads once per acquisition, and a Z FIRST burst whose channel and strobe delay are unchanged uploads nothing. build_bursts is gone with its two tests; the Z FIRST worker tests build their fixture from the plans and gain a test with an exposure-dependent strobe delay. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod
|
External review round (Codex, 2026-11-13) and fix: ad7d8f1 (plus the merge of the updated #656 head) Codex reviewed this layer's diff against the #656 head; both findings were verified against the code. One is the abort check now made in the layer below (it covers the per-burst loop here). The other is fixed here:
Evidence: 152 tests across the program builder, Z FIRST worker, retry, end-to-end and z-stack files pass; collection clean (5254); simulation GUI driver ok. 🤖 Generated with Claude Code |
…into feat/hardware-sequencer-z-first
…into feat/hardware-sequencer-z-first
…into feat/hardware-sequencer-z-first
…into feat/hardware-sequencer-z-first # Conflicts: # software/control/core/multi_point_worker.py # software/control/core/sequenced_acquisition.py # software/tests/control/test_MultiPointWorker_sequenced_retry.py # software/tests/control/test_sequenced_acquisition.py
|
Codex round 2 on this PR's own commit since round 1 (ad7d8f1): no findings at any severity. Codex checked that both the initial upload and later bursts read the strobe timing after camera setup, that program equality compares immutable values, and that the cache updates only after a successful upload. Carried up in 6bf5086: the round-2 fixes of #654 (camera rollbacks), #655 (an abort before a queued trigger fires leaves no cooldown) and #656 (no retry under a controller that did not stop, the piezo goes back before an abort, a sub-count z step falls back). The #656 changes landed inside this PR's per-burst loop by hand, since it had rewritten the retry loop they touch; the two new stack-level tests were rewritten on this PR's Z FIRST fixture. Worker, Z FIRST, eligibility, end-to-end and simulation sets: 260 passed; native 22/22; the AION trigger set 28 of 29, the one failure being the known load-sensitive stack-return timeout ("the mcu has gone quiet") on a Mac with its swap full. 🤖 Generated with Claude Code |
…le tests (#825) ## Why `tests/squid/test_stage.py::test_pi_focus_sub_window_settle_does_not_hold_the_stage_lock` fails intermittently in CI (two of three runs on #790, earlier on #654 and other branches), always with the same shape: ``` assert [False, False, False, False, False, False, False] == [False] ``` Seven probe entries instead of one, every one with the lock free. The test patched `squid.stage.pi.time.sleep`, which is the `time` module itself, so every thread in the pytest process went through the patched sleep. The simulated C-414 is instant and the test thread sleeps exactly once (the settle), so the extra entries can only be threads left running by earlier test files in the same shard (simulated microcontrollers, camera read loops) calling `time.sleep`. That is why it depends on runner load and on which files share the shard: alone, `test_stage.py` leaves no thread behind (checked locally: only `MainThread` is alive after the file, and nothing else calls `sleep`). ## What - A helper `_this_threads_sleeps(monkeypatch, record)` routes the calling thread's `time.sleep()` to the recorder and lets every other thread sleep for real. - The four settle tests use it instead of the process-wide patch. Their assertions are unchanged: one settle taken with the lock free, the fixed settle for moves inside the on-target window, none otherwise, and none when another thread widens the window after the move. - The four C-414 ramp tests that silenced `time.sleep` process-wide with `lambda s: None` go through the same helper, so threads from other tests no longer spin for the duration of those tests. No production code changes. ## Checks - `tests/squid/test_stage.py`: 102 passed, 1 skipped locally. - black and ruff (F401/F841) clean on the file. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
…into feat/hardware-sequencer-z-first
…into feat/hardware-sequencer-z-first
Stacked on #656 (
feat/hardware-sequencer-worker); host-only, no firmware file changes.What
A Z FIRST acquisition now runs on the controller. Until now it fell back to software sequencing, because the uploaded program's only order was channels inner (every channel at each z level).
Per FOV the worker runs one
SEQ_RUNper selected channel, each a plain single-channel z-stack withreturn_to_start, so the piezo is back at the stack start after every burst. Between the bursts the host does what the software Z FIRST loop does between channels: that channel's exposure, gain and emission filter, and laser AF per stack when it is asked for. CHANNEL FIRST is unchanged: one channels-inner burst per FOV.The engine's z-inner order (all z levels of channel A, then all of B, in one burst) is deliberately not used: nothing can move a serial emission filter wheel inside a burst, and on a stepper the jump back to the first plane at a channel boundary would be an uncompensated reversal.
How
sequenced_acquisition.py:BurstPlan,burst_plans(),build_bursts(). The per-burst uniformity rules (one camera gain, one emission filter position, one set of X-Light iris values) now apply to CHANNEL FIRST only; in Z FIRST each burst has one channel and gets that channel's own settings before it runs. Per-channel z offsets stay ineligible in both orders.multi_point_worker.py:_sequenced_bursts(plan + program per burst);_ensure_uploaded()(the controller holds one program: CHANNEL FIRST uploads once per acquisition as before, Z FIRST uploads each later channel's program before its burst);_set_up_for_burst()for the burst's channels; the retry policy (three attempts, then the user is asked) per burst; the dispatcher index per burst; the per-z bookkeeping after the first burst only, as the software loop does in its first channel's pass; abort checked between bursts;_re_anchor_before_stack()before every burst but the first.Cost to measure on the bench
A one-channel program is 63 bytes: 16 acked
SEQ_WRITEwords plusSEQ_COMMIT, before every burst but the first of an acquisition. Estimate 50 to 100 ms per channel per FOV. If the bench shows it matters, aSEQ_RUNchannel selector in firmware (run channel c of the committed program) removes the re-uploads; that is a firmware change and not part of this PR.Tests
build_bursts; the order-aware eligibility rules (Z FIRST eligible; Z FIRST with differing gains / filter positions / iris values eligible; a z offset still falls back).test_MultiPointWorker_sequenced_z_first.py, a bare worker with the burst runner replaced by a recorder): one single-channel burst per channel in channel order; a one-channel program uploaded before each burst; each channel's exposure and filter in place when its burst runs; the dispatcher index per burst; the per-z bookkeeping once; a failed burst retried alone; three failures ask the user and stop; AF per stack before every burst but the first; an abort between bursts stops before the next run; CHANNEL FIRST unchanged (one burst, one upload, the longest exposure once).Two F841 findings in the worker (
stack_startin_acquire_z_first,result = job.run()) pre-exist on the base branch and are left alone.🤖 Generated with Claude Code
Claude-Session: https://claude.ai/code/session_0152Tv35xKhniRMRq2Xywaod