PlasmaCamHorizontal.py 3.01 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
# -*- 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


'''
config file:

y0:                 y offset in mm
z0:                 z offset in mm

ExposureTimeAbs:    exposure time in us
TriggerMode:        On/Off

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


class PlasmaCamHorizontal:
    #mendatory functions
    def __init__(self, config):
        
        self.psize=1./127.95*3/1.70 #mm
        self.ep=1.74/self.psize
        
        print ('connecting camera')
        self.connect_camera()
        print ('done')
        
        self.set_config(config)
        
        self.plot_server_im = None
    
    def set_config(self,config):
        self.cam.properties['ExposureTimeAbs']=config['ExposureTimeAbs']
        self.cam.properties['TriggerMode']=config['TriggerMode']
        self.y0 = config['y0']
        self.z0 = config['z0']

    def get_config(self):
        config = {}
        config['ExposureTimeAbs'] = self.cam.properties['ExposureTimeAbs']
        config['TriggerMode'] = self.cam.properties['TriggerMode']
        config['y0'] = self.y0
        config['z0'] = self.z0
        return config
    
    def measure(self):
        import datetime
        time = datetime.datetime.now()
        
        D=[]
        
        for img in self.cam.grab_images(1):
            D.append(np.fliplr(img))
        
        
        measurement = {'time': time,'im1': D[0]}
        return measurement
        
    def plot_measure(self, measurement, config = None):

        
        from PlottingServers import Plot2DServer
        
        if self.plot_server_im == None:
            self.plot_server_im = Plot2DServer({'title':'PlasmaCamHorizontal', 'xlabel':'z [mm]', 'ylabel': 'y [mm]', 'image': True})  

        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([self.z0, self.z0, self.y0, self.y0]))
        
    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] == '3':
                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
        
        self.cam = cam