From 0728b6fe0857528b9e51ceb12ef4cf4554a8e5c5 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Mon, 4 Nov 2019 12:09:23 +0000 Subject: [PATCH] Moved scans into base class --- openflexure_microscope/stage/base.py | 38 +++++++++++++++++++++++++ openflexure_microscope/stage/sanga.py | 40 +-------------------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/openflexure_microscope/stage/base.py b/openflexure_microscope/stage/base.py index 05b69a9c..0fb0cc6b 100644 --- a/openflexure_microscope/stage/base.py +++ b/openflexure_microscope/stage/base.py @@ -73,3 +73,41 @@ class BaseStage(metaclass=ABCMeta): def close(self): """Cleanly close communication with the stage""" pass + + def scan_linear(self, rel_positions, backlash=True, return_to_start=True): + """ + Scan through a list of (relative) positions (generator fn) + rel_positions should be an nx3-element array (or list of 3 element arrays). + Positions should be relative to the starting position - not a list of relative moves. + backlash argument is passed to move_rel + if return_to_start is True (default) we return to the starting position after a + successful scan. NB we always attempt to return to the starting position if an + exception occurs during the scan.. + """ + starting_position = self.position + rel_positions = np.array(rel_positions) + assert rel_positions.shape[1] == 3, ValueError( + "Positions should be 3 elements long." + ) + try: + self.move_rel(rel_positions[0], backlash=backlash) + yield 0 + + for i, step in enumerate(np.diff(rel_positions, axis=0)): + self.move_rel(step, backlash=backlash) + yield i + 1 + except Exception as e: + return_to_start = True # always return to start if it went wrong. + raise e + finally: + if return_to_start: + self.move_abs(starting_position, backlash=backlash) + + def scan_z(self, dz, **kwargs): + """Scan through a list of (relative) z positions (generator fn) + This function takes a 1D numpy array of Z positions, relative to + the position at the start of the scan, and converts it into an + array of 3D positions with x=y=0. This, along with all the + keyword arguments, is then passed to ``scan_linear``. + """ + return self.scan_linear([[0, 0, z] for z in dz], **kwargs) \ No newline at end of file diff --git a/openflexure_microscope/stage/sanga.py b/openflexure_microscope/stage/sanga.py index 4359d2b0..d25d5691 100644 --- a/openflexure_microscope/stage/sanga.py +++ b/openflexure_microscope/stage/sanga.py @@ -101,7 +101,7 @@ class SangaStage(BaseStage): return config - def move_rel(self, displacement, axis=None, backlash=True): + def move_rel(self, displacement: list, axis=None, backlash=True): """Make a relative move, optionally correcting for backlash. displacement: integer or array/list of 3 integers axis: None (for 3-axis moves) or one of 'x','y','z' @@ -147,44 +147,6 @@ class SangaStage(BaseStage): with self.lock: self.board.move_abs(final, **kwargs) - def scan_linear(self, rel_positions, backlash=True, return_to_start=True): - """ - Scan through a list of (relative) positions (generator fn) - rel_positions should be an nx3-element array (or list of 3 element arrays). - Positions should be relative to the starting position - not a list of relative moves. - backlash argument is passed to move_rel - if return_to_start is True (default) we return to the starting position after a - successful scan. NB we always attempt to return to the starting position if an - exception occurs during the scan.. - """ - starting_position = self.position - rel_positions = np.array(rel_positions) - assert rel_positions.shape[1] == 3, ValueError( - "Positions should be 3 elements long." - ) - try: - self.move_rel(rel_positions[0], backlash=backlash) - yield 0 - - for i, step in enumerate(np.diff(rel_positions, axis=0)): - self.move_rel(step, backlash=backlash) - yield i + 1 - except Exception as e: - return_to_start = True # always return to start if it went wrong. - raise e - finally: - if return_to_start: - self.move_abs(starting_position, backlash=backlash) - - def scan_z(self, dz, **kwargs): - """Scan through a list of (relative) z positions (generator fn) - This function takes a 1D numpy array of Z positions, relative to - the position at the start of the scan, and converts it into an - array of 3D positions with x=y=0. This, along with all the - keyword arguments, is then passed to ``scan_linear``. - """ - return self.scan_linear([[0, 0, z] for z in dz], **kwargs) - def close(self): """Cleanly close communication with the stage""" if hasattr(self, "board"):