ophyd-async 0.2.0__py3-none-any.whl → 0.3a2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. ophyd_async/__init__.py +1 -4
  2. ophyd_async/_version.py +2 -2
  3. ophyd_async/core/__init__.py +16 -10
  4. ophyd_async/core/_providers.py +38 -5
  5. ophyd_async/core/async_status.py +3 -3
  6. ophyd_async/core/detector.py +211 -62
  7. ophyd_async/core/device.py +45 -38
  8. ophyd_async/core/device_save_loader.py +96 -23
  9. ophyd_async/core/flyer.py +30 -244
  10. ophyd_async/core/signal.py +47 -21
  11. ophyd_async/core/signal_backend.py +7 -4
  12. ophyd_async/core/sim_signal_backend.py +30 -18
  13. ophyd_async/core/standard_readable.py +4 -2
  14. ophyd_async/core/utils.py +93 -30
  15. ophyd_async/epics/_backend/_aioca.py +30 -36
  16. ophyd_async/epics/_backend/_p4p.py +75 -41
  17. ophyd_async/epics/_backend/common.py +25 -0
  18. ophyd_async/epics/areadetector/__init__.py +4 -0
  19. ophyd_async/epics/areadetector/aravis.py +69 -0
  20. ophyd_async/epics/areadetector/controllers/ad_sim_controller.py +1 -1
  21. ophyd_async/epics/areadetector/controllers/aravis_controller.py +73 -0
  22. ophyd_async/epics/areadetector/controllers/pilatus_controller.py +37 -25
  23. ophyd_async/epics/areadetector/drivers/aravis_driver.py +154 -0
  24. ophyd_async/epics/areadetector/drivers/pilatus_driver.py +4 -4
  25. ophyd_async/epics/areadetector/pilatus.py +50 -0
  26. ophyd_async/epics/areadetector/writers/_hdffile.py +21 -7
  27. ophyd_async/epics/areadetector/writers/hdf_writer.py +26 -15
  28. ophyd_async/epics/demo/__init__.py +33 -3
  29. ophyd_async/epics/motion/motor.py +20 -14
  30. ophyd_async/epics/pvi/__init__.py +3 -0
  31. ophyd_async/epics/pvi/pvi.py +318 -0
  32. ophyd_async/epics/signal/__init__.py +0 -2
  33. ophyd_async/epics/signal/signal.py +26 -9
  34. ophyd_async/panda/__init__.py +19 -5
  35. ophyd_async/panda/_common_blocks.py +49 -0
  36. ophyd_async/panda/_hdf_panda.py +48 -0
  37. ophyd_async/panda/_panda_controller.py +37 -0
  38. ophyd_async/panda/_trigger.py +39 -0
  39. ophyd_async/panda/_utils.py +15 -0
  40. ophyd_async/panda/writers/__init__.py +3 -0
  41. ophyd_async/panda/writers/_hdf_writer.py +220 -0
  42. ophyd_async/panda/writers/_panda_hdf_file.py +58 -0
  43. ophyd_async/planstubs/__init__.py +5 -0
  44. ophyd_async/planstubs/prepare_trigger_and_dets.py +57 -0
  45. ophyd_async/protocols.py +73 -0
  46. ophyd_async/sim/__init__.py +11 -0
  47. ophyd_async/sim/demo/__init__.py +3 -0
  48. ophyd_async/sim/demo/sim_motor.py +116 -0
  49. ophyd_async/sim/pattern_generator.py +318 -0
  50. ophyd_async/sim/sim_pattern_detector_control.py +55 -0
  51. ophyd_async/sim/sim_pattern_detector_writer.py +34 -0
  52. ophyd_async/sim/sim_pattern_generator.py +37 -0
  53. {ophyd_async-0.2.0.dist-info → ophyd_async-0.3a2.dist-info}/METADATA +20 -76
  54. ophyd_async-0.3a2.dist-info/RECORD +76 -0
  55. {ophyd_async-0.2.0.dist-info → ophyd_async-0.3a2.dist-info}/WHEEL +1 -1
  56. ophyd_async/epics/signal/pvi_get.py +0 -22
  57. ophyd_async/panda/panda.py +0 -294
  58. ophyd_async-0.2.0.dist-info/RECORD +0 -53
  59. /ophyd_async/panda/{table.py → _table.py} +0 -0
  60. {ophyd_async-0.2.0.dist-info → ophyd_async-0.3a2.dist-info}/LICENSE +0 -0
  61. {ophyd_async-0.2.0.dist-info → ophyd_async-0.3a2.dist-info}/entry_points.txt +0 -0
  62. {ophyd_async-0.2.0.dist-info → ophyd_async-0.3a2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,37 @@
1
+ import asyncio
2
+ from typing import Optional
3
+
4
+ from ophyd_async.core import (
5
+ AsyncStatus,
6
+ DetectorControl,
7
+ DetectorTrigger,
8
+ wait_for_value,
9
+ )
10
+ from ophyd_async.panda import PcapBlock
11
+
12
+
13
+ class PandaPcapController(DetectorControl):
14
+ def __init__(self, pcap: PcapBlock) -> None:
15
+ self.pcap = pcap
16
+
17
+ def get_deadtime(self, exposure: float) -> float:
18
+ return 0.000000008
19
+
20
+ async def arm(
21
+ self,
22
+ num: int,
23
+ trigger: DetectorTrigger = DetectorTrigger.constant_gate,
24
+ exposure: Optional[float] = None,
25
+ ) -> AsyncStatus:
26
+ assert trigger in (
27
+ DetectorTrigger.constant_gate,
28
+ trigger == DetectorTrigger.variable_gate,
29
+ ), "Only constant_gate and variable_gate triggering is supported on the PandA"
30
+ await asyncio.gather(self.pcap.arm.set(True))
31
+ await wait_for_value(self.pcap.active, True, timeout=1)
32
+ return AsyncStatus(wait_for_value(self.pcap.active, False, timeout=None))
33
+
34
+ async def disarm(self) -> AsyncStatus:
35
+ await asyncio.gather(self.pcap.arm.set(False))
36
+ await wait_for_value(self.pcap.active, False, timeout=1)
37
+ return AsyncStatus(wait_for_value(self.pcap.active, False, timeout=None))
@@ -0,0 +1,39 @@
1
+ import asyncio
2
+ from dataclasses import dataclass
3
+
4
+ from ophyd_async.core import TriggerLogic, wait_for_value
5
+ from ophyd_async.panda import SeqBlock, SeqTable, TimeUnits
6
+
7
+
8
+ @dataclass
9
+ class SeqTableInfo:
10
+ sequence_table: SeqTable
11
+ repeats: int
12
+ prescale_as_us: float = 1 # microseconds
13
+
14
+
15
+ class StaticSeqTableTriggerLogic(TriggerLogic[SeqTableInfo]):
16
+ def __init__(self, seq: SeqBlock) -> None:
17
+ self.seq = seq
18
+
19
+ async def prepare(self, value: SeqTableInfo):
20
+ await asyncio.gather(
21
+ self.seq.prescale_units.set(TimeUnits.us),
22
+ self.seq.enable.set("ZERO"),
23
+ )
24
+ await asyncio.gather(
25
+ self.seq.prescale.set(value.prescale_as_us),
26
+ self.seq.repeats.set(value.repeats),
27
+ self.seq.table.set(value.sequence_table),
28
+ )
29
+
30
+ async def kickoff(self) -> None:
31
+ await self.seq.enable.set("ONE")
32
+ await wait_for_value(self.seq.active, True, timeout=1)
33
+
34
+ async def complete(self) -> None:
35
+ await wait_for_value(self.seq.active, False, timeout=None)
36
+
37
+ async def stop(self):
38
+ await self.seq.enable.set("ZERO")
39
+ await wait_for_value(self.seq.active, False, timeout=1)
@@ -0,0 +1,15 @@
1
+ from typing import Any, Dict, Sequence
2
+
3
+
4
+ def phase_sorter(panda_signal_values: Dict[str, Any]) -> Sequence[Dict[str, Any]]:
5
+ # Panda has two load phases. If the signal name ends in the string "UNITS",
6
+ # it needs to be loaded first so put in first phase
7
+ phase_1, phase_2 = {}, {}
8
+
9
+ for key, value in panda_signal_values.items():
10
+ if key.endswith("units"):
11
+ phase_1[key] = value
12
+ else:
13
+ phase_2[key] = value
14
+
15
+ return [phase_1, phase_2]
@@ -0,0 +1,3 @@
1
+ from ._hdf_writer import PandaHDFWriter
2
+
3
+ __all__ = ["PandaHDFWriter"]
@@ -0,0 +1,220 @@
1
+ import asyncio
2
+ from dataclasses import dataclass
3
+ from enum import Enum
4
+ from pathlib import Path
5
+ from typing import Any, AsyncGenerator, AsyncIterator, Dict, List, Optional
6
+
7
+ from bluesky.protocols import Descriptor, StreamAsset
8
+ from p4p.client.thread import Context
9
+
10
+ from ophyd_async.core import (
11
+ DEFAULT_TIMEOUT,
12
+ DetectorWriter,
13
+ Device,
14
+ DirectoryProvider,
15
+ NameProvider,
16
+ SignalR,
17
+ wait_for_value,
18
+ )
19
+ from ophyd_async.core.signal import observe_value
20
+ from ophyd_async.panda import CommonPandaBlocks
21
+
22
+ from ._panda_hdf_file import _HDFDataset, _HDFFile
23
+
24
+
25
+ class Capture(str, Enum):
26
+ # Capture signals for the HDF Panda
27
+ No = "No"
28
+ Value = "Value"
29
+ Diff = "Diff"
30
+ Sum = "Sum"
31
+ Mean = "Mean"
32
+ Min = "Min"
33
+ Max = "Max"
34
+ MinMax = "Min Max"
35
+ MinMaxMean = "Min Max Mean"
36
+
37
+
38
+ def get_capture_signals(
39
+ block: Device, path_prefix: Optional[str] = ""
40
+ ) -> Dict[str, SignalR]:
41
+ """Get dict mapping a capture signal's name to the signal itself"""
42
+ if not path_prefix:
43
+ path_prefix = ""
44
+ signals: Dict[str, SignalR[Any]] = {}
45
+ for attr_name, attr in block.children():
46
+ # Capture signals end in _capture, but num_capture is a red herring
47
+ if attr_name == "num_capture":
48
+ continue
49
+ dot_path = f"{path_prefix}{attr_name}"
50
+ if isinstance(attr, SignalR) and attr_name.endswith("_capture"):
51
+ signals[dot_path] = attr
52
+ attr_signals = get_capture_signals(attr, path_prefix=dot_path + ".")
53
+ signals.update(attr_signals)
54
+ return signals
55
+
56
+
57
+ @dataclass
58
+ class CaptureSignalWrapper:
59
+ signal: SignalR
60
+ capture_type: Capture
61
+
62
+
63
+ # This should return a dictionary which contains a dict, containing the Capture
64
+ # signal object, and the value of that signal
65
+ async def get_signals_marked_for_capture(
66
+ capture_signals: Dict[str, SignalR],
67
+ ) -> Dict[str, CaptureSignalWrapper]:
68
+ # Read signals to see if they should be captured
69
+ do_read = [signal.get_value() for signal in capture_signals.values()]
70
+
71
+ signal_values = await asyncio.gather(*do_read)
72
+
73
+ assert len(signal_values) == len(
74
+ capture_signals
75
+ ), "Length of read signals are different to length of signals"
76
+
77
+ signals_to_capture: Dict[str, CaptureSignalWrapper] = {}
78
+ for signal_path, signal_object, signal_value in zip(
79
+ capture_signals.keys(), capture_signals.values(), signal_values
80
+ ):
81
+ signal_path = signal_path.replace("_capture", "")
82
+ if (signal_value.value in iter(Capture)) and (signal_value.value != Capture.No):
83
+ signals_to_capture[signal_path] = CaptureSignalWrapper(
84
+ signal_object,
85
+ signal_value.value,
86
+ )
87
+
88
+ return signals_to_capture
89
+
90
+
91
+ class PandaHDFWriter(DetectorWriter):
92
+ _ctxt: Optional[Context] = None
93
+
94
+ def __init__(
95
+ self,
96
+ prefix: str,
97
+ directory_provider: DirectoryProvider,
98
+ name_provider: NameProvider,
99
+ panda_device: CommonPandaBlocks,
100
+ ) -> None:
101
+ self.panda_device = panda_device
102
+ self._prefix = prefix
103
+ self._directory_provider = directory_provider
104
+ self._name_provider = name_provider
105
+ self._datasets: List[_HDFDataset] = []
106
+ self._file: Optional[_HDFFile] = None
107
+ self._multiplier = 1
108
+
109
+ # Triggered on PCAP arm
110
+ async def open(self, multiplier: int = 1) -> Dict[str, Descriptor]:
111
+ """Retrieve and get descriptor of all PandA signals marked for capture"""
112
+
113
+ # Get capture PVs by looking at panda. Gives mapping of dotted attribute path
114
+ # to Signal object
115
+ self.capture_signals = get_capture_signals(self.panda_device)
116
+
117
+ # Ensure flushes are immediate
118
+ await self.panda_device.data.flush_period.set(0)
119
+
120
+ to_capture = await get_signals_marked_for_capture(self.capture_signals)
121
+ self._file = None
122
+ info = self._directory_provider()
123
+ # Set the initial values
124
+ await asyncio.gather(
125
+ self.panda_device.data.hdf_directory.set(
126
+ str(info.root / info.resource_dir)
127
+ ),
128
+ self.panda_device.data.hdf_file_name.set(
129
+ f"{info.prefix}{self.panda_device.name}{info.suffix}",
130
+ ),
131
+ self.panda_device.data.num_capture.set(0),
132
+ )
133
+
134
+ # Wait for it to start, stashing the status that tells us when it finishes
135
+ await self.panda_device.data.capture.set(True)
136
+ name = self._name_provider()
137
+ if multiplier > 1:
138
+ raise ValueError(
139
+ "All PandA datasets should be scalar, multiplier should be 1"
140
+ )
141
+ self._datasets = []
142
+ for attribute_path, capture_signal in to_capture.items():
143
+ split_path = attribute_path.split(".")
144
+ signal_name = split_path[-1]
145
+ # Get block names from numbered blocks, eg INENC[1]
146
+ block_name = (
147
+ f"{split_path[-3]}{split_path[-2]}"
148
+ if split_path[-2].isnumeric()
149
+ else split_path[-2]
150
+ )
151
+
152
+ for suffix in str(capture_signal.capture_type).split(" "):
153
+ self._datasets.append(
154
+ _HDFDataset(
155
+ name,
156
+ block_name,
157
+ f"{name}-{block_name}-{signal_name}-{suffix}",
158
+ f"{block_name}-{signal_name}".upper() + f"-{suffix}",
159
+ [1],
160
+ multiplier=1,
161
+ )
162
+ )
163
+
164
+ describe = {
165
+ ds.name: Descriptor(
166
+ source=self.panda_device.data.hdf_directory.source,
167
+ shape=ds.shape,
168
+ dtype="array" if ds.shape != [1] else "number",
169
+ external="STREAM:",
170
+ )
171
+ for ds in self._datasets
172
+ }
173
+ return describe
174
+
175
+ # Next few functions are exactly the same as AD writer. Could move as default
176
+ # StandardDetector behavior
177
+ async def wait_for_index(
178
+ self, index: int, timeout: Optional[float] = DEFAULT_TIMEOUT
179
+ ):
180
+ def matcher(value: int) -> bool:
181
+ return value >= index
182
+
183
+ matcher.__name__ = f"index_at_least_{index}"
184
+ await wait_for_value(
185
+ self.panda_device.data.num_captured, matcher, timeout=timeout
186
+ )
187
+
188
+ async def get_indices_written(self) -> int:
189
+ return await self.panda_device.data.num_captured.get_value()
190
+
191
+ async def observe_indices_written(
192
+ self, timeout=DEFAULT_TIMEOUT
193
+ ) -> AsyncGenerator[int, None]:
194
+ """Wait until a specific index is ready to be collected"""
195
+ async for num_captured in observe_value(
196
+ self.panda_device.data.num_captured, timeout
197
+ ):
198
+ yield num_captured // self._multiplier
199
+
200
+ async def collect_stream_docs(
201
+ self, indices_written: int
202
+ ) -> AsyncIterator[StreamAsset]:
203
+ # TODO: fail if we get dropped frames
204
+ if indices_written:
205
+ if not self._file:
206
+ self._file = _HDFFile(
207
+ self._directory_provider(),
208
+ Path(await self.panda_device.data.hdf_file_name.get_value()),
209
+ self._datasets,
210
+ )
211
+ for doc in self._file.stream_resources():
212
+ yield "stream_resource", doc
213
+ for doc in self._file.stream_data(indices_written):
214
+ yield "stream_datum", doc
215
+
216
+ # Could put this function as default for StandardDetector
217
+ async def close(self):
218
+ await self.panda_device.data.capture.set(
219
+ False, wait=True, timeout=DEFAULT_TIMEOUT
220
+ )
@@ -0,0 +1,58 @@
1
+ from dataclasses import dataclass
2
+ from pathlib import Path
3
+ from typing import Iterator, List
4
+
5
+ from event_model import StreamDatum, StreamResource, compose_stream_resource
6
+
7
+ from ophyd_async.core import DirectoryInfo
8
+
9
+
10
+ @dataclass
11
+ class _HDFDataset:
12
+ device_name: str
13
+ block: str
14
+ name: str
15
+ path: str
16
+ shape: List[int]
17
+ multiplier: int
18
+
19
+
20
+ class _HDFFile:
21
+ def __init__(
22
+ self,
23
+ directory_info: DirectoryInfo,
24
+ full_file_name: Path,
25
+ datasets: List[_HDFDataset],
26
+ ) -> None:
27
+ self._last_emitted = 0
28
+ self._bundles = [
29
+ compose_stream_resource(
30
+ spec="AD_HDF5_SWMR_SLICE",
31
+ root=str(directory_info.root),
32
+ data_key=ds.name,
33
+ resource_path=(f"{str(directory_info.root)}/{full_file_name}"),
34
+ resource_kwargs={
35
+ "name": ds.name,
36
+ "block": ds.block,
37
+ "path": ds.path,
38
+ "multiplier": ds.multiplier,
39
+ "timestamps": "/entry/instrument/NDAttributes/NDArrayTimeStamp",
40
+ },
41
+ )
42
+ for ds in datasets
43
+ ]
44
+
45
+ def stream_resources(self) -> Iterator[StreamResource]:
46
+ for bundle in self._bundles:
47
+ yield bundle.stream_resource_doc
48
+
49
+ def stream_data(self, indices_written: int) -> Iterator[StreamDatum]:
50
+ # Indices are relative to resource
51
+ if indices_written > self._last_emitted:
52
+ indices = {
53
+ "start": self._last_emitted,
54
+ "stop": indices_written,
55
+ }
56
+ self._last_emitted = indices_written
57
+ for bundle in self._bundles:
58
+ yield bundle.compose_stream_datum(indices)
@@ -0,0 +1,5 @@
1
+ from .prepare_trigger_and_dets import (
2
+ prepare_static_seq_table_flyer_and_detectors_with_same_trigger,
3
+ )
4
+
5
+ __all__ = ["prepare_static_seq_table_flyer_and_detectors_with_same_trigger"]
@@ -0,0 +1,57 @@
1
+ from typing import List
2
+
3
+ import bluesky.plan_stubs as bps
4
+
5
+ from ophyd_async.core.detector import DetectorTrigger, StandardDetector, TriggerInfo
6
+ from ophyd_async.core.flyer import HardwareTriggeredFlyable
7
+ from ophyd_async.core.utils import in_micros
8
+ from ophyd_async.panda._table import SeqTable, SeqTableRow, seq_table_from_rows
9
+ from ophyd_async.panda._trigger import SeqTableInfo
10
+
11
+
12
+ def prepare_static_seq_table_flyer_and_detectors_with_same_trigger(
13
+ flyer: HardwareTriggeredFlyable[SeqTableInfo],
14
+ detectors: List[StandardDetector],
15
+ num: int,
16
+ width: float,
17
+ deadtime: float,
18
+ shutter_time: float,
19
+ repeats: int = 1,
20
+ period: float = 0.0,
21
+ ):
22
+ trigger_info = TriggerInfo(
23
+ num=num * repeats,
24
+ trigger=DetectorTrigger.constant_gate,
25
+ deadtime=deadtime,
26
+ livetime=width,
27
+ )
28
+
29
+ trigger_time = num * (width + deadtime)
30
+ pre_delay = max(period - 2 * shutter_time - trigger_time, 0)
31
+
32
+ table: SeqTable = seq_table_from_rows(
33
+ # Wait for pre-delay then open shutter
34
+ SeqTableRow(
35
+ time1=in_micros(pre_delay),
36
+ time2=in_micros(shutter_time),
37
+ outa2=True,
38
+ ),
39
+ # Keeping shutter open, do N triggers
40
+ SeqTableRow(
41
+ repeats=num,
42
+ time1=in_micros(width),
43
+ outa1=True,
44
+ outb1=True,
45
+ time2=in_micros(deadtime),
46
+ outa2=True,
47
+ ),
48
+ # Add the shutter close
49
+ SeqTableRow(time2=in_micros(shutter_time)),
50
+ )
51
+
52
+ table_info = SeqTableInfo(table, repeats)
53
+
54
+ for det in detectors:
55
+ yield from bps.prepare(det, trigger_info, wait=False, group="prep")
56
+ yield from bps.prepare(flyer, table_info, wait=False, group="prep")
57
+ yield from bps.wait(group="prep")
@@ -0,0 +1,73 @@
1
+ from abc import abstractmethod
2
+ from typing import Dict, Protocol, runtime_checkable
3
+
4
+ from bluesky.protocols import Descriptor, HasName, Reading
5
+
6
+
7
+ @runtime_checkable
8
+ class AsyncReadable(HasName, Protocol):
9
+ @abstractmethod
10
+ async def read(self) -> Dict[str, Reading]:
11
+ """Return an OrderedDict mapping string field name(s) to dictionaries
12
+ of values and timestamps and optional per-point metadata.
13
+
14
+ Example return value:
15
+
16
+ .. code-block:: python
17
+
18
+ OrderedDict(('channel1',
19
+ {'value': 5, 'timestamp': 1472493713.271991}),
20
+ ('channel2',
21
+ {'value': 16, 'timestamp': 1472493713.539238}))
22
+ """
23
+ ...
24
+
25
+ @abstractmethod
26
+ async def describe(self) -> Dict[str, Descriptor]:
27
+ """Return an OrderedDict with exactly the same keys as the ``read``
28
+ method, here mapped to per-scan metadata about each field.
29
+
30
+ Example return value:
31
+
32
+ .. code-block:: python
33
+
34
+ OrderedDict(('channel1',
35
+ {'source': 'XF23-ID:SOME_PV_NAME',
36
+ 'dtype': 'number',
37
+ 'shape': []}),
38
+ ('channel2',
39
+ {'source': 'XF23-ID:SOME_PV_NAME',
40
+ 'dtype': 'number',
41
+ 'shape': []}))
42
+ """
43
+ ...
44
+
45
+
46
+ @runtime_checkable
47
+ class AsyncConfigurable(Protocol):
48
+ @abstractmethod
49
+ async def read_configuration(self) -> Dict[str, Reading]:
50
+ """Same API as ``read`` but for slow-changing fields related to configuration.
51
+ e.g., exposure time. These will typically be read only once per run.
52
+ """
53
+ ...
54
+
55
+ @abstractmethod
56
+ async def describe_configuration(self) -> Dict[str, Descriptor]:
57
+ """Same API as ``describe``, but corresponding to the keys in
58
+ ``read_configuration``.
59
+ """
60
+ ...
61
+
62
+
63
+ @runtime_checkable
64
+ class AsyncPausable(Protocol):
65
+ @abstractmethod
66
+ async def pause(self) -> None:
67
+ """Perform device-specific work when the RunEngine pauses."""
68
+ ...
69
+
70
+ @abstractmethod
71
+ async def resume(self) -> None:
72
+ """Perform device-specific work when the RunEngine resumes after a pause."""
73
+ ...
@@ -0,0 +1,11 @@
1
+ from .pattern_generator import PatternGenerator
2
+ from .sim_pattern_detector_control import SimPatternDetectorControl
3
+ from .sim_pattern_detector_writer import SimPatternDetectorWriter
4
+ from .sim_pattern_generator import SimPatternDetector
5
+
6
+ __all__ = [
7
+ "PatternGenerator",
8
+ "SimPatternDetectorControl",
9
+ "SimPatternDetectorWriter",
10
+ "SimPatternDetector",
11
+ ]
@@ -0,0 +1,3 @@
1
+ from .sim_motor import SimMotor
2
+
3
+ __all__ = ["SimMotor"]
@@ -0,0 +1,116 @@
1
+ import asyncio
2
+ import time
3
+ from typing import Callable, List, Optional
4
+
5
+ from bluesky.protocols import Movable, Stoppable
6
+
7
+ from ophyd_async.core import StandardReadable
8
+ from ophyd_async.core.async_status import AsyncStatus
9
+ from ophyd_async.core.signal import soft_signal_r_and_backend, soft_signal_rw
10
+
11
+
12
+ class SimMotor(StandardReadable, Movable, Stoppable):
13
+ def __init__(self, name="", instant=True) -> None:
14
+ """
15
+ Simulated motor device
16
+
17
+ args:
18
+ - prefix: str: Signal names prefix
19
+ - name: str: name of device
20
+ - instant: bool: whether to move instantly, or with a delay
21
+ """
22
+ self._instant = instant
23
+ self._move_task: Optional[asyncio.Task] = None
24
+
25
+ # Define some signals
26
+ self.user_setpoint = soft_signal_rw(float, 0)
27
+ self.user_readback, self._user_readback = soft_signal_r_and_backend(float, 0)
28
+ self.velocity = soft_signal_rw(float, 1.0)
29
+ self.egu = soft_signal_rw(float, "mm")
30
+
31
+ # Set name and signals for read() and read_configuration()
32
+ self.set_readable_signals(
33
+ read=[self.user_readback],
34
+ config=[self.velocity, self.egu],
35
+ )
36
+ super().__init__(name=name)
37
+
38
+ # Whether set() should complete successfully or not
39
+ self._set_success = True
40
+
41
+ def stop(self, success=False):
42
+ """
43
+ Stop the motor if it is moving
44
+ """
45
+ if self._move_task:
46
+ self._move_task.cancel()
47
+ self._move_task = None
48
+
49
+ self._set_success = success
50
+
51
+ def set(self, new_position: float, timeout: Optional[float] = None) -> AsyncStatus: # noqa: F821
52
+ """
53
+ Asynchronously move the motor to a new position.
54
+ """
55
+ watchers: List[Callable] = []
56
+ coro = asyncio.wait_for(self._move(new_position, watchers), timeout=timeout)
57
+ return AsyncStatus(coro, watchers)
58
+
59
+ async def _move(self, new_position: float, watchers: List[Callable] = []):
60
+ """
61
+ Start the motor moving to a new position.
62
+
63
+ If the motor is already moving, it will stop first.
64
+ If this is an instant motor the move will be instantaneous.
65
+ """
66
+ self.stop()
67
+ start = time.monotonic()
68
+
69
+ current_position = await self.user_readback.get_value()
70
+ distance = abs(new_position - current_position)
71
+ travel_time = 0 if self._instant else distance / await self.velocity.get_value()
72
+
73
+ old_position, units = await asyncio.gather(
74
+ self.user_setpoint.get_value(),
75
+ self.egu.get_value(),
76
+ )
77
+
78
+ async def update_position():
79
+ while True:
80
+ time_elapsed = round(time.monotonic() - start, 2)
81
+
82
+ # update position based on time elapsed
83
+ if time_elapsed >= travel_time:
84
+ # successfully reached our target position
85
+ await self._user_readback.put(new_position)
86
+ self._set_success = True
87
+ break
88
+ else:
89
+ current_position = (
90
+ old_position + distance * time_elapsed / travel_time
91
+ )
92
+
93
+ await self._user_readback.put(current_position)
94
+
95
+ # notify watchers of the new position
96
+ for watcher in watchers:
97
+ watcher(
98
+ name=self.name,
99
+ current=current_position,
100
+ initial=old_position,
101
+ target=new_position,
102
+ unit=units,
103
+ time_elapsed=time.monotonic() - start,
104
+ )
105
+
106
+ # 10hz update loop
107
+ await asyncio.sleep(0.1)
108
+
109
+ # set up a task that updates the motor position at 10hz
110
+ self._move_task = asyncio.create_task(update_position())
111
+
112
+ try:
113
+ await self._move_task
114
+ finally:
115
+ if not self._set_success:
116
+ raise RuntimeError("Motor was stopped")