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 wait_end_motion(self, timeout=10):
     """Wait end of motion"""
     time.sleep(0.1)
     tstart = time.time()
     while self.is_moving():
         if timeout is not None and time.time() - tstart > timeout:
             raise MotionError('The motion took too long to complete')
         time.sleep(.01)
 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 _checklock(self, lockid=None):
        """Checks if the movement is locked"""

        if not self.locked:
            return True
        elif self.lockid == lockid:
            return True
        else:
            raise MotionError('Mouvment is locked!')
    def wait_end_motion(self, travel_time=0, timeout=None):
        """Wait end of motion"""

        time.sleep(travel_time)

        while not self.is_onTarget():
            tstart = time.time()
            if timeout is not None and time.time() - tstart > timeout:
                raise MotionError('The motion took too long to complete')
            time.sleep(.01)
Exemple #6
0
 def parse(self, filename):
     if not self.md.motor.originMoved:
         raise ScriptError("The origin needs to be defined to run a script")
     self._paused = False
     self.pause_status.emit(self._paused)
     self.lockid = self.md.lock()
     if self.lockid is None:
         raise MotionError("Can't lock motion")
     try:
         super().parse(filename)
         self.end_macro()
     except BaseException:
         print("Parse failed, setting V to 0")
         self.laser_delegate.set_intensity(0)
         raise
     finally:
         self.md.unlock()
         self.lockid = None
Exemple #7
0
    def pause_resume(self, msg='Pause', pause=None):
        if not self.isRunning():
            return False

        if pause is None:
            pause = not self._paused

        if not pause and self._paused:
            # Resume
            self.lockid = self.md.lock()
            if self.lockid is None:
                raise MotionError("Can't lock motion")
            self._paused = False
        elif pause and not self._paused:
            # Pause
            self._paused = True
            self.md.unlock()
            self.lockid = None
        self.pause_status.emit(self._paused)
        return self._paused
    def MOVVEL(self, X, V):
        X = X + self.internal_offset
        # Reverse y and z
        X[1:] = 100 - X[1:]

        if not (np.all(X >= 0) and np.all(X <= 100)):
            if self.clip_out_of_range:
                clip_neg = X[X < 0]
                clip_pos = X[X > 100] - 100
                X[X < 0] = 0
                X[X > 100] = 100
                warnings.warn(
                    RuntimeWarning(f"Clipping {clip_neg} {clip_pos}"))
            else:
                raise MotionError(f"Target Position is out of range! {X}")

        V = np.abs(V)
        V[V < 1e-3] = 1e-3
        self.__cube.VEL([1, 2, 3], list(V))
        self.__cube.MOV([1, 2, 3], list(X))
    def run_waveform(self, time_step, X, measure_time_step=None):

        if X.size >= self.max_points:
            raise MotionError(
                "The wavepoints has more points than the controller can handle"
            )
        if not (np.shape(X)[1] == 3 or np.shape(X)[1] == 4):
            raise MotionError("Waveform shape is incorrect! {np.shape(X)}")

        if measure_time_step is not None:
            self.setup_measure_rate(measure_time_step)
        # Go to first pos
        self.MOVVEL(X[0, :3], np.ones(3) * 1000)

        rate = int(np.round(time_step / self.Servo_Update_Time))

        X = self.waveToController(X)

        if not (np.all(X[..., :3] > 0) and np.all(X[..., :3] < 100)):
            if not self.clip_out_of_range:
                raise MotionError(
                    "Some points in the waveform are out of bounds.")

        # Set rate
        old_rate = self.__cube.qWTR()
        if old_rate[0] != [rate, 1]:
            print('Change rate', old_rate[0])
            self.__cube.send(f"WTR 0 {rate} 1")

        idx = np.arange(np.shape(X)[1]) + 1

        # Clear tables
        self.__cube.send("WCL " + " ".join(str(e) for e in idx))

        # Send data
        slice_size = 50
        for i in range(np.shape(X)[1]):
            append = 'X'
            for point_idx in np.arange(0, np.shape(X)[0], slice_size):
                data = X[point_idx:point_idx + slice_size, i]
                cmd = (f"WAV {i+1} {append} PNT 1 {len(data)} " +
                       " ".join(f'{e:.3f}' for e in data))
                self.__cube.send(cmd)
                append = '&'

        # Connect to wave generator
        self.__cube.send("WSL " + " ".join(str(e) for e in np.repeat(idx, 2)))

        # limit to 1 scan
        self.__cube.send('WGC ' + " ".join(str(e) + ' 1' for e in idx))

        # Offset to 0
        self.__cube.send('WOS ' + " ".join(str(e) + ' 0' for e in idx))

        # Maximum speed to a lot
        self.__cube.VEL([1, 2, 3], [VMAX, VMAX, VMAX])

        # GO
        self.__cube.send('WGO ' + " ".join(str(e) + ' 0x101' for e in idx))

        return self.Servo_Update_Time * rate * X.shape[0]