-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rebase to stage: add test, fix names, use NotImplementedError
- Loading branch information
Showing
7 changed files
with
164 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Optional | ||
import os | ||
|
||
import squid.logging | ||
from squid.abc import Pos | ||
from squid.config import StageConfig | ||
|
||
_log = squid.logging.get_logger(__package__) | ||
_DEFAULT_CACHE_PATH = "cache/last_coords.txt" | ||
""" | ||
Attempts to load a cached stage position and return it. | ||
""" | ||
def get_cached_position(cache_path=_DEFAULT_CACHE_PATH) -> Optional[Pos]: | ||
if not os.path.isfile(cache_path): | ||
_log.debug(f"Cache file '{cache_path}' not found, no cached pos found.") | ||
return None | ||
with open(cache_path, "r") as f: | ||
for line in f: | ||
try: | ||
x, y, z = line.strip("\n").strip().split(",") | ||
x = float(x) | ||
y = float(y) | ||
z = float(z) | ||
return Pos(x_mm=x, y_mm=y, z_mm=z, theta_rad=None) | ||
except RuntimeError as e: | ||
raise e | ||
pass | ||
return None | ||
|
||
""" | ||
Write out the current x, y, z position, in mm, so we can use it later as a cached position. | ||
""" | ||
def cache_position(pos: Pos, stage_config: StageConfig, cache_path=_DEFAULT_CACHE_PATH): | ||
x_min = stage_config.X_AXIS.MIN_POSITION | ||
x_max = stage_config.X_AXIS.MAX_POSITION | ||
y_min = stage_config.Y_AXIS.MIN_POSITION | ||
y_max = stage_config.Y_AXIS.MAX_POSITION | ||
z_min = stage_config.Z_AXIS.MIN_POSITION | ||
z_max = stage_config.Z_AXIS.MAX_POSITION | ||
if not (x_min <= pos.x_mm <= x_max and | ||
y_min <= pos.y_mm <= y_max and | ||
z_min <= pos.z_mm <= z_max): | ||
raise ValueError(f"Position {pos} is not cacheable because it is outside of the min/max of at least one axis. x_range=({x_min}, {x_max}), y_range=({y_min}, {y_max}), z_range=({z_min}, {z_max})") | ||
with open(cache_path, "w") as f: | ||
_log.debug(f"Writing position={pos} to cache path='{cache_path}'") | ||
f.write(",".join([str(pos.x_mm), str(pos.y_mm), str(pos.z_mm)])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pytest | ||
|
||
import squid.config | ||
from squid.config import AxisConfig | ||
|
||
|
||
def test_axis_config(): | ||
stage_config = squid.config.get_stage_config() | ||
# micro step conversion round tripping | ||
trials = (1.0, 0.001, 2.2, 3.123456) | ||
|
||
# Test with easy to reason about axis config, then with real ones | ||
easy_config = stage_config.X_AXIS | ||
easy_config.ENCODER_SIGN = 1 | ||
easy_config.USE_ENCODER = False | ||
# 400 steps -> 1 mm (2*200 = 1 rev, 1 rev == 1 mm) | ||
easy_config.SCREW_PITCH = 1.0 | ||
easy_config.MICROSTEPS_PER_STEP = 2 | ||
easy_config.FULL_STEPS_PER_REV = 200 | ||
|
||
def round_trip_mm(config: AxisConfig, mm): | ||
# Round tripping should match within 1 ustep | ||
usteps = config.convert_real_units_to_ustep(mm) | ||
mm_round_tripped = config.convert_to_real_units(usteps) | ||
eps = abs(config.convert_to_real_units(1)) | ||
assert mm_round_tripped == pytest.approx(mm, abs=eps) | ||
|
||
for trial in trials: | ||
round_trip_mm(easy_config, trial) | ||
|
||
for trial in trials: | ||
round_trip_mm(stage_config.X_AXIS, trial) | ||
round_trip_mm(stage_config.Y_AXIS, trial) | ||
round_trip_mm(stage_config.Z_AXIS, trial) | ||
round_trip_mm(stage_config.THETA_AXIS, trial) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
import tempfile | ||
|
||
import squid.stage.cephla | ||
import squid.stage.prior | ||
import squid.stage.utils | ||
import squid.config | ||
from control.microcontroller import Microcontroller, SimSerial | ||
import squid.abc | ||
|
||
def test_create_simulated_stages(): | ||
microcontroller = Microcontroller(existing_serial=SimSerial()) | ||
cephla_stage = squid.stage.cephla.CephlaStage(microcontroller, squid.config.get_stage_config()) | ||
|
||
with pytest.raises(NotImplementedError): | ||
prior_stage = squid.stage.prior.PriorStage(squid.config.get_stage_config()) | ||
|
||
def test_simulated_cephla_stage_ops(): | ||
microcontroller = Microcontroller(existing_serial=SimSerial()) | ||
stage: squid.stage.cephla.CephlaStage = squid.stage.cephla.CephlaStage(microcontroller, squid.config.get_stage_config()) | ||
|
||
assert stage.get_pos() == squid.abc.Pos(x_mm=0.0, y_mm=0.0, z_mm=0.0, theta_rad=0.0) | ||
|
||
|
||
def test_position_caching(): | ||
(unused_temp_fd, temp_cache_path) = tempfile.mkstemp(".cache", "squid_testing_") | ||
|
||
# Use 6 figures after the decimal so we test that we can capture nanometers | ||
p = squid.abc.Pos(x_mm=11.111111, y_mm=22.222222, z_mm=1.333333, theta_rad=None) | ||
squid.stage.utils.cache_position(pos=p, stage_config=squid.config.get_stage_config(), cache_path=temp_cache_path) | ||
|
||
p_read = squid.stage.utils.get_cached_position(cache_path=temp_cache_path) | ||
|
||
assert p_read == p |