Skip to content

Commit

Permalink
Moved autofocus helper to instruments
Browse files Browse the repository at this point in the history
  • Loading branch information
kingofpayne committed Aug 16, 2024
1 parent ec52e77 commit d1df11d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
19 changes: 17 additions & 2 deletions laserstudio/instruments/instruments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from pystages import Autofocus, Vector
from typing import Optional, cast, Any
import logging
from .stage import StageInstrument
from .list_serials import DeviceSearchError
from .camera import CameraInstrument
Expand All @@ -8,8 +11,6 @@
from .laserdriver import LaserDriverInstrument, LaserDriver # type: ignore
from .pdm import PDMInstrument
from .probe import ProbeInstrument
from typing import Optional, cast, Any
import logging


class Instruments:
Expand Down Expand Up @@ -91,8 +92,22 @@ def __init__(self, config: dict):
continue
self.probes.append(ProbeInstrument(config=probe_config))

# Autofocus helper: stores registered position in order to do automatic camera
# focusing. This can be considered as an abstract instrument.
self.autofocus_helper = Autofocus()

def go_next(self) -> dict[str, Any]:
results = []
for laser in self.lasers:
results.append(laser.go_next())
return {"lasers": results}

def autofocus(self):
if self.stage is None:
return
if len(self.autofocus_helper.registered_points) < 3:
return
pos = self.stage.position
z = self.autofocus_helper.focus(pos.x, pos.y)
assert abs(z - pos.z) < 500
self.stage.move_to(Vector(pos.x, pos.y, z), wait=True)
12 changes: 9 additions & 3 deletions laserstudio/laserstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
PDMToolbar,
LaserDriverToolbar,
CameraNITToolBar,
FocusToolbar
FocusToolbar,
)
import yaml
from .restserver.server import RestProxy
Expand Down Expand Up @@ -93,8 +93,14 @@ def __init__(self, config: Optional[dict]):
self.addToolBar(Qt.ToolBarArea.BottomToolBarArea, toolbar)

# Toolbar: Focusing
if (self.instruments.stage is not None) and (self.instruments.camera is not None):
toolbar = FocusToolbar(self.instruments.stage, self.instruments.camera)
if (self.instruments.stage is not None) and (
self.instruments.camera is not None
):
toolbar = FocusToolbar(
self.instruments.stage,
self.instruments.camera,
self.instruments.autofocus_helper,
)
self.addToolBar(toolbar)

# Toolbar: Scanning zone definition and usage
Expand Down
7 changes: 2 additions & 5 deletions laserstudio/restserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ def handle_go_next(self):

@pyqtSlot(result="QVariant")
def handle_autofocus(self):
return QVariant({"error": "Not implemented"})
try:
return QVariant(self.laser_studio.autofocus(no_warning=True))
except RuntimeError as e:
return {"error": str(e)}
self.laser_studio.instruments.autofocus()
return QVariant({})

@pyqtSlot(result="QVariant")
def handle_go_to_memory_point(self, index: int):
Expand Down
6 changes: 3 additions & 3 deletions laserstudio/widgets/toolbars/focustoolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
class FocusToolbar(QToolBar):
"""Toolbar for focus registration and autofocus."""

def __init__(self, stage, camera: CameraNITInstrument):
def __init__(self, stage, camera: CameraNITInstrument, autofocus_helper: Autofocus):
"""
:param autofocus: Stores the registered points and calculates focus on demand.
:param autofocus_helper: Stores the registered points and calculates focus on demand.
"""
super().__init__("Focus")
self.setObjectName("toolbar-focus") # For settings save and restore
Expand All @@ -26,7 +26,7 @@ def __init__(self, stage, camera: CameraNITInstrument):
# This is used to prevent launching two search threads at the same time.
self.focus_thread: FocusThread | None = None

self.autofocus_helper = Autofocus()
self.autofocus_helper = autofocus_helper
self.stage = stage
self.camera = camera

Expand Down

0 comments on commit d1df11d

Please sign in to comment.