Source code for dlab.hardware.wrappers.thorlabs_controller
from __future__ import annotations
from typing import Optional
import thorlabs_apt as apt
[docs]
class ThorlabsNotActivatedError(RuntimeError):
"""Raised when an operation requires an activated motor."""
[docs]
class ThorlabsController:
def __init__(self, motor_id: int) -> None:
self.motor_id: int = motor_id
self.motor: Optional[apt.Motor] = None
[docs]
def activate(self, homing: bool = True) -> None:
"""Instantiate the motor handle and optionally home."""
self.motor = apt.Motor(self.motor_id)
if homing:
self.home()
def _ensure(self) -> apt.Motor:
"""Return the motor handle or raise if not activated."""
if self.motor is None:
raise ThorlabsNotActivatedError("Motor not activated. Call activate() first.")
return self.motor
[docs]
def home(self, blocking: bool = True) -> None:
"""Send the motor to its home position."""
self._ensure().move_home(blocking=blocking)
[docs]
def move_to(self, position: float, blocking: bool = True) -> None:
"""Move the motor to an absolute position (device units)."""
self._ensure().move_to(position, blocking=blocking)
[docs]
def get_position(self) -> Optional[float]:
"""Return the current position, or None if not activated."""
return self.motor.position if self.motor is not None else None
[docs]
def identify(self) -> None:
"""Flash the device LED to identify the unit."""
self._ensure().identify()
[docs]
def disable(self) -> None:
"""Disable the motor and drop the handle."""
if self.motor is not None:
try:
if hasattr(self.motor, "disable"):
self.motor.disable()
finally:
self.motor = None