Skip to content

Commit 870a378

Browse files
authored
Stability updates to SpectrumAnalyzer (#61)
1 parent 7f1d8e7 commit 870a378

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

startup/30-detectors.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,21 @@ class SpectrumAnalyzer(Device, WritesStreamAssets, Readable):
203203
acquire = Cpt(EpicsSignal, "ACQUIRE")
204204
acquisition_status = Cpt(EpicsSignalRO, "ACQ:STATUS")
205205

206+
# Detector control
207+
det_off = Cpt(EpicsSignal, "DET:OFF")
208+
det_max_count = Cpt(EpicsSignalRO, "DET:MAX_COUNT")
209+
det_max_count_threshold = Cpt(EpicsSignal, "DET:MAX_COUNT_THRESH")
210+
det_max_count_exceeded = Cpt(EpicsSignal, "DET:MAX_COUNT_EXCEEDED")
211+
206212
# Status and info
207213
connection_status = Cpt(EpicsSignalRO, "SYS:CONNECTED")
208214
last_sync = Cpt(EpicsSignalRO, "SYS:LAST_SYNC")
209215
sync = Cpt(EpicsSignal, "SYS:SYNC")
210216

211217
# File writing
212218
file_capture = Cpt(EpicsSignal, "FILE:CAPTURE")
213-
file_name = Cpt(EpicsSignal, "FILE:NAME")
214-
file_path = Cpt(EpicsSignal, "FILE:PATH")
219+
file_name = Cpt(EpicsSignal, "FILE:NAME", string=True)
220+
file_path = Cpt(EpicsSignal, "FILE:PATH", string=True)
215221
file_status = Cpt(EpicsSignalRO, "FILE:STATUS")
216222
num_captured = Cpt(EpicsSignalRO, "FILE:NUM_CAPTURED")
217223
num_processed = Cpt(EpicsSignalRO, "FILE:NUM_PROCESSED")
@@ -288,10 +294,13 @@ def stage(self):
288294
raise RuntimeError(
289295
"File capture must be off to stage the detector, otherwise the file will be corrupted"
290296
)
291-
292-
if self.acquire.get(as_string=True) == "RUNNING":
293-
self.stage_sigs.update([(self.acquire, 0)])
294-
self.stage_sigs.update([(self.file_capture, 1)])
297+
if self.state.get(as_string=True) == "RUNNING":
298+
self.acquire.set(0).wait(3.0)
299+
self.stage_sigs.update(
300+
[
301+
(self.file_capture, 1),
302+
]
303+
)
295304

296305
path = _convert_path_to_posix(Path(self.file_path.get()))
297306
file_name = Path(self.file_name.get())
@@ -306,9 +315,19 @@ def _stage_changed(self, value=None, old_value=None, **kwargs):
306315
if self._status is None:
307316
return
308317
if value == "STANDBY" and old_value == "RUNNING":
318+
# Settle time for the detector to transition properly
309319
ttime.sleep(1.0)
310-
self._status.set_finished()
311-
self._index += 1
320+
if self.det_max_count_exceeded.get():
321+
max_count = self.det_max_count.get()
322+
max_count_threshold = self.det_max_count_threshold.get()
323+
self._status.set_exception(
324+
RuntimeError(
325+
f"Max count safety limit exceeded: {max_count} > {max_count_threshold}"
326+
)
327+
)
328+
else:
329+
self._status.set_finished()
330+
self._index += 1
312331
self._status = None
313332

314333
def trigger(self):
@@ -317,8 +336,9 @@ def trigger(self):
317336
"This detector is not ready to trigger."
318337
"Call the stage() method before triggering."
319338
)
320-
self.acquire.set(1)
339+
321340
self._status = Status()
341+
self.acquire.set(1).wait(1.0)
322342
return self._status
323343

324344
def describe(self) -> dict[str, DataKey]:
@@ -366,10 +386,14 @@ def collect_asset_docs(
366386
yield "stream_datum", self._composer.compose_stream_datum(indices)
367387

368388
def unstage(self):
389+
if self.state.get(as_string=True) == "RUNNING":
390+
self.acquire.set(0).wait(3.0)
391+
self.det_off.set(1).wait(3.0)
369392
super().unstage()
370393
self.state.unsubscribe(self._stage_changed)
371394
self._composer = None
372395

396+
373397
mbs = SpectrumAnalyzer("XF:21ID1-ES{A1Soft}", name="mbs")
374398

375399
Diag1_CamH = MyDetector("XF:21IDA-BI{Diag:1-Cam:H}", name="Diag1_CamH")

0 commit comments

Comments
 (0)