"""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) 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) 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"]