Skip to content

Commit

Permalink
fixed motors, added image to edge searcher
Browse files Browse the repository at this point in the history
Slaby committed Nov 13, 2023
1 parent 1c14752 commit 2393f17
Showing 4 changed files with 94 additions and 41 deletions.
15 changes: 4 additions & 11 deletions LabExT/Movement/Stages/ThorlabsKCube.py
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ def status(self) -> int:
def position(self):
"""Returns current position of channel in micrometers specified by SA_GetPosition_S"""
# position is in steps, One step travels 29 nm
self._position = self._stage.get_position() * 29e-6
self._position = self._stage.get_position() * 29e-3

return self._position

@@ -163,12 +163,10 @@ def move(
"""
self.movement_mode = mode
if self.movement_mode == MovementType.RELATIVE:
print('rel: ', diff)
self._stage.setup_jog(mode="step",step_size=diff / 29e-3, stop_mode="immediate")
self._stage.jog(direction="+", kind="builtin")
self._stage.wait_for_stop()
elif self.movement_mode == MovementType.ABSOLUTE:
print('abs: ', diff)
inital_pos = self._stage.get_position()
move_by = diff / 29e-3 - inital_pos
self._stage.setup_jog(mode="step",step_size=move_by, stop_mode="immediate")
@@ -226,7 +224,7 @@ def connect(self) -> bool:
# @assert_stage_connected
def disconnect(self) -> bool:
for ch in self.channels:
ch._stage.close()
ch.close()
self.connected = False

@assert_driver_loaded
@@ -287,6 +285,8 @@ def is_stopped(self, channel, stop_pos_um) -> bool: #TODO
@assert_driver_loaded
# @assert_stage_connected
def get_position(self) -> list:
""" Returns position in (um)
"""
return [
self.channels[Axis.X].position,
self.channels[Axis.Y].position,
@@ -308,10 +308,6 @@ def move_relative(
x,
y,
z)

print("MOVING RELATIVE", x, y, z)
print(self.get_position())

stop_pos_um = self.channels[Axis.X].position + x
self.channels[Axis.X].move(diff=x, mode=MovementType.RELATIVE)
if wait_for_stopping:
@@ -328,8 +324,6 @@ def move_relative(
if wait_for_stopping:
self._wait_for_stopping(self.channels[Axis.Z], stop_pos_um)

print(self.get_position())

pass

def move_absolute(
@@ -366,7 +360,6 @@ def _wait_for_stopping(self, channel, stop_pos: float, delay=0.05):
"""
Blocks until all channels have 'SA_STOPPED_STATUS' status.
"""
print('wait for stopping')
while True:
time.sleep(delay)

44 changes: 32 additions & 12 deletions LabExT/SearchForPeak/EdgeSearcher.py
Original file line number Diff line number Diff line change
@@ -275,18 +275,6 @@ def capture_data(self):
# Save power
self.results['measured power'].append(self.instr_powermeter.fetch_power())

# Plot Power
# color_strings = ['C' + str(i) for i in range(10)]
meas_plot_left = PlotData(ObservableList(), ObservableList(), 'scatter')
self.plots_left.append(meas_plot_left)
meas_plot_left.x = [coord[0] for coord in self.results['measured location']]
meas_plot_left.y = self.results['measured power']

meas_plot_right = PlotData(ObservableList(), ObservableList(), 'scatter')
self.plots_right.append(meas_plot_right)
meas_plot_right.x = [coord[2] for coord in self.results['measured location']]
meas_plot_right.y = self.results['measured power']

# Save picture
# self.results['captured image'].append(self.camera.snap_photo())
self.captured_img.append(np.uint8(self.camera.snap_photo()))
@@ -302,6 +290,38 @@ def capture_data(self):
plt.imsave(f"{self.save_file_path}\\img_{np.size(self.results['measured power']):03}.png", self.captured_img[-1], dpi=300)
np.savez(f"{self.save_file_path}\\imgs.npz", images = self.captured_img)

# Plot Power
# color_strings = ['C' + str(i) for i in range(10)]
meas_plot_left_x = PlotData(ObservableList(), ObservableList(), color = 'blue', marker='o', label="x")
meas_plot_left_z = PlotData(ObservableList(), ObservableList(), color = 'red', marker='o', label="z")
self.plots_left.append(meas_plot_left_x)
self.plots_left.append(meas_plot_left_z)

if len(self.results['measured power']) == 1:
meas_plot_left_x.x = [0.0]
meas_plot_left_x.y = self.results['measured power']
meas_plot_left_z.x = [0.0]
meas_plot_left_z.y = self.results['measured power']
else:
meas_plot_left_x.x = [0.0] + [coord[0] - self.results['measured location'][0][0] for coord in self.results['measured location'][1:]]
meas_plot_left_x.y = self.results['measured power']
meas_plot_left_z.x = [0.0] + [coord[2] - self.results['measured location'][0][2] for coord in self.results['measured location'][1:]]
meas_plot_left_z.y = self.results['measured power']
# print()
# print('HELP')
# print(type(meas_plot_left.plot_data))

meas_plot_right = PlotData(x=None, y=None, plot_type='image', image = ObservableList())
self.plots_right.append(meas_plot_right)
# meas_plot_right.x = [coord[2] for coord in self.results['measured location']]
# meas_plot_right.y = self.results['measured power']
meas_plot_right.image = self.captured_img[-1]
# print(type(meas_plot_right.image))
# plt.imshow(self.captured_img[-1])
# plt.show()
# print(type(meas_plot_right.plot_data))


return self.results

def close_instruments(self):
72 changes: 55 additions & 17 deletions LabExT/View/Controls/PlotControl.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.collections import PathCollection
from matplotlib.image import AxesImage
from matplotlib.figure import Figure

from LabExT.ViewModel.Utilities.ObservableList import ObservableList
@@ -71,10 +72,30 @@ def y(self, value):
self._y = value
self.__update__()

def __init__(self, x=None, y=None, plot_type='plot', color=None, **plot_args):
_image = None

@property
def image(self):
return self._image

@image.setter
def image(self, value):
if type(self._image) is ObservableList: # if the old value was observable stop listening for changes
self._image.item_added.remove(self.__item_changed__)
self._image.item_removed.remove(self.__item_changed__)

if type(value) is ObservableList: # if the value is observable start listening for changes
value.item_added.append(self.__item_changed__)
value.item_removed.append(self.__item_changed__)

self._image = value
self.__update__()

def __init__(self, x=None, y=None, plot_type='plot', image=None, color=None, **plot_args):
self.data_changed = list()
self.x = x
self.y = y
self.image = image
self.plot_type = plot_type
self.line_handle = None
self.plot_args = plot_args
@@ -441,8 +462,17 @@ def __plotdata_removed_from_ds__(self, item: PlotData):

def sanitize_plot_data(self, plot_data: PlotData, sanitize_lengths=True):
# do nothing if either the x or y data is set to None
if plot_data.x is None or plot_data.y is None:
if (plot_data.x is None or plot_data.y is None) and plot_data.image is None:
return None, None

if plot_data.plot_type == "image":
# If list is empty then imshow needs to plot something
if len(plot_data.image) == 0:
data_x = np.ones(shape = (2,2))
else:
data_x = plot_data.image
return data_x, None

# Copy our data into arrays that don't get updated by other threads anymore
x_data = np.array([x for x in plot_data.x]) # get data for x axis
y_data = np.array([y for y in plot_data.y]) # get data for y axis
@@ -459,13 +489,16 @@ def sanitize_plot_data(self, plot_data: PlotData, sanitize_lengths=True):
def __plotdata_changed__(self, plot_data: PlotData):
"""Gets called if a plot data item gets changed. (e.g. the y collection is overwritten with new data)"""
x_data, y_data = self.sanitize_plot_data(plot_data=plot_data)
if x_data is None or y_data is None:
if ((x_data is None) or (y_data is None)) and (plot_data.image is None):
return

if type(plot_data.line_handle) is PathCollection:
# case if plot is a scatter plot
data = np.asarray([x_data, y_data]).transpose() # prepare data such that it is compatible (2xN)
plot_data.line_handle.set_offsets(data) # update scatter plot with new data
elif type(plot_data.line_handle) is AxesImage:
# case if plot is an image
plot_data.line_handle.set_data(x_data)
elif plot_data.line_handle is not None:
# other plot types
plot_data.line_handle.set_xdata(x_data) # set x data
@@ -480,7 +513,7 @@ def __add_plot__(self, plot_data: PlotData):
"""Plots plot data according to their configuration."""

x_data, y_data = self.sanitize_plot_data(plot_data=plot_data)
if x_data is None or y_data is None:
if ((x_data is None) and (y_data is None)):
return

if plot_data.plot_type == 'plot':
@@ -490,6 +523,8 @@ def __add_plot__(self, plot_data: PlotData):
plot_data.line_handle = self.ax.plot(x_data, y_data, color=plot_data.color, **plot_data.plot_args)[0]
elif plot_data.plot_type == 'scatter':
plot_data.line_handle = self.ax.scatter(x_data, y_data, **plot_data.plot_args)
elif plot_data.plot_type == "image":
plot_data.line_handle = self.ax.imshow(x_data)
else:
raise RuntimeError("Unknown plot type. Use either plot or scatter.")

@@ -541,21 +576,24 @@ def __handle_scaling__(self):
for plot_data in self._data_source: # check each plot for its maximum and minimum values on both axis

x_data, y_data = self.sanitize_plot_data(plot_data=plot_data)
if x_data is None or y_data is None:
if x_data is None and y_data is None:
continue # skip the plot if its data is not valid because there is no data

finite_x = np.array(x_data)[np.isfinite(x_data)]
finite_y = np.array(y_data)[np.isfinite(y_data)]

length_x = len(finite_x)
length_y = len(finite_y)
if length_x == 0 or length_y == 0:
continue # skip the plot if any of the lists do not contain finite points to plot

cx_max = np.max(finite_x) # get maximum x value
cx_min = np.min(finite_x) # get minimum x value
cy_max = np.max(finite_y) # get maximum y value
cy_min = np.min(finite_y) # get minimum y value
if plot_data.plot_type == "image":
(cx_min, cx_max, cy_min, cy_max) = plot_data.line_handle.get_extent()
else:
finite_x = np.array(x_data)[np.isfinite(x_data)]
finite_y = np.array(y_data)[np.isfinite(y_data)]

length_x = len(finite_x)
length_y = len(finite_y)
if length_x == 0 or length_y == 0:
continue # skip the plot if any of the lists do not contain finite points to plot

cx_max = np.max(finite_x) # get maximum x value
cx_min = np.min(finite_x) # get minimum x value
cy_max = np.max(finite_y) # get maximum y value
cy_min = np.min(finite_y) # get minimum y value

# set all values in first iteration as comparison basis
if cx_max > x_max or first_iteration: # set new global maximum x value if the current value is higher
4 changes: 3 additions & 1 deletion configs/fiber_attach_setup_config.config
Original file line number Diff line number Diff line change
@@ -11,7 +11,9 @@
{"visa": "None", "class": "PowerMeterKoheronPD10R", "channels": [0], "args":{"lj_port": "AIN0"}},
{"visa": "None", "class": "PowerMeterKoheronPD10R", "channels": [1], "args":{"lj_port": "AIN1"}},
{"visa": "None", "class": "PowerMeterKoheronPD10R", "channels": [2], "args":{"lj_port": "AIN2"}},
{"visa": "None", "class": "PowerMeterKoheronPD10R", "channels": [3], "args":{"lj_port": "AIN3"}}
{"visa": "None", "class": "PowerMeterKoheronPD10R", "channels": [3], "args":{"lj_port": "AIN3"}},
{"visa": "None", "class": "PowerMeterKoheronPD10R", "channels": [8], "args":{"lj_port": "AIN8"}},
{"visa": "None", "class": "PowerMeterKoheronPD10R", "channels": [9], "args":{"lj_port": "AIN9"}}
],
"Camera":
[

0 comments on commit 2393f17

Please sign in to comment.