Exemple #1
0
    def __init__(self,
                 identifier=None,
                 maxtravel=12.00,
                 Osen=None,
                 hasTrigger=False):
        self.max_travel = maxtravel
        self.waveData = None
        self.numWavePoints = 0

        self.lock = threading.Lock()

        if identifier is None:
            devices = get_connected_devices()
            identifier = devices[0]

        self.id = gcs.ConnectUSB(identifier)

        if not Osen is None:
            #self.ser_port.write('SPA A8 %3.4f\n' % Osen)
            self.osen = Osen
        else:
            self.osen = 0

        self.MAXWAVEPOINTS = 64
        if self.GetFirmwareVersion() > 3.2:
            self.MAXWAVEPOINTS = 256

        #self.ser_port.write('WTO A0\n')
        #self.ser_port.write('SVO A1\n')
        gcs.SVO(self.id, b'A', [1])

        self.lastPos = self.GetPos()

        self.driftCompensation = False
        self.hasTrigger = hasTrigger
 def ReInit(self):
     """
     Reinitialize a closed connection to the pifoc. Note the pifoc
     connection must have already been closed, whether by using `close`
     or the polling loop failing.
     """
     with self.lock:
         logging.info('restarting e816')
         self.loopActive = False
         time.sleep(1.0)
         self.id = gcs.ConnectUSB(self._identifier)
         gcs.SVO(self.id, b'A', [1])
         time.sleep(1.0)
         self.lastPos = self.GetPos()
     
     logging.info('reinitialized, starting loop')
     self._start_loop()
Exemple #3
0
    def __init__(self,
                 identifier=None,
                 maxtravel=12.00,
                 Osen=None,
                 hasTrigger=False,
                 target_tol=.002,
                 update_rate=0.005):
        """

        Parameters
        ----------
        identifier
        maxtravel
        Osen
        hasTrigger
        target_tol: float
            OnTarget tolerance, units of [um]. If position and target position are within target_tol, OnTarget() returns
            True
        update_rate: float
            Seconds for the polling thread to pause between loops.
        """
        self.max_travel = maxtravel
        #self.waveData = None
        #self.numWavePoints = 0
        self.units = 'um'
        self._target_tol = target_tol
        self._update_rate = update_rate

        self.lock = threading.Lock()

        if identifier is None:
            devices = get_connected_devices()
            identifier = devices[0]

        self.id = gcs.ConnectUSB(identifier)

        if not Osen is None:
            # self.ser_port.write('SPA A8 %3.4f\n' % Osen)
            self.osen = Osen
        else:
            self.osen = 0

        #self.MAXWAVEPOINTS = 64
        #if self.GetFirmwareVersion() > 3.2:
        #    self.MAXWAVEPOINTS = 256

        #self.ser_port.write('WTO A0\n')
        #self.ser_port.write('SVO A1\n')
        gcs.SVO(self.id, b'A', [1])

        self.servo = True
        self.errCode = 0
        self.onTarget = False

        #self.lastPos = self.GetPos()

        self.driftCompensation = False
        self.hasTrigger = hasTrigger

        self.loopActive = True
        self.stopMove = False
        self.position = np.array([0.])
        # self.velocity = np.array([self.maxvelocity, self.maxvelocity])

        self.targetPosition = np.array([maxtravel / 2.0])
        # self.targetVelocity = self.velocity.copy()

        self.lastTargetPosition = self.position.copy()

        self.tloop = threading.Thread(target=self._Loop)
        self.tloop.daemon = True
        self.tloop.start()
    def _Loop(self):
        while self.loopActive:
            self.lock.acquire()
            try:
                # check position
                time.sleep(self._update_rate)

                self.position[0] = float(gcs.qPOS(self.id, b'A')[0])+ self.osen

                self.errCode = int(gcs.qERR(self.id))

                if not self.errCode == 0:  # I have yet to see this work
                    logging.info(('Stage Error: %d' % self.errCode))

                # print self.targetPosition, self.stopMove
                if not np.all(self.targetPosition == self.lastTargetPosition):
                    # update our target position
                    pos = np.clip(self.targetPosition, 0, self.max_travel)

                    gcs.MOV(self.id, b'A', pos[:1])
                    self.lastTargetPosition = pos.copy()
                    # print('p')
                    # logging.debug('Moving piezo to target: %f' % (pos[0],))

                if np.allclose(self.position, self.targetPosition, atol=self._target_tol):
                    if not self.onTarget:
                        logEvent('PiezoOnTarget', '%.3f' % self.position[0], time.time())
                        self.onTarget = True

                # check to see if we're on target
                #self.ser_port.write('ONT? A\n')
                #self.ser_port.flushOutput()
                #time.sleep(0.005)
                #res1 = self.ser_port.readline()

                #if res1 == '':
                #    time.sleep(.5)
                #    res1 = self.ser_port.readline()

                #try:
                #    self.onTarget = int(res1) == 1
                #except ValueError:
                #    self.onTarget = False
                #    logging.exception('Value error on response from ONT')

                # time.sleep(.1)
            except RuntimeError as e:
                # gcs.fcnWrap.HandleError throws Runtimes for everything
                logger.error(str(e))
                try:
                    self.errCode = int(gcs.qERR(self.id))
                    logger.error('error code: %s' % str(self.errCode))
                except:
                    logger.error('no error code retrieved')
                if '-1' in str(e):
                    logger.debug('reinitializing GCS connection, 10 s pause')
                    gcs.CloseConnection(self.id)
                    time.sleep(10.0)  # this takes at least more than 1 s
                    try:
                        self.id = gcs.ConnectUSB(self._identifier)
                        logger.debug('restablished connection to piezo')
                    except RuntimeError as e:
                        logger.error('trying to get new device ID')
                        devices = get_connected_devices()
                        self._identifier = devices[0]
                        logger.debug('new device ID acquired')
                        self.id = gcs.ConnectUSB(self._identifier)
                    time.sleep(1.0)
                    logger.debug('turning on servo')
                    gcs.SVO(self.id, b'A', [1])
                    time.sleep(1.0)
            finally:
                self.lock.release()

        # close port on loop exit
        gcs.CloseConnection(self.id)
        logging.info("Piezo USB connection closed")