From 2bad43153b6583d3be16cf81fccccaf9670003b5 Mon Sep 17 00:00:00 2001 From: Thera Pals <46314774+tepals@users.noreply.github.com> Date: Wed, 20 Nov 2024 11:41:56 +0100 Subject: [PATCH] [fix] In binary autofocus move to best position even when minimum stepsize is reached (#2840) Break the loop after moving to the best position: Otherwise the focus will not be at the best position when clipping left and right have caused left and right to be equal, but them not being equal to the center / best position. --- src/odemis/acq/align/autofocus.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/odemis/acq/align/autofocus.py b/src/odemis/acq/align/autofocus.py index bdf74fa6c2..1c2f0dd1dc 100644 --- a/src/odemis/acq/align/autofocus.py +++ b/src/odemis/acq/align/autofocus.py @@ -313,10 +313,6 @@ def _DoBinaryFocus(future, detector, emt, focus, dfbkg, good_focus, rng_focus): if future._autofocus_state == CANCELLED: raise CancelledError() - if left == right: - logging.info("Seems to have reached minimum step size (at %g m)", 2 * step_factor * min_step) - break - # if best focus was found at the center if i_max == 1: step_factor /= 2 @@ -335,6 +331,13 @@ def _DoBinaryFocus(future, detector, emt, focus, dfbkg, good_focus, rng_focus): # Clip best_pos in case the hardware reports a position outside of the range. best_pos = max(rng[0], min(best_pos, rng[1])) focus.moveAbsSync({"z": best_pos}) + + if left == right: + # Do this after moving to the best position, because clipping left and right can cause + # left and right to be equal, even when both are unequal to the best position. + logging.info("Seems to have reached minimum step size (at %g m)", 2 * step_factor * min_step) + break + step_cntr += 1 worst_fm = min(focus_levels.values())