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

Check for missing devices

parent cc931cc5
Loading
Loading
Loading
Loading
+14 −11
Original line number Diff line number Diff line
@@ -2,13 +2,7 @@ from openflexure_microscope.common.labthings import find_device
from openflexure_microscope.common.labthings.plugins import BasePlugin
from openflexure_microscope.microscope import Microscope

from openflexure_microscope.devel import (
    JsonResponse,
    request,
    jsonify,
    taskify,
    abort,
)
from openflexure_microscope.devel import JsonResponse, request, jsonify, taskify, abort
from openflexure_microscope.utilities import set_properties

from flask.views import MethodView
@@ -22,6 +16,7 @@ from contextlib import contextmanager

### Autofocus utilities


class JPEGSharpnessMonitor:
    def __init__(self, microscope, timeout=60):
        self.microscope = microscope
@@ -148,10 +143,12 @@ def sharpness_edge(image):

### Autofocus plugin


def measure_sharpness(microscope, metric_fn=sharpness_sum_lap2):
    """Measure the sharpness of the camera's current view."""
    return metric_fn(microscope.camera.array(use_video_port=True))


def autofocus(microscope, dz, settle=0.5, metric_fn=sharpness_sum_lap2):
    """Perform a simple autofocus routine.
    The stage is moved to z positions (relative to current position) in dz,
@@ -203,16 +200,13 @@ def fast_autofocus(microscope, dz=2000, backlash=None):
        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
            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()



def fast_up_down_up_autofocus(
    microscope, dz=2000, target_z=0, initial_move_up=True, mini_backlash=150
):
@@ -299,6 +293,9 @@ class MeasureSharpnessAPI(MethodView):
    def post(self):
        microscope = find_device("openflexure_microscope")

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

        return jsonify({"sharpness": measure_sharpness(microscope)})


@@ -311,6 +308,9 @@ class AutofocusAPI(MethodView):
        payload = JsonResponse(request)
        microscope = find_device("openflexure_microscope")

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

        # Figure out the range of z values to use
        dz = payload.param("dz", default=np.linspace(-300, 300, 7), convert=np.array)

@@ -334,6 +334,9 @@ class FastAutofocusAPI(MethodView):
        payload = JsonResponse(request)
        microscope = find_device("openflexure_microscope")

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

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