0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00

feat: map of motor position

This commit is contained in:
wyzula-jan
2023-08-18 11:30:31 +02:00
parent 947ba9f8b7
commit e6952a6d13
2 changed files with 177 additions and 126 deletions

View File

@ -13,139 +13,143 @@
<property name="windowTitle"> <property name="windowTitle">
<string>Motor Controller</string> <string>Motor Controller</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout_3" columnstretch="1,1,1" columnminimumwidth="0,0,0"> <layout class="QVBoxLayout" name="verticalLayout">
<item row="4" column="2"> <item>
<widget class="QGroupBox" name="groupBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="title">
<string>Motor Limits</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="2">
<widget class="QLabel" name="label">
<property name="text">
<string>+ samy</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="spinBox_samxN"/>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Update</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QSpinBox" name="spinBox_samyN"/>
</item>
<item row="4" column="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>- samy</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QSpinBox" name="spinBox_samyP"/>
</item>
<item row="2" column="3">
<widget class="QSpinBox" name="spinBox_samxP"/>
</item>
<item row="1" column="3">
<widget class="QLabel" name="label_3">
<property name="text">
<string>- samx</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_4">
<property name="text">
<string>- samx</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="4" column="0">
<widget class="GraphicsLayoutWidget" name="glw">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QGroupBox" name="motorControl">
<property name="title">
<string>Motor Controls</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QSpinBox" name="spinBox_step">
<property name="value">
<number>5</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QToolButton" name="toolButton_left">
<property name="text">
<string>...</string>
</property>
<property name="arrowType">
<enum>Qt::LeftArrow</enum>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="toolButton_up">
<property name="text">
<string>...</string>
</property>
<property name="arrowType">
<enum>Qt::UpArrow</enum>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="toolButton_down">
<property name="text">
<string>...</string>
</property>
<property name="arrowType">
<enum>Qt::DownArrow</enum>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QToolButton" name="toolButton_right">
<property name="text">
<string>...</string>
</property>
<property name="arrowType">
<enum>Qt::RightArrow</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_status"> <widget class="QLabel" name="label_status">
<property name="text"> <property name="text">
<string>Motor Status</string> <string>Motor Position</string>
</property> </property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignCenter</set> <set>Qt::AlignCenter</set>
</property> </property>
</widget> </widget>
</item> </item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,1,1">
<item>
<widget class="GraphicsLayoutWidget" name="glw">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="motorControl">
<property name="title">
<string>Motor Controls</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QSpinBox" name="spinBox_step">
<property name="value">
<number>5</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QToolButton" name="toolButton_left">
<property name="text">
<string>...</string>
</property>
<property name="arrowType">
<enum>Qt::LeftArrow</enum>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="toolButton_up">
<property name="text">
<string>...</string>
</property>
<property name="arrowType">
<enum>Qt::UpArrow</enum>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="toolButton_down">
<property name="text">
<string>...</string>
</property>
<property name="arrowType">
<enum>Qt::DownArrow</enum>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QToolButton" name="toolButton_right">
<property name="text">
<string>...</string>
</property>
<property name="arrowType">
<enum>Qt::RightArrow</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="title">
<string>Motor Limits</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="4" column="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>- samy</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label">
<property name="text">
<string>+ samy</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_4">
<property name="text">
<string>- samx</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="spinBox_samxN"/>
</item>
<item row="1" column="2">
<widget class="QSpinBox" name="spinBox_samyP"/>
</item>
<item row="2" column="3">
<widget class="QSpinBox" name="spinBox_samxP"/>
</item>
<item row="1" column="3">
<widget class="QLabel" name="label_3">
<property name="text">
<string>- samx</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QSpinBox" name="spinBox_samyN"/>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Update</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<customwidgets> <customwidgets>

View File

@ -1,5 +1,6 @@
import os import os
import numpy as np
from PyQt5.QtCore import pyqtSignal from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtWidgets import QApplication, QWidget
from pyqtgraph.Qt import QtCore, QtWidgets, uic from pyqtgraph.Qt import QtCore, QtWidgets, uic
@ -8,6 +9,7 @@ import pyqtgraph as pg
class MotorApp(QWidget): class MotorApp(QWidget):
motor_position = pyqtSignal(float) # TODO hook to motor position update motor_position = pyqtSignal(float) # TODO hook to motor position update
motor_update = pyqtSignal()
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -29,6 +31,10 @@ class MotorApp(QWidget):
self.image_map = pg.ImageItem() self.image_map = pg.ImageItem()
self.plot_map.addItem(self.image_map) self.plot_map.addItem(self.image_map)
self.image_map_data = np.zeros((100, 100), dtype=np.float32)
x, y = self.get_xy() # Assuming get_xy returns the motor position
self.update_image_map(x, y)
self.image_map.setImage(self.image_map_data.T) # Set the image
########################## ##########################
# Signals # Signals
########################## ##########################
@ -46,11 +52,52 @@ class MotorApp(QWidget):
lambda: self.move_motor(dev.samy, -self.spinBox_step.value()) lambda: self.move_motor(dev.samy, -self.spinBox_step.value())
) )
# Map
self.motor_position.connect(lambda x: self.update_image_map(*self.get_xy()))
# make keybindings for motor movement
self.toolButton_right.setShortcut("Right")
self.toolButton_left.setShortcut("Left")
self.toolButton_up.setShortcut("Up")
self.toolButton_down.setShortcut("Down")
# self.toolButton_left.clicked.connect(lambda: print(self.client)) # self.toolButton_left.clicked.connect(lambda: print(self.client))
self.motor_position.connect(lambda x: print(f"motor position updated: {x}"))
self.motor_update.connect(
lambda: self.label_status.setText(
f"Motor position: ({dev.samx.position():.2f}, {dev.samy.position():.2f})"
)
)
def move_motor(self, motor, value: float) -> None: def move_motor(self, motor, value: float) -> None:
scans.mv(motor, value, relative=True) scans.mv(motor, value, relative=True)
self.motor_position.emit(motor.position) motor_position = motor.position()
self.motor_position.emit(motor_position)
self.motor_update.emit()
def get_xy(self): # TODO decide if useful
"""Get current motor position"""
x = dev.samx.position()
y = dev.samy.position()
return x, y
def update_image_map(self, x, y):
"""Update the image map with the new motor position"""
# Reduce the brightness of all pixels by a fixed fraction (e.g., 5%)
self.image_map_data *= 0.8
# Modify this mapping as needed
pixel_x = int(x)
pixel_y = int(y)
# Set the bright pixel at the new position
self.image_map_data[pixel_x, pixel_y] = 255
# Update the display
self.image_map.setImage(self.image_map_data)
if __name__ == "__main__": if __name__ == "__main__":