Source code for gui.widget_newport_control
"""
This widget contains the functionality of the newport_control hardware
class provided in newport_control.py. The widget can be executed as a
standalone version if the micrometerscrew is connected and the correct
port is chosen.
########################################
Functionalities provided in this widget:
########################################
1. Display COM port
2. Set the position in mm to where the screw should move to (if the
parameters in the newport screw configuration have been changed the
screw can drive to far. If someone changed the configuration it is
extremely bad and I wish him Patla paikhana)
3. Set the velocity in mm/s
4. Set an upper and lower bound between which the stage can move in
continuous movement. These bounds are also used for the split sample
experiment.
5. Choose the stepsize to move it in steps between lower and upper
bound.
6. Start continuous movement which is not influenced by the stepsize
parameter (since it is continuous).
"""
if __name__ == "__main__":
# Add directories to path for imports
import os, sys, inspect
currentdir = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe()))
)
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, os.path.join(currentdir, "ui_files"))
sys.path.insert(0, parentdir)
sys.path.insert(0, os.path.join(parentdir, "hardware_interfaces"))
# PyQt imports
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QRunnable, QThreadPool
# Import RegEx input validators
from regex_validators import *
# Import Qt Threading wrapper
from qt_multithreading_wrapper import Worker
# Import of file made from UI designer
from ui_newport_control import Ui_GroupBox_newport as Ui_newport
# Import newport control
from newport_control import NewportControl
# Logger Settings
import logging
# logging.basicConfig(level=logging.INFO)
[docs]class WidgetNewportControl(QtWidgets.QGroupBox, Ui_newport):
def __init__(self, *args, obj=None, mu_screw: NewportControl = None, **kwargs):
super(QtWidgets.QGroupBox, self).__init__(*args, **kwargs)
self.setupUi(self)
self.mu_screw = mu_screw
# Instantiate threadpool
self.threadpool = QThreadPool()
self.update_values()
# Setting input validators -----------------------------
self.lineEdit_newport_pos.setValidator(
positive_float_input_validator
) # Assign input validator to this lineEdit
self.lineEdit_newport_vel.setValidator(
positive_float_input_validator
) # Assign input validator to this lineEdit
self.lineEdit_newport_upper_bound.setValidator(
positive_float_input_validator
) # Assign input validator to this lineEdit
self.lineEdit_newport_lower_bound.setValidator(
positive_float_input_validator
) # Assign input validator to this lineEdit
self.lineEdit_newport_stepsize.setValidator(
positive_float_input_validator
) # Assign input validator to this lineEdit
# ------------------------------------------------------
# Setup signal connections ----------------------------
self.lineEdit_newport_pos.returnPressed.connect(self.update_position)
self.lineEdit_newport_vel.returnPressed.connect(self.update_velocity)
self.lineEdit_newport_upper_bound.editingFinished.connect(
self.update_upper_bound
)
self.lineEdit_newport_lower_bound.editingFinished.connect(
self.update_lower_bound
)
self.pushButton_newport_continuous_movement.clicked.connect(
self.toggle_continuous_move
)
# ------------------------------------------------------
[docs] def update_values(self):
if self.mu_screw:
self.lineEdit_newport_com_port.setText(str(self.mu_screw.port))
self.lineEdit_newport_pos.setText(str(round(self.mu_screw.position, 6)))
self.lineEdit_newport_vel.setText(str(round(self.mu_screw.velocity, 6)))
self.lineEdit_newport_upper_bound.setText(
str(round(self.mu_screw.upper_bound, 6))
)
self.lineEdit_newport_lower_bound.setText(
str(round(self.mu_screw.lower_bound, 6))
)
# Disable the position and velocity lineEdit When the stage
# is moving
self.lineEdit_newport_pos.setDisabled(self.mu_screw.continuously_moving)
self.lineEdit_newport_vel.setDisabled(self.mu_screw.continuously_moving)
# Change text of button depending on whether movement can be
# stopped or started
if self.mu_screw.continuously_moving:
self.pushButton_newport_continuous_movement.setText(
"Stop Continuous Movement"
)
else:
self.pushButton_newport_continuous_movement.setText(
"Start Continuous Movement"
)
else:
self.setDisabled(True)
[docs] def work(self, qt_obj: QtWidgets, mu_screw_attr: str, mu_screw_method: str):
def run(qt_obj: QtWidgets, mu_screw_attr: str, mu_screw_method: str):
current_value = getattr(self.mu_screw, mu_screw_attr)
# Get requested value
requested_value = getattr(qt_obj, "text")()
# Convert requested value to correct type
requested_value = type(current_value)(requested_value)
if round(current_value, 6) != requested_value:
# Call method / Execute
getattr(self.mu_screw, mu_screw_method)(requested_value)
worker = Worker(run, qt_obj, mu_screw_attr, mu_screw_method)
# Deactivate GUI
worker.signals.started.connect(lambda: self.setDisabled(True))
# Reactivate GUI
worker.signals.finished.connect(lambda: self.setEnabled(True))
# Update values
worker.signals.finished.connect(self.update_values)
self.threadpool.start(worker)
[docs] def update_upper_bound(self):
self.mu_screw.upper_bound = float(self.lineEdit_newport_upper_bound.text())
[docs] def update_lower_bound(self):
self.mu_screw.lower_bound = float(self.lineEdit_newport_lower_bound.text())
[docs] def toggle_continuous_move(self):
# This needs a seperate run/worker function because
# toggle_continuous_movement does not take any arguments
worker = Worker(self.mu_screw.toggle_continuous_movement)
# Deactivate GUI
worker.signals.started.connect(lambda: self.setDisabled(True))
# Reactivate GUI
worker.signals.finished.connect(lambda: self.setEnabled(True))
# Update values
worker.signals.finished.connect(self.update_values)
self.threadpool.start(worker)
if __name__ == "__main__":
# TESTING -----------------------------------------------------------
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, mu_screw, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Please god let this work too")
widget = WidgetNewportControl(mu_screw=mu_screw)
self.setCentralWidget(widget)
app = QtWidgets.QApplication(sys.argv)
# Initial settings for newport screw
mu_screw_port = "COM5" #! ---------
upper_bound = 12
lower_bound = 8
with NewportControl(
port=mu_screw_port,
reference_time=1,
lower_bound=lower_bound,
upper_bound=upper_bound,
) as mu_screw:
window = MainWindow(mu_screw)
window.show()
app.exec()