feat(smargon): drivable O-vector motors; stop leaves the O-vector alone #355

Open
menzel wants to merge 4 commits from feat/smargon into main
pull from: feat/smargon
10 changed files with 207 additions and 13 deletions

No files matched your search

+30 -1
View File
@@ -6,7 +6,7 @@
# host/port so they share the controller.
#
# The motor NAME (the top-level key: sgx, sgchi, ...) is yours to choose. The
# `scs_axis` field binds it to the fixed smargopolo axis (SHX/SHY/SHZ/CHI/PHI/OMEGA).
# `scs_axis` field binds it to the fixed smargopolo axis (SHX/SHY/SHZ/CHI/PHI/OMEGA/OX/OY/OZ).
#
# Referencing is a deliberate operator action: after loading, run once
# dev.smargon.reference() # (or dev.sgx.controller.reference())
@@ -75,6 +75,35 @@ sgphi:
onFailure: retry
readoutPriority: baseline
sgox:
description: SmarGon OX
deviceClass: csaxs_bec.devices.smargon.positioner.SmargopoloMotor
deviceConfig: {scs_axis: OX, host: tt-smargopolo, port: 3000, limits: [-2, 2], tolerance: 0.001}
deviceTags: [goniometer, smargon]
enabled: true
onFailure: retry
readoutPriority: baseline
sgoy:
description: SmarGon OY
deviceClass: csaxs_bec.devices.smargon.positioner.SmargopoloMotor
deviceConfig: {scs_axis: OY, host: tt-smargopolo, port: 3000, limits: [-2, 2], tolerance: 0.001}
deviceTags: [goniometer, smargon]
enabled: true
onFailure: retry
readoutPriority: baseline
sgoz:
description: SmarGon OZ
deviceClass: csaxs_bec.devices.smargon.positioner.SmargopoloMotor
deviceConfig: {scs_axis: OZ, host: tt-smargopolo, port: 3000, limits: [-2, 2], tolerance: 0.001}
deviceTags: [goniometer, smargon]
enabled: true
onFailure: retry
readoutPriority: baseline
# --- Optional OMEGA rotation drive (only on builds that have it) -------------------
# sgomega:
# description: SmarGon OMEGA (rotation drive)
@@ -1,6 +1,7 @@
# Self-contained laptop test session for the SmarGon device (SIMULATION).
#
# Per-axis motors (dev.sgx, dev.sgy, dev.sgz, dev.sgchi, dev.sgphi, dev.sgomega) +
# Per-axis motors (dev.sgx, dev.sgy, dev.sgz, dev.sgchi, dev.sgphi, dev.sgox/sgoy/sgoz,
# optional dev.sgomega) +
# a `smargon` coordinator, all sharing one simulated controller, plus a sim monitor
# so scans record data. No smargopolo server / hardware needed.
#
@@ -102,6 +103,36 @@ sgphi:
onFailure: retry
readoutPriority: baseline
# --- O-vector (SCS offset vector OX/OY/OZ, mm) ------------------------------------
# Drivable like any SCS axis and recorded in the baseline. No soft limits by default
# (smargopolo enforces the real ones) -- add `limits: [low, high]` per experiment.
sgox:
description: SmarGon OX (offset vector, sim)
deviceClass: csaxs_bec.devices.smargon.positioner.SmargopoloMotor
deviceConfig: {scs_axis: OX, simulation: true, gradual_motion: true, tolerance: 0.001}
deviceTags: [goniometer, smargon, simulation]
enabled: true
onFailure: retry
readoutPriority: baseline
sgoy:
description: SmarGon OY (offset vector, sim)
deviceClass: csaxs_bec.devices.smargon.positioner.SmargopoloMotor
deviceConfig: {scs_axis: OY, simulation: true, gradual_motion: true, tolerance: 0.001}
deviceTags: [goniometer, smargon, simulation]
enabled: true
onFailure: retry
readoutPriority: baseline
sgoz:
description: SmarGon OZ (offset vector, sim)
deviceClass: csaxs_bec.devices.smargon.positioner.SmargopoloMotor
deviceConfig: {scs_axis: OZ, simulation: true, gradual_motion: true, tolerance: 0.001}
deviceTags: [goniometer, smargon, simulation]
enabled: true
onFailure: retry
readoutPriority: baseline
# sgomega:
# description: SmarGon OMEGA (sim)
# deviceClass: csaxs_bec.devices.smargon.positioner.SmargopoloMotor
+12 -2
View File
@@ -34,8 +34,13 @@ poll-to-tolerance moves), since both wrap a non-EPICS, poll-to-confirm HTTP back
- **One motor per axis**, top-level: the YAML key is the BEC name (**you choose it**:
`sgx`, `sgchi`, …); a `scs_axis:` field binds it to the fixed smargopolo axis name
(`SHX/SHY/SHZ/CHI/PHI/OMEGA`). OMEGA is optional hardware — add a motor entry only on
builds that have it.
(`SHX/SHY/SHZ/CHI/PHI/OMEGA/OX/OY/OZ`). OMEGA is optional hardware — add a motor entry
only on builds that have it.
- **O-vector motors** `sgox/sgoy/sgoz` drive the SCS offset vector `OX/OY/OZ` (all mm).
They are ordinary motors (`umv`, scans, baseline readout), so the offset is recorded
with every scan. The beamline config gives them `limits: [-2, 2]`; adjust per experiment.
`nudge_beamline(modify_offset=True)` (`/nudgeOBCS`) also changes them; their readback
catches up on the next read.
- **`dev.smargon`** — the coordinator: `reference()`, `get_mode()`, `is_ready()`,
`wait_until_ready()`, `move_scs(...)`, `nudge_beamline(...)`, `motor_positions()`,
`stop_all()`, `transport_info()`, plus read-only `mode`/`rosout` signals.
@@ -115,9 +120,14 @@ All run against `FakeTransport` — no hardware or server needed.
- **Stop semantics**: no explicit stop endpoint; we retarget axes to their readback
("Follow Target" halt). Confirm this is the intended way to abort a move.
The O-vector (`OX/OY/OZ`) is never retargeted by a stop: its readback fluctuates,
so writing it back would shift the offset on every stop (`STOP_EXCLUDED_AXES`).
- **Mid-move fault signal**: we infer faults from `Mode.ERROR`. Confirm an
out-of-range / coupled-limit violation reliably drives `/mode` to 99 (and that
`rosout` carries the reason), so the abort path is dependable.
- **PHI wrap**: the MX-lab reference client wraps PHI to `[-180,180)` before sending.
Confirm whether smargopolo wraps server-side, or whether the motor should.
- **O-vector semantics**: confirm what `OX/OY/OZ` represent physically, whether
changing them moves the sample (or only re-expresses `SH*`), sensible travel ranges,
and that `OZ` is in mm (the API example shows `OZ: 300.0`, the example client 180).
- *(resolved)* OMEGA is drivable via `/targetSCS` wherever it physically exists.
+1 -1
View File
@@ -1,7 +1,7 @@
"""Per-axis SmarGon motor -- a top-level BEC device for one SCS axis.
``SmargopoloMotor`` is an independent ophyd positioner (``Device`` +
``PositionerBase``): each SCS axis (SHX/SHY/SHZ/CHI/PHI/OMEGA) is its own top-level
``PositionerBase``): each SCS axis (SHX/SHY/SHZ/CHI/PHI/OMEGA/OX/OY/OZ) is its own top-level
BEC motor, exactly like the ``smaract`` motors. It talks to hardware only through a
shared :class:`~csaxs_bec.devices.smargon.controller.SmargopoloController` (singleton
per smargopolo server), so ``dev.sgx`` and ``dev.sgchi`` pointing at the same host
@@ -14,6 +14,7 @@ from .base import (
BeamlineAxis,
Mode,
SmargopoloInfo,
STOP_EXCLUDED_AXES,
SmargopoloTransport,
)
from .errors import (
@@ -36,6 +37,7 @@ __all__ = [
"MOTOR_CHANNELS",
"SmargopoloInfo",
"SmargopoloTransport",
"STOP_EXCLUDED_AXES",
"FakeTransport",
"RestTransport",
"SmargopoloTransportError",
+17 -2
View File
@@ -17,6 +17,8 @@ smargopolo runs the kinematics; we drive the *virtual* SmarGon Coordinate System
* **SCS** -- the user axes: ``SHX SHY SHZ OMEGA CHI PHI OX OY OZ`` (mm and deg).
Settable via ``/targetSCS`` (+ ``/targetSCS_rel``), read via ``/readbackSCS``.
``OX OY OZ`` are the O-vector (offset) components, a length in mm like ``SH*``;
``/nudgeOBCS`` shifts the same vector in the beamline frame.
* **MCS** -- the raw motor positions ``q1..q6``, read-only via ``/readbackMCS``
(diagnostics only; we never command them directly).
* **BCS** -- beamline-frame nudges ``BX BY BZ`` via ``/nudgeBCS`` / ``/nudgeOBCS``
@@ -98,9 +100,20 @@ AXIS_INFO: dict[Axis, AxisLimits] = {
Axis.PHI: AxisLimits(egu="deg"),
Axis.OX: AxisLimits(egu="mm"),
Axis.OY: AxisLimits(egu="mm"),
Axis.OZ: AxisLimits(egu="deg"),
Axis.OZ: AxisLimits(egu="mm"), # O-vector component (mm), not an angle
}
# Axes a stop never retargets. Stop works by writing the current readback back as
# the target; the O-vector readback fluctuates, so doing that would shift the offset
# on every stop. Stopping only the moving SCS axes is enough to halt the sample.
STOP_EXCLUDED_AXES: frozenset[Axis] = frozenset({Axis.OX, Axis.OY, Axis.OZ})
def axes_to_stop(axes) -> list[Axis]:
"""The axes a stop request may retarget: *axes* (or all) minus STOP_EXCLUDED_AXES."""
chosen = list(Axis) if axes is None else [Axis(a) for a in axes]
return [a for a in chosen if a not in STOP_EXCLUDED_AXES]
@dataclass
class SmargopoloInfo:
@@ -195,7 +208,9 @@ class SmargopoloTransport(abc.ABC):
smargopolo exposes no explicit stop endpoint; in ``Mode.READY`` ("Follow
Target") setting the target equal to the current readback brings the axis
to rest. Best-effort: must not raise during teardown.
to rest. Axes in :data:`STOP_EXCLUDED_AXES` (the O-vector) are never
retargeted, even when named explicitly. Best-effort: must not raise during
teardown.
"""
# -- diagnostics -------------------------------------------------------
+2 -3
View File
@@ -29,6 +29,7 @@ from .base import (
Mode,
SmargopoloInfo,
SmargopoloTransport,
axes_to_stop,
)
from .errors import SmargopoloModeError
@@ -198,9 +199,7 @@ class FakeTransport(SmargopoloTransport):
def stop(self, axes: list[Axis] | None = None) -> None:
self._record("stop")
chosen = list(axes) if axes is not None else list(Axis)
for axis in chosen:
axis = Axis(axis)
for axis in axes_to_stop(axes):
if self._gradual:
self._settle(axis)
self._targets[axis] = self._positions[axis]
+6 -2
View File
@@ -28,6 +28,7 @@ from .base import (
Mode,
SmargopoloInfo,
SmargopoloTransport,
axes_to_stop,
)
from .errors import (
SmargopoloCommandError,
@@ -167,12 +168,15 @@ class RestTransport(SmargopoloTransport):
def stop(self, axes: list[Axis] | None = None) -> None:
# Retarget the chosen axes to their current readback: in "Follow Target"
# mode this halts them. Best-effort -- never raise.
# mode this halts them. The O-vector is never retargeted (see
# STOP_EXCLUDED_AXES). Best-effort -- never raise.
chosen = axes_to_stop(axes)
if not chosen:
return
try:
positions = self.get_positions()
except Exception: # pylint: disable=broad-except
return
chosen = list(axes) if axes is not None else list(positions)
targets = {a: positions[a] for a in chosen if a in positions}
if not targets:
return
+60 -1
View File
@@ -7,7 +7,7 @@ import pytest
from ophyd.utils import LimitError, ReadOnlyError
from csaxs_bec.devices.smargon import SmarGon, SmargopoloController, SmargopoloMotor
from csaxs_bec.devices.smargon.transport import FakeTransport, Mode
from csaxs_bec.devices.smargon.transport import Axis, FakeTransport, Mode
from csaxs_bec.devices.smargon.transport.errors import SmargopoloCommandError, SmargopoloModeError
@@ -266,3 +266,62 @@ def test_monitor_stops_with_the_last_axis_and_can_be_disabled():
off = _motor("sgz", "SHZ", monitor_interval=0)
assert off.controller._monitor_thread is None
off.destroy()
# -- O-vector (OX/OY/OZ) motors ----------------------------------------------
@pytest.mark.parametrize("axis", ["OX", "OY", "OZ"])
def test_o_vector_axes_are_mm_motors(axis):
m = _motor(f"sgo{axis[1].lower()}", axis)
assert m.egu == "mm"
assert m.limits == (0.0, 0.0) # unbounded unless configured
def test_o_vector_initial_readback_and_record():
sgoz = _motor("sgoz", "OZ")
assert sgoz.position == pytest.approx(180.0) # fake's home O-vector
assert sgoz.read()["sgoz"]["value"] == pytest.approx(180.0)
def test_o_vector_move_and_limits():
coord = _coordinator()
sgox = _motor("sgox", "OX", limits=[-1, 1])
sgoz = _motor("sgoz", "OZ")
coord.reference()
sgox.move(0.25, wait=True)
sgoz.move(200.0, wait=True)
assert sgox.position == pytest.approx(0.25)
assert sgoz.position == pytest.approx(200.0)
with pytest.raises(LimitError):
sgox.move(5.0)
def test_o_vector_move_refused_until_referenced():
sgoy = _motor("sgoy", "OY")
with pytest.raises(SmargopoloModeError):
sgoy.move(0.1)
def test_move_scs_includes_o_vector():
coord = _coordinator()
sgx = _motor("sgx", "SHX")
sgoy = _motor("sgoy", "OY", limits=[-1, 1])
coord.reference()
coord.move_scs(SHX=0.1, OY=-0.4, wait=True)
assert sgx.position == pytest.approx(0.1)
assert sgoy.position == pytest.approx(-0.4)
with pytest.raises(LimitError):
coord.move_scs(OY=3.0)
def test_stop_all_and_o_motor_stop_leave_o_vector_untouched():
coord = _coordinator()
sgox = _motor("sgox", "OX")
coord.reference()
transport = coord.controller.transport
transport._targets[Axis.OX] = 2.0
coord.stop_all()
sgox.stop()
assert transport._targets[Axis.OX] == pytest.approx(2.0)
assert sgox.motor_is_moving.get() == 0
@@ -80,3 +80,48 @@ def test_gradual_motion_advances_over_time():
assert t.get_position(Axis.SHX) == pytest.approx(50.0)
clock["t"] = 5.0
assert t.get_position(Axis.SHX) == pytest.approx(100.0)
# -- stop leaves the O-vector alone -----------------------------------------
class _RecordingRest:
"""A RestTransport with HTTP replaced: canned readback, PUTs recorded."""
def __new__(cls):
from csaxs_bec.devices.smargon.transport import RestTransport
t = RestTransport.__new__(RestTransport)
t.puts = []
readback = {a.value: float(i) for i, a in enumerate(Axis)}
t._get = lambda resource: dict(readback)
t._put = lambda resource, params: t.puts.append((resource, dict(params))) or {}
return t
def test_rest_stop_all_never_writes_o_vector():
t = _RecordingRest()
t.stop(None)
[(resource, params)] = t.puts
assert resource == "targetSCS"
assert not {"OX", "OY", "OZ"} & set(params)
assert {"SHX", "SHY", "SHZ", "CHI", "PHI"} <= set(params)
def test_rest_stop_of_o_axis_sends_nothing():
t = _RecordingRest()
t.stop([Axis.OX, Axis.OZ])
assert t.puts == []
def test_rest_stop_mixed_axes_keeps_only_non_o():
t = _RecordingRest()
t.stop([Axis.SHX, Axis.OY])
assert t.puts == [("targetSCS", {"SHX": float(list(Axis).index(Axis.SHX))})]
def test_fake_stop_all_leaves_o_targets(transport):
transport.set_mode(Mode.REFERENCING)
transport._targets[Axis.OX] = 1.5 # pending O target, readback still 0
transport.stop(None)
assert transport._targets[Axis.OX] == pytest.approx(1.5)