Fixed autofocus to not combine logs

This commit is contained in:
jaknapper 2024-01-29 19:56:24 +00:00
parent 528406137f
commit a0b98a1adc
3 changed files with 6 additions and 28 deletions

View file

@ -15,29 +15,6 @@ CamDep = direct_thing_client_dependency(StreamingPiCamera2, "/camera/")
CSMDep = direct_thing_client_dependency(CameraStageMapper, "/camera_stage_mapping/") CSMDep = direct_thing_client_dependency(CameraStageMapper, "/camera_stage_mapping/")
AutofocusDep = direct_thing_client_dependency(AutofocusThing, "/autofocus/") AutofocusDep = direct_thing_client_dependency(AutofocusThing, "/autofocus/")
def turningpoints(lst):
dx = np.diff(lst)
return dx[1:] * dx[:-1] < 0
def unpack_autofocus(scan_data):
"""Extract z, sharpness data from a move_and_measure call"""
scan_data = dict(scan_data)
jpeg_times = scan_data["jpeg_times"]
jpeg_sizes = scan_data["jpeg_sizes"]
jpeg_sizes_MB = [x / 10**3 for x in jpeg_sizes]
stage_times = scan_data["stage_times"]
stage_positions = scan_data["stage_positions"]
stage_height = [pos["z"] for pos in stage_positions]
jpeg_heights = np.interp(jpeg_times, stage_times, stage_height)
turning = np.where(turningpoints(jpeg_heights))[0]
return jpeg_heights[turning[0] : turning[1]], jpeg_sizes_MB[turning[0] : turning[1]]
class RecentringThing(Thing): class RecentringThing(Thing):
@thing_action @thing_action
def looping_autofocus(self, autofocus: AutofocusDep, stage: StageDep, dz=2000, start='centre'): def looping_autofocus(self, autofocus: AutofocusDep, stage: StageDep, dz=2000, start='centre'):
@ -51,8 +28,7 @@ class RecentringThing(Thing):
repeat = True repeat = True
attempts = 0 attempts = 0
while repeat and attempts < 10: while repeat and attempts < 10:
data = autofocus.fast_autofocus(dz=dz, start=start) heights, sizes = autofocus.fast_autofocus(dz=dz, start=start)
heights, _ = unpack_autofocus(data)
height_min = np.min(heights) height_min = np.min(heights)
height_max = np.max(heights) height_max = np.max(heights)
# TODO: max heights seems badly wrong! Something about turning? # TODO: max heights seems badly wrong! Something about turning?

View file

@ -119,7 +119,7 @@ class JPEGSharpnessMonitor:
raise ValueError( raise ValueError(
"No images were captured during the move of the stage. Perhaps the camera is not streaming images?" "No images were captured during the move of the stage. Perhaps the camera is not streaming images?"
) )
return jz[np.argmax(js)] return jz[np.argmax(js)], jz, js
def data_dict(self) -> SharpnessDataArrays: def data_dict(self) -> SharpnessDataArrays:
"""Return the gathered data as a single convenient dictionary""" """Return the gathered data as a single convenient dictionary"""
@ -161,13 +161,13 @@ class AutofocusThing(Thing):
# z: Final z position after move # z: Final z position after move
i, z = m.focus_rel(dz, block_cancellation=True) i, z = m.focus_rel(dz, block_cancellation=True)
# Get the z position with highest sharpness from the previous move (index i) # Get the z position with highest sharpness from the previous move (index i)
fz: int = m.sharpest_z_on_move(i) fz, heights, sizes = m.sharpest_z_on_move(i)
# Move all the way to the start so it's consistent # Move all the way to the start so it's consistent
i, z = m.focus_rel(-dz) i, z = m.focus_rel(-dz)
# Move to the target position fz (relative move of (fz - z)) # Move to the target position fz (relative move of (fz - z))
m.focus_rel(fz - z) m.focus_rel(fz - z)
# Return all focus data # Return all focus data
return m.data_dict() return heights, sizes
@thing_action @thing_action
def move_and_measure( def move_and_measure(

View file

@ -484,6 +484,8 @@ class SmartScanThing(Thing):
if len(focused_path) > 1: if len(focused_path) > 1:
z_index = closest(loc, focused_path) z_index = closest(loc, focused_path)
z=int(focused_path[z_index][2]) z=int(focused_path[z_index][2])
else:
z = loc[2]
# print('Moving to {0}'.format([coords[0], coords[1], focused_path[z_index][2]])) # print('Moving to {0}'.format([coords[0], coords[1], focused_path[z_index][2]]))
# print(focused_path) # print(focused_path)
stage.move_absolute( stage.move_absolute(