From 6f3cf00b3642b66563713c175ebcdea565ce629f Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Mon, 23 Feb 2026 13:03:58 +0000 Subject: [PATCH] Add a base stage method for update_position, splitting out hardware specifics --- .../things/stage/__init__.py | 11 +++++++++-- .../things/stage/dummy.py | 17 ++++++++++------- .../things/stage/sangaboard.py | 7 +++++-- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/openflexure_microscope_server/things/stage/__init__.py b/src/openflexure_microscope_server/things/stage/__init__.py index 2c06eaf3..5a350ce1 100644 --- a/src/openflexure_microscope_server/things/stage/__init__.py +++ b/src/openflexure_microscope_server/things/stage/__init__.py @@ -133,9 +133,16 @@ class BaseStage(lt.Thing): """Used to convert coordinates between the program frame and the hardware frame.""" def update_position(self) -> None: - """Read position from the stage and set the corresponding property.""" + """Update the position property from the stage.""" + self._hardware_update_position() + + def _hardware_update_position(self) -> None: + """Read position from the stage and set internal attribute _hardware_position. + + _hardware_position should only be set in this function. + """ raise NotImplementedError( - "StageThings must define their own update_position method" + "StageThings must define their own _hardware_update_position method" ) @overload diff --git a/src/openflexure_microscope_server/things/stage/dummy.py b/src/openflexure_microscope_server/things/stage/dummy.py index 3b1aaf0a..4948d266 100644 --- a/src/openflexure_microscope_server/things/stage/dummy.py +++ b/src/openflexure_microscope_server/things/stage/dummy.py @@ -81,9 +81,12 @@ class DummyStage(BaseStage): ) """Used to convert coordinates between the program frame and the hardware frame.""" - def update_position(self) -> None: - """Read position from the stage and set the corresponding property.""" - pass + def _hardware_update_position(self) -> None: + """Read position from the stage and set internal attribute _hardware_position. + + _hardware_position should only be set in this function. + """ + self._hardware_position = self.instantaneous_position def _set_pos_during_move( self, displacement: Sequence[int], fraction_complete: float @@ -109,7 +112,7 @@ class DummyStage(BaseStage): # If there is a new movement. if movement_request is not None: # Set the hardware position from instantaneous before continuing. - self._hardware_position = self.instantaneous_position + self.update_position() if movement_request.displacement is None: # If it is a stop command, stop moving @@ -138,7 +141,7 @@ class DummyStage(BaseStage): # move is complete fraction_complete = 1.0 self._set_pos_during_move(displacement, fraction_complete) - self._hardware_position = self.instantaneous_position + self.update_position() self._movement_ongoing = False def _check_for_new_move_request(self) -> Optional[DummyStageMovement]: @@ -233,5 +236,5 @@ class DummyStage(BaseStage): stage. """ with self._hardware_lock: - self._hardware_position = dict.fromkeys(self.axis_names, 0) - self.instantaneous_position = self._hardware_position + self.instantaneous_position = dict.fromkeys(self.axis_names, 0) + self.update_position() diff --git a/src/openflexure_microscope_server/things/stage/sangaboard.py b/src/openflexure_microscope_server/things/stage/sangaboard.py index 128586f3..a6c99eb9 100644 --- a/src/openflexure_microscope_server/things/stage/sangaboard.py +++ b/src/openflexure_microscope_server/things/stage/sangaboard.py @@ -80,8 +80,11 @@ class SangaboardThing(BaseStage): ) """Used to convert coordinates between the program frame and the hardware frame.""" - def update_position(self) -> None: - """Read position from the stage and set the corresponding property.""" + def _hardware_update_position(self) -> None: + """Read position from the stage and set internal attribute _hardware_position. + + _hardware_position should only be set in this function. + """ with self._hardware_lock: self._hardware_position = dict( zip(self.axis_names, self._sangaboard.position, strict=True)