From 500e08555cea6cbd4282621b21db0a9df2f93000 Mon Sep 17 00:00:00 2001 From: jaknapper Date: Wed, 1 Apr 2026 18:45:16 +0100 Subject: [PATCH] Measure settling time action and hold in sharpness monitor --- .../things/autofocus.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/openflexure_microscope_server/things/autofocus.py b/src/openflexure_microscope_server/things/autofocus.py index 00cd7a4d..0093a2de 100644 --- a/src/openflexure_microscope_server/things/autofocus.py +++ b/src/openflexure_microscope_server/things/autofocus.py @@ -345,6 +345,10 @@ class JPEGSharpnessMonitor: final_z_position: int = self.stage_positions[-1]["z"] return data_index, final_z_position + def hold(self, delay: float) -> None: + """Continue to monitor sharpness without moving for delay seconds.""" + time.sleep(delay) + def move_data( self, istart: int, istop: Optional[int] = None ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: @@ -836,6 +840,18 @@ class AutofocusThing(lt.Thing): final_z = self._stage.position["z"] return final_z, z_positions + @lt.action + def measure_settling_time(self, delay: float = 2, dz: int = 800) -> dict: + """Make a Z move down then up by dz, then pause for delay while monitoring sharpness. + + This is useful to see how long to wait for the sharpness value to converge + """ + with JPEGSharpnessMonitor(self._stage, self._cam) as sharpness_monitor: + sharpness_monitor.focus_rel(-dz) + sharpness_monitor.focus_rel(dz) + sharpness_monitor.hold(delay) + return sharpness_monitor.data_dict() + class NotAPeakError(lt.exceptions.InvocationError): """The data to fit isn't a peak."""