Skip to content

Commit

Permalink
beg of day
Browse files Browse the repository at this point in the history
  • Loading branch information
dpeana committed Nov 16, 2022
1 parent d2bd9a2 commit 9ef2ebf
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 21 deletions.
64 changes: 50 additions & 14 deletions andor_repo/andor/andor_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self):
(ret) = self.cam.SetBaselineClamp(1)
(ret) = self.cam.SetOutputAmplifier(0) # 0 is EMCCD. David added this
(ret) = self.cam.SetEMGainMode(3) # Max is 300? David added this
(ret) = self.cam.SetEMCCDGain(11) # Max is 300? David added this ###########################################################
(ret) = self.cam.SetEMCCDGain(100) # Max is 300? David added this ###########################################################
(ret) = self.cam.SetCountConvertMode(1) # 1 is electron, 2 is photon
(ret) = self.cam.SetFrameTransferMode(0)
# (ret) = self.cam.SetCountConvertWavelength(float(852))
Expand All @@ -53,7 +53,7 @@ def __init__(self):
(ret, self.xpixels, self.ypixels) = self.cam.GetDetector()
self.imageSize = self.xpixels*self.ypixels
(ret) = self.cam.SetImage(1, 1, 1, self.xpixels, 1, self.ypixels)
(ret) = self.cam.SetExposureTime(0.012) # Set exposure time here! 0.012
(ret) = self.cam.SetExposureTime(0.010) # Set exposure time here! WAS 0.010 ms!
(ret, self.fminExposure, self.fAccumulate, self.fKinetic) = self.cam.GetAcquisitionTimings()

(ret) = self.cam.PrepareAcquisition()
Expand Down Expand Up @@ -82,26 +82,34 @@ def __init__(self):
# # # else:
# # # print("shift_speed_change failed")

def retrieve_trigger(self,images=1,timeout = 0): # Time out time in seconds
# print("Retrieving trigger...")
# time1 = time.time()
def retrieve_trigger(self,images=2,timeout = 0): # Time out time in milliseconds

for i in range(images):
win32event.WaitForSingleObject(self.event, (timeout*1000 + 1000)) # 12000 is 12 second timeout
# print(time.time()-time1)
# win32event.WaitForSingleObject(self.event, (timeout*1000 + 1000)) # This was the old, working one
ret = win32event.WaitForSingleObject(self.event, (timeout)) # 1 second extra delay was removed as of 7/29
# time.sleep(0.07)
# ret = win32event.WaitForSingleObject(self.event, (timeout)) # 1 second extra delay was removed as of 7/29
return np.int32(ret)

def reset_image(self):
self.cam.CancelWait()
self.cam.AbortAcquisition()

def start_acquisition_event(self):
self.event = win32event.CreateEvent(None, 0, 0, None)
self.cam.SetDriverEvent(self.event)
self.cam.StartAcquisition()
ret = self.cam.SetDriverEvent(self.event)
if atmcd.DRV_SUCCESS==ret:
ret = self.cam.StartAcquisition()
else:
pass

return np.int32(ret)
# print('started')

def trigger_count(self):
_,__,x = self.cam.GetNumberAvailableImages()
print(x,"triggers received")
return x
x = self.cam.GetNumberAvailableImages()[2]
# print(x,"triggers received")
return np.int32(x)

def get_last_image(self):
(ret, fullFrameBuffer) = self.cam.GetMostRecentImage(self.imageSize) # GetImages(somethinghere) might be a better command if we want to do background subtraction
Expand All @@ -112,7 +120,35 @@ def get_specific_image(self, image_number):
(ret, fullFrameBuffer, validfirst, validlast) = self.cam.GetImages(image_number,image_number,self.imageSize)
image = np.reshape(fullFrameBuffer, (512,512)) #Converts c_long 262k array to 512x512 image numpy array
return image


def get_crop_image(self, cropx_min = 401, cropx_max = 441, cropy_min = 358, cropy_max = 398, image_number = 1, zero = False):
(ret, fullFrameBuffer, validfirst, validlast) = self.cam.GetImages(image_number,image_number,self.imageSize)
image = np.reshape(fullFrameBuffer, (512,512))
image = image[cropx_min:cropx_max,cropy_min:cropy_max]*1
# image[17:20,18:21] = image[17:20,18:21] + 10000 # This line is used for visualizing pixels of interest on image in artiq
if zero == False:
image = image * 1
if zero == True:
image = image * 0
return image

def get_both_crop_image(self, cropx_min = 401, cropx_max = 441, cropy_min = 358, cropy_max = 398, zero = False):
(ret, image0, validfirst, validlast) = self.cam.GetImages(1,1,self.imageSize)
image0 = np.reshape(image0, (512,512))
image0 = image0[cropx_min:cropx_max,cropy_min:cropy_max]

(ret, image1, validfirst, validlast) = self.cam.GetImages(2,2,self.imageSize)
image1 = np.reshape(image1, (512,512))
image1 = image1[cropx_min:cropx_max,cropy_min:cropy_max]
if zero == False:
image0 = image0 * 1
image1 = image1 * 1
if zero == True:
image0 = image0 * 0
image1 = image1 * 0
return image0


def get_all_images(self,image_number = 2):
image_arr = [0]*image_number
for i in range(image_number):
Expand Down Expand Up @@ -162,4 +198,4 @@ def take_pic(self):
def reset(self):
(ret) = self.cam.SetShutter(0,2,50,50)
(ret) = self.cam.ShutDown()
print("Camera Reset Successful...")
print("Camera Reset Successful...")
86 changes: 86 additions & 0 deletions andor_repo/andor/andor_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from pyAndorSDK2 import atmcd
import win32event
import numpy as np
import matplotlib.pyplot as plt
import time
''' To be run in Spyder. Run andor_driver first then run this in the same terminal '''



cam = atmcd()# atmcd("") was working. Switch back if a problem! Without the "" also appears to be working #load the atmcd library
# reset()

(ret) = cam.Initialize("/usr/local/etc/andor") #initialise camera # C:\Program Files\Andor SDK
(ret, iSerialNumber) = cam.GetCameraSerialNumber()
(ret) = cam.CoolerON()
(ret) = cam.SetCoolerMode(1)
(ret) = cam.SetTemperature(-60)
(ret) = cam.SetADChannel(0) # Added 4/6/22
(ret) = cam.SetHSSpeed(0,0) # 0 corresponds to 17MHz on AD channel 0 (the only channel for our camera) in EM Gain mode
# print("HSSpeed",cam.GetHSSpeed(0, 0, 0))
(ret) = cam.SetVSSpeed(4) # 4 corresponds to 3.3us. 0 is .3
# print("VSSpeed",cam.GetVSSpeed(4))
(ret) = cam.SetVSAmplitude(0) # 0 corresponds to 'Normal' 3 required for good image at 0.3 usec VSSpeed
# print("Number VSAmplitudes is",cam.GetNumberVSAmplitudes())
(ret) = cam.SetPreAmpGain(2) # 2 corresponds to a gain of 3.0
# print("PreAmpGain", cam.GetPreAmpGain(2))

(ret) = cam.SetBaselineClamp(1)
(ret) = cam.SetOutputAmplifier(0) # 0 is EMCCD. David added this
(ret) = cam.SetEMGainMode(3) # Max is 300? David added this
(ret) = cam.SetEMCCDGain(100) # Max is 300? David added this ###########################################################
(ret) = cam.SetCountConvertMode(1) # 1 is electron, 2 is photon
(ret) = cam.SetFrameTransferMode(0)
# (ret) = cam.SetCountConvertWavelength(float(852))
(ret) = cam.SetShutter(0,1,50,50) # (0,1,50,50) opens shutter. (0,2,50,50) closes shutter.
# (ret) = cam.SetAcquisitionMode(1)
##############################################################
(ret) = cam.SetFastExtTrigger(1) #when FastExtTrigger is active, set to 1, camera will NOT wait until a Keep Clean cycle is complete before taking an image
(ret) = cam.SetAcquisitionMode(3) #3 is Kinetics
(ret) = cam.SetNumberKinetics(2) #number of images taken in an acquisition
(ret) = cam.EnableKeepCleans(1)
(ret) = cam.SetNumberAccumulations(1) #Default value is 1. Excplicitly stating this is probably unnecessary.
# (ret) = cam.SaveEEPROMToFile(r'C:\Users\hoodl\Desktop\EEPROM.txt') # This breaks the camera initialization.
#(ret) = SetIsolatedCropMode(1, 30, 30, 1, 1)
##############################################################
(ret) = cam.SetReadMode(4)
(ret) = cam.SetTriggerMode(1) # 0 is internal, 1 is external, 6 is external start?
(ret, xpixels, ypixels) = cam.GetDetector()
imageSize = xpixels*ypixels
(ret) = cam.SetImage(1, 1, 1, xpixels, 1, ypixels)
(ret) = cam.SetExposureTime(0.010) # Set exposure time here! WAS 0.010 ms!
(ret, fminExposure, fAccumulate, fKinetic) = cam.GetAcquisitionTimings()
(ret) = cam.PrepareAcquisition()

if atmcd.DRV_SUCCESS==ret:
print("Andor Initializiation was successful...")
else:
print("Andor Initializiation failed...Try restarting Artiq and/or powercycling camera")

#%%

event = win32event.CreateEvent(None, 0, 0, None)
cam.SetDriverEvent(event)
ret = cam.StartAcquisition() # It needs time.sleep(0.07) between StartAcq and WaitForSingleObject
#%%

time.sleep(0.07) # Needed
print(cam.GetNumberAvailableImages()[2])
# retrieve_trigger
# for i in range(2):
ret = win32event.WaitForSingleObject(event, 1) # 1 second extra delay was removed as of 7/29
# print(ret)

# time.sleep(0.5)

(ret, image,_,_) = cam.GetImages(1,1,imageSize)
img0 = np.reshape(image, (512,512)) #Converts c_long 262k array to 512x512 image numpy array
(ret, image,_,_) = cam.GetImages(2,2,imageSize)
img1 = np.reshape(image, (512,512)) #Converts c_long 262k array to 512x512 image numpy array

# plt.imshow(img0)

print('img0/img1',np.mean(img0),np.mean(img1))

cam.CancelWait()
cam.AbortAcquisition()
Binary file modified flir_repo/flir/__pycache__/driver.cpython-38.pyc
Binary file not shown.
26 changes: 19 additions & 7 deletions flir_repo/flir/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def __init__(self):
# try:

# Set camera serial numbers
serial_1 = '20343286' # currently replacing Andor
#serial_1 = '20409335' # diagonal flourescence (on stage carriage)
#serial_1 = '20343286' # Back objective camera
serial_1 = '20409335' # Diagonal fluorescence (on stage carriage)

# Get system
self.system = PySpin.System.GetInstance()
Expand All @@ -75,7 +75,7 @@ def __init__(self):
cam.AcquisitionMode.SetValue(PySpin.AcquisitionMode_Continuous)
cam.ExposureAuto.SetValue(PySpin.ExposureAuto_Off)
cam.ExposureMode.SetValue(PySpin.ExposureMode_Timed) #Timed or TriggerWidth (must comment out trigger parameters other that Line)
cam.ExposureTime.SetValue(100) #us
cam.ExposureTime.SetValue(50) #us
cam.GainAuto.SetValue(PySpin.GainAuto_Off)
cam.Gain.SetValue(23.8)# Previously 23.8

Expand Down Expand Up @@ -131,16 +131,14 @@ def await_trigger(self, EXPOSURE_TIME):
except:
pass
print("Error in await_trigger(), try restarting artiq")




def get_image(self,num_of_im):
# try:
# Acquire images. Output is python list of images as numpy arrays
images=[]
for i in range(num_of_im):
# print("Before GetNextImage")
image = self.cam_1.GetNextImage(50000) #timeout (ms) 20000
image = self.cam_1.GetNextImage(6000) #timeout (ms) 20000
# print("Picture Successfully taken at: ",time.strftime("%d %b %Y %H:%M:%S", time.localtime()))
images.append(np.array(image.GetData(),dtype="uint16").reshape((image.GetHeight(),image.GetWidth()))) #convert PySpin ImagePtr into numpy array
image.Release()
Expand All @@ -153,7 +151,21 @@ def get_image(self,num_of_im):
# except:
# pass
# print("Error in get_image(), try restarting artiq")

def get_next_image(self):
# try:
# Acquire images. Output is python list of images as numpy arrays

# print("Before GetNextImage")
image = self.cam_1.GetNextImage(20000) #timeout (ms) 20000
# print("Picture Successfully taken at: ",time.strftime("%d %b %Y %H:%M:%S", time.localtime()))
img = np.array(image.GetData(),dtype="uint16").reshape((image.GetHeight(),image.GetWidth())) #convert PySpin ImagePtr into numpy array
image.Release()

return img

def end_acquisition(self):
self.cam_1.EndAcquisition()

def save_image(self, imagearray):#,image_1, image_2):
try:
Expand Down

0 comments on commit 9ef2ebf

Please sign in to comment.