diff --git a/src/openflexure_microscope_server/things/autofocus.py b/src/openflexure_microscope_server/things/autofocus.py index 9bb4b801..5cbc050c 100644 --- a/src/openflexure_microscope_server/things/autofocus.py +++ b/src/openflexure_microscope_server/things/autofocus.py @@ -775,8 +775,8 @@ class AutofocusThing(lt.Thing): target_offset = -total_range else: target_offset = 0 - - self._stage.move_relative(z=target_offset) + if target_offset != 0: + self._stage.move_relative(z=target_offset) # Capture images_to_save images for move_count in range(stack_parameters.images_to_save): diff --git a/tests/unit_tests/test_stack.py b/tests/unit_tests/test_stack.py index 6adf2df7..6e022143 100644 --- a/tests/unit_tests/test_stack.py +++ b/tests/unit_tests/test_stack.py @@ -694,7 +694,6 @@ def test_run_basic_stack_simple( stack_dz=10, images_to_save=3, settling_time=0, - backlash_correction=0, origin=StackOrigin.START, ) @@ -757,7 +756,6 @@ def test_run_basic_stack_center_origin( stack_dz=10, images_to_save=5, settling_time=0, - backlash_correction=0, origin=StackOrigin.CENTER, ) @@ -804,97 +802,6 @@ def test_run_basic_stack_center_origin( assert final_z == expected_final_z, "Final Z position is incorrect" -def test_run_basic_stack_backlash_applied( - autofocus_thing, mocker, fake_capture, fake_move_relative -): - """Backlash correction should overshoot first, then move back before starting stack.""" - stack_params = StackParams( - stack_dz=10, - images_to_save=3, - settling_time=0, - backlash_correction=50, - origin=StackOrigin.START, - ) - - capture_params = mocker.Mock() - capture_params.images_dir = "dummy" - capture_params.save_resolution = (100, 100) - - start_z = 0 - autofocus_thing._stage.position = {"x": 0, "y": 0, "z": start_z} - - autofocus_thing.capture_stack_image = mocker.Mock(side_effect=fake_capture) - autofocus_thing._stage.move_relative = mocker.Mock(side_effect=fake_move_relative) - autofocus_thing._cam.save_from_memory = mocker.Mock() - autofocus_thing._cam.clear_buffers = mocker.Mock() - - autofocus_thing.run_basic_stack(stack_params, capture_params) - - calls = autofocus_thing._stage.move_relative.call_args_list - - # First move is overshoot for backlash - assert calls[0].kwargs["z"] == -stack_params.backlash_correction, ( - "Backlash overshoot not applied correctly" - ) - - # Second move corrects back to starting point - assert calls[1].kwargs["z"] == stack_params.backlash_correction, ( - "Backlash correction move not applied correctly" - ) - - -def test_run_basic_stack_backlash_and_offset( - autofocus_thing, mocker, fake_capture, fake_move_relative -): - """Backlash correction and stack offset both applied. - - Stack should begin by moving down by half the height of the stack, - plus backlash correction, then move up by backlash correction, then run the basic stack. - """ - stack_params = StackParams( - stack_dz=100, - images_to_save=3, - settling_time=0, - backlash_correction=50, - origin=StackOrigin.CENTER, - ) - - capture_params = mocker.Mock() - capture_params.images_dir = "dummy" - capture_params.save_resolution = (100, 100) - - start_z = 0 - autofocus_thing._stage.position = {"x": 0, "y": 0, "z": start_z} - - autofocus_thing.capture_stack_image = mocker.Mock(side_effect=fake_capture) - autofocus_thing._stage.move_relative = mocker.Mock(side_effect=fake_move_relative) - autofocus_thing._cam.save_from_memory = mocker.Mock() - autofocus_thing._cam.clear_buffers = mocker.Mock() - - autofocus_thing.run_basic_stack(stack_params, capture_params) - - calls = autofocus_thing._stage.move_relative.call_args_list - - # Combined initial offset + backlash overshoot - total_stack_range = stack_params.stack_dz * (stack_params.images_to_save - 1) - expected_first_move = -(total_stack_range // 2 + stack_params.backlash_correction) - assert calls[0].kwargs["z"] == expected_first_move, ( - "Combined overshoot not applied correctly" - ) - - # Backlash correction back to base of stack - assert calls[1].kwargs["z"] == stack_params.backlash_correction, ( - "Backlash correction move not applied correctly" - ) - - # Subsequent moves in stack - expected_stack_moves = [stack_params.stack_dz] * (stack_params.images_to_save - 1) - actual_stack_moves = [c.kwargs["z"] for c in calls[2:]] - assert actual_stack_moves == expected_stack_moves, ( - "Stack moves after backlash/offset not correct" - ) - - def test_run_basic_stack_end_origin( autofocus_thing, mocker, fake_capture, fake_move_relative ): @@ -903,7 +810,6 @@ def test_run_basic_stack_end_origin( stack_dz=10, images_to_save=4, settling_time=0, - backlash_correction=0, origin=StackOrigin.END, ) @@ -948,7 +854,6 @@ def test_invalid_stack_images_raises(): stack_dz=10, images_to_save=capture_count, settling_time=0, - backlash_correction=0, origin=StackOrigin.START, ) @@ -960,7 +865,6 @@ def test_invalid_stack_settling_raises(): stack_dz=10, images_to_save=1, settling_time=-1, - backlash_correction=0, origin=StackOrigin.START, )