Files
mx_bec/tests/unit/test_smargon.py
perl_dandx10sa d18c33f3b6
CI for mx_bec / e2e tests with pxi_bec (push) Failing after 1m4s
CI for mx_bec / e2e tests with pxii_bec (push) Failing after 1m17s
CI for mx_bec / e2e tests with pxiii_bec (push) Failing after 1m17s
CI for mx_bec / unit tests with pxiii_bec (push) Failing after 1m47s
CI for mx_bec / unit tests with pxii_bec (push) Failing after 1m55s
CI for mx_bec / unit tests with pxi_bec (push) Failing after 1m55s
AareBec task service (#2)
# MX task architecture: AareBec service, tasks, queues and widgets

Brings the AareBec task architecture into `mx_bec`

## Components

- **AareBec service** (`service.py`, `main.py`): custom BEC client that owns the task queue,
  the actor manager and the external API clients.
- **Task framework** (`tasks/core.py`): `@task` decorator and `TaskManager` with `Server` and
  `Client` roles. The server runs tasks one at a time and pauses the queue on failure; clients
  request tasks over Redis. `MxTaskQueue` works off the execution list.
- **MX tasks** (`tasks/mx_tasks.py`): sample mount, optical centering, raster and multi-raster
  scans, X-ray centering, rotation data collection, beamline state change and cleanup.
- **Automation actors** (`actors/`, `actor_manager.py`): actors that react to sequence
  milestones and queue the next task, each switchable through a Redis config value.
- **External clients**: `AareDaq` REST client (Kerberos token, baton handling), `JFJoch`
  raster client, `OpticalPrediction` (AareLC). The `AareDaq` client is to be replaced with native BEC
  implementations.
- **Devices** (`devices/`): Aerotech, Smargon (smargopolo), HTTP device base, simulated stages
  and sample changer, plus a demo config.
- **Widgets** (`bec_widgets/widgets/aare_bec/`): actor config, task queue, task panel, sample
  queue and sample spreadsheet display - for development and admin use.
- **Endpoints and messages** for task requests, the execution list, queue-change
  notifications and sequence state.

## Notes

- BEC-driven optical centering was removed; it will be reimplemented from scratch.
- The sample spreadsheet is copied between Redis instances by a hack in the service, to be
  replaced by the sample database connection module.
- CI installs from the MX Gitea package index (`jfjoch_client` is pre-release only).

---------

Co-authored-by: x10sa <x10sa@psi.ch>
Reviewed-on: #2
Co-authored-by: David Perl <david.perl@psi.ch>
2026-11-24 14:41:31 +01:00

107 lines
3.7 KiB
Python

"""Tests of the smargopolo Smargon device against the in-process mock server."""
import numpy as np
import pytest
import requests
from mock_smargopolo import AXES, MockSmargopolo, motors
from mx_bec.devices.smargopolo_smargon import Smargon
TOL = 0.01
@pytest.fixture(scope="module")
def server():
with MockSmargopolo() as srv:
yield srv
@pytest.fixture
def smargon(server):
for motor in motors.values():
motor.position = motor.target = 0.0
motor.moving = False
dev = Smargon(name="smargon", prefix=server.url)
dev.wait_for_connection()
yield dev
dev.controller._stop_monitor_readback_event.set()
dev.destroy()
def reference_nudge(x, y, z, phi_deg, chi_deg, omega_deg):
"""aarecommon.math.sample_geometry.SampleGeometry.smargon_nudge"""
phi, chi, omega = (np.radians(np.around(a, decimals=1)) for a in (phi_deg, chi_deg, omega_deg))
co, so = np.cos(omega), np.sin(omega)
cp, sp = np.cos(phi), np.sin(phi)
cc, sc = np.cos(chi), np.sin(chi)
return (
-z * co * sp - y * so * sp + x * cp * sc - y * co * cc * cp + z * so * cc * cp,
-z * co * cp - y * so * cp - x * sc * sp + y * co * cc * sp - z * so * cc * sp,
-x * cc - y * co * sc + z * so * sc,
)
def test_read_all_axes(smargon):
motors["SHX"].position = 1.5
motors["CHI"].position = 42.0
smargon.controller.manual_update()
reading = smargon.read()
assert set(reading) == {f"smargon_{ax}" for ax in ("x", "y", "z", "phi", "chi")}
assert reading["smargon_x"]["value"] == 1.5
assert reading["smargon_chi"]["value"] == 42.0
def test_set_moves_and_completes(smargon):
smargon.x.set(0.5).wait(timeout=2)
assert smargon.x.get() == pytest.approx(0.5, abs=TOL)
assert motors["SHX"].target == 0.5
def test_set_sends_only_that_axis(smargon):
before = {ax: motors[ax].target for ax in AXES}
smargon.phi.set(0.3).wait(timeout=2)
before["PHI"] = 0.3
assert {ax: motors[ax].target for ax in AXES} == before
def test_nudge_in_lab_frame_at_zero_angles(smargon):
# With omega = phi = chi = 0 a lab-frame x offset is a smargon -z move, y -> -x, z -> -y
smargon.nudge_in_lab_frame(0.2, 0.0, 0.0).wait(timeout=2)
assert smargon.z.get() == pytest.approx(-0.2, abs=TOL)
assert smargon.x.get() == pytest.approx(0.0, abs=TOL)
assert smargon.y.get() == pytest.approx(0.0, abs=TOL)
smargon.nudge_in_lab_frame(0.0, 0.2, 0.0).wait(timeout=2)
assert smargon.x.get() == pytest.approx(-0.2, abs=TOL)
@pytest.mark.parametrize(
"phi_deg, chi_deg, omega_deg", [(90.0, 0.0, 0.0), (0.0, 20.0, 0.0), (30.0, 20.0, 45.0)]
)
def test_nudge_matches_reference(smargon, phi_deg, chi_deg, omega_deg):
for ax, val in (("PHI", phi_deg), ("CHI", chi_deg)):
motors[ax].position = motors[ax].target = val
smargon.controller.manual_update()
# omega is the Aerotech rotation, which the smargon does not know: it is passed in
assert smargon._lab_frame_offset_to_smargon_nudge(1.0, 0.5, 0.25, omega_deg) == pytest.approx(
reference_nudge(1.0, 0.5, 0.25, phi_deg, chi_deg, omega_deg)
)
def test_stop_targets_current_position(smargon):
smargon.x.put(5.0)
assert motors["SHX"].moving
smargon.stop()
assert smargon.stopped
# the target was re-set to the (buffered) readback, not to 5.0, and nothing but axes was sent
assert motors["SHX"].target == pytest.approx(smargon.x.get(), abs=0.1)
assert motors["SHX"].target != 5.0
def test_server_rejects_unknown_axis(server):
resp = requests.put(f"{server.url}/targetSCS", params={"__timestamp": 1.0})
assert resp.status_code == 404
def test_user_access_is_a_list():
assert Smargon.USER_ACCESS == ["nudge_in_lab_frame"]