Skip to content

Commit

Permalink
Add a run_program to the pmac (#661)
Browse files Browse the repository at this point in the history
* Add a way for the pmac to run a program and wait on it

* Only exit program run is scanstatus is 0 ie collection done

* Use wait_for_value

* Fix run_program

* Fix linting

* Fix linting again

* Fix linting again maybe

* Go back

* try to fix linting

* try to fix linting

* correctly check float type

---------

Co-authored-by: David Perl <[email protected]>
  • Loading branch information
noemifrisina and dperl-dls committed Jul 26, 2024
1 parent cfcea2e commit f4c703d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
88
],
"editor.codeActionsOnSave": {
"source.fixAll.ruff": false,
"source.organizeImports.ruff": true
"source.fixAll.ruff": "never",
"source.organizeImports.ruff": "explicit"
}
},
"python.analysis.extraPaths": [
Expand Down
47 changes: 43 additions & 4 deletions src/dodal/devices/i24/pmac.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from enum import Enum
from enum import Enum, IntEnum
from typing import SupportsFloat

from bluesky.protocols import Triggerable
from ophyd_async.core import AsyncStatus, StandardReadable
from ophyd_async.core.signal import SignalRW
from ophyd_async.core import AsyncStatus, StandardReadable, wait_for_value
from ophyd_async.core.signal import SignalR, SignalRW
from ophyd_async.core.signal_backend import SignalBackend
from ophyd_async.core.soft_signal_backend import SoftSignalBackend
from ophyd_async.core.utils import DEFAULT_TIMEOUT
Expand All @@ -13,6 +14,11 @@
ZERO_STR = "!x0y0z0" # Command to blend any ongoing move into new position


class ScanState(IntEnum):
RUNNING = 1
DONE = 0


class LaserSettings(str, Enum):
"""PMAC strings to switch laser on and off.
Note. On the PMAC, M-variables usually have to do with position compare
Expand Down Expand Up @@ -78,7 +84,7 @@ async def set(self, laser_setting: LaserSettings):


class PMACStringEncReset(SignalRW):
""""""
"""Set a pmac_string to control the encoder channels in the controller."""

def __init__(
self,
Expand All @@ -95,6 +101,35 @@ async def set(self, enc_string: EncReset):
await self.signal.set(enc_string.value, wait=True)


class ProgramRunner(SignalRW):
"""Trigger the collection by setting the program number on the PMAC string.
Once the program number has been set, wait for the collection to be complete.
This will only be true when the status becomes 0.
"""

def __init__(
self,
pmac_str_sig: SignalRW,
status_sig: SignalR,
backend: SignalBackend,
timeout: float | None = DEFAULT_TIMEOUT,
name: str = "",
) -> None:
self.signal = pmac_str_sig
self.status = status_sig
super().__init__(backend, timeout, name)

@AsyncStatus.wrap
async def set(self, value: int, wait=True, timeout=None):
prog_str = f"&2b{value}r"
assert isinstance(timeout, SupportsFloat) or (
timeout is None
), f"ProgramRunner does not support calculating timeout itself, {timeout=}"
await self.signal.set(prog_str, wait=wait)
await wait_for_value(self.status, ScanState.DONE, timeout)


class PMAC(StandardReadable):
"""Device to control the chip stage on I24."""

Expand All @@ -121,4 +156,8 @@ def __init__(self, prefix: str, name: str = "") -> None:
self.scanstatus = epics_signal_r(float, "BL24I-MO-STEP-14:signal:P2401")
self.counter = epics_signal_r(float, "BL24I-MO-STEP-14:signal:P2402")

self.run_program = ProgramRunner(
self.pmac_string, self.scanstatus, backend=SoftSignalBackend(str)
)

super().__init__(name)
9 changes: 9 additions & 0 deletions tests/devices/unit_tests/i24/test_pmac.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import bluesky.plan_stubs as bps
import pytest
from bluesky.run_engine import RunEngine
from ophyd_async.core import set_mock_value

from dodal.devices.i24.pmac import HOME_STR, PMAC, EncReset, LaserSettings
from dodal.devices.util.test_utils import patch_motor
Expand Down Expand Up @@ -60,3 +61,11 @@ async def test_set_pmac_string_for_enc_reset(fake_pmac: PMAC, RE):
RE(bps.abs_set(fake_pmac.enc_reset, EncReset.ENC7, wait=True))

assert await fake_pmac.pmac_string.get_value() == "m708=100 m709=150"


async def test_run_proogram(fake_pmac: PMAC, RE):
set_mock_value(fake_pmac.scanstatus, 0)
prog_num = 10
RE(bps.abs_set(fake_pmac.run_program, prog_num, timeout=1, wait=True))

assert await fake_pmac.pmac_string.get_value() == f"&2b{prog_num}r"

0 comments on commit f4c703d

Please sign in to comment.