Commit 65fc58c6 authored by Joel Collins's avatar Joel Collins
Browse files

Fixed scan to use newer fast autofocus

parent 945c7e4e
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line
from labthings.server.find import find_component
from labthings.server.extensions import BaseExtension
from labthings.server.view import View
from labthings.server.decorators import ThingAction, ThingProperty
from labthings.server.decorators import ThingAction, ThingProperty, marshal_task

from openflexure_microscope.devel import JsonResponse, request, jsonify, taskify, abort
from openflexure_microscope.utilities import set_properties
@@ -195,7 +195,7 @@ def move_and_find_focus(microscope, dz):

def fast_autofocus(microscope, dz=2000, backlash=None):
    """Perform a down-up-down-up autofocus"""
    with monitor_sharpness(microscope) as m:
    with monitor_sharpness(microscope) as m, microscope.camera.lock:
        i, z = m.focus_rel(-dz / 2)
        i, z = m.focus_rel(dz)
        fz = m.sharpest_z_on_move(i)
@@ -208,7 +208,7 @@ def fast_autofocus(microscope, dz=2000, backlash=None):


def fast_up_down_up_autofocus(
    microscope, dz=2000, target_z=0, initial_move_up=True, mini_backlash=150
    microscope, dz=2000, target_z=0, initial_move_up=True, mini_backlash=25
):
    """Autofocus by measuring on the way down, and moving back up with feedback.

@@ -239,7 +239,7 @@ def fast_up_down_up_autofocus(
            from the starting position.  Mostly useful if you're able to combine
            the initial move with something else, e.g. moving to the next scan point.

        mini_backlash: (optional, default 50) is a small extra move made in step
        mini_backlash: (optional, default 25) is a small extra move made in step
            3 to help counteract backlash.  It should be small enough that you
            would always expect there to be greater backlash than this.  Too small
            might slightly hurt accuracy, but is unlikely to be a big issue.  Too big
@@ -305,6 +305,7 @@ class AutofocusAPI(View):
    Run a standard autofocus
    """

    @marshal_task
    def post(self):
        payload = JsonResponse(request)
        microscope = find_component("org.openflexure.microscope")
@@ -320,7 +321,7 @@ class AutofocusAPI(View):
            task = taskify(autofocus)(microscope, dz)

            # return a handle on the autofocus task
            return jsonify(task.state), 201
            return task

        else:
            abort(503, "No stage connected. Unable to autofocus.")
@@ -332,6 +333,7 @@ class FastAutofocusAPI(View):
    Run a fast autofocus
    """

    @marshal_task
    def post(self):
        payload = JsonResponse(request)
        microscope = find_component("org.openflexure.microscope")
@@ -341,16 +343,16 @@ class FastAutofocusAPI(View):

        # Figure out the parameters to use
        dz = payload.param("dz", default=2000, convert=int)
        backlash = payload.param("backlash", default=0, convert=int)
        backlash = payload.param("backlash", default=25, convert=int)
        if backlash < 0:
            backlash = 0

        if microscope.has_real_stage():
            logging.info("Running autofocus...")
            task = taskify(fast_autofocus)(microscope, dz, backlash=backlash)
            task = taskify(fast_up_down_up_autofocus)(microscope, dz=dz, mini_backlash=backlash)

            # return a handle on the autofocus task
            return jsonify(task.state), 201
            return task

        else:
            abort(503, "No stage connected. Unable to autofocus.")
@@ -361,6 +363,7 @@ autofocus_extension_v2 = BaseExtension(
)

autofocus_extension_v2.add_method(fast_autofocus, "fast_autofocus")
autofocus_extension_v2.add_method(fast_up_down_up_autofocus, "fast_up_down_up_autofocus")
autofocus_extension_v2.add_method(autofocus, "autofocus")

autofocus_extension_v2.add_view(MeasureSharpnessAPI, "/measure_sharpness")
+4 −2
Original line number Diff line number Diff line
@@ -197,13 +197,15 @@ def tile(
            # Refocus
            if autofocus_enabled:
                if fast_autofocus:
                    autofocus_extension.fast_autofocus(
                    # Run fast autofocus. Client should provide dz ~ 2000
                    autofocus_extension.fast_up_down_up_autofocus(
                        microscope,
                        dz=autofocus_dz,
                        target_z=-z_stack_dz / 2.0,  # Finish below the focus
                        initial_move_up=False,  # We're already at the top of the scan
                    )
                else:
                    logging.debug("Running autofocus")
                    # Run slow autofocus. Client should provide dz ~ 50
                    autofocus_extension.autofocus(
                        range(-3 * autofocus_dz, 4 * autofocus_dz, autofocus_dz)
                    )