Start autofocus from base of range for speed

This commit is contained in:
Joe Knapper 2024-01-29 18:10:11 +00:00
parent 53eb51e964
commit 528406137f
3 changed files with 16 additions and 14 deletions

View file

@ -33,14 +33,14 @@ def unpack_autofocus(scan_data):
jpeg_heights = np.interp(jpeg_times, stage_times, stage_height)
turning = np.where(turningpoints(jpeg_heights))[0] + 1
turning = np.where(turningpoints(jpeg_heights))[0]
return jpeg_heights[turning[0] : turning[1]], jpeg_sizes_MB[turning[0] : turning[1]]
class RecentringThing(Thing):
@thing_action
def looping_autofocus(self, autofocus: AutofocusDep, stage: StageDep, dz=2000):
def looping_autofocus(self, autofocus: AutofocusDep, stage: StageDep, dz=2000, start='centre'):
"""Repeatedly autofocus the stage until it looks focused.
This action will run the `fast_autofocus` action until it settles on a point
@ -51,17 +51,17 @@ class RecentringThing(Thing):
repeat = True
attempts = 0
while repeat and attempts < 10:
height_min = stage.position["z"] - dz / 2
height_max = stage.position["z"] + dz / 2
data = autofocus.fast_autofocus(dz=dz)
data = autofocus.fast_autofocus(dz=dz, start=start)
heights, _ = unpack_autofocus(data)
time.sleep(0.3)
height_min = np.min(heights)
height_max = np.max(heights)
# TODO: max heights seems badly wrong! Something about turning?
if (
stage.position["z"] - height_min < dz / 5
or height_max - stage.position["z"] < dz / 5
):
attempts += 1
start = 'centre'
else:
repeat = False

View file

@ -143,7 +143,8 @@ class AutofocusThing(Thing):
def fast_autofocus(
self,
m: SharpnessMonitorDep,
dz: int=2000
dz: int=2000,
start: str='centre',
) -> SharpnessDataArrays:
"""Sweep the stage up and down, then move to the sharpest point
@ -153,7 +154,8 @@ class AutofocusThing(Thing):
"""
with m.run():
# Move to (-dz / 2)
m.focus_rel(-dz / 2)
if start == 'centre':
m.focus_rel(-dz / 2)
# Move to dz while monitoring sharpness
# i: Sharpness monitor index for this move
# z: Final z position after move

View file

@ -480,15 +480,15 @@ class SmartScanThing(Thing):
# TODO: combine this with the move below for speed (I think this could just be "else")
logger.info(f"Moving to {loc}")
stage.move_absolute(x=int(loc[0]), y=int(loc[1]), z=int(loc[2]))
if len(focused_path) > 1:
z_index = closest(loc, focused_path)
z=int(focused_path[z_index][2])
# print('Moving to {0}'.format([coords[0], coords[1], focused_path[z_index][2]]))
# print(focused_path)
stage.move_absolute(
x=int(loc[0]), y=int(loc[1]), z=int(focused_path[z_index][2])
)
stage.move_absolute(
x=int(loc[0]), y=int(loc[1]), z = z - self.autofocus_dz / 2
)
# Check if the image is background
if self.skip_background:
@ -517,7 +517,7 @@ class SmartScanThing(Thing):
attempts = 0
if self.autofocus_dz > 200:
while True:
recentre.looping_autofocus(dz=self.autofocus_dz)
recentre.looping_autofocus(dz=self.autofocus_dz, start = 'base')
current_height = stage.position["z"]
# if there have been successful autofocuses in this scan, find the closest one in x-y
@ -542,7 +542,7 @@ class SmartScanThing(Thing):
focused_path.append(loc)
break
if attempts >= 3:
logger.warning("Could not autofocus after 5 attempts.")
logger.warning("Could not autofocus after 3 attempts.")
break
# if the autofocus was rejected, we return to the height of the closest successful autofocus. not perfect, but better than wandering out of focus
logger.info(