Controlling digital twins

Remote control with EPICS

Files in examples/withRaycing/18_EPICS.

This example demonstrates how to control a virtual xrt beamline remotely with EPICS control tools. All three scripts use the same xrtQook template, 1crystal.xml, featured in the xrtQook tutorial.

Note

Run this example using the supplied Python scripts. It contains detector and EPICS features not fully supported by xrtQook. Editing the template or generating code in xrtQook is not the intended workflow. Launching xrtGlow through the supplied script remains supported.

EPICS provides a framework for distributed control systems used at large scientific facilities, such as particle accelerators and synchrotron light sources. It lets applications monitor and control equipment over a network through process variables (PVs). In a real system, a PV typically represents one physical property, such as a device voltage or a motor position.

xrt maps individual properties of beamline elements to PVs, including shape, position and orientation parameters. Not every property of an xrt object is controllable through EPICS. The complete list of available PVs is printed in the terminal at script startup. In the xrtGlow inspector widget, active PV names are also displayed alongside their corresponding properties.

xrt DetectorScreen objects calculate histograms during expose() when withHistogram=True. EPICS propagation enables this option and publishes the detector image to a waveform PV, BL:screen01:image. The waveform contains the flattened two-dimensional histogram; its dimensions can be read or edited through BL:screen01:histShape:width and BL:screen01:histShape:height.

Make sure the EPICS dependencies are installed. We use pythonSoftIOC and PyEpics for the server and client components, respectively.

pip install softioc pyepics

0. Generate Phoebus widgets

This step is optional. You need a working installation of the CS-Studio/Phoebus framework to open the widget files generated by the script 1cr_0_generate_bob.py directly from the template.

The files are written to bob/ and use the PV prefix BL, compatible with the headless beamline example.

1. Run the headless beamline

1cr_1_run_headless.py loads the same template and exposes PV controls without opening a GUI.

Start the script and leave it running.

The IOC uses prefix BL and serves Channel Access locally. In a separate Python console on the same computer, configure the local connection before importing PyEpics’ caget and caput functions:

import os
os.environ["EPICS_CA_ADDR_LIST"] = "127.0.0.1"
os.environ["EPICS_CA_AUTO_ADDR_LIST"] = "NO"

from epics import caget, caput

Trigger a propagation to resolve the template’s automatic crystal pitch and screen position:

caput("BL:Acquire", 1, wait=True)

Once caget("BL:AcquireStatus") returns to zero after that propagation, read the crystal pitch and the screen’s vertical position:

caget("BL:oe01:pitch_RBV")          # radians
caget("BL:screen01:center:z_RBV")   # mm

The _RBV suffix selects the current numerical readback, including values resolved from auto. The headless beamline process can be stopped with Ctrl+C or by closing the terminal.

2. Control the beamline in xrtGlow

1cr_2_run_glow.py opens the same template in xrtGlow and starts its EPICS controls with the distinct prefix VBL. The headless BL IOC and the xrtGlow VBL IOC are independent and can run at the same time as long as the full PV names are unique across both IOCs. Each controls its own beamline instance, so changes made through either prefix affect only the corresponding instance.

The script’s two main calls are:

beamLine = raycing.BeamLine(fileName=fileName)
beamLine.glow(epicsPrefix="VBL")

Running the script opens an xrtGlow window. Once the initial propagation finishes, use the same client console from the previous step to read the same properties with the VBL prefix:

caget("VBL:oe01:pitch_RBV")         # radians
screen_z = caget("VBL:screen01:center:z_RBV")  # mm

Now change the crystal energy to 10005 eV, within the source’s energy range, and move the screen up by 1 mm:

caput("VBL:oe01:ENERGY", 10005.0, wait=True)
caput("VBL:screen01:center:z", screen_z + 1.0, wait=True)
 xrtGlow screen01 inspector showing active PV names in the EPICS PV column.

 xrtGlow screen01 inspector showing active PV names in the EPICS PV column.

The ENERGY control is available only for crystal optics and double crystal monochromators (DCMs); it adjusts their Bragg angle. With automatic updates enabled, these writes trigger propagation and the result appears in xrtGlow. wait=True waits for the PV write to complete; propagation and updated readbacks arrive afterward.

You can use the xrtGlow inspector to check active PVs for an element. The EPICS PV column lists the PV names alongside their corresponding properties, as shown on the right for screen01.