Interferometer.py 5.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 14 10:46:54 2016

@author: hermann_b
"""
import numpy as np
import pypylon

import matplotlib.pyplot as plt

import Devices.InterferometerTools.routines as r

# camera setup


# optical parameters


##parameters for spectrometer that are fixed
#xmax=6.0
#xmin=2.6
#ymin=3
#ymax=12

'''
config file:
xmax:               maximal x in mm
xmin:               minimal x in mm
Nick Sauerwein's avatar
Nick Sauerwein committed
30 31
zmax:               maximal z in mm
zmin:               minimal z in mm
32 33
z0:                 offset of z to fit blade edge
x0:                 offset of x to fit blade edge
34
ExposureTimeAbs:    exposure time in us
Nick Sauerwein's avatar
Nick Sauerwein committed
35
TriggerMode:        On/Off
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

measurement file:
time:               time (datetime object)
im1:                first image
im2:                second image
'''


class Interferometer:
    #mendatory functions
    def __init__(self, config):
        
        #fixed variables
        self.X_nozzle=1300
        self.psize=1./127.95 #mm
        self.mm=int(1./self.psize)
        self.ep=1.74/self.psize
Nick Sauerwein's avatar
Nick Sauerwein committed
53
        print ('connecting camera')
54
        self.connect_camera()
Nick Sauerwein's avatar
Nick Sauerwein committed
55
        print ('done')
56 57
        
        self.set_config(config)
Nick Sauerwein's avatar
Nick Sauerwein committed
58 59 60
        
        self.plot_server_im = None
        self.plot_server_ph = None
61 62 63
    
    def set_config(self,config):
        self.cam.properties['ExposureTimeAbs']=config['ExposureTimeAbs']
64
        self.cam.properties['TriggerMode']=config['TriggerMode']
Nick Sauerwein's avatar
Nick Sauerwein committed
65 66
        self.xmin, self.xmax, self.zmin, self.zmax = (config['xmin'], config['xmax'],
                                                      config['zmin'], config['zmax'])
67
        self.x0, self.z0 = (config['x0'], config['z0'])
68
        self.set_frame(config['xmin'], config['xmax'],
Nick Sauerwein's avatar
Nick Sauerwein committed
69
                       config['zmin'], config['zmax'])
70
        
71 72 73
    def get_config(self):
        config = {}
        config['ExposureTimeAbs'] = self.cam.properties['ExposureTimeAbs']
74
        config['TriggerMode'] = self.cam.properties['TriggerMode']
Nick Sauerwein's avatar
Nick Sauerwein committed
75
        config['xmin'], config['xmax'],config['zmin'], config['zmax']= (self.xmin, self.xmax, self.zmin, self.zmax)
76 77
        config['x0'], config['z0'] = (self.x0, self.z0)
        
78 79
        return config
    def measure(self):
80 81 82 83 84
        import datetime
        time = datetime.datetime.now()
        
        D=[]
        
85
        for img in self.cam.grab_images(2):
86
            D.append(np.fliplr(img))
87
        
88
        measurement = {'time': time,'im1': D[0], 'im2': D[1]}
89 90
        return measurement
        
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
91 92
    def plot_measure(self, measurement, config = None):
        if config != None:
Nick Sauerwein's avatar
Nick Sauerwein committed
93 94
            zmin = config['zmin']
            zmax = config['zmax']
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
95 96
            xmin = config['xmin']
            xmax = config['xmax']
97 98
            z0 = config['z0']
            x0 = config['x0']
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
99
        else:
Nick Sauerwein's avatar
Nick Sauerwein committed
100 101
            zmin = self.zmin
            zmax = self.zmax
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
102 103
            xmin = self.xmin
            xmax = self.xmax
104 105
            x0 = self.x0
            z0 = self.z0
Nick Sauerwein's avatar
Nick Sauerwein committed
106 107 108 109
        
        from PlottingServers import Plot2DServer
        
        if self.plot_server_im == None:
110
            self.plot_server_im = Plot2DServer({'title':'Interferometer Image', 'xlabel':'z [mm]', 'ylabel': 'x [mm]','image': True})
Nick Sauerwein's avatar
Nick Sauerwein committed
111
        if self.plot_server_ph == None:
112
            self.plot_server_ph = Plot2DServer({'title':'Interferometer Phase', 'xlabel':'z [mm]', 'ylabel': 'x [mm]','image': False})    
Nick Sauerwein's avatar
Nick Sauerwein committed
113 114

        im1 = measurement['im1']
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
115
        
116 117
        shape = measurement['im1'].shape
        
118
        self.plot_server_im.update(np.flipud(im1),extent=self.psize*np.array([0.,shape[1], 0., shape[0]])-np.array([z0, z0, x0,x0]))
Nick Sauerwein's avatar
Nick Sauerwein committed
119 120
        

121
        alpha= self.Alpha(measurement['im1'],measurement['im2'])
Nick Sauerwein's avatar
Nick Sauerwein committed
122

123
        self.plot_server_ph.update(np.flipud(alpha), extent=[zmin, zmax, xmin, xmax])
124 125 126 127 128 129 130 131 132 133
        
    def turn_off(self):
        self.cam.close()
        
    #additional function
    def connect_camera(self):
        available_cameras = pypylon.factory.find_devices()
        
        if len(available_cameras) == 0:
            assert 'No cameras found'
134
            
135 136 137
        print ('available cameras:')
        for cam in available_cameras:
            print(cam)
138 139 140
            
        for availca in available_cameras:
            if str(availca)[-3] == '4':
141
                self.cam_to_connect = availca
142 143 144 145 146 147 148
                cam = pypylon.factory.create_device(availca)
                print ('camera found =) Be happy')
           
        try:
            cam.open()
        except:
            assert('camera not found')
149 150
        
        
151
        #cam.properties['GevSCPSPacketSize']=9014
152 153 154 155 156 157
        cam.properties['AcquisitionFrameRateEnable']=False
        cam.properties['AcquisitionFrameRateAbs']=100
        cam.properties['GainRaw']=150

        self.cam = cam
    
158 159 160 161 162
    def set_frame(self, xmin, xmax, zmin, zmax):
        self.x1p=int(1750-(xmax + self.x0)/self.psize)
        self.x2p=int(1750-(xmin+ self.x0)/self.psize)
        self.z1p=int((zmin+ self.z0)/self.psize)
        self.z2p=int((zmax+ self.z0)/self.psize)
163 164 165 166 167 168 169 170 171 172 173 174
        
    def Alpha(self, D1, D2):
        sigmax=5
        sigmay=0.5
        
        from scipy import ndimage
        
        #smoothing
        Ds1=ndimage.filters.gaussian_filter(np.array(D1,dtype=float), [sigmax, sigmay], mode='nearest', truncate=2)
        Ds2=ndimage.filters.gaussian_filter(np.array(D2,dtype=float), [sigmax, sigmay], mode='nearest', truncate=2)
        
        #calculate phase
175
        alpha=-r.unwrap(Ds1[self.x1p:self.x2p,:],Ds2[self.x1p:self.x2p,:])[:,self.z1p:self.z2p]
176 177 178
        
        return alpha