Skip to content

Commit

Permalink
Updated Firefly to use Haven's new instrument loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
canismarko committed Oct 6, 2024
1 parent a3dc79e commit 8657c2e
Show file tree
Hide file tree
Showing 22 changed files with 61 additions and 163 deletions.
17 changes: 15 additions & 2 deletions src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def blade_slits(sim_registry):
"""A fake set of slits using the 4-blade setup."""
FakeSlits = make_fake_device(BladeSlits)
slits = FakeSlits(prefix="255idc:KB_slits", name="kb_slits", labels={"slits"})
sim_registry.register(slits)
return slits


Expand Down Expand Up @@ -116,6 +117,7 @@ def beamline_manager(sim_registry):
manager = FakeManager(
prefix="companionCube:", name="companion_cube", labels={"beamline_manager"}
)
sim_registry.register(manager)
return manager


Expand All @@ -132,13 +134,15 @@ def aperture_slits(sim_registry):
diagonal_motor="m2",
labels={"slits"},
)
sim_registry.register(slits)
return slits


@pytest.fixture()
def sim_camera(sim_registry):
FakeCamera = make_fake_device(AravisDetector)
camera = FakeCamera(name="s255id-gige-A", labels={"cameras", "area_detectors"})
sim_registry.register(camera)
camera.pva.pv_name._readback = "255idSimDet:Pva1:Image"
# Registry with the simulated registry
yield camera
Expand All @@ -158,6 +162,7 @@ def dxp(sim_registry):
FakeDXP = make_fake_device(DxpVortex)
vortex = FakeDXP(name="vortex_me4", labels={"xrf_detectors", "detectors"})
# vortex.net_cdf.dimensions.set([1477326, 1, 1])
sim_registry.register(vortex)
yield vortex


Expand All @@ -174,25 +179,29 @@ class Xspress3Vortex(Xspress3Detector):
def xspress(sim_registry):
FakeXspress = make_fake_device(Xspress3Vortex)
vortex = FakeXspress(name="vortex_me4", labels={"xrf_detectors"})
sim_registry.register(vortex)
yield vortex


@pytest.fixture()
def robot():
def robot(sim_registry):
RobotClass = make_fake_device(Robot)
robot = RobotClass(name="robotA", prefix="255idA:")
sim_registry.register(robot)
return robot


@pytest.fixture()
def mono(sim_registry):
mono = instantiate_fake_device(Monochromator, name="monochromator")
sim_registry.register(mono)
yield mono


@pytest.fixture()
def aps(sim_registry):
aps = instantiate_fake_device(ApsMachine, name="APS")
sim_registry.register(aps)
yield aps


Expand All @@ -214,12 +223,14 @@ def __new__(cls, *args, **kwargs):

FakeBank = make_fake_device(ShutterBank)
bank = FakeBank(prefix="255id:pfcu4:", name="xia_filter_bank", shutters=[[3, 4]])
sim_registry.register(bank)
yield bank


@pytest.fixture()
def xia_shutter(xia_shutter_bank):
yield xia_shutter_bank.shutters.shutter_0
shutter = xia_shutter_bank.shutters.shutter_0
yield shutter


@pytest.fixture()
Expand All @@ -233,6 +244,7 @@ def shutters(sim_registry):
FakeShutter(name="Shutter A", **kw),
FakeShutter(name="Shutter C", **kw),
]
[sim_registry.register(s) for s in shutters]
yield shutters


Expand All @@ -246,6 +258,7 @@ def filters(sim_registry):
FakeFilter(name="Filter A", prefix="filter1", **kw),
FakeFilter(name="Filter B", prefix="filter2", **kw),
]
[sim_registry.register(f) for f in filters]
return filters


Expand Down
4 changes: 2 additions & 2 deletions src/firefly/area_detector_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pydm
import pyqtgraph

import haven
from haven import beamline
from firefly import display

np.set_printoptions(threshold=sys.maxsize)
Expand All @@ -25,7 +25,7 @@ class AreaDetectorViewerDisplay(display.FireflyDisplay):

def customize_device(self):
device_name = name = self.macros()["AD"]
device = haven.registry.find(device_name)
device = beamline.registry.find(device_name)
self.device = device
img_pv = device.pva.pv_name.get(as_string=True)
addr = f"pva://{img_pv}"
Expand Down
14 changes: 7 additions & 7 deletions src/firefly/bss.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from qtpy.QtCore import Signal
from qtpy.QtGui import QStandardItem, QStandardItemModel

import haven
from haven import beamline, load_config
from firefly import display

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -51,10 +51,10 @@ def customize_ui(self):
self.ui.refresh_models_button.setIcon(qta.icon("fa5s.sync"))

def customize_device(self):
self._device = haven.registry.find("beamline_manager")
self._device = beamline.registry.find("beamline_manager")

def proposals(self):
config = haven.load_config()
config = load_config()
proposals = []
beamline = self._device.bss.proposal.beamline_name.get()
cycle = self._device.bss.esaf.aps_cycle.get()
Expand All @@ -79,7 +79,7 @@ def proposals(self):
return proposals

def esafs(self):
config = haven.load_config()
config = load_config()
esafs_ = []
beamline = self._device.bss.proposal.beamline_name.get()
cycle = self._device.bss.esaf.aps_cycle.get()
Expand All @@ -104,7 +104,7 @@ def esafs(self):
return esafs_

def load_models(self):
config = haven.load_config()
config = load_config()
# Create proposal model object
col_names = self._proposal_col_names
self.proposal_model = QStandardItemModel()
Expand Down Expand Up @@ -143,7 +143,7 @@ def select_proposal(self, current, previous):
def update_proposal(self):
new_id = self._proposal_id
# Change the proposal in the EPICS record
bss = haven.registry.find("beamline_manager.bss")
bss = beamline.registry.find("beamline_manager.bss")
bss.proposal.proposal_id.set(new_id).wait()
# Notify any interested parties that the proposal has been changed
self.proposal_changed.emit()
Expand All @@ -160,7 +160,7 @@ def select_esaf(self, current, previous):
def update_esaf(self):
new_id = self._esaf_id
# Change the esaf in the EPICS record
bss = haven.registry.find("beamline_manager.bss")
bss = beamline.registry.find("beamline_manager.bss")
bss.wait_for_connection()
bss.esaf.esaf_id.set(new_id).wait(timeout=5)
# Notify any interested parties that the esaf has been changed
Expand Down
1 change: 1 addition & 0 deletions src/firefly/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def sync_motors(sim_registry):
for name in motor_names:
this_motor = make_fake_device(HavenMotor)(name=name, labels={"motors"})
motors.append(this_motor)
sim_registry.register(this_motor)
return motors


Expand Down
19 changes: 6 additions & 13 deletions src/firefly/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import pydm
import pyqtgraph as pg
import qtawesome as qta
from ophydregistry import Registry
from qasync import asyncSlot
from qtpy import QtCore, QtWidgets
from qtpy.QtCore import Signal
from qtpy.QtGui import QIcon, QKeySequence
from qtpy.QtWidgets import QAction
from ophydregistry import Registry

from haven import load_config
from haven import load_instrument as load_haven_instrument
from haven import registry
from haven import beamline
from haven.device import titelize
from haven.exceptions import ComponentNotFound, InvalidConfiguration

Expand Down Expand Up @@ -72,7 +71,7 @@ def __init__(self, parent=None, display="status", use_main_window=False):
self.actions = ActionsRegistry()
self.windows = OrderedDict()
self.queue_re_state_changed.connect(self.enable_queue_controls)
self.registry = registry
self.registry = beamline.registry

def _setup_window_action(
self, action_name: str, text: str, slot: QtCore.Slot, shortcut=None, icon=None
Expand All @@ -90,7 +89,7 @@ def _setup_window_action(

def reload_instrument(self, load_instrument=True):
"""(Re)load all the instrument devices."""
load_haven_instrument(registry=self.registry)
await beamline.load()
self.registry_changed.emit(self.registry)

async def setup_instrument(self, load_instrument=True):
Expand All @@ -110,14 +109,8 @@ async def setup_instrument(self, load_instrument=True):
"""
if load_instrument:
await load_haven_instrument(registry=self.registry)
self.registry_changed.emit(self.registry)
# Fake device for testing
from ophyd_async.epics.motor import Motor

sim_async_motor = Motor("255idcVME", name="sim_async_motor")
await sim_async_motor.connect(mock=True)
registry.register(sim_async_motor, labels={"motors", "extra_motors"})
await beamline.load()
self.registry_changed.emit(beamline.registry)
# Make actions for launching other windows
self.setup_window_actions()
# Actions for controlling the bluesky run engine
Expand Down
4 changes: 2 additions & 2 deletions src/firefly/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from qtpy import QtWidgets
from qtpy.QtCore import Signal, Slot

from haven import registry
from haven import beamline


class FireflyDisplay(Display):
Expand Down Expand Up @@ -100,7 +100,7 @@ def customize_device(self):
# Retrieve the device
device = self.macros().get("DEVICE")
if device is not None:
device = registry.find(device)
device = beamline.registry.find(device)
self.device = device
return device

Expand Down
65 changes: 2 additions & 63 deletions src/firefly/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from xraydb.xraydb import XrayDB

from firefly import display
from haven import load_config, registry
from haven import load_config, beamline

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -49,10 +49,6 @@ def __init__(self, *args, **kwargs):


class EnergyDisplay(display.FireflyDisplay):
caqtdm_mono_ui_file = "/net/s25data/xorApps/ui/DCMControlCenter.ui"
caqtdm_id_ui_file = (
"/net/s25data/xorApps/epics/synApps_6_2/ioc/25ida/25idaApp/op/ui/IDControl.ui"
)
stylesheet_danger = (
"background: rgb(220, 53, 69); color: white; border-color: rgb(220, 53, 69)"
)
Expand All @@ -66,68 +62,11 @@ def __init__(self, args=None, macros={}, **kwargs):

def customize_device(self):
try:
self.energy_positioner = registry.find("energy")
self.energy_positioner = beamline.registry.find("energy")
except ComponentNotFound:
warnings.warn("Could not find energy positioner.")
log.warning("Could not find energy positioner.")

def prepare_caqtdm_actions(self):
"""Create QActions for opening mono/ID caQtDM panels.
Creates two actions, one for the mono and one for the
insertion device.
"""
self.caqtdm_actions = []
# Create an action for launching the mono caQtDM file
action = QtWidgets.QAction(self)
action.setObjectName("launch_mono_caqtdm_action")
action.setText("Mono caQtDM")
action.triggered.connect(self.launch_mono_caqtdm)
action.setIcon(qta.icon("fa5s.wrench"))
action.setToolTip("Launch the caQtDM panel for the monochromator.")
self.caqtdm_actions.append(action)
# Create an action for launching the ID caQtDM file
action = QtWidgets.QAction(self)
action.setObjectName("launch_id_caqtdm_action")
action.setText("ID caQtDM")
action.triggered.connect(self.launch_id_caqtdm)
action.setIcon(qta.icon("fa5s.wrench"))
action.setToolTip("Launch the caQtDM panel for the insertion device.")
self.caqtdm_actions.append(action)

def launch_mono_caqtdm(self):
config = load_config()
mono = self.energy_positioner.monochromator
ID = self.energy_positioner.undulator
prefix = mono.prefix
caqtdm_macros = {
"P": prefix,
"MONO": config["monochromator"]["ioc_branch"],
"BRAGG": mono.bragg.prefix.replace(prefix, ""),
"GAP": mono.gap.prefix.replace(prefix, ""),
"ENERGY": mono.energy.prefix.replace(prefix, ""),
"OFFSET": mono.offset.prefix.replace(prefix, ""),
"IDENERGY": ID.energy.prefix,
}
self.launch_caqtdm(macros=caqtdm_macros, ui_file=self.caqtdm_mono_ui_file)

def launch_id_caqtdm(self):
"""Launch the pre-built caQtDM UI file for the ID."""
prefix = self.energy_positioner.undulator.prefix
# caQtDM doesn't expect the trailing ";"
prefix = prefix.rstrip(":")
# Strip leading "ID" from the ID IOC since caQtDM adds it
prefix = prefix.strip("ID")
caqtdm_macros = {
# No idea what "M", and "D" do, they're not in the UI
# file.
"ID": prefix,
"M": 2,
"D": 2,
}
self.launch_caqtdm(macros=caqtdm_macros, ui_file=self.caqtdm_id_ui_file)

def set_energy(self, *args, **kwargs):
energy = float(self.ui.target_energy_lineedit.text())
log.info(f"Setting new energy: {energy}")
Expand Down
4 changes: 2 additions & 2 deletions src/firefly/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydm.widgets import PyDMEmbeddedDisplay

from firefly import display
from haven import registry
from haven import beamline


class FiltersDisplay(display.FireflyDisplay):
Expand All @@ -14,7 +14,7 @@ def ui_filename(self):
return "filters.ui"

def customize_device(self):
filters = registry.findall(label="filters", allow_none=True)
filters = beamline.registry.findall(label="filters", allow_none=True)
self.filters = sorted(
filters, key=lambda dev: (dev.material.get(), dev.thickness.get())
)
Expand Down
Loading

0 comments on commit 8657c2e

Please sign in to comment.