diff --git a/csaxs_bec/devices/smargon/README.md b/csaxs_bec/devices/smargon/README.md index 817a5393..a067bd75 100644 --- a/csaxs_bec/devices/smargon/README.md +++ b/csaxs_bec/devices/smargon/README.md @@ -120,6 +120,8 @@ 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. diff --git a/csaxs_bec/devices/smargon/transport/__init__.py b/csaxs_bec/devices/smargon/transport/__init__.py index 1dd3cf7e..b379a49d 100644 --- a/csaxs_bec/devices/smargon/transport/__init__.py +++ b/csaxs_bec/devices/smargon/transport/__init__.py @@ -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", diff --git a/csaxs_bec/devices/smargon/transport/base.py b/csaxs_bec/devices/smargon/transport/base.py index cd017100..c18a3f08 100644 --- a/csaxs_bec/devices/smargon/transport/base.py +++ b/csaxs_bec/devices/smargon/transport/base.py @@ -103,6 +103,17 @@ AXIS_INFO: dict[Axis, AxisLimits] = { 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: @@ -197,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 ------------------------------------------------------- diff --git a/csaxs_bec/devices/smargon/transport/fake.py b/csaxs_bec/devices/smargon/transport/fake.py index 7fcc1e40..9889e431 100644 --- a/csaxs_bec/devices/smargon/transport/fake.py +++ b/csaxs_bec/devices/smargon/transport/fake.py @@ -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] diff --git a/csaxs_bec/devices/smargon/transport/rest.py b/csaxs_bec/devices/smargon/transport/rest.py index 507adffc..1df32cb8 100644 --- a/csaxs_bec/devices/smargon/transport/rest.py +++ b/csaxs_bec/devices/smargon/transport/rest.py @@ -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 diff --git a/tests/tests_devices/test_smargon_device.py b/tests/tests_devices/test_smargon_device.py index d221b347..b0121a58 100644 --- a/tests/tests_devices/test_smargon_device.py +++ b/tests/tests_devices/test_smargon_device.py @@ -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 @@ -407,3 +407,15 @@ def test_move_scs_includes_o_vector(): 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 diff --git a/tests/tests_devices/test_smargon_transport.py b/tests/tests_devices/test_smargon_transport.py index 8aba62c6..02723ec2 100644 --- a/tests/tests_devices/test_smargon_transport.py +++ b/tests/tests_devices/test_smargon_transport.py @@ -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)