def run(self): if self._settings is None: self.callback(None, np.nan, FocusError("Settings not initialised!")) return if self._checkid is None: lockid = self._md.lock() if lockid is None: self.callback(None, np.nan, MotionError("Unable to lock the movement")) return else: lockid = self._checkid try: data, z_best, error = self._zcorrector.focus( self._settings, checkid=lockid, change_coordinates=self._change_coordinates) self._md.unlock() self.callback(data, z_best, error) except (FocusError, MotionError, CameraError) as e: logError() self.callback(None, np.nan, e) finally: self._md.unlock() self._settings = None
def run(self): HW = None try: HW = self._newConnection() except: logError() raise finally: self._callback(HW)
def set_velocity(self, V): V = V / 1000 try: velocity_parameters = self._kCubeDCServoMotor.GetVelocityParams() velocity_parameters.MaxVelocity = Decimal(float(V)) self._kCubeDCServoMotor.SetVelocityParams(velocity_parameters) except Thorlabs.MotionControl.DeviceManagerCLI.DeviceException as exc: logError() print("Can't set velocity {}".format(V))
def get_image(self, Ntries=3): try: im = self.cam.grab() except PxLerror: logError() if Ntries <= 0: raise HardwareError(f"Failed to grab") print(f"Grab fail, retrying") self.restart_streaming() return self.get_image(Ntries - 1) if self.flip_image: im = im[::-1, ::-1] return im
def run(self): try: self._parser.parse(self._filename) except FileNotFoundError: print("Can't open", self._filename) self.error.emit(f"Can't find {self._filename}") except (ScriptError, ParseError) as e: logError() self.error.emit(f"Error: {e}") except BaseException as e: print("Parse failed") print(e) raise
def try_connect(fconnect, Error, max_tests=20): connected = False Ntests = 0 while not connected and Ntests < max_tests: try: fconnect() connected = True except Error: logError() time.sleep(1) Ntests += 1 if Ntests == max_tests: raise
def restart_streaming(self, Ntries=10): try: next_state = False self.cam.streaming = False next_state = True self.cam.streaming = True except PxLerror: logError() if Ntries <= 0: raise HardwareError( f"Failed to restart streaming ({next_state})") print(f"Restart fail, retrying ({next_state})") warnings.warn( RuntimeWarning(f"Can't restart Camera ({next_state})")) self.nuclear_option() self.restart_streaming(Ntries - 1)
def show_frame(self, frame=None): try: if frame is None: frame = self.get_frame() extent = (0, frame.shape[1] * self._pixelSize, frame.shape[0] * self._pixelSize, 0) self.update_image(frame, vmin=self._vmin, vmax=self._vmax, extent=extent) except self._parent.camera_delegate.error: logError() pass except BaseException as e: print("Can't show frame", e)
def MOVVEL(self, X, V): if V[0] < 1e-3: return elif V[0] < 5: V[0] = 5 print("Speed too small") # Reverse z Z = -X[0] self.set_velocity(V[0]) try: self._move_to(Z) except MoveToInvalidPositionException: logError() raise MotionError(f"{Z} is out of range!") except MoveTimeoutException: logError() raise MotionError(f"The motion was too slow with speed {V}")
def focus(piezo): if piezo: stage = application_delegate.movement_delegate.piezo else: stage = application_delegate.movement_delegate.motor try: self.fd.focus(start_offset=float(back_input.text()), stop_offset=float(forth_input.text()), step=float(step_input.text()), stage=stage, intensity=float(intensity_input.text()), Nloops=int(float(Nloops_input.text())), speed=float(speed_input.text()), quick=quick_input.isChecked()) except FocusError as e: logError() print(e) application_delegate.error.emit(f"Focus Error: {e}")
def run(self): try: start, stop, step = self._range positions = [] for i, corner in enumerate(self._pos): corner[2] = start self._stage.goto_position(corner, speed=1000, wait=True, checkid=self.checkid) self._fd.focus(start_offset=0, stop_offset=(stop - start), step=step, stage=self._stage, wait=True, checkid=self.checkid, change_coordinates=False, quick=False, speed=1000) data, z_best, error = self._fd.get_result() if error is None: positions.append(self._stage.get_position(raw=True)) if len(positions) < 3: raise FocusError('Need at least 3 successful focus.') corrections = self._stage.corrections offset, rotation_angles = solve_z( positions, offset=corrections["offset"], rotation_angles=corrections["rotation angles"]) corrections["offset"] = offset corrections["rotation angles"] = rotation_angles self._stage.corrections = corrections except (FocusError, MotionError) as e: logError() self.error = e self.error_signal.emit(f"Plane Error {e}")
def parse_line(self, fun, args): try: fun(*args) except BaseException as e: logError() self.handle_error(e)