ParabolicMirror.py 5.17 KB
Newer Older
1 2
import numpy as np
import matplotlib.pyplot as plt
3 4 5 6

import sys
sys.path.append('../pyAPT/')
from pyAPT import Z806
7 8 9 10 11 12 13 14 15 16 17 18 19

'''
config file:
pos_Pay:                  current position of motor Pay
pos_Paz:                  current position of motor Paz


measurement file (n.a)
'''

class ParabolicMirror:
    def __init__(self, config, home = False):
        import time
20
        time.sleep(0.1)
21 22 23 24 25 26 27 28 29 30 31 32 33 34
        try:
            self.connect_motors()
            print ('Connnection with motors sucessful!')
        except:
            print ('Connection to motors failed. Close all programs that'+' use the motors or restart the controllers')
            raise
            
        if home:
            self.home()
            
        self.set_config(config)
        
        
    def set_config(self,config):
35 36
        self.Pay.goto(config['pos_Pay'])
        self.Paz.goto(config['pos_Paz'])
37 38
        
    def get_config(self):
39
        config = {}                          
40 41
        config['pos_Pay'] = float(self.Pay.position())
        config['pos_Paz'] = float(self.Paz.position())
42 43 44 45 46
        
        return config
   
    def connect_motors(self):

47 48
        self.Paz = Z806(83835804)
        self.Pay = Z806(83835879)
49 50 51 52
        self.motors = [self.Pay, self.Paz]
        
    def home(self):
        for motor in self.motors:
53
            motor.home()
54
        
55
    def optimize(self, focus, laser, Cz, wanted_focus_um, n_av = 10, sleep_time = 0.2, output = True, maxiter = 100):
56
        #this variable is necessary to compensate the attenuation of the laser
57
        global intmult
58
        intmult = 1.
59 60 61
        
        global reduce_int
        reduce_int = False
62
        def to_min():
63 64
            global intmult
            
65 66
            FWHMxs = []
            FWHMys = []
67
            maxints = []
68
            for i in range(n_av):
69
                measurement = focus.measure()
70 71
                FWHMxs.append(measurement['FWHMx'])
                FWHMys.append(measurement['FWHMy'])
72
                maxints.append(measurement['maxit'])
73
                
74 75 76
            #if output:
            #    focus.plot_measure(measurement)
            #    plt.pause(0.01)
77
            del measurement
78 79
            if output:
                print ('FWHMxs: ',FWHMxs)
80
                print ('maxints: ',maxints)
81
                
82
            res = np.sqrt(np.array(FWHMxs)**2 + np.array(FWHMys)**2)/np.sqrt(2)*4096/np.array(maxints)*intmult
83
            mean, error = np.mean(res), np.std(res, ddof = -1)
84
            
85 86 87
            if np.mean(maxints) > 4e3:
                redurce_int = True
                
88 89 90 91 92 93 94 95
            if output:
                print ('mean: ',mean,'error: ', error)
                
            if mean*1e3 < wanted_focus_um:
                if output:
                    print ('The optimization converged. Final value: ',mean)
                return
            
96
            return mean, error
97 98 99


        
100 101 102
        
        def change_parameter(y, z, zc):
            print ('Parameters: ',(y, z, zc))
103 104 105 106
            config = {'pos_Pay': y, 'pos_Paz': z}
            
            self.set_config(config)
            
107
            #config_per = per.get_config()
108
            
109 110 111
            #config_per['deltaz_Cz'] = zc 
            Cz.goto(zc)
            #per.set_config(config_per)
112 113 114 115 116
            return True
        
        def opt_f(x):
            y = x[0]
            z = x[1]
117
            zc = x[2] 
118 119
            if output:
                print ('-------------------------------------------')
120
            change_parameter(y,z, zc)
121
            
122
            #wait for oszillation to decay
123 124 125
            import time
            time.sleep(sleep_time)
            
126
            return to_min()[0]
127 128
        
        config0 = self.get_config()
129
        #config_per0 = per.get_config()
130 131 132 133 134
        config0_focus = focus.get_config()
        config0_focus['measure_full']=True
        focus.set_config(config0_focus)
        x0 = [config0['pos_Pay'],
              config0['pos_Paz'], 
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
              Cz.position()]
              #config_per0['deltaz_Cz']]
        
        def after_it(res):
            print ('###################')
            print ('step done')
            global reduce_int
            if reduce_int:
                config_laser = laser.get_config()
                config_laser['attenuation'] = config_laser['attenuation']/1.2
                laser.set_config(config_laser)
                intmult *= 1.2
                reduce_int = False
                print ('laser attenuated')
            
150
        
151
        # directly Nedler-Mead
152
        import scipy.optimize
153
        res = scipy.optimize.minimize(opt_f, x0, method='Nelder-Mead',callback = after_it, options={'disp': output,
154 155
                                                                          'initial_simplex': None, 
                                                                          'maxiter': maxiter, 
156
                                                                          'xatol': 0.00005, 
157
                                                                          'return_all': False, 
158
                                                                          'fatol': wanted_focus_um})
159
        
160 161
        if output:
            print (res)
162 163 164
        return res
        
        
165 166 167 168