From 96fabeb1c7032bee6bc5ac679a5a731e024c1f9d Mon Sep 17 00:00:00 2001 From: x01dc Date: Fri, 4 Sep 2026 17:16:25 +0200 Subject: [PATCH] fix(omny): simulate oshuttlealign's quirky low-limit-trips-positive hardware behavior _otransfer_shuttle_align()'s own comment documents this as "a routine for broken hardware": on real hardware, driving the shuttle aligner (oshuttlealign, axis H) positive of its forward-reference zero also trips the low/reverse limit switch, due to how the two switches are wired -- not just the normal reference-axis routine. The sim had no concept of this: plain PR/BG moves never update limit_low_active/ limit_high_active at all (those flags are only ever set by the separate #FES/#FRM reference-axis path), so the routine's final "searchwrongendswitch" search loop (a 22-iteration, +200-step-at-a-time hunt for the low switch) could never find it and the whole otransfer_put_sample()/otransfer_get_sample() flow hung until RPC timeout, raising "The shuttle aligner did not move back." Added a per-axis opt-in flag (quirky_positive_trips_low_limit, wired through configure_axis()/_sim_state_kwargs()/SimOMNYGalilMotor) that makes the simulated low limit switch position-derived (active whenever position > 0) instead of purely flag-based, and set it only for oshuttlealign in simulated_omny.yaml. Verified end-to-end against a replay of the real command sequence (reference, +200 recovery nudge, the five alignment excursions, then the search loop) -- the low switch is now found within the 22-iteration budget, same as real hardware. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_018Mupip8x7Hean6E3FFvpbj --- .../simulated_omny/simulated_omny.yaml | 7 + csaxs_bec/devices/sim/sim_galil.py | 15 +++ csaxs_bec/devices/sim/sim_omny.py | 2 + ...st_sim_galil_oshuttlealign_quirky_limit.py | 122 ++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 tests/tests_devices/test_sim_galil_oshuttlealign_quirky_limit.py diff --git a/csaxs_bec/device_configs/simulated_omny/simulated_omny.yaml b/csaxs_bec/device_configs/simulated_omny/simulated_omny.yaml index dbd84c4c..ce3a87dd 100644 --- a/csaxs_bec/device_configs/simulated_omny/simulated_omny.yaml +++ b/csaxs_bec/device_configs/simulated_omny/simulated_omny.yaml @@ -493,6 +493,13 @@ oshuttlealign: sign: 1 sim_velocity: 10.0 sim_initial_position: 0 + # _otransfer_shuttle_align()'s "routine for broken hardware" comment + # (omny_sample_transfer_mixin.py): the negative/low limit switch is + # wired close enough to the positive/high one that it also trips once + # driven positive of the forward-reference zero point, not just via + # normal referencing. Without this, the alignment routine's final + # "searchwrongendswitch" loop never finds the low switch and times out. + sim_quirky_positive_trips_low_limit: true enabled: true onFailure: buffer readOnly: true diff --git a/csaxs_bec/devices/sim/sim_galil.py b/csaxs_bec/devices/sim/sim_galil.py index c61e8b86..e13cf17f 100644 --- a/csaxs_bec/devices/sim/sim_galil.py +++ b/csaxs_bec/devices/sim/sim_galil.py @@ -99,6 +99,13 @@ class _SimGalilAxis: self._move_t1 = 0.0 self.limit_low_active = False self.limit_high_active = False + # Models oshuttlealign's documented "broken hardware" quirk + # (omny_sample_transfer_mixin.py's _otransfer_shuttle_align()): the + # low/reverse limit switch is wired close enough to the high/forward + # one that it also trips once the axis is driven positive of its + # forward-reference zero point -- not just via the normal + # reference-axis routine. See the _LR handling in _handle_mg(). + self.quirky_positive_trips_low_limit = False def position(self) -> float: now = time.time() @@ -222,6 +229,10 @@ class SimGalilState: ax.limit_low_active = bool(kwargs["limit_low_active"]) if kwargs.get("limit_high_active") is not None: ax.limit_high_active = bool(kwargs["limit_high_active"]) + if kwargs.get("quirky_positive_trips_low_limit") is not None: + ax.quirky_positive_trips_low_limit = bool( + kwargs["quirky_positive_trips_low_limit"] + ) self.recompute_allaxref() def thread_active(self, thread_id: int) -> bool: @@ -516,6 +527,8 @@ class SimGalilSocket(SimSocketBase): for token, axis_char in zip(re.findall(r"_L[RF]", arg), axes): ax = state.axis(ord(axis_char.lower()) - 97) active = ax.limit_low_active if token == "_LR" else ax.limit_high_active + if token == "_LR" and ax.quirky_positive_trips_low_limit and ax.position() > 0: + active = True parts.append(f"{0.0 if active else 1.0:.4f}") return " ".join(parts) if arg.startswith("_TD"): @@ -640,6 +653,7 @@ def _sim_state_kwargs( sim_encpermm=None, sim_digital_outputs=None, sim_variables=None, + sim_quirky_positive_trips_low_limit=None, ) -> dict: initial_position = None if sim_initial_position is not None: @@ -651,6 +665,7 @@ def _sim_state_kwargs( "referenced": sim_referenced, "analog_inputs": sim_analog_inputs, "digital_inputs": sim_digital_inputs, + "quirky_positive_trips_low_limit": sim_quirky_positive_trips_low_limit, "limit_low_active": sim_limit_low_active, "limit_high_active": sim_limit_high_active, "encpermm": sim_encpermm, diff --git a/csaxs_bec/devices/sim/sim_omny.py b/csaxs_bec/devices/sim/sim_omny.py index 19a6015e..1ed23d9f 100644 --- a/csaxs_bec/devices/sim/sim_omny.py +++ b/csaxs_bec/devices/sim/sim_omny.py @@ -72,6 +72,7 @@ class SimOMNYGalilMotor(OMNYGalilMotor): sim_limit_low_active=None, sim_limit_high_active=None, sim_variables=None, + sim_quirky_positive_trips_low_limit=None, **kwargs, ): super().__init__( @@ -112,6 +113,7 @@ class SimOMNYGalilMotor(OMNYGalilMotor): self.sign, sim_digital_outputs=sim_digital_outputs, sim_variables=variables, + sim_quirky_positive_trips_low_limit=sim_quirky_positive_trips_low_limit, ), ) diff --git a/tests/tests_devices/test_sim_galil_oshuttlealign_quirky_limit.py b/tests/tests_devices/test_sim_galil_oshuttlealign_quirky_limit.py new file mode 100644 index 00000000..98022baf --- /dev/null +++ b/tests/tests_devices/test_sim_galil_oshuttlealign_quirky_limit.py @@ -0,0 +1,122 @@ +import time + +import pytest +from bec_server.device_server.tests.utils import DMMock + +from csaxs_bec.devices.omny.galil.ogalil_ophyd import OMNYGalilController +from csaxs_bec.devices.sim.sim_omny import SimOMNYGalilMotor +from csaxs_bec.devices.sim.sim_socket import SimStateRegistry + +_HOST = "mpc3217.psi.ch" +_PORT = 8082 + + +@pytest.fixture(autouse=True) +def _reset_sim_state(): + OMNYGalilController._reset_controller() + SimStateRegistry.reset() + yield + SimStateRegistry.reset() + OMNYGalilController._reset_controller() + + +def _make_align(quirky: bool) -> SimOMNYGalilMotor: + align = SimOMNYGalilMotor( + "H", + name="oshuttlealign", + host=_HOST, + port=_PORT, + sign=1, + device_manager=DMMock(), + sim_initial_position=0, + sim_velocity=100.0, + sim_quirky_positive_trips_low_limit=quirky, + ) + align.wait_for_connection() + return align + + +def _drive_relative(align: SimOMNYGalilMotor, steps: int): + align.controller.socket_put_confirmed(f"PRH={steps}") + align.controller.socket_put_confirmed("BGH") + while bool(float(align.controller.socket_put_and_receive("MG_BGH"))): + time.sleep(0.01) + + +def test_low_limit_inactive_at_position_zero_even_when_quirky(): + """Matches the real hardware log: right after reaching the forward/high + reference (position 0), the low switch has NOT yet tripped -- only a + subsequent positive move trips it. Otherwise + _otransfer_shuttle_align()'s "Not in correct start position for + alignment." branch (and the whole recovery routine it guards) would + never even run.""" + align = _make_align(quirky=True) + try: + assert align.get_motor_limit_switch()[0] is False + finally: + align.destroy() + + +def test_quirky_low_limit_trips_after_positive_move_past_zero(): + align = _make_align(quirky=True) + try: + _drive_relative(align, 200) + assert align.get_motor_limit_switch()[0] is True + finally: + align.destroy() + + +def test_quirky_low_limit_inactive_when_negative_of_zero(): + align = _make_align(quirky=True) + try: + _drive_relative(align, -5000) + assert align.get_motor_limit_switch()[0] is False + finally: + align.destroy() + + +def test_non_quirky_axis_never_trips_low_limit_from_position_alone(): + """The quirk is opt-in per axis (only oshuttlealign has it configured) -- + an ordinary axis at a positive position must not spuriously report its + low limit switch as active.""" + align = _make_align(quirky=False) + try: + _drive_relative(align, 200) + assert align.get_motor_limit_switch()[0] is False + finally: + align.destroy() + + +def test_shuttle_align_search_loop_finds_low_switch_within_budget(): + """End-to-end replay of _otransfer_shuttle_align()'s real command + sequence (omny_sample_transfer_mixin.py): reach the forward/high + reference, nudge +200 (recovery), run the big alignment excursions, + then the final +200-step search loop (budget: 22 iterations) must find + the low switch -- this is the exact sequence that timed out before + sim_quirky_positive_trips_low_limit was wired up.""" + align = _make_align(quirky=True) + try: + # reach the forward/high reference the same way drive_axis_to_limit() does + align.controller.socket_put_confirmed("naxis=7") + align.controller.socket_put_confirmed("ndir=1") + align.controller.socket_put_confirmed("XQ#NEWPAR") + align.controller.socket_put_confirmed("XQ#FES") + assert align.get_motor_limit_switch() == [False, True] + + # recovery nudge + _drive_relative(align, 200) + + # big alignment excursions + for steps in (-5000, -19500, 3000, -3000, 23500): + _drive_relative(align, steps) + assert align.get_motor_limit_switch()[0] is False + + # final search loop + for i in range(22): + _drive_relative(align, 200) + if align.get_motor_limit_switch()[0]: + break + else: + pytest.fail("search loop never found the low limit switch") + finally: + align.destroy()