# -*- 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 zmax: maximal z in mm zmin: minimal z in mm z0: offset of z to fit blade edge x0: offset of x to fit blade edge ExposureTimeAbs: exposure time in us TriggerMode: On/Off 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 print ('connecting camera') self.connect_camera() print ('done') self.set_config(config) self.plot_server_im = None self.plot_server_ph = None def set_config(self,config): self.cam.properties['ExposureTimeAbs']=config['ExposureTimeAbs'] self.cam.properties['TriggerMode']=config['TriggerMode'] self.xmin, self.xmax, self.zmin, self.zmax = (config['xmin'], config['xmax'], config['zmin'], config['zmax']) self.x0, self.z0 = (config['x0'], config['z0']) self.set_frame(config['xmin'], config['xmax'], config['zmin'], config['zmax']) def get_config(self): config = {} config['ExposureTimeAbs'] = self.cam.properties['ExposureTimeAbs'] config['TriggerMode'] = self.cam.properties['TriggerMode'] config['xmin'], config['xmax'],config['zmin'], config['zmax']= (self.xmin, self.xmax, self.zmin, self.zmax) config['x0'], config['z0'] = (self.x0, self.z0) return config def measure(self): import datetime time = datetime.datetime.now() D=[] for img in self.cam.grab_images(2): D.append(np.fliplr(img)) measurement = {'time': time,'im1': D[0], 'im2': D[1]} return measurement def plot_measure(self, measurement, config = None): if config != None: zmin = config['zmin'] zmax = config['zmax'] xmin = config['xmin'] xmax = config['xmax'] z0 = config['z0'] x0 = config['x0'] else: zmin = self.zmin zmax = self.zmax xmin = self.xmin xmax = self.xmax x0 = self.x0 z0 = self.z0 from PlottingServers import Plot2DServer if self.plot_server_im == None: self.plot_server_im = Plot2DServer({'title':'Interferometer Image', 'xlabel':'z [mm]', 'ylabel': 'x [mm]','image': True}) if self.plot_server_ph == None: self.plot_server_ph = Plot2DServer({'title':'Interferometer Phase', 'xlabel':'z [mm]', 'ylabel': 'x [mm]','image': False}) im1 = measurement['im1'] shape = measurement['im1'].shape 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])) alpha= self.Alpha(measurement['im1'],measurement['im2']) self.plot_server_ph.update(np.flipud(alpha), extent=[zmin, zmax, xmin, xmax]) 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' print ('available cameras:') for cam in available_cameras: print(cam) for availca in available_cameras: if str(availca)[-3] == '4': self.cam_to_connect = availca cam = pypylon.factory.create_device(availca) print ('camera found =) Be happy') try: cam.open() except: assert('camera not found') #cam.properties['GevSCPSPacketSize']=9014 cam.properties['AcquisitionFrameRateEnable']=False cam.properties['AcquisitionFrameRateAbs']=100 cam.properties['GainRaw']=150 self.cam = cam 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) 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 alpha=-r.unwrap(Ds1[self.x1p:self.x2p,:],Ds2[self.x1p:self.x2p,:])[:,self.z1p:self.z2p] return alpha