From 3e408b304ba6b755fc472ea0d39d457b93b55be9 Mon Sep 17 00:00:00 2001 From: wyzula-jan <133381102+wyzula-jan@users.noreply.github.com> Date: Thu, 24 Aug 2023 18:18:12 +0200 Subject: [PATCH] refactor: migrate to use just np.array for tracking position, only latest N points are being dimmed. --- .../examples/motor_movement/motor_example.py | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/bec_widgets/examples/motor_movement/motor_example.py b/bec_widgets/examples/motor_movement/motor_example.py index a8c2252f..bf6c2b3d 100644 --- a/bec_widgets/examples/motor_movement/motor_example.py +++ b/bec_widgets/examples/motor_movement/motor_example.py @@ -20,8 +20,9 @@ class MotorApp(QWidget): uic.loadUi(os.path.join(current_path, "motor_controller.ui"), self) self.limit_x, self.limit_y = None, None + # Coordinates tracking - self.motor_positions = [] + self.motor_positions = np.array([]) # QThread for motor movement + signals self.motor_thread = MotorControl() @@ -184,15 +185,13 @@ class MotorApp(QWidget): (map_width, map_height), self.background_value, dtype=np.float32 ) self.limit_map.setImage(self.limit_map_data) - # # Set the initial position on the map - init_pos = self.motor_thread.retrieve_coordinates() - init_brush = pg.mkBrush(255, 255, 255, 255) - self.motor_positions.append({"pos": init_pos, "brush": init_brush}) - self.motor_map.setData( - pos=[point["pos"] for point in self.motor_positions], - brush=[point["brush"] for point in self.motor_positions], - ) + # Set the initial position on the map + init_pos = self.motor_thread.retrieve_coordinates() + self.motor_positions = np.array([init_pos]) + self.brushes = [pg.mkBrush(255, 255, 255, 255)] + + self.motor_map.setData(pos=self.motor_positions, brush=self.brushes) # Translate and scale the image item to match the motor coordinates self.tr = QtGui.QTransform() @@ -203,19 +202,18 @@ class MotorApp(QWidget): # Update label self.label_coorditanes.setText(f"Motor position: ({x:.2f}, {y:.2f})") - # Dim previous points - last_brush = self.motor_positions[-1]["brush"].color().getRgb() - dimmer_brush = tuple(max(int(0.8 * c), 50) for c in last_brush[:3]) + (255,) - self.motor_positions[-1]["brush"] = pg.mkBrush(dimmer_brush) + # Determine brushes based on position in the array + self.brushes = [pg.mkBrush(50, 50, 50, 255)] * len(self.motor_positions) + for i in range(1, min(6, len(self.motor_positions) + 1)): + brightness = max(50, 255 - 20 * (i - 1)) + self.brushes[-i] = pg.mkBrush(brightness, brightness, brightness, 255) # Add new point with full brightness new_pos = (x, y) - new_brush = pg.mkBrush(255, 255, 255, 255) - self.motor_positions.append({"pos": new_pos, "brush": new_brush}) - self.motor_map.setData( - pos=[point["pos"] for point in self.motor_positions], - brush=[point["brush"] for point in self.motor_positions], - ) + self.motor_positions = np.vstack((self.motor_positions, new_pos)) + self.brushes.append(pg.mkBrush(255, 255, 255, 255)) + + self.motor_map.setData(pos=self.motor_positions, brush=self.brushes) def update_all_motor_limits( self, x_limit: list = None, y_limit: list = None