From 21168d50869c15f00b2ed756c73db352d59c0196 Mon Sep 17 00:00:00 2001 From: Mirko Holler Date: Fri, 25 Sep 2026 14:12:33 +0200 Subject: [PATCH] fix(lamni): scan defaults, sim feedback interlock, fttrz draft - lamni/omny fermat scan: default exp_time 0.1, drop unused readout_time (devices already fall back to their own configured readout time) - sim_lamni: refuse the sd scan start and abort a running scan while the interferometer feedback loop is off; add tests - simulated_lamni.yaml: lsamrot sim_velocity 48 (4x hw) - lamni_optics_mixin / ptycho_lamni.yaml: commented-out draft for moving the fttrz flight tube in leye_out/leye_in, pending commissioned positions Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_016gUjUT2xCNpnBQkThn3CqP --- .../plugins/LamNI/lamni_optics_mixin.py | 33 ++++++++++ csaxs_bec/device_configs/ptycho_lamni.yaml | 4 ++ .../simulated_omny/simulated_lamni.yaml | 2 +- csaxs_bec/devices/sim/sim_lamni.py | 15 ++++- csaxs_bec/scans/lamni_fermat_scan.py | 11 +--- csaxs_bec/scans/omny_fermat_scan.py | 11 +--- .../tests_devices/test_sim_lamni_feedback.py | 61 +++++++++++++++++++ 7 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 tests/tests_devices/test_sim_lamni_feedback.py diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_optics_mixin.py b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_optics_mixin.py index c04a4d24..c5f4095c 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_optics_mixin.py +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_optics_mixin.py @@ -223,10 +223,43 @@ class LamNIOpticsMixin: umv(dev.lsamrot, 0) #umv(dev.dttrz, 5854, dev.fttrz, 2395) + # DRAFT, disabled until the fttrz positions are known: move the flight tube + # in after the eye is out, like flomni's feye_out(). Needs the userParameters + # fttrz_in / fttrz_out on leyex (see ptycho_lamni.yaml). + # if "fttrz" not in dev: + # if not self.OMNYTools.yesno( + # "Device 'fttrz' does not exist in the current config, so it " + # "can't be moved into position. Continue anyway? WARNING: " + # "this could cause collisions when operating on the real " + # "beamline." + # ): + # print("Aborting leye_out(): fttrz not moved.") + # return + # else: + # fttrz_in = self._get_user_param_safe("leyex", "fttrz_in") + # umv(dev.fttrz, fttrz_in) + def leye_in(self): bec.queue.next_dataset_number += 1 umv(dev.lsamrot, 0) #umv(dev.dttrz, 6419.677, dev.fttrz, 2959.979) + + # DRAFT, disabled until the fttrz positions are known: move the flight tube + # out before the eye goes in, like flomni's feye_in(). When enabled, this + # replaces the manual "Did the flight tube move out?" prompt below. + # if "fttrz" not in dev: + # if not self.OMNYTools.yesno( + # "Device 'fttrz' does not exist in the current config, so it " + # "can't be moved out of the way. Continue anyway? WARNING: " + # "this could cause collisions when operating on the real " + # "beamline." + # ): + # print("Aborting leye_in(): fttrz not moved.") + # return + # else: + # fttrz_out = self._get_user_param_safe("leyex", "fttrz_out") + # umv(dev.fttrz, fttrz_out) + if not self.OMNYTools.yesno("Did the flight tube move out?", "y"): return leyex_in = self._get_user_param_safe("leyex", "in") diff --git a/csaxs_bec/device_configs/ptycho_lamni.yaml b/csaxs_bec/device_configs/ptycho_lamni.yaml index bb294dc3..efec07c9 100644 --- a/csaxs_bec/device_configs/ptycho_lamni.yaml +++ b/csaxs_bec/device_configs/ptycho_lamni.yaml @@ -20,6 +20,10 @@ leyex: connectionTimeout: 20 userParameter: in: 14.117 + # flight tube positions used by leye_out()/leye_in() (draft, still commented there); + # fill in and uncomment once the fttrz positions are known + # fttrz_in: + # fttrz_out: deviceTags: - ptycho_lamni leyey: diff --git a/csaxs_bec/device_configs/simulated_omny/simulated_lamni.yaml b/csaxs_bec/device_configs/simulated_omny/simulated_lamni.yaml index b8ffe91e..63600cb4 100644 --- a/csaxs_bec/device_configs/simulated_omny/simulated_lamni.yaml +++ b/csaxs_bec/device_configs/simulated_omny/simulated_lamni.yaml @@ -141,7 +141,7 @@ lsamrot: sign: 1 sim_stppermm: 50154.32099 sim_encpermm: 36000 - sim_velocity: 5 # 48 #4x than hw + sim_velocity: 48 # 4x faster than hw sim_initial_position: 0 tolerance: 1 enabled: true diff --git a/csaxs_bec/devices/sim/sim_lamni.py b/csaxs_bec/devices/sim/sim_lamni.py index 406c237c..abfa4ac6 100644 --- a/csaxs_bec/devices/sim/sim_lamni.py +++ b/csaxs_bec/devices/sim/sim_lamni.py @@ -12,7 +12,8 @@ RT (LAMNI_server.cpp, port 3333): its own protocol dialect — As (start cap sampling, no reply) / Ar -> 8-field cap sensor data, Ss (start rt sampler, no reply) / Sr -> 7-field sampler sums, V velocity limit (no reply), a (no reply) / ar -> angle, - pa/pr, scan s/sc/sd/sr/sh, r -> 21-field sample rows + pa/pr, scan s/sc/sd/sr/sh (sd only starts, and the scan only continues, while the + feedback loop is running), r -> 21-field sample rows (x/y target-avg-stdev, five cap-sensor avg/stdev pairs, angle avg/stdev), o/t. """ @@ -183,6 +184,9 @@ class SimRtLamniState: num_pos = len(self.scan_positions) if num_pos == 0 or self.scan_mode > 0: return num_pos + if not self.feedback_running: + logger.warning("[sim rt lamni] scan not started: feedback loop is not running") + return num_pos self.scan_mode = 3 self.scan_current = 0 self._scan_abort.clear() @@ -198,6 +202,13 @@ class SimRtLamniState: if self._scan_abort.wait(self.point_dwell_s): return with self.lock: + if not self.feedback_running: + logger.warning( + "[sim rt lamni] scan aborted at point" + f" {self.scan_current}: feedback loop stopped" + ) + self.scan_mode = 0 + return self.scan_current = index + 1 self.targets[: len(self.scan_positions[index])] = self.scan_positions[index] with self.lock: @@ -305,6 +316,8 @@ class SimRtLamniSocket(SimSocketBase): return f"{mode:.0f},{num_pos:.0f},{current:.0f}\n" if args.startswith("d"): num_pos = state.start_scan() + if num_pos > 0 and not state.feedback_running: + return "Scan not started: feedback loop not running.\n" return f"Scan mode started with #positions in scan {num_pos:.0f}. Timing: Detector Trigger.\n" if args.startswith("h"): state.clear_scan() diff --git a/csaxs_bec/scans/lamni_fermat_scan.py b/csaxs_bec/scans/lamni_fermat_scan.py index e2312ff1..b95969ca 100644 --- a/csaxs_bec/scans/lamni_fermat_scan.py +++ b/csaxs_bec/scans/lamni_fermat_scan.py @@ -60,7 +60,7 @@ class LamniFermatScan(ScanBase): "fov_circular", "stitch_overlap", ], - "Acquisition Parameters": ["exp_time", "frames_per_trigger", "readout_time"], + "Acquisition Parameters": ["exp_time", "frames_per_trigger"], } MIN_POSITIONS = 20 # Minimum number of positions required for a valid scan @@ -81,9 +81,8 @@ class LamniFermatScan(ScanBase): fov_circular: Annotated[float, ScanArgument(display_name="Fov Circular", description="Generate a circular field of view in the sample plane. This is an additional cropping within the rectangular fovx/fovy range", units=Units.µm)] = 0, stitch_overlap: Annotated[float, ScanArgument(display_name="Stitch Overlap", description="Overlap of the stitched regions", units=Units.µm)] = 1, corridor_size: Annotated[float | None, ScanArgument(display_name="Corridor Size", description="Corridor size for the corridor optimization.", units=Units.µm)] = None, - exp_time: DefaultArgType.ExposureTime = 0, + exp_time: DefaultArgType.ExposureTime = 0.1, frames_per_trigger: DefaultArgType.FramesPerTrigger = 1, - readout_time: DefaultArgType.ReadoutTime = 0, **kwargs, # fmt: on ): @@ -106,7 +105,6 @@ class LamniFermatScan(ScanBase): exp_time (float): Exposure time in seconds corridor_size (float | None): Corridor size for the corridor optimization. If None, the corridor size will be estimated. frames_per_trigger (int): Number of frames per trigger for devices that support configurable frame counts per trigger. - readout_time (float): Configuration for devices that support configurable readout times. Returns: ScanReport @@ -128,12 +126,9 @@ class LamniFermatScan(ScanBase): self.stitch_overlap = stitch_overlap self.exp_time = exp_time self.frames_per_trigger = frames_per_trigger - self.readout_time = readout_time self.corridor_size = corridor_size - self.update_scan_info( - exp_time=exp_time, frames_per_trigger=frames_per_trigger, readout_time=readout_time - ) + self.update_scan_info(exp_time=exp_time, frames_per_trigger=frames_per_trigger) @scan_hook def prepare_scan(self): diff --git a/csaxs_bec/scans/omny_fermat_scan.py b/csaxs_bec/scans/omny_fermat_scan.py index 35b9ca58..7dce2e29 100644 --- a/csaxs_bec/scans/omny_fermat_scan.py +++ b/csaxs_bec/scans/omny_fermat_scan.py @@ -57,7 +57,7 @@ class OmnyFermatScan(ScanBase): "angle", "corridor_size", ], - "Acquisition Parameters": ["exp_time", "frames_per_trigger", "readout_time"], + "Acquisition Parameters": ["exp_time", "frames_per_trigger"], } def __init__( @@ -71,9 +71,8 @@ class OmnyFermatScan(ScanBase): zshift: Annotated[float, ScanArgument(display_name="Zshift", description="Shift in z.", units=Units.µm, example=0)], angle: Annotated[float | None, ScanArgument(display_name="Angle", description="Rotation angle (will rotate first)", units=Units.deg)] = None, corridor_size: Annotated[float, ScanArgument(display_name="Corridor Size", description="Corridor size for the corridor optimization. ", units=Units.µm)] = 3, - exp_time: DefaultArgType.ExposureTime = 0, + exp_time: DefaultArgType.ExposureTime = 0.1, frames_per_trigger: DefaultArgType.FramesPerTrigger = 1, - readout_time: DefaultArgType.ReadoutTime = 0, **kwargs, # fmt: on ): @@ -91,7 +90,6 @@ class OmnyFermatScan(ScanBase): corridor_size (float): Corridor size for the corridor optimization. exp_time (float): Exposure time in seconds frames_per_trigger (int): Number of frames per trigger for devices that support configurable frame counts per trigger. - readout_time (float): Configuration for devices that support configurable readout times. Returns: ScanReport @@ -108,7 +106,6 @@ class OmnyFermatScan(ScanBase): self.corridor_size = corridor_size self.exp_time = exp_time self.frames_per_trigger = frames_per_trigger - self.readout_time = readout_time if self.zshift > 100: logger.warning("The zshift is larger than 100 um. It will be limited to 100 um.") @@ -117,9 +114,7 @@ class OmnyFermatScan(ScanBase): logger.warning("The zshift is smaller than -100 um. It will be limited to -100 um.") self.zshift = -100 - self.update_scan_info( - exp_time=exp_time, frames_per_trigger=frames_per_trigger, readout_time=readout_time - ) + self.update_scan_info(exp_time=exp_time, frames_per_trigger=frames_per_trigger) self.omny_rotation_status = None diff --git a/tests/tests_devices/test_sim_lamni_feedback.py b/tests/tests_devices/test_sim_lamni_feedback.py new file mode 100644 index 00000000..58222876 --- /dev/null +++ b/tests/tests_devices/test_sim_lamni_feedback.py @@ -0,0 +1,61 @@ +"""The simulated LamNI RT server must not run scans while the feedback loop is off.""" + +import time + +import pytest + +from csaxs_bec.devices.sim.sim_lamni import SimRtLamniSocket, SimRtLamniState + +N_POSITIONS = 10 + + +@pytest.fixture +def state(): + state = SimRtLamniState() + state.point_dwell_s = 0.02 + for ii in range(N_POSITIONS): + state.add_scan_position([float(ii), float(ii)]) + yield state + state.clear_scan() + + +def _wait_for_scan_end(state, timeout=5): + deadline = time.time() + timeout + while state.scan_mode != 0 and time.time() < deadline: + time.sleep(0.005) + assert state.scan_mode == 0 + + +def test_scan_not_started_without_feedback(state): + state.feedback_running = False + assert state.start_scan() == N_POSITIONS + assert state.scan_mode == 0 + assert state.scan_current == 0 + assert state._scan_thread is None + + +def test_scan_runs_with_feedback(state): + state.feedback_running = True + state.start_scan() + _wait_for_scan_end(state) + assert state.scan_current == N_POSITIONS + + +def test_scan_aborts_when_feedback_drops(state): + state.feedback_running = True + state.start_scan() + time.sleep(3.5 * state.point_dwell_s) + state.feedback_running = False + _wait_for_scan_end(state) + assert 0 < state.scan_current < N_POSITIONS + + +def test_sd_command_reply_without_feedback(state): + socket = SimRtLamniSocket.__new__(SimRtLamniSocket) + socket.state = state + state.feedback_running = False + assert "not started" in socket.handle_command("sd") + assert state.scan_mode == 0 + state.feedback_running = True + assert "Scan mode started" in socket.handle_command("sd") + _wait_for_scan_end(state)