Commit 4ceb6bca authored by Joel Collins's avatar Joel Collins
Browse files

Separate "classic" fast AF from up-down-up AF

parent e6913b23
Loading
Loading
Loading
Loading
+76 −58
Original line number Diff line number Diff line
@@ -218,18 +218,14 @@ def move_and_measure(microscope, dz):
        return m.data_dict()


def fast_autofocus(microscope, dz=2000, backlash=None):
def fast_autofocus(microscope, dz=2000):
    """Perform a down-up-down-up autofocus"""
    with monitor_sharpness(
        microscope
    ) as m, microscope.camera.lock as _, microscope.stage.lock as _:
    with microscope.camera.lock, microscope.stage.lock:
        with monitor_sharpness(microscope) as m:
            i, z = m.focus_rel(-dz / 2)
            i, z = m.focus_rel(dz)
            fz = m.sharpest_z_on_move(i)
        if backlash is None:
            i, z = m.focus_rel(-dz)  # move all the way to the start so it's consistent
        else:
            i, z = m.focus_rel(fz - z - backlash)
            m.focus_rel(fz - z)
            return m.data_dict()

@@ -272,9 +268,8 @@ def fast_up_down_up_autofocus(
            might slightly hurt accuracy, but is unlikely to be a big issue.  Too big
            may cause you to overshoot, which is a problem.
    """
    with monitor_sharpness(
        microscope
    ) as m, microscope.camera.lock as _, microscope.stage.lock as _:
    with microscope.camera.lock, microscope.stage.lock:
        with monitor_sharpness(microscope) as m:
            # Ensure the MJPEG stream has started
            microscope.camera.start_stream_recording()

@@ -346,7 +341,6 @@ class MoveAndMeasureAPI(ActionView):
        if microscope.has_real_stage():
            # Acquire microscope lock with 1s timeout
            with microscope.lock(timeout=1):
                # Run fast_up_down_up_autofocus
                return move_and_measure(microscope, dz=args.get("dz"))

        else:
@@ -384,6 +378,30 @@ class FastAutofocusAPI(ActionView):
    Run a fast autofocus
    """

    args = {"dz": fields.Int(missing=2000)}

    def post(self, args):
        microscope = find_component("org.openflexure.microscope")

        if not microscope:
            abort(503, "No microscope connected. Unable to autofocus.")

        if microscope.has_real_stage():
            logging.debug("Running autofocus...")
            # Acquire microscope lock with 1s timeout
            with microscope.lock(timeout=1):
                # Run fast_autofocus
                return fast_autofocus(microscope, dz=args.get("dz"))

        else:
            abort(503, "No stage connected. Unable to autofocus.")


class UpDownUpAutofocusAPI(ActionView):
    """
    Run a fast up-down-up autofocus
    """

    args = {
        "dz": fields.Int(missing=2000),
        "backlash": fields.Int(missing=25, minimum=0),