From f643b50f5c94b06247cebc4c07c9e85d6b086ad0 Mon Sep 17 00:00:00 2001 From: gac-x10da Date: Mon, 23 Jun 2025 15:27:12 +0200 Subject: [PATCH] use pyepics automonitor and callbacks --- superxas_bec/devices/test_script_pyepics.py | 89 +++++++++++++-------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/superxas_bec/devices/test_script_pyepics.py b/superxas_bec/devices/test_script_pyepics.py index 095038a..3dc9926 100644 --- a/superxas_bec/devices/test_script_pyepics.py +++ b/superxas_bec/devices/test_script_pyepics.py @@ -2,50 +2,73 @@ # PyEpics version of the script import time +import threading import epics -# Define PVs -I0 = epics.PV("X10DA-ES1-SAI_01:MEAN") -Trigger = epics.PV("X10DA-ES1:SMPL") -TriggerDone = epics.PV("X10DA-ES1:SMPL-DONE") +def main(): + # Define PVs + I0 = epics.PV("X10DA-ES1-SAI_01:MEAN") + Trigger = epics.PV("X10DA-ES1:SMPL") + TriggerDone = epics.PV("X10DA-ES1:SMPL-DONE", auto_monitor=True) -XMAPStart = epics.PV("X10DA-SITORO:EraseStart") -XMAPStop = epics.PV("X10DA-SITORO:StopAll") -XMAPAcquiring = epics.PV("X10DA-SITORO:Acquiring") -XMAPICR = epics.PV("X10DA-SITORO:dxp1:InputCountRate") -XMAPOCR = epics.PV("X10DA-SITORO:dxp1:OutputCountRate") -XMAPROI = epics.PV("X10DA-SITORO:mca1.R0") + XMAPStart = epics.PV("X10DA-SITORO:EraseStart") + XMAPStop = epics.PV("X10DA-SITORO:StopAll") + XMAPAcquiring = epics.PV("X10DA-SITORO:Acquiring", auto_monitor=True) + XMAPICR = epics.PV("X10DA-SITORO:dxp1:InputCountRate") + XMAPOCR = epics.PV("X10DA-SITORO:dxp1:OutputCountRate") + XMAPROI = epics.PV("X10DA-SITORO:mca1.R0") -# Wait for connections (optional) -for pv in [I0, Trigger, TriggerDone, XMAPStart, XMAPStop, XMAPAcquiring, XMAPICR, XMAPOCR, XMAPROI]: - pv.wait_for_connection(timeout=2) + + acquire_started = threading.Event() + acquire_stopped = threading.Event() + trigger_done = threading.Event() -# Measurement loop -for i in range(25): - start_time = time.time() - XMAPStart.put(1, wait=True) + def acquiring_cb(pvname=None, value=None, **kwargs): + if value == 1: + acquire_started.set() + elif value == 0: + acquire_stopped.set() - while XMAPAcquiring.get() == 0: - time.sleep(0.05) + def trigger_done_cb(pvname=None, value=None, **kwargs): + if value == 1: + trigger_done.set() - Trigger.put(1, wait=True) - time.sleep(0.2) + # Wait for connections (optional) + for pv in [I0, Trigger, TriggerDone, XMAPStart, XMAPStop, XMAPAcquiring, XMAPICR, XMAPOCR, XMAPROI]: + pv.wait_for_connection(timeout=2) - while TriggerDone.get() == 0: - time.sleep(0.05) + TriggerDone.add_callback(trigger_done_cb) + XMAPAcquiring.add_callback(acquiring_cb) - XMAPStop.put(1, wait=True) + # Measurement loop + for i in range(25): + acquire_started.clear() + acquire_stopped.clear() + trigger_done.clear() - while XMAPAcquiring.get() == 1: - time.sleep(0.05) + start_time = time.time() + XMAPStart.put(1, wait=False) - time.sleep(0.1) + acquire_started.wait(timeout=5) - i0 = I0.get() - icr = XMAPICR.get() - ocr = XMAPOCR.get() - roi = XMAPROI.get() - end_time = time.time() + Trigger.put(1, wait=False) - print(f"time={end_time - start_time:.4f}", i0, icr, ocr, roi) + trigger_done.wait(3) + + XMAPStop.put(1, wait=True) + + acquire_stopped.wait(5) + + time.sleep(0.1) + + i0 = I0.get() + icr = XMAPICR.get() + ocr = XMAPOCR.get() + roi = XMAPROI.get() + end_time = time.time() + + print(f"time={end_time - start_time:.4f}", i0, icr, ocr, roi) + +if __name__=="__main__": + main()