|
| 1 | +import logging |
| 2 | +import requests |
| 3 | +import json |
| 4 | +from PyQt5.QtCore import QObject, QTimer |
| 5 | + |
| 6 | +# Set logger name |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | +logger.setLevel(logging.WARNING) |
| 9 | + |
| 10 | +# Set the logging level for PyQt5.uic.uiparser/properties to WARNING to ignore DEBUG messages |
| 11 | +logging.getLogger("PyQt5.uic.uiparser").setLevel(logging.WARNING) |
| 12 | +logging.getLogger("PyQt5.uic.properties").setLevel(logging.WARNING) |
| 13 | + |
| 14 | +class StageController(QObject): |
| 15 | + def __init__(self, model): |
| 16 | + super().__init__() |
| 17 | + self.model = model |
| 18 | + self.url = self.model.stage_listener_url |
| 19 | + self.timer_count = 0 |
| 20 | + |
| 21 | + # These commands will be updated dynamically based on the parsed probe index |
| 22 | + self.probeStepMode_command = { |
| 23 | + "PutId": "ProbeStepMode", |
| 24 | + "Probe": 0, # Default value, will be updated dynamically |
| 25 | + "StepMode": 0 # StepMode=0 (for Coarse), =1 (for Fine), =2 (for Insertion) |
| 26 | + } |
| 27 | + self.probeMotion_command = { |
| 28 | + "PutId" : "ProbeMotion", |
| 29 | + "Probe": 0, # Probe=0 (for probe A), =1 (for Probe B), etc. Default value, will be updated dynamically |
| 30 | + "Absolute": 1, # Absolute=0 (for relative move) =1 (for absolute target) |
| 31 | + "Stereotactic": 0, # Stereotactic=0 (for local [stage] coordinates) =1 (for stereotactic) |
| 32 | + "AxisMask": 7 # AxisMask=1 (for X), =2 (for Y), =4 (for Z) or any combination (e.g. 7 for XYZ) |
| 33 | + } |
| 34 | + |
| 35 | + self.probeStop_command = { |
| 36 | + "PutId": "ProbeStop", |
| 37 | + "Probe": 0 # Default value, will be updated dynamically |
| 38 | + } |
| 39 | + |
| 40 | + def stop_request(self, command): |
| 41 | + move_type = command["move_type"] |
| 42 | + if move_type == "stopAll": |
| 43 | + # Stop the timer if it's active |
| 44 | + if hasattr(self, 'timer') and self.timer.isActive(): |
| 45 | + self.timer.stop() |
| 46 | + logger.info("Timer stopped. Outside SW may be interrupting.") |
| 47 | + |
| 48 | + # Get the status to retrieve all available probes |
| 49 | + status = self._get_status() |
| 50 | + if status is None: |
| 51 | + logger.warning("Failed to retrieve status while trying to stop all probes.") |
| 52 | + return |
| 53 | + |
| 54 | + # Iterate over all probes and send the stop command |
| 55 | + probe_array = status.get("ProbeArray", []) |
| 56 | + for i, probe in enumerate(probe_array): |
| 57 | + self.probeStop_command["Probe"] = i # Set the correct probe index |
| 58 | + self._send_command(self.probeStop_command) |
| 59 | + logger.info(f"Sent stop command to probe {i}") |
| 60 | + logger.info("Sent stop command to all available probes.") |
| 61 | + |
| 62 | + def move_request(self, command): |
| 63 | + """ |
| 64 | + input format: |
| 65 | + command = { |
| 66 | + "stage_sn": stage_sn, |
| 67 | + "move_type": move_type # "moveXY" |
| 68 | + "x": x, |
| 69 | + "y": y, |
| 70 | + "z": z |
| 71 | + } |
| 72 | + """ |
| 73 | + move_type = command["move_type"] |
| 74 | + stage_sn = command["stage_sn"] |
| 75 | + # Get index of the probe based on the serial number |
| 76 | + probe_index = self._get_probe_index(stage_sn) |
| 77 | + if probe_index is None: |
| 78 | + logger.warning(f"Failed to get probe index for stage: {stage_sn}") |
| 79 | + return |
| 80 | + |
| 81 | + if move_type == "moveXY": |
| 82 | + # update command to coarse and the command |
| 83 | + self.probeStepMode_command["Probe"] = probe_index |
| 84 | + self._send_command(self.probeStepMode_command) |
| 85 | + |
| 86 | + # update command to move z to 15 |
| 87 | + self._update_move_command(probe_index, x=None, y=None, z=15.0) |
| 88 | + # move the probe |
| 89 | + self._send_command(self.probeMotion_command) |
| 90 | + |
| 91 | + # Reset timer_count for this new move command |
| 92 | + self.timer_count = 0 |
| 93 | + self.timer = QTimer(self) |
| 94 | + self.timer.setInterval(500) # 500 ms |
| 95 | + self.timer.timeout.connect(lambda: self._check_z_position(probe_index, 15.0, command)) |
| 96 | + self.timer.start() |
| 97 | + |
| 98 | + def _check_z_position(self, probe_index, target_z, command): |
| 99 | + """Check Z position and proceed with X, Y movement once target is reached.""" |
| 100 | + self.timer_count += 1 |
| 101 | + if self.timer_count > 30: # 30 * 500 ms = 15 seconds |
| 102 | + if hasattr(self, 'timer') and self.timer.isActive(): |
| 103 | + self.timer.stop() |
| 104 | + logger.warning("Timer stopped due to timeout.") |
| 105 | + return |
| 106 | + |
| 107 | + if self._is_z_at_target(probe_index, target_z): |
| 108 | + if hasattr(self, 'timer') and self.timer.isActive(): |
| 109 | + self.timer.stop() |
| 110 | + logger.info("Timer stopped due to z is on the target.") |
| 111 | + |
| 112 | + # Update command to move (x, y, 0) |
| 113 | + x = command["x"] |
| 114 | + y = command["y"] |
| 115 | + self._update_move_command(probe_index, x=x, y=y, z=None) |
| 116 | + # Move the probe |
| 117 | + self._send_command(self.probeMotion_command) |
| 118 | + |
| 119 | + def _is_z_at_target(self, probe_index, target_z): |
| 120 | + """Check if the probe's Z coordinate has reached the target value.""" |
| 121 | + status = self._get_status() |
| 122 | + if status is None: |
| 123 | + return False |
| 124 | + |
| 125 | + # Find the correct probe in the status by probe index |
| 126 | + probe_array = status.get("ProbeArray", []) |
| 127 | + if probe_index >= len(probe_array): |
| 128 | + logger.warning(f"Invalid probe index: {probe_index}") |
| 129 | + return False |
| 130 | + |
| 131 | + current_z = probe_array[probe_index].get("Stage_Z", None) |
| 132 | + if current_z is None: |
| 133 | + logger.warning(f"Failed to retrieve Z position for probe {probe_index}") |
| 134 | + return False |
| 135 | + |
| 136 | + # Return whether the current Z value is close enough to the target |
| 137 | + return abs(current_z - target_z) < 0.01 # Tolerance of 10 um |
| 138 | + |
| 139 | + def _update_move_command(self, probe_index, x=None, y=None, z=None): |
| 140 | + self.probeMotion_command["Probe"] = probe_index |
| 141 | + if x is not None: |
| 142 | + self.probeMotion_command["X"] = x |
| 143 | + if y is not None: |
| 144 | + self.probeMotion_command["Y"] = y |
| 145 | + if z is not None: |
| 146 | + self.probeMotion_command["Z"] = z |
| 147 | + |
| 148 | + axis_mask = 0 |
| 149 | + if x is not None: |
| 150 | + axis_mask |= 1 # X-axis |
| 151 | + if y is not None: |
| 152 | + axis_mask |= 2 # Y-axis |
| 153 | + if z is not None: |
| 154 | + axis_mask |= 4 # Z-axis |
| 155 | + self.probeMotion_command["AxisMask"] = axis_mask |
| 156 | + |
| 157 | + def _get_probe_index(self, stage_sn): |
| 158 | + status = self._get_status() |
| 159 | + if status is None: |
| 160 | + return None |
| 161 | + |
| 162 | + # Find probe index based on serial number |
| 163 | + probe_array = status.get("ProbeArray", []) |
| 164 | + for i, probe in enumerate(probe_array): |
| 165 | + if probe["SerialNumber"] == stage_sn: |
| 166 | + return i # Set the corresponding probe index |
| 167 | + |
| 168 | + return None |
| 169 | + |
| 170 | + def _get_status(self): |
| 171 | + response = requests.get(self.url) |
| 172 | + if response.status_code == 200: |
| 173 | + try: |
| 174 | + return response.json() |
| 175 | + except json.JSONDecodeError: |
| 176 | + print("Response is not in JSON format:", response.text) |
| 177 | + return None |
| 178 | + else: |
| 179 | + print(f"Failed to get status: {response.status_code}, {response.text}") |
| 180 | + return None |
| 181 | + |
| 182 | + def _send_command(self, command): |
| 183 | + headers = {'Content-Type': 'application/json'} |
| 184 | + requests.put(self.url, data=json.dumps(command), headers=headers) |
0 commit comments