-
Notifications
You must be signed in to change notification settings - Fork 2
builtin actor manager and scan interlock actor #877
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
Open
d-perl
wants to merge
6
commits into
main
Choose a base branch
from
feat/scan_interlock_actor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
50351d0
feat: scan interlock actor
d-perl cba2e90
feat: add and remove states from interlock
d-perl e1eae9c
feat: interlock restarts scans
d-perl 82c678e
test: test that scan is restarted
d-perl bd8a204
test: simplify scan interlock unit tests
d-perl f2bb265
fix: handle no interlock data in redis
d-perl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import time | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
|
|
||
| from bec_lib.bl_states import DeviceWithinLimitsStateConfig | ||
| from bec_lib.builtin_actor_hli import BuiltinActorHli | ||
| from bec_lib.logger import bec_logger | ||
| from bec_lib.messages import ScanQueueStatus | ||
|
|
||
| logger = bec_logger.logger | ||
|
|
||
| if TYPE_CHECKING: # pragma: no cover | ||
| from ophyd_devices.sim.sim_test_devices import SimDeviceWithSignalDelay | ||
|
|
||
| from bec_ipython_client.main import BECIPythonClient | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def bec_with_delay_device(bec_ipython_client_fixture): | ||
| bec = bec_ipython_client_fixture | ||
| bec.builtin_actors.scan_interlock.enabled = True | ||
| dev = bec.device_manager.devices | ||
| dev.ramp_up.min_val.put(0) | ||
| dev.ramp_up.max_val.put(400) | ||
| dev.ramp_up.value.put(400) | ||
| dev.ramp_up.delay.put(2) | ||
| dev.ramp_up.rampup_time.put(2) | ||
| yield bec, dev.ramp_up | ||
| dev.ramp_up.stop() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def ramp_up_bl_state(bec_with_delay_device): | ||
| bec, _ = bec_with_delay_device | ||
| ramp_up_state_config = DeviceWithinLimitsStateConfig( | ||
| name="beam_intensity_sufficient", device="ramp_up", low_limit=200 | ||
| ) | ||
| bec.beamline_states.add(ramp_up_state_config) | ||
| yield bec_with_delay_device | ||
| bec.beamline_states.delete("beam_intensity_sufficient") | ||
| bec.builtin_actors.scan_interlock.clear_all() | ||
|
|
||
|
|
||
| def _wait_for(pred, timeout=10, retries=100): | ||
| for i in range(retries): | ||
| if pred(): | ||
| return True | ||
| time.sleep(timeout / retries) | ||
| return pred() | ||
|
|
||
|
|
||
| # pylint: disable=protected-accesstest | ||
| @pytest.mark.timeout(100) | ||
| def test_scan_interlock( | ||
| ramp_up_bl_state: tuple[BECIPythonClient, SimDeviceWithSignalDelay], bec_with_delay_device | ||
| ): | ||
| bec, ramp_up = ramp_up_bl_state | ||
| actors: BuiltinActorHli = bec.builtin_actors | ||
| assert bec.beamline_states.beam_intensity_sufficient.get()["status"] == "valid" | ||
| assert actors.scan_interlock.enabled | ||
| current_q_status_msg: ScanQueueStatus = bec.queue.queue_storage.current_scan_queue["primary"] | ||
| assert current_q_status_msg.status == "RUNNING" | ||
| actors.scan_interlock.add_state_to_interlock("beam_intensity_sufficient", "valid") | ||
|
|
||
| assert _wait_for(lambda: "beam_intensity_sufficient" in actors.scan_interlock.states_watched) | ||
|
|
||
| def _beam_down(): | ||
| return (ramp_up.value.get() < 200) and bec.beamline_states.beam_intensity_sufficient.get()[ | ||
| "status" | ||
| ] == "invalid" | ||
|
|
||
| def _beam_up(): | ||
| return (ramp_up.value.get() > 200) and bec.beamline_states.beam_intensity_sufficient.get()[ | ||
| "status" | ||
| ] == "valid" | ||
|
|
||
| ramp_up.start.set(1) | ||
| scan = bec.scans.line_scan( | ||
| "samx", -5, 5, steps=10, exp_time=0.5, relative=False, hide_report=True | ||
| ) | ||
|
|
||
| assert _wait_for(_beam_down) | ||
| assert _wait_for( | ||
| lambda: bec.queue.queue_storage.current_scan_queue["primary"].status == "LOCKED" | ||
| ) | ||
| assert scan.status == "STOPPED" | ||
| assert _wait_for(_beam_up) | ||
| assert _wait_for( | ||
| lambda: bec.queue.queue_storage.current_scan_queue["primary"].status == "RUNNING" | ||
| ) | ||
|
|
||
| def second_scan_has_run(): | ||
| return ( | ||
| bec.history[-2].metadata["bec"]["status"] == "aborted" | ||
| and bec.history[-1].metadata["bec"]["status"] == "closed" | ||
| ) | ||
|
|
||
| assert _wait_for(second_scan_has_run) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from bec_lib.endpoints import MessageEndpoints | ||
| from bec_lib.messages import ( | ||
| BlStateStatus, | ||
| BuiltinActorStateChangeMessage, | ||
| ScanInterlockModifyStateTableMessage, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from bec_lib.client import BECClient | ||
|
|
||
| VAR_PREFIX = "_BuiltinActors" | ||
|
|
||
|
|
||
| def builtin_actor_enabled_var(actor_name: str): | ||
| return f"{VAR_PREFIX}/enabled/{actor_name}" | ||
|
|
||
|
|
||
| class ScanInterlockHli: | ||
| def __init__(self, client: "BECClient", parent: "BuiltinActorHli") -> None: | ||
| self._client = client | ||
| self._parent = parent | ||
| self._actor_name = "ScanInterlockActor" | ||
|
|
||
| @property | ||
| def enabled(self): | ||
| return self._parent.check_enabled(self._actor_name) | ||
|
|
||
| @enabled.setter | ||
| def enabled(self, enabled: bool): | ||
| if enabled: | ||
| self._parent.set_enabled(self._actor_name) | ||
| else: | ||
| self._parent.set_disabled(self._actor_name) | ||
|
|
||
| @property | ||
| def states_watched(self) -> dict[str, BlStateStatus]: | ||
| """Return the table of beamline states currently watched by the scan interlock actor""" | ||
| if msg := self._client.connector.get(MessageEndpoints.scan_interlock_states()): | ||
| return msg.states_watched | ||
| return {} | ||
|
|
||
| def add_state_to_interlock(self, state_name: str, required_value: BlStateStatus): | ||
| """ | ||
| Add a beamline state and its status to watch to the ScanInterlockActor. If the state no | ||
| longer has this status, an interlock will be placed on the primary scan queue. | ||
| Args: | ||
| state_name (str): the state to watch | ||
| status (Literal["valid","invalid","warning","unknown"]): the status to watch for. | ||
| """ | ||
| self._client.connector.xadd( | ||
| MessageEndpoints.modify_interlock_table(), | ||
| { | ||
| "data": ScanInterlockModifyStateTableMessage( | ||
| action="add", state_name=state_name, status=required_value | ||
| ) | ||
| }, | ||
| ) | ||
|
|
||
| def remove_state_from_interlock(self, state_name: str): | ||
| """ | ||
| No longer watch the given state for the scan interlock. | ||
| Args: | ||
| state_name (str): the state to watch | ||
| """ | ||
| self._client.connector.xadd( | ||
| MessageEndpoints.modify_interlock_table(), | ||
| {"data": ScanInterlockModifyStateTableMessage(action="remove", state_name=state_name)}, | ||
| ) | ||
|
|
||
| def clear_all(self): | ||
| """ | ||
| Remove all beamline states from the interlock watch table | ||
| Args: | ||
| state_name (str): the state to watch | ||
| """ | ||
| self._client.connector.xadd( | ||
| MessageEndpoints.modify_interlock_table(), | ||
| {"data": ScanInterlockModifyStateTableMessage(action="remove_all")}, | ||
| ) | ||
|
|
||
|
|
||
| class BuiltinActorHli: | ||
| def __init__(self, client: "BECClient") -> None: | ||
| self._client = client | ||
| self.scan_interlock = ScanInterlockHli(self._client, self) | ||
|
|
||
| def _notify(self, actor_name): | ||
| self._client.connector.send( | ||
| MessageEndpoints.builtin_actor_notification(), | ||
| BuiltinActorStateChangeMessage(actor_name=actor_name), | ||
| ) | ||
|
|
||
| def check_enabled(self, actor_name: str): | ||
| return bool(self._client.get_global_var(builtin_actor_enabled_var(actor_name))) | ||
|
|
||
| def set_enabled(self, actor_name: str): | ||
| self._client.set_global_var(builtin_actor_enabled_var(actor_name), True) | ||
| self._notify(actor_name) | ||
|
|
||
| def set_disabled(self, actor_name: str): | ||
| self._client.set_global_var(builtin_actor_enabled_var(actor_name), False) | ||
| self._notify(actor_name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.