Merge branch 'joram-fix-test' into 'master'

Joram fixes

See merge request openflexure/openflexure-microscope-server!21
This commit is contained in:
Joel Collins 2019-05-28 12:17:29 +00:00
commit 1f2799d9e1
2 changed files with 53 additions and 7 deletions

View file

@ -45,7 +45,7 @@ class AutofocusPlugin(MicroscopePlugin):
sharpnesses.append(self.measure_sharpness(metric_fn))
newposition = positions[np.argmax(sharpnesses)]
stage.focus_rel(newposition - stage.position[2])
stage.move_rel([0, 0, newposition - stage.position[2]])
return positions, sharpnesses

View file

@ -1,5 +1,7 @@
import numpy as np
import time
import logging
from collections.abc import Iterable
from .sangaboard import Sangaboard
from openflexure_microscope.stage.base import BaseStage
@ -21,6 +23,7 @@ class SangaStage(BaseStage):
self.board = Sangaboard(port, **kwargs)
self._backlash = None
self.axis_names = ['x', 'y', 'z']
@property
def state(self):
@ -61,14 +64,21 @@ class SangaStage(BaseStage):
back by ``backlash[i]``. This is computed per-axis, so if some axes are moving
in the same direction as ``backlash``, they won't do two moves.
"""
return self._backlash if self._backlash else np.array([0]*self.n_axes)
if self._backlash is not None:
return self._backlash
else:
return np.array([0] * self.n_axes)
@backlash.setter
def backlash(self, blsh):
logging.debug("Setting backlash to {}".format(blsh))
if blsh is None:
self._backlash = None
assert len(blsh) == self.n_axes
self._backlash = np.array([int(blsh)]*self.n_axes, dtype=np.int)
elif isinstance(blsh, Iterable):
assert len(blsh) == self.n_axes
self._backlash = np.array(blsh)
else:
self._backlash = np.array([int(blsh)]*self.n_axes, dtype=np.int)
def move_rel(self, displacement, axis=None, backlash=True):
"""Make a relative move, optionally correcting for backlash.
@ -98,7 +108,7 @@ class SangaStage(BaseStage):
# For each axis where we're moving in the *opposite*
# direction to self.backlash, we deliberately overshoot:
initial_move -= np.where(
self.backlash*displacement < 0,
self.backlash * displacement < 0,
self.backlash,
np.zeros(self.n_axes, dtype=self.backlash.dtype)
)
@ -114,6 +124,42 @@ 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"""
self.board.close()
@ -141,8 +187,8 @@ class SangaStage(BaseStage):
need to worry about that here.
"""
if type is not None:
print("An exception occurred inside a with block, resetting " +
"position to its value at the start of the with block")
print("An exception occurred inside a with block, resetting position \
to its value at the start of the with block")
try:
time.sleep(0.5)
self.move_abs(self._position_on_enter)