Ejemplo n.º 1
0
  def getMoreImages(self, iNumImage):
    for iImage in xrange(iNumImage):
      if (not self.bSimMode) and self.mccBurst.mccBurstCheckStatusChange():
        print "Burst Mode status changed"
        break

      timeBeforeImage = time.time()

      print "# Image %d/%d (%d Shots) [total images: %d]" % (1+iImage, iNumImage, self.iNumShot, self.iNumTotalImage)
      iEventStart = self.daq.eventnum()

      caput(pvPlayCtrl, 1) # PlayCtrl = Play

      timeAfterPlay = time.time()
      
      time.sleep(0.05)
      while True:
        if caget(pvPlayStat).find("Stopped") != -1:
          break
        if self.bSimMode:
          iSeqStep = int(caget(pvPlayCurStep))
          print "Burst count: %d / %d\r" % (iSeqStep, self.iNumShot),
          sys.stdout.flush()
        time.sleep(0.05)

      if self.pvLaserShutter:
        caput(self.pvLaserShutter, self.iShutterOpenVal)
        time.sleep(0.5)
        iLaserShutterState = int(caget(self.pvLaserShutter))
        if iLaserShutterState != self.iShutterOpenVal:
          print "!!! Shutter %s Not Open: Current value = %d (expected %d)" % (self.pvLaserShutter, iLaserShutterState, self.iShutterOpenVal)

      if not self.bSimMode:
        time.sleep(0.02)
        while True:
          if self.mccBurst.mccBurstIsComplete():
            break
          iNumBurstCount = self.mccBurst.mccBurstCount()
          print "Burst count: %d / %d\r" % (iNumBurstCount, self.iNumShot),
          sys.stdout.flush()
          time.sleep(0.05)

      timeAfterBurstComplete = time.time()

      if self.pvLaserShutter:
        caput(self.pvLaserShutter, self.iShutterCloseVal)
        time.sleep(0.5)
        iLaserShutterState = int(caget(self.pvLaserShutter))
        if iLaserShutterState != self.iShutterCloseVal:
          print "!!! Shutter %s Not Closed: Current value = %d (expected %d)" % (self.pvLaserShutter, iLaserShutterState, self.iShutterCloseVal)

      while True:
        if self.daq.eventnum() >= iEventStart + self.iNumShot:
          break
        print "DAQ event: %d / %d\r" % (self.daq.eventnum(), iEventStart + self.iNumShot),
        sys.stdout.flush()
        time.sleep(0.05)
      print "                           \r",

      self.iNumTotalImage += 1
Ejemplo n.º 2
0
    def load(self, verbose=False):
      
        if verbose:
            print_file = sys.stdout
        else:
            print_file = open(os.devnull, 'w')
    
        bPrintSet = True # Always print "set ..." in caput()    	
        print >> print_file, caput( pvSeqEvent    , self.event_codes    , verbose=bPrintSet)
        print >> print_file, caput( pvSeqBeamDelay, self.beam_delays    , verbose=bPrintSet)
        print >> print_file, caput( pvSeqFiduDelay, self.fiducial_delays, verbose=bPrintSet)
        print >> print_file, caput( pvSeqBurst    , self.burst_requests , verbose=bPrintSet)

        print >> print_file, caput( pvSeqLen      , self.length         , verbose=bPrintSet)

        print >> print_file, caput( pvSeqUpdate   , 1                   , verbose=bPrintSet)

        sSeqStatus  = caget(pvSeqStatus)
        iErrorIndex = int(caget(pvSeqErrorIndex))

        if sSeqStatus.find("Valid Sequence") != -1:
          print "Status: %s" % sSeqStatus
          return 0

        print "!! Status: %s  Error Index: %d" % (sSeqStatus, iErrorIndex)
        return 1
Ejemplo n.º 3
0
 def mccBurstCheckStatusChange(self):
   if self.bSimMode:
     return False
   bMccBurstCheck   = not (caget(pvMccBurstReqBykik).find("No") != -1 and caget(pvMccBurstReqPoc).find("No") != -1)
   if bMccBurstCheck != self.bMccBurstRunning:
     print "Original Burst mode: %s  Current: %s" % (("Yes" if self.bMccBurstRunning else "No"), ("Yes" if bMccBurstCheck else "No"))
     return True
   return False
Ejemplo n.º 4
0
 def mccBurstSetRate(self, fBurstRate):
   if self.bSimMode:
     return True
   if caget(pvBeamOwner) != caget(pvHutchId):
     print "Settings Test Burst Rate %f (%d)" % (fBurstRate, self.dictBeamToBurstRate[fBurstRate])
     caput(pvMccTestBurstDep   , 0)
     caput(pvMccTestBurstRate  , self.dictBeamToBurstRate[fBurstRate])
   else:
     print "Settings Mcc Burst Rate %f (%d)" % (fBurstRate, self.dictBeamToBurstRate[fBurstRate])
     caput(pvMccBurstRate   , self.dictBeamToBurstRate[fBurstRate])
   return 0
def wait(motor):
  time.sleep(0.8)
  while(True):
    status = caget("MEC:HEX:01:moving")
    if status == "In Position":
      break
    time.sleep(0.02)
Ejemplo n.º 6
0
    def stopAtSeqEnd(self):
        if caget(pvPlayStat).find("Stopped") != -1:
            return

        iPlayCountOld = caget(pvPlayCount)
        print "Waiting for current round to finish..."
        while True:
            iPlayCountNew = caget(pvPlayCount)
            if iPlayCountNew != iPlayCountOld:
                caput(pvPlayCtrl, (0, ))  # PlayCtrl = Stop
            if caget(pvPlayStat).find("Stopped") != -1:
                break

            time.sleep(0.05)

        print "Done."
        return
Ejemplo n.º 7
0
    def runOnce(self):
        caput(pvPlayMode, (0, ))  # PlayMode = Once
        caput(pvPlayCtrl, (1, ))  # PlayCtrl = Play

        while True:
            if caget(pvPlayStat).find("Stopped") != -1:
                break

            time.sleep(0.05)
Ejemplo n.º 8
0
  def run(self, princetonDaq, iShutter):
    iFail = princetonDaq.init()
    if iFail != 0:
      return 1

    princetonDaq.begin(princetonDaq.controls)
    
    try:
      while True:
        #
        #  Wait for the user to continue
        #
        print('--Enter a number + <Enter> to get more images, or just Hit <Enter> to end run and exit--')
        sMoreImage = raw_input("> ")

        try:
          iMoreImage = int(sMoreImage)
        except:
          iMoreImage = 0

        if iMoreImage <= 0:
          break

        print("Will get %d more images..." % iMoreImage)
        princetonDaq.getMoreImages(iMoreImage)
        # go to next loop to get more Images
      #end of while True:

      daq = princetonDaq.daq
      print "# (If Record Run is ON) Experiment %d Run %d (%d images)" % (daq.experiment(),daq.runnumber(), princetonDaq.numTotalImage())
      princetonDaq.deinit()
      time.sleep(0.5)
    except:
      print
      if iShutter != 0:
        for pvLaserShutter in princetonController.lPvShutter:
          caput(pvLaserShutter, princetonController.iShutterCloseVal)
          time.sleep(0.5)
          iLaserShutterState = int(caget(pvLaserShutter))
          if iLaserShutterState != princetonController.iShutterCloseVal:
            print "!!! Shutter %s Not Close: Current value = %d (expected %d)" % (pvLaserShutter, iLaserShutterState, princetonController.iShutterCloseVal)
        print "!!! Please remember to check if laser shutters are closed"
        print "!!! Script exits abnormally (may be interrupted by user)"

    return 0
Ejemplo n.º 9
0
  def init(self):
    self.pvLaserShutter = None
    if self.iShutter > 0 and self.iShutter <= len(self.lPvShutter):
      self.pvLaserShutter = self.lPvShutter[self.iShutter-1]

    fBeamFullRate = float(caget(pvBeamRate));
    if not self.bSimMode:
      print "\n## Beam rate = %.1f HZ" % (fBeamFullRate)
      if not (self.dictBeamToBurstRate.has_key(fBeamFullRate)):
        print "!!! Beam rate is not stable, please wait for beam to stablize and run the script again"
        return 1
      self.mccBurst.mccBurstCheckInit()

    if self.fBurstRate == -1:
      if self.bSimMode:
        self.fBurstRate = 120.0
      else:
        self.fBurstRate = fBeamFullRate

    if not self.bSimMode:
      self.mccBurst.mccBurstSetNumShot(self.iNumShot, self.dictBeamToBurstRate[self.fBurstRate])

    print "## Burst rate = %.1f HZ"   % (self.fBurstRate)
    print "## Multiple shot: %d" % (self.iNumShot)

    self.daq = pydaq.Control(self.daq_host, self.daq_platform)

    print ' *** DaqMultipleShot.init(): self.controls =', self.controls

    #
    #  Send the structure the first time to put the control variables
    #    in the file header

    print "daq connect...",
    sys.stdout.flush()
    try:
      self.daq.connect() # get dbpath and key from DAQ
    except Exception, e:
      print e
      print "Please select devices and click Select in the DAQ Control window"
Ejemplo n.º 10
0
 def mccBurstCount(self):
   if self.bSimMode:
     return 0
   return int(caget(pvMccBurstCount))
Ejemplo n.º 11
0
  def getMoreImages(self, iNumImage):
    for iImage in xrange(iNumImage):
      if (not self.bSimMode) and self.mccBurst.mccBurstCheckStatusChange():
        print "Burst Mode status changed"
        break

      timeBeforeImage = time.time()

      print "# Image %d/%d (%d Shots) [total images: %d]" % (1+iImage, iNumImage, self.iNumShot, self.iNumTotalImage)
      iEventStart = self.daq.eventnum()

      timeAfterImageBegin = time.time()

#
#  Add a retry loop in case the EVR isn't ready when the sequence runs
#
      while True:

        caput(pvPlayCtrl, 1) # PlayCtrl = Play

        timeAfterPlay = time.time()

        time.sleep(0.05)
        while True:
          if caget(pvPlayStat).find("Stopped") != -1:
            break
          if self.bSimMode:
            iSeqStep = int(caget(pvPlayCurStep))
            print "Burst count: %d / %d\r" % (iSeqStep, self.iNumShot),
            sys.stdout.flush()
          time.sleep(0.05)

        if self.pvLaserShutter:
          caput(self.pvLaserShutter, self.iShutterOpenVal)
          time.sleep(0.5)
          iLaserShutterState = int(caget(self.pvLaserShutter))
          if iLaserShutterState != self.iShutterOpenVal:
            print "!!! Shutter %s Not Open: Current value = %d (expected %d)" % (self.pvLaserShutter, iLaserShutterState, self.iShutterOpenVal)

        timeBeforeBurst = time.time()

        if not self.bSimMode:
          time.sleep(0.02)
          while True:
            if self.mccBurst.mccBurstIsComplete():
              break
            iNumBurstCount = self.mccBurst.mccBurstCount()
            print "Burst count: %d / %d\r" % (iNumBurstCount, self.iNumShot),
            sys.stdout.flush()
            time.sleep(0.05)

        timeAfterBurstComplete = time.time()

        if self.pvLaserShutter:
          caput(self.pvLaserShutter, self.iShutterCloseVal)
          time.sleep(0.5)
          iLaserShutterState = int(caget(self.pvLaserShutter))
          if iLaserShutterState != self.iShutterCloseVal:
            print "!!! Shutter %s Not Closed: Current value = %d (expected %d)" % (self.pvLaserShutter, iLaserShutterState, self.iShutterCloseVal)

        print "Waiting for device readout... "
        if self.fPostDelay > 0:
          print "Sleeping for %.1f seconds... " % (self.fPostDelay),
          sys.stdout.flush()
          time.sleep(self.fPostDelay) # post delay: wait for DAQ to really ends if data valume is too large
          print "done."

        print "Waiting for princeton images (* may not wait long enough with multiple readout groups)"

        timeToRetry = time.time() + 15
      
        while True:
          nevents = self.daq.eventnum()
          if nevents >= iEventStart + self.iNumShot:
            break
          if time.time() > timeToRetry:
            break
          print "DAQ event: %d / %d\r" % (self.daq.eventnum(), iEventStart + self.iNumShot),
          sys.stdout.flush()
          time.sleep(0.05)
        print "                           \r",

        timeEndImage = time.time()
        print "time (sec): Image %.2f Begin %.2f SeqPvSet %.2f PlaySeq %.2f Burst %.2f End %.2f" %\
              (timeEndImage-timeBeforeImage,  \
               timeAfterImageBegin - timeBeforeImage, timeAfterPlay - timeAfterImageBegin, \
               timeBeforeBurst - timeAfterPlay, timeAfterBurstComplete - timeBeforeBurst,  \
               timeEndImage-timeAfterBurstComplete )
        
        if nevents >= iEventStart + self.iNumShot:
          self.iNumTotalImage += 1
          break

        print "Failed to receive image... moving on."
        break
Ejemplo n.º 12
0
 def mccBurstIsComplete(self):
   if self.bSimMode:
     return True
   return caget(pvMccBurstCtrl).find("Off") != -1
Ejemplo n.º 13
0
 def mccBurstCheckInit(self):
   self.bMccBurstRunning   = not (caget(pvMccBurstReqBykik).find("No") != -1 and caget(pvMccBurstReqPoc).find("No") != -1)
   print "Mcc Burst Mode: %s" % ("Yes" if self.bMccBurstRunning else "No")
Ejemplo n.º 14
0
    def run(self, iNumPreDark, iNumPostDark, iNumPreX, iNumPostX, iNumPreO,
            iNumPostO, iNumDuring, fBurstRate, bSimMode, bShutterMode, bNoPI,
            fExpDelay, fPostDelay, iOpenDelay, bNoShutter12, bNoXCheck, iDebug,
            bInteracvive, bBeamShare, fGasDetector, fAttPreX, fAttDuring,
            iPreLaserTrig, fUpDown, fLeftRight):

        x0 = x_motor.wm()  # use current target position for initial position
        y0 = y_motor.wm()  # use current target position for initial position
        #x0           = float( caget("MEC:NOTE:DOUBLE:61") ) # use EPICS PV for initial position
        #y0           = float( caget("MEC:NOTE:DOUBLE:62") ) # use EPICS PV for initial position
        predelay = float(caget("MEC:NOTE:DOUBLE:63"))
        travel_range = float(caget("MEC:NOTE:DOUBLE:64"))
        postdelay = float(caget("MEC:NOTE:DOUBLE:65"))
        ystepsize = float(caget("MEC:NOTE:DOUBLE:66"))
        steps = int(caget("MEC:NOTE:DOUBLE:67"))

        # running at 10 Hz or 5Hz ?
        daqrate = fBurstRate
        # default 10 Hz
        if daqrate == -1:
            daqrate = 10.0

        motor_speed = float(caget("MEC:USR:MMS:17.VELO"))

        xend = x0 + (predelay + travel_range + postdelay) * fLeftRight

        print "\nInitial position of target is set to: x0 = %s mm, y0 = %s mm" % (
            x0, y0)
        print "Scan on x-direction to: xend = %s mm with DAQ running at %s Hz " % (
            xend, daqrate)
        print "with predelay = %s mm; postdelay = %s mm; travel range = %s mm" % (
            predelay, postdelay, travel_range)
        print "Scan on y-direction: %s steps with step size %s mm\n" % (
            steps, ystepsize)

        print "\nGiven the nominal speed of the motor, which is %s mm/s," % motor_speed
        Nevents = int((abs(travel_range) / motor_speed) * daqrate)
        predelayT = predelay / motor_speed
        postdelayT = postdelay / motor_speed
        print "Number of events (shots) for a single row is computed to be: %s with DAQ running at %s Hz" % (
            Nevents, daqrate)
        print "When moving forward from x0 to xend:"
        print "Pre  delay: DAQ will record first event %s s after the motor started." % (
            predelayT)
        print "Post delay: DAQ will record last event more or less %s s before the motor stopped." % (
            postdelayT)

        print "When moving backword from xend to x0:"
        print "Post  delay: DAQ will record first event %s s after the motor started." % (
            postdelayT)
        print "Pre   delay: DAQ will record last event more or less %s s before the motor stopped." % (
            predelayT)

        # update token position
        token_x = x_motor.wm()
        token_y = y_motor.wm()
        timeBeforeInitialize = time.time()

        # Initialization
        if (abs(token_x - x0) > 0.01 or abs(token_y - y0) > 0.01):
            print '\nTarget is not currently at (x0 = %s mm, y0 = %s mm)! Will start initialization now!' % (
                x0, y0)
            print 'Target at : (x = %s mm, y = %s mm) before initialization' % (
                token_x, token_y)

            # move motor to the initial position
            # start moving motor
            x_motor.mv(x0)
            y_motor.mv(y0)
            x_motor.wait()
            wait(y_motor)

            # update token position
            token_x = x_motor.wm()
            token_y = y_motor.wm()
            print 'Target at : (x = %s mm, y = %s mm) after initialization\n' % (
                token_x, token_y)

        else:
            print '\nTarget is in position at (x0 = %s, y0 = %s). Will start scan now.\n' % (
                x0, y0)

        timeAfterInitialize = time.time()

        #Pre-defined number of events (triggers) for Princeton device
        iNumDuring = Nevents

        laser1 = Laser1(iNumPreDark, iNumPostDark, iNumPreX, iNumPostX,
                        iNumPreO, iNumPostO, iNumDuring, fBurstRate, bSimMode,
                        bShutterMode, bNoPI, fExpDelay, fPostDelay, iOpenDelay,
                        bNoXCheck, iDebug, bBeamShare, fGasDetector, fAttPreX,
                        fAttDuring, iPreLaserTrig)

        iFail = laser1.init(bDaqBegin=False)
        if iFail != 0:
            return 1

        print shutter1.status()
        print shutter2.status()
        print shutter3.status()
        print shutter4.status()

        if not bNoShutter12 and (shutter1.isopen() or shutter2.isopen()
                                 or shutter3.isopen() or shutter4.isopen()):
            print "!!! Shutter 1 , 2 , 3 or 4 is still open, try to close it again for 2 seconds"
            #shutter1.close()
            shutter2.close()
            shutter3.close()
            shutter4.close()
            time.sleep(2)
            if shutter2.isopen() or shutter3.isopen() or shutter4.isopen():
                print "!!! Shutter 1, 2 , 3 or 4 is still open"
                return 2

        try:
            shots_per_motorposition = 1

            # Loops of scan on y-direction starts here
            timeBeforeScanonY = time.time()

            for iScan in range(steps):

                print "Step #%s on Y starts now:" % (iScan + 1)

                if iScan != 0:
                    print "Moving target %s to the next row now." % (
                        "Up" if fUpDown > 0 else "Down")
                    y_motor.mv(token_y + fUpDown * (1.0) * ystepsize)
                    wait(y_motor)
                    token_y = y_motor.wm()
                    print "Target now at (x = %s mm, y = %s mm)" % (token_x,
                                                                    token_y)

                # define movement on x-direction now
                destination = (xend if (iScan % 2 == 0) else x0)
                delayT = (predelayT if (iScan % 2 == 0) else postdelayT)

                laser1.nextCycle()

                # Move motor right before start DAQ, no need to move during beginCycle()
                timeBeforeScanonX = time.time()

                laser1.runMoreRounds(shots_per_motorposition, x_motor, token_x,
                                     destination,
                                     delayT)  #this lines runs the DAQ

                timeAfterScanonX = time.time()

                # check if motor has been moved to the end position xend #
                while (True):
                    if not x_motor.ismoving():
                        print "Motor reaches xend now!"
                        break
                    else:
                        time.sleep(0.02)

                # updating token
                token_x = destination
                print "Scan on X ends at (x = %s mm, y = %s mm)" % (token_x,
                                                                    token_y)
                print "Time for this scan on X: %3.2f s\n" % (
                    timeAfterScanonX - timeBeforeScanonX)

                time.sleep(0.5)

                # Loops of scan on y-direction ends here

            timeAfterScanonY = time.time()

            print "\nTime for the whole scan on both X and Y: %3.2f s" % (
                timeAfterScanonY - timeBeforeScanonY)
            print "Final position of the target is (x = %s mm, y = %s mm)" % (
                token_x, token_y)

            daq = laser1.daq
            print "# (If Record Run is ON) Experiment %d Run %d (%d Rounds)" % (
                daq.experiment(), daq.runnumber(), laser1.numTotalRounds())

            print('--Press <Enter> to exit--'),
            sMoreRun = raw_input(">")

            laser1.deinit()
            time.sleep(0.5)
        except:
            traceback.print_exc(file=sys.stdout)
            laser1.deinit()
            time.sleep(0.5)

        return 0
Ejemplo n.º 15
0
    def run(self, iNumPreDark, iNumPostDark, iNumPreX, iNumPostX, iNumPreO,
            iNumPostO, iNumDuring, fBurstRate, bSimMode, bShutterMode, bNoPI,
            fExpDelay, fPostDelay, iOpenDelay, bNoShutter12, bNoXCheck, iDebug,
            bInteracvive, bBeamShare, fGasDetector, fAttPreX, fAttDuring,
            iPreLaserTrig, fLeftRight):

        laser1 = Laser1(iNumPreDark, iNumPostDark, iNumPreX, iNumPostX,
                        iNumPreO, iNumPostO, iNumDuring, fBurstRate, bSimMode,
                        bShutterMode, bNoPI, fExpDelay, fPostDelay, iOpenDelay,
                        bNoXCheck, iDebug, bBeamShare, fGasDetector, fAttPreX,
                        fAttDuring, iPreLaserTrig)

        x0 = x_motor.wm()
        y0 = y_motor.wm()
        #x0           = float( caget("MEC:NOTE:DOUBLE:61") )
        #y0           = float( caget("MEC:NOTE:DOUBLE:62") )
        xstepsize = float(caget("MEC:NOTE:DOUBLE:68"))
        steps = int(caget("MEC:NOTE:DOUBLE:69"))
        xend = x0 + fLeftRight * xstepsize * steps
        print "\nInitial position of target is set to: x0 = %s mm, y0 = %s mm" % (
            x0, y0)
        print "Scan on x-direction to: xend = %s mm" % xend
        print "with %s steps at step size = %s mm" % (steps, xstepsize)

        # update token position
        token_x = x_motor.wm()
        token_y = y_motor.wm()
        timeBeforeInitialize = time.time()

        # Initialization
        if (abs(token_x - x0) > 0.01 or abs(token_y - y0) > 0.01):
            print '\nTarget is not currently at (x0 = %s mm, y0 = %s mm)! Will start initialization now!' % (
                x0, y0)
            print 'Target at : (x = %s mm, y = %s mm) before initialization' % (
                token_x, token_y)

            # move motor to the initial position
            # start moving motor
            x_motor.mv(x0)
            y_motor.mv(y0)
            x_motor.wait()
            wait(y_motor)

            # update token position
            token_x = x_motor.wm()
            token_y = y_motor.wm()
            print 'Target at : (x = %s mm, y = %s mm) after initialization\n' % (
                token_x, token_y)

        else:
            print '\nTarget is in position at (x0 = %s, y0 = %s). Will start scan now.\n' % (
                x0, y0)

        timeAfterInitialize = time.time()

        iFail = laser1.init(bDaqBegin=False)
        if iFail != 0:
            return 1

        print shutter1.status()
        print shutter2.status()
        print shutter3.status()
        print shutter4.status()

        if not bNoShutter12 and (shutter1.isopen() or shutter2.isopen()
                                 or shutter3.isopen() or shutter4.isopen()):
            print "!!! Shutter 1 , 2 , 3 or 4 is still open, try to close it again for 2 seconds"
            #shutter1.close()
            shutter2.close()
            shutter3.close()
            shutter4.close()
            time.sleep(2)
            if shutter2.isopen() or shutter3.isopen() or shutter4.isopen():
                print "!!! Shutter 1, 2 , 3 or 4 is still open"
                return 2

        try:
            shots_per_motorposition = 1
            number_of_motorpositions = steps + 1

            for n in range(0, number_of_motorpositions):

                if n == 0:
                    print "\nImage #%s at (x0 = %s mm,y0 = %s mm)\n" % (n, x0,
                                                                        y0)
                else:
                    x_motor.mv(x0 + fLeftRight * n * xstepsize)
                    x_motor.wait()
                    token_x = x0 + fLeftRight * n * xstepsize
                    print "\nImage #%s at (x = %s mm,y = %s mm)\n" % (
                        n, token_x, token_y)

                #delay(1e-12)
                laser1.nextCycle()
                laser1.runMoreRounds(
                    shots_per_motorposition)  #this lines runs the DAQ

            daq = laser1.daq
            print "# (If Record Run is ON) Experiment %d Run %d (%d Rounds)" % (
                daq.experiment(), daq.runnumber(), laser1.numTotalRounds())

            print('--Press <Enter> to exit--'),
            sMoreRun = raw_input(">")

            laser1.deinit()
            time.sleep(0.5)
        except:
            traceback.print_exc(file=sys.stdout)
            laser1.deinit()
            time.sleep(0.5)

        return 0
Ejemplo n.º 16
0
    def getMoreImages(self,
                      iNumImage,
                      x_motor=None,
                      token_x=None,
                      destination=None,
                      delayT=None):
        for iImage in xrange(iNumImage):
            if (not self.bShutterMode
                ) and self.mccBurst.mccBurstCheckStatusChange():
                print "Burst Mode status changed"
                break

            timeBeforeImage = time.time()

            print "# Image %d/%d (%d Shots) [total images: %d]" % (
                1 + iImage, iNumImage, self.iNumShot, self.iNumTotalImage)
            iEventStart = self.getEventNum()

            timeAfterImageBegin = time.time()

            if x_motor is not None:
                # should be close to the 1st event
                print "Scan on X starts now from %s mm to %s mm" % (
                    token_x, destination)
                print "DAQ starts recording 1st event %s s after motor started." % delayT
                print "Time right before moving motor ", datetime.datetime.now(
                ).time()
                x_motor.mv(destination)

                # user specficy delay in distance
                time.sleep(delayT)
                print "Motor position before first DAQ event:", float(
                    caget("MEC:USR:MMS:17.RBV")), " mm"

            caput(pvPlayCtrl, 1)  # PlayCtrl = Play

            timeAfterPlay = time.time()
            print "###################### Event sequence played at:", timeAfterPlay

            time.sleep(0.02)
            while True:
                if caget(pvPlayStat).find("Stopped") != -1:
                    break
                if self.bShutterMode:
                    iSeqStep = int(caget(pvPlayCurStep))
                    print "Sequence step: %d\r" % (iSeqStep),
                    sys.stdout.flush()
                time.sleep(0.05)

            timeBeforeBurst = time.time()

            if not self.bShutterMode and caget(pvBeamOwner) == caget(
                    pvHutchId):
                time.sleep(0.02)
                while True:
                    if self.mccBurst.mccBurstIsComplete():
                        break
                    iNumBurstCount = self.mccBurst.mccBurstCount()
                    print "Burst count: %d / %d\r" % (iNumBurstCount,
                                                      self.iNumShot),
                    sys.stdout.flush()
                    time.sleep(0.05)

            timeAfterBurstComplete = time.time()

            print "Waiting for device readout... "
            if self.fPostDelay > 0:
                print "Sleeping for %.1f seconds... " % (self.fPostDelay),
                sys.stdout.flush()
                time.sleep(
                    self.fPostDelay
                )  # post delay: wait for DAQ to really ends if data valume is too large
                print "done."

            print "Waiting for DAQ event: %d" % self.iNumShot
            time.sleep(1)

            #      while True:
            #        print "GOING TO READOUT EVENTNUM"
            #        eventnum = self.getEventNum()
            #        if eventnum >= iEventStart + self.iNumShot:
            #    if x_motor is not None:
            #      print "Time after DAQ ", datetime.datetime.time(datetime.datetime.now())
            #      print "Motor position after last DAQ event:" , float( caget("MEC:USR:MMS:17.RBV") ), " mm"
            #          break
            #        print "DAQ event: %d / %d\r" % (eventnum, iEventStart + self.iNumShot),
            #        sys.stdout.flush()
            #        time.sleep(0.05)
            #      print "                           \r",

            if self.bShutterMode:
                if ShutterArm(self.iNumShot) != 0:
                    print "\nShutter Arm Failed"
                    break

            timeEndImage = time.time()
            print "time (sec): Image %.2f Begin %.2f SeqPvSet %.2f PlaySeq %.2f Burst %.2f End %.2f" %\
             (timeEndImage-timeBeforeImage,  \
              timeAfterImageBegin - timeBeforeImage, timeAfterPlay - timeAfterImageBegin, \
              timeBeforeBurst - timeAfterPlay, timeAfterBurstComplete - timeBeforeBurst,  \
              timeEndImage-timeAfterBurstComplete )

            self.iNumTotalImage += 1
Ejemplo n.º 17
0
    def init(self, controls=None, bDaqBegin=True):
        while True:
            fBeamFullRate = float(caget(pvBeamRate))
            if fBeamFullRate == 0.5 or float(
                    int(fBeamFullRate)) == fBeamFullRate:
                break

        if not self.bSimMode:
            if not (self.mccBurst.isLegalBeamRate(fBeamFullRate)):
                print "!!! Beam rate %g is not stable, please wait for beam to stablize and run the script again" % (
                    fBeamFullRate)
                return 1
            self.mccBurst.mccBurstCheckInit()

        if self.fBurstRate == -1:
            self.fBurstRate = fBeamFullRate

        if not (self.fBurstRate in dictRateToSyncMarker):
            print "!!! Burst rate %g not supported" % (self.fBurstRate)
            return 1

        caput(pvPlayMode, 0)  # PlayMode = Once
        caput(pvMccSeqSyncMarker, dictRateToSyncMarker[self.fBurstRate])
        caput(pvMccSeqBeamRequest,
              0)  # Set seuqencer rate to be 120Hz (TS4|TS1)

        caget(pvMccSeqSyncMarker
              )  # caget bug: need to get twice to have latest value
        print "\n## Beam rate = %g HZ, Sync marker = %g HZ" % (
            fBeamFullRate, dictSyncMarkerToRate[int(
                caget(pvMccSeqSyncMarker, bGetNumEnum=True))])

        if not self.bSimMode:
            if caget(pvBeamOwner) != caget(pvHutchId):
                if self.fBurstRate == 60:
                    print "!!! Test Burst rate %g is not supported by MCC" % (
                        self.fBurstRate)
                    return 1
                self.mccBurst.mccBurstSetRate(self.fBurstRate)
            elif self.fBurstRate == fBeamFullRate:
                self.mccBurst.mccBurstSetRate(0)
            elif self.fBurstRate < fBeamFullRate:
                if self.fBurstRate == 60:
                    print "!!! Burst rate %g is not supported by MCC" % (
                        self.fBurstRate)
                    return 1
                self.mccBurst.mccBurstSetRate(self.fBurstRate)
            else:
                print "!!! Burst rate %g is faster than Beam rate %g" % (
                    self.fBurstRate, fBeamFullRate)
                return 1

        print "## Multiple shot: %d" % (self.iNumShot)

        # Exposure Time =
        #     camear open (clean CCD) delay ( self.iSlowCamOpenDelay: 0 for PI, 5*120Hz for FLI)
        #     + manual sleep (fExpDelay)
        #     + exposure Time ((self.iNumShot / self.fBurstRate) second)
        #   = 0.0 + max(1, self.iSlowCamOpenDelay)/120.0 + (self.iNumShot / self.fBurstRate) + fExpDelay  second
        self.fExposureTime = 0.0 + self.iSlowCamOpenDelay / 120.0 + self.fExpDelay + self.iNumShot / float(
            self.fBurstRate)

        print "CREATE DAQ CONTROL OBJECT"
        self.daq = pydaq.Control(self.daq_host, self.daq_platform)
        try:
            print "daq connect...",
            sys.stdout.flush()
            self.daq.connect()  # get dbpath and key from DAQ
            print " done."
        except:
            print "!! daq.connect() failed"
            print "!! Possibly because 1. DAQ devices has NOT been allocated"
            print "!!               or 2. You are running DAQ control GUI in remote machines"
            print "!! Please check 1. DAQ is in good state and re-select the devices"
            print "!!              2. If the restart script actually runs DAQ in another machine"
            print "ERROR", sys.exc_info()[0]
            return 1

        if self.bSimMode:
            self.alias = "PRINCETON_SIM"
        else:
            self.alias = "SLOW_CAMERA"

        if self.daq.dbalias() != self.alias:
            print "!!! the current DAQ config type is %s" % (
                self.daq.dbalias())
            print "!!! please switch to %s to run this script" % (self.alias)

            print "daq disconnect...",
            sys.stdout.flush()
            self.daq.disconnect()
            print " done."
            return 2

        print "CONFIGURE DAQ"
        if controls is None:
            iFail = self.configure(controls=[])
        else:
            iFail = self.configure(controls=controls)
        if iFail != 0:
            print "daq disconnect...",
            sys.stdout.flush()
            self.daq.disconnect()
            print " done."
            return 3

        if self.bShutterMode:
            if ShutterInit(self.iNumShot) != 0:
                print "Shutter Init Failed"
                return 4

        print "SET CAMERA CONTROL SEQUENCE"
        self.setCameraControlSequence()

        self.iNumTotalImage = 0

        print "## Please make sure"
        if not self.bSimMode:
            print "##   - MCC has switched to Burst Mode,"
        print "##   - Andor/Princeton camera is selected (if you need it),"
        print "##   - chiller is running and the cooling temperature is set properly."

        if bDaqBegin:
            # cpo changed this line
            self.beginCycle(controls)
        return 0
  def run(self, iNumShot, fExpDelay, fPostDelay, iOpenDelay, fBurstRate, bSimMode, bShutterMode, fUpDown):
    

    # parameterize the scan on X
    x0 = x_motor.wm() 
    y0 = y_motor.wm()
    #x0           = float( caget("MEC:NOTE:DOUBLE:61") )
    #y0           = float( caget("MEC:NOTE:DOUBLE:62") )
    predelay     = float( caget("MEC:NOTE:DOUBLE:63") )
    travel_range = float( caget("MEC:NOTE:DOUBLE:64") )
    postdelay    = float( caget("MEC:NOTE:DOUBLE:65") )
    ystepsize    = float( caget("MEC:NOTE:DOUBLE:66") )
    steps        = int( caget("MEC:NOTE:DOUBLE:67") )

    # running at 120 Hz or 60Hz etc ?
    daqrate = fBurstRate
    # default to beam rate
    if daqrate == -1:
      daqrate = float(caget("EVNT:SYS0:1:LCLSBEAMRATE"));
      

    motor_speed = float( caget("MEC:USR:MMS:17.VELO") )

    xend = x0 + predelay + travel_range + postdelay
    
    print "\nInitial position of target is set to: x0 = %s mm, y0 = %s mm" % (x0, y0)
    print "Scan on x-direction to: xend = %s mm with DAQ running at 120Hz " % xend
    print "with predelay = %s mm; postdelay = %s mm; travel range = %s mm" % (predelay, postdelay, travel_range)
    print "Scan on y-direction: %s steps with step size %s mm\n" % (steps, ystepsize)

    print "\nGiven the nominal speed of the motor, which is %s mm/s," % motor_speed
    Nevents    = int( (abs(travel_range)/motor_speed)*daqrate)
    predelayT  = predelay/motor_speed
    postdelayT = postdelay/motor_speed
    print "Number of events (shots) for a single row is computed to be: %s with DAQ running at %s Hz" % (Nevents,daqrate)
    print "When moving forward from x0 to xend:"
    print "Pre  delay: DAQ will record first event %s s after the motor started." % (predelayT)
    print "Post delay: DAQ will record last event more or less %s s before the motor stopped." % (postdelayT)

    print "When moving backword from xend to x0:"
    print "Post  delay: DAQ will record first event %s s after the motor started." % (postdelayT)
    print "Pre   delay: DAQ will record last event more or less %s s before the motor stopped." % (predelayT)
    
    
    # update token position
    token_x = x_motor.wm()
    token_y = y_motor.wm()
    timeBeforeInitialize = time.time()


    # Initialization
    if( abs(token_x-x0)>0.01 or abs(token_y-y0)>0.01 ):
      print '\nTarget is not currently at (x0 = %s mm, y0 = %s mm)! Will start initialization now!' % (x0,y0)
      print 'Target at : (x = %s mm, y = %s mm) before initialization' % (token_x,token_y)
      
      # move motor to the initial position 
      # start moving motor
      x_motor.mv(x0)
      y_motor.mv(y0)
      x_motor.wait()   
      wait(y_motor)    
          

      # update token position
      token_x = x_motor.wm()
      token_y = y_motor.wm()
      print 'Target at : (x = %s mm, y = %s mm) after initialization\n' % (token_x,token_y)
    
    else:
      print '\nTarget is in position at (x0 = %s, y0 = %s). Will start scan now.\n' % (x0,y0)
    
    timeAfterInitialize = time.time()


    #Pre-defined number of events (triggers) for Princeton device
    iNumShot = Nevents

    princetonDaq = PrincetonDaqMultipleShot(iNumShot, fExpDelay, fPostDelay, iOpenDelay, fBurstRate, bSimMode, bShutterMode)

    try:

      # Loops of scan on y-direction starts here
      timeBeforeScanonY = time.time()
      
      for iScan in xrange( steps ):
      
        print "Step #%s on Y starts now:" % (iScan+1)

        if iScan!=0:
          print "Moving target %s to the next row now." % ("Up" if fUpDown>0 else "Down")
          y_motor.mv(token_y+fUpDown*(1.0)*ystepsize)
          wait(y_motor)
          token_y = y_motor.wm()
          print "Target now at (x = %s mm, y = %s mm)" % (token_x, token_y)
      
        # define movement on x-direction now
        destination = (xend if (iScan%2 ==0) else x0 )
        delayT = (predelayT if (iScan%2 ==0) else postdelayT )

        # connect to DAQ
        iFail = princetonDaq.init(bDaqBegin = False)
        if iFail != 0:
          return 1    

        # calibration cycle ready
        princetonDaq.nextCycle()

        # Move motor right before start DAQ, no need to move during beginCycle()
        timeBeforeScanonX = time.time()

        # motor movement in sub-routine getMoreImage()
        princetonDaq.getMoreImages(1, x_motor, token_x, destination, delayT)
        
        timeAfterScanonX = time.time()
               

        daq = princetonDaq.daq
        print "# (If Record Run is ON) Experiment %d Run %d (%d images)" % (daq.experiment(),daq.runnumber(), princetonDaq.numTotalImage())

        princetonDaq.deinit()



        # check if motor has been moved to the end position xend #
        while(True):
          if not x_motor.ismoving():
            print "Motor reaches xend now!"
            break
          else:
            time.sleep(0.02)


        # updating token
        token_x = destination
        print "Scan on X ends at (x = %s mm, y = %s mm)" % (token_x, token_y)
        print "Time for this scan on X: %3.2f s\n" % (timeAfterScanonX-timeBeforeScanonX)
        

        time.sleep(0.5)

        # Loops of scan on y-direction ends here
        

      timeAfterScanonY = time.time()
      
      print "\nTime for the whole scan on both X and Y: %3.2f s" % (timeAfterScanonY-timeBeforeScanonY)
      print "Final position of the target is (x = %s mm, y = %s mm)" % (token_x, token_y)
        
        
    except:
      traceback.print_exc(file=sys.stdout)
      princetonDaq.deinit()
      time.sleep(0.5)
        
    return 0
  def run(self, iNumShot, fExpDelay, fPostDelay, iOpenDelay, fBurstRate, bSimMode, bShutterMode, fLeftRight):
    princetonDaq = PrincetonDaqMultipleShot(iNumShot, fExpDelay, fPostDelay, iOpenDelay, fBurstRate, bSimMode, bShutterMode)
    
    x0 = x_motor.wm()
    y0 = y_motor.wm()
    #x0           = float( caget("MEC:NOTE:DOUBLE:61") )
    #y0           = float( caget("MEC:NOTE:DOUBLE:62") )
    xstepsize    = float( caget("MEC:NOTE:DOUBLE:68") )
    steps        = int( caget("MEC:NOTE:DOUBLE:69") )
    xend = x0 + fLeftRight*xstepsize*steps
    print "\nInitial position of target is set to: x0 = %s mm, y0 = %s mm" % (x0, y0)
    print "Scan on x-direction to: xend = %s mm" % xend
    print "with %s steps at step size = %s mm" % (steps, xstepsize )

    # update token position
    token_x = x_motor.wm()
    token_y = y_motor.wm()
    timeBeforeInitialize = time.time()


    # Initialization
    if( abs(token_x-x0)>0.01 or abs(token_y-y0)>0.01 ):
      print '\nTarget is not currently at (x0 = %s mm, y0 = %s mm)! Will start initialization now!' % (x0,y0)
      print 'Target at : (x = %s mm, y = %s mm) before initialization' % (token_x,token_y)
      
      # move motor to the initial position 
      # start moving motor
      x_motor.mv(x0)
      y_motor.mv(y0)
      x_motor.wait()   
      wait(y_motor)    
          

      # update token position
      token_x = x_motor.wm()
      token_y = y_motor.wm()
      print 'Target at : (x = %s mm, y = %s mm) after initialization\n' % (token_x,token_y)
    
    else:
      print '\nTarget is in position at (x0 = %s, y0 = %s). Will start scan now.\n' % (x0,y0)
    
    timeAfterInitialize = time.time()





    iFail = princetonDaq.init(bDaqBegin = False)
    if iFail != 0:
      return 1

    try:
      shots_per_motorposition=1      
      number_of_motorpositions=steps+1


      # Scan on X loop starts here
      for n in range(0,number_of_motorpositions):

        if n==0:
          print "\nImage #%s at (x0 = %s mm,y0 = %s mm)\n" % (n, x0, y0)

        else:
          x_motor.mv(x0+fLeftRight*n*xstepsize)
          x_motor.wait()
          token_x = x0+fLeftRight*n*xstepsize
          print "\nImage #%s at (x = %s mm,y = %s mm)\n" % (n, token_x , token_y)
          
          
        
        princetonDaq.nextCycle()
        princetonDaq.getMoreImages(shots_per_motorposition)
      # Scan on X loops ends here


      daq = princetonDaq.daq
      print "# (If Record Run is ON) Experiment %d Run %d (%d images)" % (daq.experiment(),daq.runnumber(), princetonDaq.numTotalImage())

      print('--Press <Enter> to exit--'),
      sMoreRun = raw_input(">")

      princetonDaq.deinit()
      time.sleep(0.5)
    except:
      traceback.print_exc(file=sys.stdout)
      princetonDaq.deinit()
      time.sleep(0.5)

    return 0
Ejemplo n.º 20
0
  def init(self):
    self.pvLaserShutter = None
    if self.iShutter > 0 and self.iShutter <= len(self.lPvShutter):
      self.pvLaserShutter = self.lPvShutter[self.iShutter-1]

    fBeamFullRate = float(caget(pvBeamRate));
    if not self.bSimMode:
      print "\n## Beam rate = %.1f HZ" % (fBeamFullRate)
      if not (self.dictBeamToBurstRate.has_key(fBeamFullRate)):
        print "!!! Beam rate is not stable, please wait for beam to stablize and run the script again"
        return 1
      self.mccBurst.mccBurstCheckInit()

    caput(pvPlayMode         , 0) # PlayMode = Once
    caput(pvMccSeqSyncMarker , 6) # Set sequencer sync marker to 120Hz
    caput(pvMccSeqBeamRequest, 0) # Set seuqencer rate to be 120Hz (TS4|TS1)
    if self.bSimMode:
      caput(pvMccSynEvtBurst   , 0) # Diable SEB support
    else:
      caput(pvMccSynEvtBurst   , 1) # Enable SEB support

    if self.fBurstRate == -1:
      if self.bSimMode:
        self.fBurstRate = 120.0
      else:
        self.fBurstRate = fBeamFullRate

    if not self.bSimMode:
      self.mccBurst.mccBurstSetNumShot(self.iNumShot, self.dictBeamToBurstRate[self.fBurstRate])

    print "## Burst rate = %.1f HZ"   % (self.fBurstRate)
    print "## Multiple shot: %d" % (self.iNumShot)

    self.codeCommand = self.setPrincetonExposureSequence()

    # Exposure Time =
    #     delay from "real" princeton open and SEB burst start event (0 second)
    #     delay from SEB burst start event and MCC firing beam ( max( 1/60, 1/self.fBurstRate) )
    #     (x) + laser shutter open delay (Pv set + shutter physical open) (0.5 second)
    #     + manual sleep (fExpDelay)
    #     + exposure Time ((self.iNumShot / self.fBurstRate) second)
    #   = 0.0 + (self.iNumShot / self.fBurstRate) + fExpDelay  second

    # Setup Time =
    #     delay between setting pvPlayCtrl and the "real" princeton open (0.2 second)
    #   = 0.2

    self.fSetupTime    = 0.1
    self.fExposureTime = 0.0 + self.fExpDelay + self.iNumShot / float(self.fBurstRate) + max(1.0/120, 1.0/self.fBurstRate) + self.iSlowCamOpenDelay / 360.0

    self.daq = pydaq.Control(self.daq_host, self.daq_platform)

    print ' *** PrincetonDaqMultipleShot.init(): self.controls =', self.controls

    #
    #  Send the structure the first time to put the control variables
    #    in the file header

    print "daq connect...",
    sys.stdout.flush()
    try:
      self.daq.connect() # get dbpath and key from DAQ
    except Exception, e:
      print e
      print "Please select devices and click Select button in DAQ Control window"