Esempio n. 1
0
def OptimizePitch(tPitch,
                  tQBPM=None,
                  PitchPosArr=None,
                  DeltaRange=0.0004,
                  NumPoints=31,
                  AvgPerPoint=4,
                  Detune=0.0):
    t0 = time.time()
    while True:
        c0, c1, c2, c3 = ScanPitch(tPitch=tPitch, tQBPM=tQBPM, PitchPosArr=PitchPosArr, \
                                   DeltaRange=DeltaRange, NumPoints=NumPoints, AvgPerPoint=AvgPerPoint, Detune=Detune)
        if c0 == True:
            print('%s: Fit of pitch position succeeded.' %
                  misc.GetTimeString())
            break
        elif c0 == False:
            if abs(c2[0] - c1) < abs(c2[-1] - c1):
                delta = 0.8 * DeltaRange
            else:
                delta = -0.8 * DeltaRange
            tPitch.write_attribute('Position', c1 + delta)
        if time.time() - t0 > 300:
            print('%s: Warning: Iteration timeout. Retry.' %
                  misc.GetTimeString())
            break
    return None
Esempio n. 2
0
 def OpenConnection(self, silent = False):
     """
     Method to close the connection to the PMAC controller.
     """
     if self.connection_active and not (self.silentmode):
         print(misc.GetTimeString() + ': Warning. Connection to PMAC already active. Request ignored' )
     if self.connection_active == False:
         tmp = self.io.OpenPmacDevice(self.pmac_num)
         if tmp != 0:
             self.io.PmacGetVariableDouble.restype = ctypes.c_double
             self.connection_active = True
         else:
             self.error = True
         if self.error == False and self.ioconsole and not self.silentmode: \
             print(misc.GetTimeString() + ': Successfully established connection to PMAC.')
     return None
Esempio n. 3
0
 def GetVarValue(self, variable, valreturn = False, silent = False):
     """
     Method to read a variable value (number) from a PMAC.
     Variable has to consist of variable type representing char M, P, I, or Q
     and the variable number.
     
     Example:
     GetVarValue('Q1234')
     GetVarValue('p80')
     """
     if self.connection_active:
         self.var_value = None
         self.var_name = None
         self.var_syntaxerror = False
         _type = variable[0].upper()
         if _type not in ['Q', 'I', 'M', 'P']:
             self.var_syntaxerror = True
             if not self.silentmode and self.ioconsole:
                 print(misc.GetTimeString() + ': Error. the specified variable \'%s\' is unknown.' %variable)
                 return None
         try:
             count = int(variable[1:])
         except:
             self.var_syntaxerror = True
             print(misc.GetTimeString() + ': Error. the specified variable \'%s\' is unknown.' %variable)
             return None
         if not self.var_syntaxerror:
             tmp = self.io.PmacGetVariableDouble(self.pmac_num, ctypes.c_char(_type.encode()), ctypes.c_uint(count),
                                                 ctypes.c_double(-12345))
             if tmp != -12345:
                 self.var_value = tmp
                 self.var_name = variable.upper()
                 if not (silent or self.silentmode) and self.ioconsole: 
                     print('%s = %f' %(variable, tmp))
                 
                 if not valreturn:  
                     return None
                 else:
                     return self.var_value
             else:
                 self.var_syntaxerror = True
                 if not (silent or self.silentmode) and self.ioconsole:
                     print(misc.GetTimeString() + ': Error. the specified variable \'%s\' is unknown.' %variable)
         return None
Esempio n. 4
0
 def setExptime(self, value):
     try:
         self.tPixelLink.write_attribute('SHUTTER', value)
         self.exptime = value
     except Exception as e:
         print(
             misc.GetTimeString() +
             ': PixelLink server not responding while setting new ExposureTime:\n%s'
             % e)
     return None
Esempio n. 5
0
 def setExptime(self, value):
     try:
         while self.tZyla.read_attribute('ready_for_next_acq') == False:
             time.sleep(0.01)
         self.tZyla.write_attribute('acq_expo_time', value)
         self.exptime = value
     except Exception as e:
         print(
             misc.GetTimeString() +
             ': Hamamatsu server not responding while setting new ExposureTime:\n%s'
             % e)
     while not self.tZyla.state() == PyTango.DevState.ON:
         time.sleep(0.01)
     return None
Esempio n. 6
0
 def setExptime(self, value):
     try:
         while not self.tDet.state() == PyTango.DevState.ON:
             time.sleep(0.01)
         self.tDet.write_attribute('ShutterTime', value)
         self.exptime = value
     except Exception as e:
         print(
             misc.GetTimeString() +
             ': Lambda server not responding while setting new ExposureTime:\n%s'
             % e)
     while not self.tDet.state() == PyTango.DevState.ON:
         time.sleep(0.01)
     return None
Esempio n. 7
0
 def setKITAttribute(self, attribute, value):
     try:
         #            if self.tKIT.state() == PyTango.DevState.STANDBY:
         #                self.tKIT.command_inout('Stop')
         #            while not self.tKIT.state() == PyTango.DevState.ON:
         #                time.sleep(0.01)
         self.tKIT.write_attribute(attribute, value)
     #            self.tKIT.command_inout('Start')
     #            while not self.tKIT.state() == PyTango.DevState.STANDBY:
     #                time.sleep(0.01)
     except Exception as e:
         print(
             misc.GetTimeString() +
             ': KIT server not responding while setting new ExposureTime:\n%s'
             % e)
     return None
Esempio n. 8
0
 def GetResponse(self, instruction, silent = False):
     """
     Method to get a response from the PMAC
     """
     if self.connection_active:
         self.string_buffer.value = b''
         tmp = self.io.PmacGetResponseExA(self.pmac_num, self.string_buffer, ctypes.c_uint(600),
                                          ctypes.c_char_p(instruction.encode()))
         if self.silentmode or silent:
             return self.string_buffer.value.decode()
         else:
             print('Query: %s' %instruction)
             print('Response: %s' %self.string_buffer.value.decode())
     else:
         if self.ioconsole and not self.silentmode:
             print(misc.GetTimeString() + ': Warning. Connection to PMAC not active. Request ignored.')
     return None
Esempio n. 9
0
    def __init__(self, controller=0, silentmode=False, ioconsole = True):
        """
        Method to initialize the connection to the controllers
        calling parameters:
            - controller = <n>    with n= 1..6
            - silentmode = True / False
                with silentmode = True, no output will be printed on screen.
        
        Function overview of the controllers:
        Controller 1: In-Vacuum SpaceFab and pushers for photodiodes
        Controller 2: In-Vacuum long translation
        Controller 3: Granite sliders and aperture x/y/z stage
        Controller 4: Optics SpaceFab and pushers for photodiodes
        Controller 5: Tip/tilt pods and x-translation for rotational axis, 
                        rotational axis and detector x/z stage
        Controller 6: Sample SpaceFab
        """
        self.ControllerList = \
                 {1: [1, '192.168.10.3', 'Controller 1', 'In-Vacuum SpaceFab and pushers for photodiodes'], \
                  2: [2, '192.168.10.4', 'Controller 2', 'In-Vacuum long translation'], \
                  3: [3, '192.168.10.5', 'Controller 3', 'Granite sliders and aperture x/y/z stage'], \
                  4: [4, '192.168.10.6', 'Controller 4', 'Optics SpaceFab and pushers for photodiodes'], \
                  5: [5, '192.168.10.7', 'Controller 5', 'Tip/tilt pods and x-translation for\nrotational axis, rotational axis and detector x/z stage'], \
                  6: [6, '192.168.10.8', 'Controller 6', 'Sample SpaceFab'], \
                  7: [7, '192.168.10.9', 'Controller 7', 'Optics SpaceFab (slider 1)']
                  #  7: [7, '192.168.10.9', 'Controller 7', 'Optics SpaceFab (slider 1)']
                  }

        self.error = False
        self.connection_active = False
        self.ioconsole = ioconsole
        self.string_buffer = ctypes.create_string_buffer(600)
        #check and set silentmode parameter
        if silentmode not in [True, False]:
            self.silentmode = False
        else: 
            self.silentmode = silentmode
        # check and set controller number:
        if controller not in [1, 2, 3, 4, 5, 6, 7]:
            if self.ioconsole and not self.silentmode: print(misc.GetTimeString() + ': Error. Please choose a controller from 1 to 6')
            self.error = True
        else:
            print("Opening PMacDevice...")
            self.pmac_num = self.ControllerList.get(controller)[0]
            self.pmac_ip = self.ControllerList.get(controller)[1]
            # TODO replace direct dll load with dynamic load depending on environment
            self.io = ctypes.windll.LoadLibrary('PComm32W.dll')
            # TODO same as OpenConnection? extract and replace
            tmp = self.io.OpenPmacDevice(self.pmac_num)
            print('Done. Result:')
            print(tmp)
            if tmp != 0:
                self.io.PmacGetVariableDouble.restype = ctypes.c_double
                self.connection_active = True
            else:
                self.error = True
        if self.error == False and not (self.silentmode):
            if self.ioconsole and not self.silentmode: 
                print(misc.GetTimeString() + ': Successfully established connection to PMAC Nr. %i.' %controller)
        
        return None
Esempio n. 10
0
def ScanPitch(tPitch=None,
              tQBPM=None,
              PitchPosArr=None,
              DeltaRange=0.0004,
              NumPoints=31,
              AvgPerPoint=4,
              Detune=0.0):
    """
    return values:
        <Bool> :  Max in center
        <Float>:  Value of maximum (fit / edge position)
        <float arr>: Pitch scanning positions
        <float arr>: pitch current results
    """
    # TODO remove if tPitch - make tQBPM be mandatory argument as it is used anyway
    # If you define tPitch as PyTango.DeviceProxy it will be never None
    if tPitch == None:
        print('%s: Error - no pitch motor selected' % misc.GetTimeString())
        return None
    # If you define tPitch only as PyTango.DeviceProxy it will be never 'qbpm2' or 'QBPM2'
    if tPitch == 'qbpm2' or tPitch == 'QBPM2':
        tQBPM = PyTango.DeviceProxy(proxies.tQBPM_i404_exp02)
    # OptimizePitch is used only in NanoScriptHelper were tQBPM is defined as the in the following if.
    # Why not to use already defined tQBPM form NanoScriptHelper?
    if tQBPM == None:
        tQBPM = PyTango.DeviceProxy(proxies.tQBPM_i404_exp01)

    Pitch0 = tPitch.read_attribute('Position').value
    if Detune != 0.0:
        Pitch0 -= Detune
    if PitchPosArr == None:
        PitchPosArr = numpy.linspace(Pitch0 + DeltaRange,
                                     Pitch0 - DeltaRange,
                                     num=NumPoints,
                                     endpoint=True)
    PitchResArr = numpy.zeros((NumPoints))
    while tPitch.state() == PyTango.DevState.MOVING:
        time.sleep(0.1)
    for i in range(PitchPosArr.size):
        tPitch.write_attribute('Position', PitchPosArr[i])
        time.sleep(0.1)
        while tPitch.state() == PyTango.DevState.MOVING:
            time.sleep(0.1)
        for ii in range(AvgPerPoint):
            PitchResArr[i] += 1.0 * tQBPM.read_attribute(
                'PosAndAvgCurr').value[2] / AvgPerPoint
            time.sleep(0.05)
    inum = PitchResArr.argmax()
    if inum == 0 or inum == (PitchResArr.size - 1):
        print('%s: Maximum at edge of scan detected. Redo scan!' %
              misc.GetTimeString())
        FitSuccessful = False
        PitchMaxPos = PitchPosArr[inum]
        tPitch.write_attribute('Position', PitchPosArr[inum] + Detune)
    else:
        FitSuccessful = True
        polyf = numpy.polyfit(PitchPosArr, PitchResArr, deg=2)
        PitchMaxPos = -polyf[1] / (2 * polyf[0])
        if PitchMaxPos > numpy.amin(PitchPosArr) and PitchMaxPos < numpy.amax(
                PitchPosArr):
            tPitch.write_attribute('Position', PitchMaxPos + Detune)
        else:
            PitchMaxPos = PitchPosArr[inum]
            FitSuccessful = False
            tPitch.write_attribute('Position', PitchPosArr[inum] + Detune)
    time.sleep(0.1)
    while tPitch.state() == PyTango.DevState.MOVING:
        time.sleep(0.1)
    return FitSuccessful, PitchMaxPos, PitchPosArr, PitchResArr