Skip to content

Commit

Permalink
TST: added rotational stage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpartzsch committed Jan 17, 2025
1 parent c3f5b56 commit 5d0f787
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Install dependencies
run: |
pip install -e .[test]
pytest
pytest -sv
2 changes: 1 addition & 1 deletion motor_stage_ui/motor_stage_gui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from motor_stage_ui.PIStagesInterface import PIStagesInterface
from motor_stage_ui.pi_stages_interface import PIStagesInterface
from motor_stage_ui import logger
import motor_stage_ui

Expand Down
2 changes: 1 addition & 1 deletion motor_stage_ui/motor_stage_terminal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from motor_stage_ui.PIStagesInterface import PIStagesInterface as MC
from motor_stage_ui.pi_stages_interface import PIStagesInterface as MC
import motor_stage_ui

import os
Expand Down
6 changes: 3 additions & 3 deletions motor_stage_ui/pi_stages_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def get_position(
self.log.warning("Invalid stage type")

def _move_to_position(self, address: int, value: int) -> None:
"""Helper function for basil
"""Helper function for pyserial
Args:
address (int): Address of the motorstage
Expand All @@ -297,7 +297,7 @@ def _move_to_position(self, address: int, value: int) -> None:
)

def _move_relative(self, address: int, value: int = 1000000) -> None:
"""Helper function for basil
"""Helper function for pyserial
Args:
address (int): Address of the motorstage
Expand All @@ -309,7 +309,7 @@ def _move_relative(self, address: int, value: int = 1000000) -> None:
)

def _get_position(self, address: int) -> None:
"""Helper function for basil
"""Helper function for pyserial
Args:
address (int): Address of the motorstage
Expand Down
8 changes: 8 additions & 0 deletions motor_stage_ui/test/test_configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ x_axis:
unit: mm
port: '/dev/ttyUSB0'
baud_rate: 9600

rot:
stage_type: rotation
address: 3
step_size: 34e-06
unit: deg
port: '/dev/ttyUSB0'
baud_rate: 9600
115 changes: 106 additions & 9 deletions motor_stage_ui/test/test_pi_stage_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest
import yaml
from motor_stage_ui.pi_stages_interface import PIStagesInterface
from motor_stage_ui.pi_stages_interface import SerialInterface
from motor_stage_ui.test.utils import SerialInterfaceMock


Expand All @@ -12,10 +11,6 @@

with open(CONFIG_FILE) as yaml_file:
TESTCONFIG = yaml.safe_load(yaml_file)
ADDRESS = TESTCONFIG["x_axis"]["address"]
UNIT = TESTCONFIG["x_axis"]["unit"]
STEPSIZE = TESTCONFIG["x_axis"]["step_size"]
STAGE = TESTCONFIG["x_axis"]["stage_type"]


INTERFACE = SerialInterfaceMock
Expand All @@ -28,15 +23,29 @@


def test_init_motor():
ADDRESS = TESTCONFIG["x_axis"]["address"]
PISTAGES.init_motor(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-3:] == [
b"\x010MN\r",
b"\x010RT\r",
b"\x010SV200000\r",
]

ADDRESS = TESTCONFIG["rot"]["address"]
PISTAGES.init_motor(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-3:] == [
b"\x012MN\r",
b"\x012RT\r",
b"\x012SV200000\r",
]


def test_find_edge():
ADDRESS = TESTCONFIG["x_axis"]["address"]
UNIT = TESTCONFIG["x_axis"]["unit"]
STEPSIZE = TESTCONFIG["x_axis"]["step_size"]
STAGE = TESTCONFIG["x_axis"]["stage_type"]

def __get_position(self, address):
# Blank get Position function
return 0
Expand All @@ -46,37 +55,115 @@ def __get_position(self, address):
PISTAGES.find_edge(address=ADDRESS, unit=UNIT, stage=STAGE, step_size=STEPSIZE)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010FE0\r"

ADDRESS = TESTCONFIG["rot"]["address"]
UNIT = TESTCONFIG["rot"]["unit"]
STEPSIZE = float(TESTCONFIG["rot"]["step_size"])
STAGE = TESTCONFIG["rot"]["stage_type"]

def __get_position(self, address):
# Blank get Position function
return 0

func_type = type(PISTAGES._get_position)
PISTAGES._get_position = func_type(__get_position, PISTAGES)
PISTAGES.find_edge(address=ADDRESS, unit=UNIT, stage=STAGE, step_size=STEPSIZE)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x012FE0\r"


def test_set_home() -> None:
ADDRESS = TESTCONFIG["x_axis"]["address"]
PISTAGES.set_home(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010DH\r"

ADDRESS = TESTCONFIG["rot"]["address"]
PISTAGES.set_home(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x012DH\r"


def test_go_home() -> None:
ADDRESS = TESTCONFIG["x_axis"]["address"]
PISTAGES.go_home(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010GH\r"

ADDRESS = TESTCONFIG["rot"]["address"]
PISTAGES.go_home(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x012GH\r"


def test_abort():
ADDRESS = TESTCONFIG["x_axis"]["address"]
PISTAGES.abort(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010AB\r"

ADDRESS = TESTCONFIG["rot"]["address"]
PISTAGES.abort(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x012AB\r"


def test_move_to_position():
ADDRESS = TESTCONFIG["x_axis"]["address"]
UNIT = TESTCONFIG["x_axis"]["unit"]
STEPSIZE = TESTCONFIG["x_axis"]["step_size"]
STAGE = TESTCONFIG["x_axis"]["stage_type"]
PISTAGES.move_to_position(
address=ADDRESS, amount="1mm", unit=UNIT, stage=STAGE, step_size=STEPSIZE
)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010MA55555\r"

ADDRESS = TESTCONFIG["rot"]["address"]
UNIT = TESTCONFIG["rot"]["unit"]
STEPSIZE = float(TESTCONFIG["rot"]["step_size"])
STAGE = TESTCONFIG["rot"]["stage_type"]
PISTAGES.move_to_position(
address=ADDRESS, amount="0", unit=UNIT, stage=STAGE, step_size=STEPSIZE
address=ADDRESS, amount="1deg", unit=UNIT, stage=STAGE, step_size=STEPSIZE
)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010MA0\r"
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x012MA29411\r"


def test_move_relative():
ADDRESS = TESTCONFIG["x_axis"]["address"]
UNIT = TESTCONFIG["x_axis"]["unit"]
STEPSIZE = TESTCONFIG["x_axis"]["step_size"]
STAGE = TESTCONFIG["x_axis"]["stage_type"]
PISTAGES.move_relative(
address=ADDRESS, amount="-1cm", unit=UNIT, stage=STAGE, step_size=STEPSIZE
)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010MR-555555\r"

ADDRESS = TESTCONFIG["rot"]["address"]
UNIT = TESTCONFIG["rot"]["unit"]
STEPSIZE = float(TESTCONFIG["rot"]["step_size"])
STAGE = TESTCONFIG["rot"]["stage_type"]
PISTAGES.move_relative(
address=ADDRESS, amount="0", unit=UNIT, stage=STAGE, step_size=STEPSIZE
address=ADDRESS, amount="-1rad", unit=UNIT, stage=STAGE, step_size=STEPSIZE
)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010MR0\r"
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x012MR-1685169\r"


def test_get_position():
ADDRESS = TESTCONFIG["x_axis"]["address"]
UNIT = TESTCONFIG["x_axis"]["unit"]
STEPSIZE = TESTCONFIG["x_axis"]["step_size"]
STAGE = TESTCONFIG["x_axis"]["stage_type"]

def __get_position(self, address):
# Blank get Position function
return 0

func_type = type(PISTAGES._get_position)
PISTAGES._get_position = func_type(__get_position, PISTAGES)
assert (
PISTAGES.get_position(
address=ADDRESS, unit=UNIT, stage=STAGE, step_size=STEPSIZE
)
== "0.000"
)

ADDRESS = TESTCONFIG["rot"]["address"]
UNIT = TESTCONFIG["rot"]["unit"]
STEPSIZE = float(TESTCONFIG["rot"]["step_size"])
STAGE = TESTCONFIG["rot"]["stage_type"]

def __get_position(self, address):
# Blank get Position function
return 0
Expand All @@ -91,5 +178,15 @@ def __get_position(self, address):
)


def test_get_stat():
ADDRESS = TESTCONFIG["x_axis"]["address"]
PISTAGES.get_stat(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x010TS\r"

ADDRESS = TESTCONFIG["rot"]["address"]
PISTAGES.get_stat(address=ADDRESS)
assert PISTAGES.serial_interface._serial_commands[-1] == b"\x012TS\r"


if __name__ == "__main__":
pytest.main()
1 change: 0 additions & 1 deletion motor_stage_ui/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def _read(self):
Returns:
str: message
"""
self.log.warning(self._serial_commands)
msg = self._serial_commands[-1].decode().strip().replace(self._terminator, "")
if msg == "":
self.log.error("No responds from serial interface.")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "motot_stage_ui"
version = "1.0"
version = "1.1"
license = { "text" = "AGPL-3.0" }
description = "User interface for the Mercury motor controller"
readme = {file = "README.md", content-type = "text/markdown"}
Expand Down

0 comments on commit 5d0f787

Please sign in to comment.