Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions asyncroscopy/instruments/electron_microscope/auto_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
# Wrapped in try/except so the device can still be imported and tested
# on a development machine without AutoScript installed.
try:
import autoscript_tem_microscope_client
from autoscript_tem_microscope_client import TemMicroscopeClient
from autoscript_tem_microscope_client.enumerations import EdsDetectorType
from autoscript_tem_microscope_client.enumerations import CameraType, RegionCoordinateSystem, ExposureTimeType
Expand Down Expand Up @@ -470,8 +471,12 @@ def _move_stage(self, position) -> None:

def _auto_focus(self):
"""Perform autofocus routine C1A1"""
settings = RunOptiStemSettings(method="C1A1") # method=OptiStemMethod.C1_A1, dwell_time=2e-06, cutoff_in_pixels=5)
self._microscope.auto_functions.run_opti_stem(settings)
if self._microscope.optics.optical_mode == 'Stem':
settings = RunOptiStemSettings(method="C1A1")
self._microscope.auto_functions.run_opti_stem(settings)
else:
settings = autoscript_tem_microscope_client.structuresRunObjectiveAutoStigmatorSettings(camera_detector="Flucam")
self.microscope.auto_functions.run_objective_auto_stigmator(settings)

def _set_image_shift(self, shift):
"""Apply image shift in meters."""
Expand Down
40 changes: 27 additions & 13 deletions notebooks/stage.py → startup_guis/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from gevent import os
import numpy as np
import tango
from autoscript_tem_microscope_client import TemMicroscopeClient
# from autoscript_tem_microscope_client import TemMicroscopeClient



Expand All @@ -26,13 +26,13 @@ def __init__(self, root):
# --- Internal Coordinate States ---
self.stage_x, self.stage_y, self.stage_z = 0.0, 0.0, 0.0
self.stage_alpha, self.stage_beta = 0.0, 0.0
self.beam_x, self.beam_y, self.df = 0.0, 0.0, 0.0
self.beam_x, self.beam_y, self.defocus = 0.0, 0.0, 0.0
self.beam_alpha, self.beam_beta = 0.0, 0.0

# --- StringVars for UI Inputs ---
self.x_var, self.y_var, self.z_var = tk.StringVar(value="0.00"), tk.StringVar(value="0.00"), tk.StringVar(value=f"{0.00:.2f}")
self.alpha_var, self.beta_var = tk.StringVar(value="0.00"), tk.StringVar(value="0.00")
self.bsx_var, self.bsy_var, self.df_var = tk.StringVar(value="0.00"), tk.StringVar(value="0.00"), tk.StringVar(value=f"{0.00:.2f}")
self.bsx_var, self.bsy_var, self.defocus_var = tk.StringVar(value="0.00"), tk.StringVar(value="0.00"), tk.StringVar(value=f"{0.00:.2f}")
self.btx_var, self.bty_var = tk.StringVar(value="0.00"), tk.StringVar(value="0.00")

# Custom Styling
Expand All @@ -56,7 +56,7 @@ def __init__(self, root):
# Row 1: Electronic Beam Coordinates
self.create_grid_field(self.readout_frame, "Beam S-X:", self.bsx_var, "nm", 1, 0)
self.create_grid_field(self.readout_frame, "Beam S-Y:", self.bsy_var, "nm", 1, 3)
self.create_grid_field(self.readout_frame, "Defocus Δf:", self.df_var, "nm", 1, 6)
self.create_grid_field(self.readout_frame, "Defocus Δf:", self.defocus_var, "nm", 1, 6)
self.create_grid_field(self.readout_frame, "Beam T-X:", self.btx_var, "mrad", 1, 9)
self.create_grid_field(self.readout_frame, "Beam T-Y:", self.bty_var, "mrad", 1, 12)

Expand Down Expand Up @@ -142,15 +142,26 @@ def __init__(self, root):
self.position_dropdown.grid(row=1, column=1, padx=2)
tk.Button(self.step_frame, text='Go To', bg="#222222", fg="#ffffff", command=self.go_to_position).grid(row=1, column=2, padx=2)

tk.Button(self.step_frame, text='autoFocus', bg="#222222", fg="#ffffff", command=self.auto_focus).grid(row=1, column=3, padx=2)

self.current_conditions()
self.sync_ui_to_vars()

def auto_focus(self):
# Placeholder for autofocus functionality
if 'auto_focus' in self.microscope.get_command_list():
self.microscope.auto_focus()
else:
messagebox.showinfo("Auto Focus", "Auto-focus functionality is not implemented yet.")

def save_position(self):
from tkinter import simpledialog
positions = self.microscope.get_stage()
name = simpledialog.askstring("Save Position", "Enter a name for this position:")
self.saved_stage_positions.setdefault(name, positions)
self.position_dropdown.config(values=list(self.saved_stage_positions.keys()))
self.position_dropdown.set(name)

def go_to_position(self):
name = self.position_dropdown.get()
if name in self.saved_stage_positions:
Expand All @@ -172,9 +183,12 @@ def current_conditions(self):
if starting_stage_position[4] is not None:
self.stage_beta = np.degrees(starting_stage_position[4])
self.beam_x, self.beam_y = self.microscope.get_image_shift()
self.df = self.microscope.get_defocus()
self.beam_x *=1e9
self.beam_y *=1e9
self.defocus = self.microscope.get_defocus() * 1e9
self.beam_alpha, self.beam_beta = self.microscope.get_beam_tilt()

self.beam_alpha *= 1e3
self.beam_beta *= 1e3

# --- UI Helpers ---
def create_grid_field(self, parent, text, var, unit, r, c):
Expand Down Expand Up @@ -202,7 +216,7 @@ def set_stage(self):

def set_defocus(self):
"""Set the defocus value."""
self.microscope.set_defocus(self.df * 1e-9) # Convert nm to meters
self.microscope.set_defocus(self.defocus * 1e-9) # Convert nm to meters

def set_beam_shift(self):
"""Set the beam shift values."""
Expand Down Expand Up @@ -231,7 +245,7 @@ def sync_ui_to_vars(self):
move_stage = True
if move_stage:
self.set_stage()
if self.df_var != f"{self.df:.2f}":
if self.defocus_var != f"{self.defocus:.2f}":
self.set_defocus()
if self.bsx_var != f"{self.beam_x:.2f}"or self.bsy_var != f"{self.beam_y:.2f}":
self.set_beam_shift()
Expand All @@ -245,13 +259,13 @@ def sync_ui_to_vars(self):
self.beta_var.set(f"{self.stage_beta:.2f}")
self.bsx_var.set(f"{self.beam_x:.2f}");
self.bsy_var.set(f"{self.beam_y:.2f}");
self.df_var.set(f"{self.df:.2f}")
self.defocus_var.set(f"{self.defocus:.2f}")
self.btx_var.set(f"{self.beam_alpha:.2f}");
self.bty_var.set(f"{self.beam_beta:.2f}")
def fetch_vars_from_ui(self):
self.stage_x, self.stage_y, self.stage_z = float(self.x_var.get()), float(self.y_var.get()), float(self.z_var.get())
self.stage_alpha, self.stage_beta = float(self.alpha_var.get()), float(self.beta_var.get())
self.beam_x, self.beam_y, self.df = float(self.bsx_var.get()), float(self.bsy_var.get()), float(self.df_var.get())
self.beam_x, self.beam_y, self.defocus = float(self.bsx_var.get()), float(self.bsy_var.get()), float(self.defocus_var.get())
self.beam_alpha, self.beam_beta = float(self.btx_var.get()), float(self.bty_var.get())
def jog_engine(self, mx=0, my=0, mz=0, ma=0, mb=0, mbsx=0, mbsy=0, mdf=0, mbtx=0, mbty=0):
try:
Expand All @@ -266,7 +280,7 @@ def jog_engine(self, mx=0, my=0, mz=0, ma=0, mb=0, mbsx=0, mbsy=0, mdf=0, mbtx=0
self.stage_beta += (mb * step_stg_ang)
self.beam_x += (mbsx * step_bm_sft)
self.beam_y += (mbsy * step_bm_sft)
self.df += (mdf * step_bm_sft)
self.defocus += (mdf * step_bm_sft)
self.beam_alpha += (mbtx * step_bm_tlt)
self.beam_beta += (mbty * step_bm_tlt)
self.sync_ui_to_vars()
Expand Down Expand Up @@ -295,11 +309,11 @@ def reset_beam_tilt(self):
self.beam_alpha, self.beam_beta = 0.0, 0.0
self.sync_ui_to_vars()
def reset_defocus(self):
self.df = 0.0
self.defocus = 0.0
self.sync_ui_to_vars()
def reset_all(self):
self.stage_x, self.stage_y, self.stage_z, self.stage_alpha, self.stage_beta = 0.0, 0.0, 0.0, 0.0, 0.0
self.beam_x, self.beam_y, self.df, self.beam_alpha, self.beam_beta = 0.0, 0.0, 0.0, 0.0, 0.0
self.beam_x, self.beam_y, self.defocus, self.beam_alpha, self.beam_beta = 0.0, 0.0, 0.0, 0.0, 0.0
self.sync_ui_to_vars()


Expand Down
Loading