Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to check if there is beam and wait for beam. #29

Open
wants to merge 1 commit into
base: esrf-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion mxcubecore/HardwareObjects/ESRF/ESRFBeam.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class ESRFBeam(AbstractBeam):

unit = "mm"

def __init__(self, name):
super().__init__(name)
self._beam_check_obj = None
self._monitorbeam_obj = None

def init(self):
"""Initialize hardware"""
super().init()
Expand All @@ -48,10 +53,10 @@ def init(self):
_definer_type.append("aperture")

_slits = self.get_property("slits")
_bliss_obj = self.get_object_by_role("bliss")
if _slits:
self._slits = {}
_definer_type.append("slits")
_bliss_obj = self.get_object_by_role("bliss")
for name in _slits.split():
_key, _val = name.split(":")
self._slits.update({_key: _bliss_obj.getattribute(_val)})
Expand Down Expand Up @@ -80,6 +85,12 @@ def init(self):
self._definer.connect("valueChanged", self._re_emit_values)
self._definer.connect("stateChanged", self._re_emit_values)

self._monitorbeam_obj = self.get_object_by_role("monitor_beam")

beam_check = self.get_property("beam_check_name")
if beam_check and _bliss_obj:
self._beam_check_obj = getattr(_bliss_obj, beam_check)

def _re_emit_values(self, value):
# redefine as re_emit_values takes no arguments
self.re_emit_values()
Expand Down Expand Up @@ -313,3 +324,27 @@ def get_beam_position_on_screen(self):
def get_beam_size(self):
beam_value = self.get_value()
return (beam_value[0], beam_value[1])

def _is_beam(self):
"""Check if there is beam
Returns:
(bool): True if beam present, False otherwise
"""
return self._beam_check_obj.is_beam()

def wait_for_beam(self, timeout=None):
"""Wait until beam present
Args:
timeout (float): optional - timeout [s],
If timeout == 0: return at once and do not wait
(default);
if timeout is None: wait forever.
"""
if self._monitorbeam_obj:
try:
timeout = timeout or self._beam_check_obj.timeout
if self._monitorbeam_obj.get_value().value:
return self._beam_check_obj.wait_for_beam(timeout)
except AttributeError:
return True
return True
27 changes: 27 additions & 0 deletions mxcubecore/HardwareObjects/abstract/AbstractBeam.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,30 @@ def re_emit_values(self):
self.emit("beamSizeChanged", (self._beam_width, self._beam_height))
self.emit("beamInfoChanged", (self._beam_info_dict))
self.emit("beamPosChanged", (self._beam_position_on_screen,))

@property
def is_beam(self):
"""Check if there is beam.
Returns:
(bool): True if beam present, False otherwise
"""
return self._is_beam()

def _is_beam(self):
"""Specific implementation to check the presence of the beam.
Raises:
NotImplementedError.
"""
return True

def wait_for_beam(self, timeout=None):
"""Wait until beam present
Args:
timeout (float): optional - timeout [s],
If timeout == 0: return at once and do not wait
(default);
if timeout is None: wait forever.
Returns:
(bool): True if beam present, False otherwise
"""
return True
16 changes: 16 additions & 0 deletions mxcubecore/HardwareObjects/mockup/BeamMockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<definer_type>definer</definer_type>
<beam_divergence_vertical>0</beam_divergence_vertical>
<beam_divergence_horizontal>0</beam_divergence_horizontal>
<check_beam>(10,10)</check_beam>
</object>
"""

Expand All @@ -48,6 +49,7 @@ class BeamMockup(AbstractBeam):
def __init__(self, name):
super().__init__(name)
self._definer_type = None
self._check_beam = ()

def init(self):
"""Initialize hardware"""
Expand All @@ -74,6 +76,10 @@ def init(self):
self.get_property("beam_position", "[318, 238]")
)

_check_beam = self.get_property("check_beam")
if _check_beam:
self._check_beam = literal_eval(_check_beam)

self.re_emit_values()
self.emit("beamPosChanged", (self._beam_position_on_screen,))

Expand Down Expand Up @@ -296,3 +302,13 @@ def set_value(self, size=None):
if not isinstance(size, str):
raise TypeError("Incorrect input value for definer")
self.definer.set_value(self.definer.VALUES[size], timeout=2)

def _is_beam(self):
"""Check if there is beam
Returns:
(bool): True if beam present, False otherwise
"""
if not self._check_beam:
return True
beam = self.get_value()
return all([x1 <= x2 for (x1, x2) in zip(self._check_beam, (beam[0], beam[1]))])
2 changes: 2 additions & 0 deletions mxcubecore/configuration/mockup/beam-mockup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
<!-- <definer_type>definer</definer_type>-->
<beam_divergence_horizontal>12.0</beam_divergence_horizontal>
<beam_divergence_vertical>12.0</beam_divergence_vertical>
<!-- value to check againt the size of the beam -->
<check_beam>(0.030,0.030)</check_beam>
</object>
18 changes: 18 additions & 0 deletions test/pytest/test_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,21 @@ def test_set_definer_size(self, test_object):
assert beam_height == dsize.value[1]
assert beam_shape == BeamShape.ELLIPTICAL
assert beam_label == dsize.name

def test_is_beam(self, test_object):
"""Check if there is beam"""
check_beam = test_object._check_beam
if not check_beam:
assert test_object.is_beam == True
else:
test_object._definer_type = "aperture"
for val in test_object.aperture.get_diameter_size_list():
app_size = int(test_object.aperture.VALUES[val].value[0]) / 1000
test_object.aperture.set_value(
test_object.aperture.VALUES[val], timeout=2
)
_beam = test_object.is_beam
if check_beam[0] > app_size:
assert _beam == False
else:
assert _beam == True
Loading