Skip to content

Commit

Permalink
moving calculating_rectangle to scanning utils
Browse files Browse the repository at this point in the history
  • Loading branch information
edyoshikun committed Dec 12, 2023
1 parent a8249a6 commit b6cf5e0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 104 deletions.
2 changes: 1 addition & 1 deletion copylot/assemblies/photom/demo/demo_photom_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
# %%

curr_pos = photom_device.get_position(mirror_index=0)

print(curr_pos)
#%%
photom_device.set_position(mirror_index=0,position=[0.009,0.009])
curr_pos = photom_device.get_position(mirror_index=0)
print(curr_pos)

# %%
photom_device.calibrate(mirror_index=0, rectangle_size_xy=(0.01, 0.01))
107 changes: 8 additions & 99 deletions copylot/assemblies/photom/demo/photom_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
QFileDialog,
)
from PyQt5.QtGui import QColor, QPen
from copylot.assemblies.photom.utils.affine_transform import AffineTransform
from copylot.assemblies.photom.utils.scanning_algorithms import (
calculate_rectangle_corners,
)
import numpy as np
from copylot.assemblies.photom.photom import PhotomAssembly
from pathlib import Path
from typing import Tuple

DEMO_MODE = True

# TODO: deal with the logic when clicking calibrate. Mirror dropdown
# TODO: check that the calibration step is implemented properly.
# TODO: connect marker to actual mirror position
# TODO: connect marker to actual mirror position. Unclear why it's not working.


class LaserWidget(QWidget):
Expand Down Expand Up @@ -377,19 +378,6 @@ def update_transparency(self, value):
opacity = 1.0 - (transparency_percent / 100.0) # Calculate opacity (0.0 to 1.0)
self.photom_window.setWindowOpacity(opacity) # Update photom_window opacity

def calculate_rectangle_corners(self, window_size):
# window_size is a tuple of (width, height)

# Calculate the coordinates of the rectangle corners
x0y0 = (
-window_size[0] / 2,
-window_size[1] / 2,
)
x1y0 = (x0y0[0] + window_size[0], x0y0[1])
x1y1 = (x0y0[0] + window_size[0], x0y0[1] + window_size[1])
x0y1 = (x0y0[0], x0y0[1] + window_size[1])
return x0y0, x1y0, x1y1, x0y1

def display_rectangle(self):
# Calculate the coordinates of the rectangle corners
rectangle_scaling = 0.5
Expand All @@ -398,7 +386,7 @@ def display_rectangle(self):
(window_size[0] * rectangle_scaling),
(window_size[1] * rectangle_scaling),
)
rectangle_coords = self.calculate_rectangle_corners(rectangle_size)
rectangle_coords = calculate_rectangle_corners(rectangle_size)
self.photom_window.updateVertices(rectangle_coords)
self.photom_window.switch_to_calibration_scene()

Expand Down Expand Up @@ -529,87 +517,7 @@ def mouseReleaseEvent(self, event):
import os

if DEMO_MODE:

class MockLaser:
def __init__(self, name, power=0, **kwargs):
# Initialize the mock laser
self.name = name
self.laser_on = False

self.toggle_emission = 0
self.laser_power = power

@property
def toggle_emission(self):
"""
Toggles Laser Emission On and Off
(1 = On, 0 = Off)
"""
print(f'Toggling laser {self.name} emission')
return self._toggle_emission

@toggle_emission.setter
def toggle_emission(self, value):
"""
Toggles Laser Emission On and Off
(1 = On, 0 = Off)
"""
print(f'Laser {self.name} emission set to {value}')
self._toggle_emission = value

@property
def laser_power(self):
print(f'Laser {self.name} power: {self.power}')
return self.power

@laser_power.setter
def laser_power(self, power):
self.power = power
print(f'Laser {self.name} power set to {power}')

class MockMirror:
def __init__(self, name, pos_x=0, pos_y=0, **kwargs):
# Initialize the mock mirror with the given x and y positions
self.name = name

self.pos_x = pos_x
self.pos_y = pos_y

self.position = (self.pos_x, self.pos_y)

@property
def position(self):
print(f'Getting mirror position ({self.pos_x}, {self.pos_y})')
return self.position_x, self.position_y

@position.setter
def position(self, value: Tuple[float, float]):
self.position_x = value[0]
self.position_y = value[1]
print(f'Mirror {self.name} position set to {value}')

@property
def position_x(self) -> float:
"""Get the current mirror position X"""
print(f'Mirror {self.name} Position_X {self.pos_x}')
return self.pos_x

@position_x.setter
def position_x(self, value: float):
"""Set the mirror position X"""
self.pos_x = value
print(f'Mirror {self.name} Position_X {self.pos_x}')

@property
def position_y(self) -> float:
"""Get the current mirror position Y"""
return self.pos_y

@position_y.setter
def position_y(self, value: float):
"""Set the mirror position Y"""
self.pos_y = value
print(f'Mirror {self.name} Position_Y {self.pos_y}')
from copylot.assemblies.photom.photom_mock_devices import MockLaser, MockMirror

Laser = MockLaser
Mirror = MockMirror
Expand All @@ -621,6 +529,7 @@ def position_y(self, value: float):

try:
os.environ["DISPLAY"] = ":1003"

except:
raise Exception("DISPLAY environment variable not set")

Expand Down Expand Up @@ -681,7 +590,7 @@ def position_y(self, value: float):
(window_size[0] * rectangle_scaling),
(window_size[1] * rectangle_scaling),
)
rectangle_coords = ctrl_window.calculate_rectangle_corners(rectangle_size)
rectangle_coords = calculate_rectangle_corners(rectangle_size)
# translate each coordinate by the offset
rectangle_coords = [(x + 30, y) for x, y in rectangle_coords]
camera_window.updateVertices(rectangle_coords)
Expand Down
16 changes: 12 additions & 4 deletions copylot/assemblies/photom/photom.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def calibrate(self, mirror_index: int, rectangle_size_xy: tuple[int, int]):
i = 0
while self._calibrating:
# Logic for calibrating the mirror
self.position(mirror_index, rectangle_coords[i])
self.set_position(mirror_index, rectangle_coords[i])
time.sleep(1)
i += 1
if i == 3:
Expand All @@ -55,6 +55,12 @@ def calibrate(self, mirror_index: int, rectangle_size_xy: tuple[int, int]):
else:
raise IndexError("Mirror index out of range.")

def stop_mirror(self, mirror_index: int):
if mirror_index < len(self.mirror):
self._calibrating = False
else:
raise IndexError("Mirror index out of range.")

# TODO probably will replace the camera with zyx or yx image array input
## Camera Functions
def capture(self):
Expand All @@ -66,7 +72,8 @@ def get_position(self, mirror_index: int) -> list[float, float]:
if self.DAC is None:
NotImplementedError("No DAC found.")
else:
return list(self.mirror[mirror_index].position)
position = self.mirror[mirror_index].position
return list(position)
else:
raise IndexError("Mirror index out of range.")

Expand All @@ -82,8 +89,9 @@ def set_position(self, mirror_index: int, position: list[float]):
raise IndexError("Mirror index out of range.")

## LASER Fuctions
def get_laser_power(self, laser_index: int, power: float):
return self.laser[laser_index].power
def get_laser_power(self, laser_index: int) -> float:
power = self.laser[laser_index].power
return power

def set_laser_power(self, laser_index: int, power: float):
self.laser[laser_index].power = power
14 changes: 14 additions & 0 deletions copylot/assemblies/photom/utils/scanning_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,17 @@ def generate_rect(self):
cord_x.append(x)
cord_y.append(y)
return cord_x, cord_y


def calculate_rectangle_corners(window_size):
# window_size is a tuple of (width, height)

# Calculate the coordinates of the rectangle corners
x0y0 = (
-window_size[0] / 2,
-window_size[1] / 2,
)
x1y0 = (x0y0[0] + window_size[0], x0y0[1])
x1y1 = (x0y0[0] + window_size[0], x0y0[1] + window_size[1])
x0y1 = (x0y0[0], x0y0[1] + window_size[1])
return x0y0, x1y0, x1y1, x0y1

0 comments on commit b6cf5e0

Please sign in to comment.