|
| 1 | +import pathlib |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import time |
| 5 | +import numpy as np |
| 6 | +import tomli |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +import yaqc |
| 11 | +import yaqd_core |
| 12 | +from yaqd_core import testing |
| 13 | + |
| 14 | + |
| 15 | +config = pathlib.Path(__file__).parent / "config.toml" |
| 16 | + |
| 17 | + |
| 18 | +@testing.run_daemon_entry_point("fake-has-transformed-position", config=config) |
| 19 | +def test_transform(): |
| 20 | + c = yaqc.Client(38001) |
| 21 | + c.set_native_reference(1.0) |
| 22 | + val = 3.0 |
| 23 | + bb = c.to_transformed(c.to_native(val)) |
| 24 | + assert np.isclose(val, bb) |
| 25 | + |
| 26 | + |
| 27 | +@testing.run_daemon_entry_point("fake-has-transformed-position", config=config) |
| 28 | +def test_limits(): |
| 29 | + for port in [38001, 38002]: |
| 30 | + c = yaqc.Client(port) |
| 31 | + config = tomli.loads(c.get_config()) |
| 32 | + |
| 33 | + limits = c.get_limits() |
| 34 | + lims2 = list(map(c.to_transformed, c.get_native_limits())) |
| 35 | + assert np.isclose(limits, lims2).all() |
| 36 | + |
| 37 | + for setpoint, actual in zip( |
| 38 | + [limits[0] - 0.5, 0.5 * sum(limits), limits[1] + 0.5], |
| 39 | + [limits[0], 0.5 * sum(limits), limits[1]], |
| 40 | + ): |
| 41 | + c.set_position(setpoint) |
| 42 | + time.sleep(0.10) |
| 43 | + assert np.isclose(c.get_position(), actual) |
| 44 | + assert np.isclose(c.get_destination(), actual) |
| 45 | + assert np.isclose(c.get_native_position(), c.to_native(actual)) |
| 46 | + assert np.isclose(c.get_native_destination(), c.to_native(actual)) |
| 47 | + |
| 48 | + |
| 49 | +@testing.run_daemon_entry_point("fake-has-transformed-position", config=config) |
| 50 | +def test_change_native_reference(): |
| 51 | + for port in [38001, 38002]: |
| 52 | + c = yaqc.Client(port) |
| 53 | + factor = tomli.loads(c.get_config())["factor"] |
| 54 | + |
| 55 | + midrange = 0.5 * sum(c.get_limits()) |
| 56 | + |
| 57 | + c.set_native_reference(1) |
| 58 | + c.set_position(midrange) |
| 59 | + time.sleep(0.1) |
| 60 | + native_position = c.get_native_position() |
| 61 | + assert np.isclose(native_position, c.to_native(midrange)) |
| 62 | + c.set_native_reference(1.5) |
| 63 | + |
| 64 | + assert np.isclose(native_position, c.get_native_position()) |
| 65 | + assert np.isclose( |
| 66 | + c.get_position(), c.to_transformed(c.to_native(midrange) - 0.5) |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@testing.run_daemon_entry_point("fake-has-transformed-position", config=config) |
| 71 | +def test_set_relative(): |
| 72 | + c = yaqc.Client(38001) |
| 73 | + c.set_position(c.get_limits()[0]) |
| 74 | + initial = c.get_position() |
| 75 | + time.sleep(0.1) |
| 76 | + destination = c.set_relative(1.0) |
| 77 | + time.sleep(0.1) |
| 78 | + |
| 79 | + assert np.isclose(destination, initial + 1.0) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + test_change_native_reference() |
| 84 | + test_limits() |
| 85 | + test_set_relative() |
| 86 | + test_transform() |
0 commit comments