145 lines
4.6 KiB
Python
145 lines
4.6 KiB
Python
import epics
|
|
import numpy as np
|
|
|
|
|
|
class epicsAD(object):
|
|
def __init__(self, prefix, cam="cam1:", image="image1:"):
|
|
self.img = None
|
|
self.monitored = False
|
|
|
|
self.image_mode = epics.PV(prefix + cam + "ImageMode")
|
|
self.gain = epics.PV(prefix + cam + "Gain")
|
|
self.acquire = epics.PV(prefix + cam + "Acquire")
|
|
self.color = epics.PV(prefix + cam + "ColorMode")
|
|
self.gain_mode = epics.PV(prefix + cam + "GainAuto")
|
|
# 0 - manual, GainMode, 2 - auto GainAuto
|
|
self.expo_mode = epics.PV(prefix + cam + "ExposureAuto")
|
|
# 0 - manual, ExposureMode, 2 - auto ExposureAutoself.gain = epics.PV(prefix + cam + "Gain")
|
|
self.expo = epics.PV(prefix + cam + "AcquireTime")
|
|
|
|
self.ndim = epics.PV(prefix + image + "NDimensions_RBV")
|
|
self.dim0 = epics.PV(prefix + image + "ArraySize0_RBV")
|
|
self.dim1 = epics.PV(prefix + image + "ArraySize1_RBV")
|
|
self.dim2 = epics.PV(prefix + image + "ArraySize2_RBV")
|
|
self.uid = epics.PV(prefix + image + "UniqueId_RBV")
|
|
self.data = epics.PV(prefix + image + "ArrayData")
|
|
self.counter = epics.PV(prefix + cam + "ArrayCounter_RBV")
|
|
self.busy = epics.PV(prefix + cam + "AcquireBusy", auto_monitor=True)
|
|
|
|
try:
|
|
epics.ca.pend_io()
|
|
except:
|
|
pass
|
|
|
|
def monitorCB(self, epicsArgs, userArgs):
|
|
self.img = epicsArgs["pv_value"]
|
|
self.img.dtype = np.uint8
|
|
self.img.shape = self.shape
|
|
self.monitored = True
|
|
|
|
def setMonitor(self):
|
|
ndim = self.ndim.get()
|
|
dim0 = self.dim0.get()
|
|
dim1 = self.dim1.get()
|
|
dim2 = self.dim2.get()
|
|
if ndim == 3:
|
|
size = dim0 * dim1 * dim2
|
|
self.shape = (dim2, dim1, dim0)
|
|
else:
|
|
size = dim0 * dim1
|
|
self.shape = (dim1, dim0)
|
|
self.data.add_masked_array_event(None, size, None, self.monitorCB)
|
|
|
|
def clearMonitor(self):
|
|
self.data.clear_event()
|
|
self.monitored = False
|
|
|
|
def getShape(self):
|
|
ndim = self.ndim.get()
|
|
dim0 = self.dim0.get()
|
|
dim1 = self.dim1.get()
|
|
dim2 = self.dim2.get()
|
|
|
|
if ndim == 3:
|
|
shape = (dim2, dim1, dim0)
|
|
else:
|
|
shape = (dim1, dim0)
|
|
return shape
|
|
|
|
def get_image_center(self):
|
|
shape = self.getShape()
|
|
return (shape[0] / 2, shape[1] / 2)
|
|
|
|
def get_single_image(self):
|
|
self.image_mode.put(0)
|
|
counter = self.counter.get()
|
|
self.acquire.put(1)
|
|
while self.counter.get() == counter:
|
|
epics.poll(0.01)
|
|
|
|
def collect_one_image(self):
|
|
self.acquire.put(0)
|
|
self.image_mode.put(0, wait=True)
|
|
|
|
self.acquire.put(1, wait=True)
|
|
while self.busy.value == 1:
|
|
epics.poll(0.01)
|
|
|
|
def collect_auto(self):
|
|
self.acquire.put(0)
|
|
self.image_mode.put(2)
|
|
self.acquire.put(1)
|
|
|
|
def get_image(self, gray=True) -> np.ndarray:
|
|
if self.monitored:
|
|
return self.img
|
|
|
|
ndim = self.ndim.get()
|
|
dim0 = self.dim0.get()
|
|
dim1 = self.dim1.get()
|
|
dim2 = self.dim2.get()
|
|
if ndim == 3:
|
|
size = dim0 * dim1 * dim2
|
|
else:
|
|
size = dim0 * dim1
|
|
data = self.data.get(count=size)
|
|
data.dtype = np.uint8
|
|
|
|
if ndim == 3:
|
|
data.shape = (dim2, dim1, dim0)
|
|
# convert to grayscale
|
|
if gray:
|
|
data = (
|
|
data[:, :, 0] * 299.0 / 1000.0
|
|
+ data[:, :, 1] * 587.0 / 1000.0
|
|
+ data[:, :, 2] * 114.0 / 1000.0
|
|
)
|
|
else:
|
|
data.shape = (dim1, dim0)
|
|
|
|
return data
|
|
|
|
def setup(self, gain: float = 15, expo: float = 0.025):
|
|
# self.color.put(0) # black and white
|
|
self.acquire.put(0, wait=True)
|
|
self.gain_mode.put(0) # manual gain control
|
|
self.gain.put(gain)
|
|
self.expo_mode.put(0) # manual exposure control
|
|
self.expo.put(expo) # 20 hz acquisition
|
|
self.acquire.put(1)
|
|
|
|
def set_auto(self):
|
|
self.acquire.put(0, wait=True)
|
|
self.gain_mode.put(2) # automatic gain control
|
|
self.expo_mode.put(2) # automatic exposure control
|
|
self.acquire.put(1)
|
|
|
|
def restore(self, gain: float = 5, expo: float = 0.025):
|
|
# Put this values, so the camera faster resores proper values in Auto mode
|
|
self.acquire.put(0, wait=True)
|
|
self.gain.put(gain)
|
|
self.expo.put(expo)
|
|
self.gain_mode.put(2) # automatic gain control
|
|
self.expo_mode.put(2) # automatic exposure control
|
|
self.acquire.put(1)
|