|
| 1 | +# Copyright (c) 2021-2025 The University of Texas Southwestern Medical Center. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted for academic and research use only (subject to the |
| 6 | +# limitations in the disclaimer below) provided that the following conditions are met: |
| 7 | +# |
| 8 | +# * Redistributions of source code must retain the above copyright notice, |
| 9 | +# this list of conditions and the following disclaimer. |
| 10 | +# |
| 11 | +# * Redistributions in binary form must reproduce the above copyright |
| 12 | +# notice, this list of conditions and the following disclaimer in the |
| 13 | +# documentation and/or other materials provided with the distribution. |
| 14 | +# |
| 15 | +# * Neither the name of the copyright holders nor the names of its |
| 16 | +# contributors may be used to endorse or promote products derived from this |
| 17 | +# software without specific prior written permission. |
| 18 | +# |
| 19 | +# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
| 20 | +# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 21 | +# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 23 | +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 24 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | +# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 28 | +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | +# POSSIBILITY OF SUCH DAMAGE. |
| 31 | + |
| 32 | +"""Thorlabs Kinesis controller wrapper backed by pylablib.""" |
| 33 | + |
| 34 | +import logging |
| 35 | +from time import sleep |
| 36 | +from typing import Any |
| 37 | + |
| 38 | +# Logger Setup |
| 39 | +p = __name__.split(".")[1] |
| 40 | +logger = logging.getLogger(p) |
| 41 | + |
| 42 | +SLEEP_AFTER_WAIT = 0.100 |
| 43 | + |
| 44 | + |
| 45 | +class KinesisStage: |
| 46 | + """Simple wrapper around pylablib's Kinesis motor API.""" |
| 47 | + |
| 48 | + def __init__(self, dev_path: str, verbose: bool = False): |
| 49 | + self.verbose = verbose |
| 50 | + self.dev_path = str(dev_path) |
| 51 | + self.stage = None |
| 52 | + self.move_params: dict[str, float] = { |
| 53 | + "min_velocity": 0.0, |
| 54 | + "max_velocity": 0.0, |
| 55 | + "acceleration": 0.0, |
| 56 | + } |
| 57 | + self.open() |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def _load_thorlabs_backend() -> Any: |
| 61 | + """Load pylablib Thorlabs backend lazily.""" |
| 62 | + try: |
| 63 | + from pylablib.devices import Thorlabs |
| 64 | + except ImportError as e: |
| 65 | + raise ImportError("pylablib is required for KINESIS stage support.") from e |
| 66 | + return Thorlabs |
| 67 | + |
| 68 | + def open(self) -> None: |
| 69 | + """Open the device for communications.""" |
| 70 | + thorlabs = self._load_thorlabs_backend() |
| 71 | + connection = {"port": self.dev_path, "baudrate": 115200, "rtscts": True} |
| 72 | + try: |
| 73 | + self.stage = thorlabs.KinesisMotor(("serial", connection), scale="step") |
| 74 | + except Exception as e: |
| 75 | + raise ConnectionError(f"KINESIS stage connection failed: {e}") from e |
| 76 | + |
| 77 | + def close(self) -> None: |
| 78 | + """Disconnect and close the device.""" |
| 79 | + if self.stage is None: |
| 80 | + return |
| 81 | + try: |
| 82 | + self.stage.stop() |
| 83 | + except Exception as exc: |
| 84 | + logger.debug( |
| 85 | + "Failed to stop KINESIS stage cleanly during close: %s", |
| 86 | + exc, |
| 87 | + exc_info=True, |
| 88 | + ) |
| 89 | + self.stage.close() |
| 90 | + |
| 91 | + def move_to_position( |
| 92 | + self, position_um: float, steps_per_um: float, wait_till_done: bool |
| 93 | + ) -> None: |
| 94 | + """Move to absolute position in microns.""" |
| 95 | + current_steps = self.stage.get_position(channel=1, scale=False) |
| 96 | + target_steps = int(round(float(position_um) * float(steps_per_um))) |
| 97 | + delta_steps = target_steps - int(current_steps) |
| 98 | + self.stage.move_by(delta_steps, channel=1, scale=False) |
| 99 | + if wait_till_done: |
| 100 | + self.stage.wait_move(channel=1) |
| 101 | + if SLEEP_AFTER_WAIT: |
| 102 | + sleep(SLEEP_AFTER_WAIT) |
| 103 | + |
| 104 | + def get_current_position(self, steps_per_um: float) -> float: |
| 105 | + """Get current position in microns.""" |
| 106 | + current_steps = self.stage.get_position(channel=1, scale=False) |
| 107 | + position_um = float(current_steps) / float(steps_per_um) |
| 108 | + return round(position_um, 2) |
| 109 | + |
| 110 | + def stop(self) -> None: |
| 111 | + """Halt motion.""" |
| 112 | + self.stage.stop() |
| 113 | + |
| 114 | + def home_stage(self) -> None: |
| 115 | + """Run homing sequence.""" |
| 116 | + self.stage.home() |
| 117 | + |
| 118 | + def set_velocity_params( |
| 119 | + self, |
| 120 | + min_velocity: float, |
| 121 | + max_velocity: float, |
| 122 | + acceleration: float, |
| 123 | + steps_per_um: float, |
| 124 | + ) -> None: |
| 125 | + """Set motion profile parameters.""" |
| 126 | + min_velocity_steps = min_velocity * steps_per_um |
| 127 | + max_velocity_steps = max_velocity * steps_per_um |
| 128 | + acceleration_steps = acceleration * steps_per_um |
| 129 | + self.stage.set_move_params( |
| 130 | + min_velocity_steps, max_velocity_steps, acceleration_steps |
| 131 | + ) |
| 132 | + self.move_params = { |
| 133 | + "min_velocity": min_velocity_steps, |
| 134 | + "max_velocity": max_velocity_steps, |
| 135 | + "acceleration": acceleration_steps, |
| 136 | + } |
0 commit comments