Merge branch 'old-fast-autofocus' into 'master'
Old fast autofocus See merge request openflexure/openflexure-microscope-server!77
This commit is contained in:
commit
b9bed71272
2 changed files with 96 additions and 59 deletions
|
|
@ -80,13 +80,19 @@ class JPEGSharpnessMonitor:
|
||||||
|
|
||||||
def focus_rel(self, dz, backlash=False, **kwargs):
|
def focus_rel(self, dz, backlash=False, **kwargs):
|
||||||
self.keep_alive()
|
self.keep_alive()
|
||||||
|
# Start time and position
|
||||||
self.stage_times.append(time.time())
|
self.stage_times.append(time.time())
|
||||||
self.stage_positions.append(self.stage.position)
|
self.stage_positions.append(self.stage.position)
|
||||||
|
# Main move
|
||||||
self.stage.move_rel([0, 0, dz], backlash=backlash, **kwargs)
|
self.stage.move_rel([0, 0, dz], backlash=backlash, **kwargs)
|
||||||
|
# End time and position
|
||||||
self.stage_times.append(time.time())
|
self.stage_times.append(time.time())
|
||||||
self.stage_positions.append(self.stage.position)
|
self.stage_positions.append(self.stage.position)
|
||||||
i = len(self.stage_positions) - 2
|
# Index of the data for this movement
|
||||||
return i, self.stage_positions[-1][2]
|
data_index = len(self.stage_positions) - 2
|
||||||
|
# Final z position after move
|
||||||
|
final_z_position = self.stage_positions[-1][2]
|
||||||
|
return data_index, final_z_position
|
||||||
|
|
||||||
def move_data(self, istart, istop=None):
|
def move_data(self, istart, istop=None):
|
||||||
"Extract sharpness as a function of (interpolated) z"
|
"Extract sharpness as a function of (interpolated) z"
|
||||||
|
|
@ -218,19 +224,25 @@ def move_and_measure(microscope, dz):
|
||||||
return m.data_dict()
|
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"""
|
"""Perform a down-up-down-up autofocus"""
|
||||||
with monitor_sharpness(
|
with microscope.camera.lock, microscope.stage.lock:
|
||||||
microscope
|
with monitor_sharpness(microscope) as m:
|
||||||
) as m, microscope.camera.lock as _, microscope.stage.lock as _:
|
# Move to (-dz / 2)
|
||||||
i, z = m.focus_rel(-dz / 2)
|
m.focus_rel(-dz / 2)
|
||||||
|
# Move to dz while monitoring sharpness
|
||||||
|
# i: Sharpness monitor index for this move
|
||||||
|
# z: Final z position after move
|
||||||
i, z = m.focus_rel(dz)
|
i, z = m.focus_rel(dz)
|
||||||
|
# Get the z position with highest sharpness from the previous move (index i)
|
||||||
fz = m.sharpest_z_on_move(i)
|
fz = m.sharpest_z_on_move(i)
|
||||||
if backlash is None:
|
# 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
|
# Store final absolute z position from this return move
|
||||||
else:
|
i, z = m.focus_rel(-dz)
|
||||||
i, z = m.focus_rel(fz - z - backlash)
|
# Move to the target position fz
|
||||||
|
# Can't do absolute move here yet so move by (fz - z)
|
||||||
m.focus_rel(fz - z)
|
m.focus_rel(fz - z)
|
||||||
|
# Return all focus data
|
||||||
return m.data_dict()
|
return m.data_dict()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -272,9 +284,8 @@ def fast_up_down_up_autofocus(
|
||||||
might slightly hurt accuracy, but is unlikely to be a big issue. Too big
|
might slightly hurt accuracy, but is unlikely to be a big issue. Too big
|
||||||
may cause you to overshoot, which is a problem.
|
may cause you to overshoot, which is a problem.
|
||||||
"""
|
"""
|
||||||
with monitor_sharpness(
|
with microscope.camera.lock, microscope.stage.lock:
|
||||||
microscope
|
with monitor_sharpness(microscope) as m:
|
||||||
) as m, microscope.camera.lock as _, microscope.stage.lock as _:
|
|
||||||
# Ensure the MJPEG stream has started
|
# Ensure the MJPEG stream has started
|
||||||
microscope.camera.start_stream_recording()
|
microscope.camera.start_stream_recording()
|
||||||
|
|
||||||
|
|
@ -346,7 +357,6 @@ class MoveAndMeasureAPI(ActionView):
|
||||||
if microscope.has_real_stage():
|
if microscope.has_real_stage():
|
||||||
# Acquire microscope lock with 1s timeout
|
# Acquire microscope lock with 1s timeout
|
||||||
with microscope.lock(timeout=1):
|
with microscope.lock(timeout=1):
|
||||||
# Run fast_up_down_up_autofocus
|
|
||||||
return move_and_measure(microscope, dz=args.get("dz"))
|
return move_and_measure(microscope, dz=args.get("dz"))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -384,8 +394,32 @@ class FastAutofocusAPI(ActionView):
|
||||||
Run a fast autofocus
|
Run a fast autofocus
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
args = {"dz": fields.Int(missing=2000, example=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 = {
|
args = {
|
||||||
"dz": fields.Int(missing=2000),
|
"dz": fields.Int(missing=2000, example=2000),
|
||||||
"backlash": fields.Int(missing=25, minimum=0),
|
"backlash": fields.Int(missing=25, minimum=0),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -433,6 +467,9 @@ autofocus_extension_v2.add_view(AutofocusAPI, "/autofocus", endpoint="autofocus"
|
||||||
autofocus_extension_v2.add_view(
|
autofocus_extension_v2.add_view(
|
||||||
FastAutofocusAPI, "/fast_autofocus", endpoint="fast_autofocus"
|
FastAutofocusAPI, "/fast_autofocus", endpoint="fast_autofocus"
|
||||||
)
|
)
|
||||||
|
autofocus_extension_v2.add_view(
|
||||||
|
UpDownUpAutofocusAPI, "/updownup_autofocus", endpoint="updownup_autofocus"
|
||||||
|
)
|
||||||
|
|
||||||
autofocus_extension_v2.add_view(
|
autofocus_extension_v2.add_view(
|
||||||
MoveAndMeasureAPI, "/move_and_measure", endpoint="move_and_measure"
|
MoveAndMeasureAPI, "/move_and_measure", endpoint="move_and_measure"
|
||||||
|
|
|
||||||
|
|
@ -203,7 +203,7 @@ class ScanExtension(BaseExtension):
|
||||||
if autofocus_enabled:
|
if autofocus_enabled:
|
||||||
if fast_autofocus:
|
if fast_autofocus:
|
||||||
# Run fast autofocus. Client should provide dz ~ 2000
|
# Run fast autofocus. Client should provide dz ~ 2000
|
||||||
autofocus_extension.fast_up_down_up_autofocus(
|
autofocus_extension.fast_autofocus(
|
||||||
microscope, dz=autofocus_dz
|
microscope, dz=autofocus_dz
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue