Repository navigation
Feature switch element #526
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
Changes from all commits
6858f64
6255a82
07356c6
f0c540f
848e6c7
fe3ff83
800c44b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| """Switchable accelerator functionality without a required lattice counterpart.""" | ||
|
|
||
| import copy | ||
| from typing import TYPE_CHECKING, Self | ||
|
|
||
| from ..validation import DynamicValidation, register_schema | ||
| from . import abstract | ||
| from .element import Element | ||
| from .exception import PyAMLException | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .holders.element_holder import ElementHolder | ||
|
|
||
| PYAMLCLASS = "SwitchElement" | ||
|
|
||
|
|
||
| @register_schema | ||
| class SwitchElement(Element, DynamicValidation): | ||
| """ | ||
| Enable or disable accelerator functionality through one boolean channel. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| name : str | ||
| Unique element name, for example ``SOFB`` or ``FOFB``. | ||
| device : str | ||
| Opaque backend reference to one writable boolean channel. Status is | ||
| read from that channel's actual readback, not its requested setpoint. | ||
| Simulator modes do not resolve this reference. | ||
| description : str | None, optional | ||
| Human-readable description of the functionality. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| enabled | ||
| Boolean get/set accessor, shared by control-system and simulator modes. | ||
|
|
||
| Methods | ||
| ------- | ||
| attach(peer, enabled) | ||
| Return a copy bound to a peer and its boolean accessor. | ||
| enable() | ||
| Write True through the boolean accessor. | ||
| disable() | ||
| Write False through the boolean accessor. | ||
| is_enabled() | ||
| Return actual boolean readback, not the commanded state. | ||
|
|
||
| Notes | ||
| ----- | ||
| Control-system access uses the reusable RWBooleanScalar adapter: get() | ||
| reads the backend readback (including numeric 0/1 flags) and set() writes | ||
| a Python boolean through DeviceAccess.set(). Value metadata is unwrapped; | ||
| this interface adds no quality, waiting, or completion policy. Backend | ||
| errors propagate. Construction and attachment never initialize hardware. | ||
| A missing control-system channel does not prevent configuration loading; | ||
| reading or writing its accessor raises an explicit error instead. | ||
|
|
||
| Each simulator attachment starts disabled and maintains independent state | ||
| without looking up or modifying a lattice element. This element does not | ||
| implement measurement preparation or automatic state restoration. | ||
|
|
||
| Configuration | ||
| ------------- | ||
| .. code-block:: yaml | ||
|
|
||
| - class: pyaml.common.switch_element.SwitchElement | ||
| name: SOFB | ||
| device: SOFB_ENABLED | ||
| description: Slow orbit feedback | ||
|
|
||
| Examples | ||
| -------- | ||
| With this element configured in an accelerator named ``sr``: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| feedback = sr.live["SOFB"] | ||
| feedback.disable() | ||
| enabled = feedback.enabled.get() | ||
| feedback.enabled.set(True) | ||
| feedback.enable() | ||
|
|
||
| simulated_feedback = sr.design["SOFB"] | ||
| assert simulated_feedback.enabled.get() is False | ||
| simulated_feedback.enabled.set(True) # Does not affect hardware or the lattice. | ||
| """ | ||
|
|
||
| __pyaml_repr_exclude__ = ("enabled",) | ||
|
|
||
| def __init__(self, name: str, device: str, description: str | None = None): | ||
| """Initialize the element configuration without accessing hardware.""" | ||
| super().__init__(name=name, description=description) | ||
| self.device = device | ||
| self._enabled: abstract.RWBooleanScalar | None = None | ||
|
|
||
| def attach(self, peer: "ElementHolder", enabled: abstract.RWBooleanScalar) -> Self: | ||
| """ | ||
| Return a copy bound to a peer and its boolean state interface. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| peer : ElementHolder | ||
| Control system or simulator owning this runtime element. | ||
| enabled : abstract.RWBooleanScalar | ||
| Boolean get/set interface owned by this attachment. | ||
|
|
||
| Returns | ||
| ------- | ||
| Self | ||
| Attached copy; the original configuration remains unattached. | ||
| """ | ||
| obj = copy.copy(self) | ||
| obj._enabled = enabled | ||
| obj._peer = peer | ||
| return obj | ||
|
|
||
| @property | ||
| def enabled(self) -> abstract.RWBooleanScalar: | ||
| """Return the boolean get/set accessor for this holder.""" | ||
| if self._enabled is None: | ||
| raise PyAMLException(f"{self.name}: enabled state is unavailable before attachment") | ||
| return self._enabled | ||
|
|
||
| def enable(self) -> None: | ||
| """Enable the functionality through its boolean accessor.""" | ||
| self.enabled.set(True) | ||
|
|
||
| def disable(self) -> None: | ||
| """Disable the functionality through its boolean accessor.""" | ||
| self.enabled.set(False) | ||
|
|
||
| def is_enabled(self) -> bool: | ||
| """ | ||
| Return actual boolean readback, not the commanded state. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| Whether the functionality is enabled. | ||
|
|
||
| Raises | ||
| ------ | ||
| PyAMLException | ||
| If unattached, or if the backend value is not a boolean flag. | ||
| """ | ||
| return self.enabled.get() | ||
|
|
||
| def _fill_device(self, holder: "ElementHolder") -> None: | ||
| holder._fill_switch_element(self) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about an if statement here that does the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's what would've needed to be done in _fill_unbound_element. I don't see much difference between another def() and another if(). Same number of lines of code. But it could be done. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| from ..common.abstract_aggregator import ScalarAggregator | ||
| from ..common.exception import PyAMLConfigException, PyAMLException | ||
| from ..common.holders.element_holder import ElementHolder | ||
| from ..common.switch_element import SwitchElement | ||
| from ..common.units import SI_ANGLE, SI_FREQUENCY, SI_POSITION, SI_TUNE, SI_VOLTAGE, is_frequency_unit | ||
| from ..configuration.unbound_element import UnboundElement | ||
| from ..control.abstract_impl import ( | ||
|
|
@@ -25,6 +26,7 @@ | |
| CSStrengthScalarAggregator, | ||
| RBetatronTuneArray, | ||
| RBpmArray, | ||
| RWBooleanScalar, | ||
| RWBpmOffsetArray, | ||
| RWBpmTiltScalar, | ||
| RWHardwareArray, | ||
|
|
@@ -360,6 +362,11 @@ def _fill_betatron_tune_monitor(self, monitor: BetatronTuneMonitor) -> None: | |
| def _fill_tool(self, tool: TuningTool | MeasurementTool) -> None: | ||
| self.add_tool(tool.attach(self)) | ||
|
|
||
| def _fill_switch_element(self, element: SwitchElement) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a shame that we need to keep adding new "_fill" functions every time we need to define a new element. Maybe we could categorize this as a "tool" and use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like it so much because it does not share a type with TuningTool or MeasurementTool, and the functionality is closer to an element. But maybe typing it as TuningTool is ok. But I agree that these fill* methods need to be revised somehow.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then maybe it could use _fill_unbound_element if it is left uncategorized?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like it either... It reduces the number of functions, but the lines of code are roughly the same. In fact, I don't like this unbound element so much. |
||
| device = self.get_device_access(element.device) | ||
| enabled = RWBooleanScalar(device, name=f"{element.name}: switch channel {element.device!r}") | ||
| self._add(self._ALL, element.attach(self, enabled)) | ||
|
|
||
| def _fill_unbound_element(self, element: UnboundElement) -> None: | ||
| if self.name() not in element._control_modes: | ||
| return | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.