Merge branch 'Sanga-firmware-check' into 'v3'

Log an error if the sangaboard firmware is below v1.0.4

Closes #623

See merge request openflexure/openflexure-microscope-server!447
This commit is contained in:
Julian Stirling 2025-12-02 15:14:50 +00:00
commit 38c596fb49
3 changed files with 112 additions and 5 deletions

View file

@ -10,6 +10,7 @@ from types import TracebackType
from contextlib import contextmanager
from collections.abc import Mapping
import semver
import sangaboard
import labthings_fastapi as lt
@ -17,6 +18,9 @@ from . import BaseStage
LOGGER = logging.getLogger(__name__)
REQUIRED_VERSION = semver.Version.parse("1.0.0")
RECOMMENDED_VERSION = semver.Version.parse("1.0.4")
class SangaboardThing(BaseStage):
"""A Thing to manage a Sangaboard motor controller.
@ -42,18 +46,15 @@ class SangaboardThing(BaseStage):
"""
self.sangaboard_kwargs = copy(kwargs)
self.sangaboard_kwargs["port"] = port
self._sangaboard_lock = threading.RLock()
super().__init__(**kwargs)
def __enter__(self) -> None:
"""Connect to the sangaboard when the Thing context manager is opened."""
self._sangaboard = sangaboard.Sangaboard(**self.sangaboard_kwargs)
self._sangaboard_lock = threading.RLock()
with self.sangaboard() as sb:
if sb.version_tuple[0] != 1:
raise RuntimeError(
"Please update your Sangaboard Firmware. v1 is required."
)
sb.query("blocking_moves false")
self.check_firmware()
self.update_position()
def __exit__(
@ -89,6 +90,33 @@ class SangaboardThing(BaseStage):
zip(self.axis_names, sb.position, strict=True)
)
def check_firmware(self) -> None:
"""Error/warn if firmware doesn't meet requirements/recommendations.
Raise a Runtime Error if the version is below REQUIRED_VERSION
Log a warning if the version is below RECOMMENDED_VERSION
"""
with self.sangaboard() as sb:
# This will raise a ValueError is if the firmware version cannot be parsed
# this error will stop the microscope booting as we cannot ensure valid
# firmware.
version = semver.Version.parse(sb.firmware_version)
# Raise an error if version is below required
if version < REQUIRED_VERSION:
raise RuntimeError(
f"Sangaboard firmware version {version} is below the required "
f"{REQUIRED_VERSION}."
)
# Warn if version is below recommended
if version < RECOMMENDED_VERSION:
LOGGER.warning(
f"Sangaboard firmware version {version} is below the recommended "
f"{RECOMMENDED_VERSION}."
)
def _hardware_move_relative(
self,
cancel: lt.deps.CancelHook,