From e6952a6d13c84487fd6ab08056f1f5b46d594b8a Mon Sep 17 00:00:00 2001
From: wyzula-jan <133381102+wyzula-jan@users.noreply.github.com>
Date: Fri, 18 Aug 2023 11:30:31 +0200
Subject: [PATCH] feat: map of motor position
---
.../motor_movement/motor_controller.ui | 254 +++++++++---------
.../examples/motor_movement/motor_example.py | 49 +++-
2 files changed, 177 insertions(+), 126 deletions(-)
diff --git a/bec_widgets/examples/motor_movement/motor_controller.ui b/bec_widgets/examples/motor_movement/motor_controller.ui
index ebb64c7b..ba2dc0da 100644
--- a/bec_widgets/examples/motor_movement/motor_controller.ui
+++ b/bec_widgets/examples/motor_movement/motor_controller.ui
@@ -13,139 +13,143 @@
Motor Controller
-
- -
-
-
- false
-
-
- Motor Limits
-
-
-
-
-
-
- + samy
-
-
-
- -
-
-
- -
-
-
- Update
-
-
-
- -
-
-
- -
-
-
- - samy
-
-
-
- -
-
-
- -
-
-
- -
-
-
- - samx
-
-
-
- -
-
-
- - samx
-
-
-
-
-
-
- -
-
-
- false
-
-
-
- -
-
-
- Motor Controls
-
-
-
-
-
-
- 5
-
-
-
- -
-
-
- ...
-
-
- Qt::LeftArrow
-
-
-
- -
-
-
- ...
-
-
- Qt::UpArrow
-
-
-
- -
-
-
- ...
-
-
- Qt::DownArrow
-
-
-
- -
-
-
- ...
-
-
- Qt::RightArrow
-
-
-
-
-
-
- -
+
+
-
- Motor Status
+ Motor Position
Qt::AlignCenter
+ -
+
+
-
+
+
+ false
+
+
+
+ -
+
+
+ Motor Controls
+
+
+
-
+
+
+ 5
+
+
+
+ -
+
+
+ ...
+
+
+ Qt::LeftArrow
+
+
+
+ -
+
+
+ ...
+
+
+ Qt::UpArrow
+
+
+
+ -
+
+
+ ...
+
+
+ Qt::DownArrow
+
+
+
+ -
+
+
+ ...
+
+
+ Qt::RightArrow
+
+
+
+
+
+
+ -
+
+
+ false
+
+
+ Motor Limits
+
+
+
-
+
+
+ - samy
+
+
+
+ -
+
+
+ + samy
+
+
+
+ -
+
+
+ - samx
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ - samx
+
+
+
+ -
+
+
+ -
+
+
+ Update
+
+
+
+
+
+
+
+
diff --git a/bec_widgets/examples/motor_movement/motor_example.py b/bec_widgets/examples/motor_movement/motor_example.py
index 2c0d5106..27eaaf73 100644
--- a/bec_widgets/examples/motor_movement/motor_example.py
+++ b/bec_widgets/examples/motor_movement/motor_example.py
@@ -1,5 +1,6 @@
import os
+import numpy as np
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QWidget
from pyqtgraph.Qt import QtCore, QtWidgets, uic
@@ -8,6 +9,7 @@ import pyqtgraph as pg
class MotorApp(QWidget):
motor_position = pyqtSignal(float) # TODO hook to motor position update
+ motor_update = pyqtSignal()
def __init__(self):
super().__init__()
@@ -29,6 +31,10 @@ class MotorApp(QWidget):
self.image_map = pg.ImageItem()
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
##########################
@@ -46,11 +52,52 @@ class MotorApp(QWidget):
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.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:
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__":