Files
sf-op/script/camtool.py
2016-07-06 15:36:20 +02:00

163 lines
6.2 KiB
Python

#import ch.psi.pshell.device.DeviceBase as DeviceBase
from startup import *
import ch.psi.utils.Convert.toBidimensional as mono_to_bidi
import ch.psi.utils.Convert.toDouble as toDouble
import org.apache.commons.math3.linear.Array2DRowRealMatrix as Matrix
class CamToolImage(ReadableMatrix):
def __init__(self, camtool):
self.camtool = camtool
shape = camtool.shape.read()
self._width = shape[1] #len(camtool.profile_x.read())
self._height = shape[0] #len(camtool.(profile_y.read())
def read(self):
raw = self.camtool.data.read()
#return Matrix(toDouble(mono_to_bidi(raw, self.getHeight(), self.getWidth()))).transpose().getData() #data is transposed
return mono_to_bidi(raw, self.getWidth(), self.getHeight())
def getWidth(self):
return self._width
def getHeight(self):
return self._height
class CamTool(DeviceBase):
def __init__(self, name, prefix = "cam:", latch = False):
DeviceBase.__init__(self, name)
self.prefix = prefix
self.latch = latch
self.mode = "latch" if latch else "pipeline"
self.data_prefix = prefix + self.mode + "."
self.cam_run = Channel(prefix + "camera.run", alias = name + " run")
self.cam_latch = Channel(prefix + "latch.capture", alias = name + " latch")
self.timestamp = Channel(self.data_prefix + "timestamp", alias = name + " timestamp") #[D1, D2, ...]
self.com_x = Channel(self.data_prefix + "x_stats.com", alias = name + " com x")
self.com_y = Channel(self.data_prefix + "y_stats.com", alias = name + " com y")
self.profile_x = Channel(self.data_prefix + "profile.x", alias = name + " profile x")
self.profile_y = Channel(self.data_prefix + "profile.y", alias = name + " profile y")
self.data = Channel(self.data_prefix + "image", alias = name + " data")
self.shape = Channel(self.data_prefix + "image.shape", alias = name + " shape") #[D1, D2, ...]
self.bg_image = Channel(self.data_prefix + "bg_image", alias = name + "bg image") #[D1, D2, ...]
self.bg_en = Channel(self.prefix + "bg.enabled", type = 'l', alias = name + " bg enabled")
self.bg_capture = Channel(self.prefix + "bg.capture", type = 'l', alias = name + " bg capture")
self.bg_capture_remain = Channel(self.prefix + "bg.capture_remain", alias = name + " capture remain")
self.num_images = 1
self.grab_timeout = 1.0
self.image = CamToolImage(self)
set_device_alias(self.image, name + " image")
self.com_x_samples, self.com_y_samples = [], []
class CamToolComX(Readable):
def read(self):
return mean(self.camtool.com_x_samples)
self.com_x_mean = CamToolComX(); self.com_x_mean.camtool = self
class CamToolComY(Readable):
def read(self):
return mean(self.camtool.com_y_samples)
self.com_y_mean = CamToolComY(); self.com_y_mean.camtool = self
class CamToolComXVar(Readable):
def read(self):
return stdev(self.camtool.com_x_samples)
self.com_x_stdev = CamToolComXVar(); self.com_x_stdev.camtool = self
class CamToolComXVar(Readable):
def read(self):
return stdev(self.camtool.com_y_samples)
self.com_y_stdev = CamToolComXVar(); self.com_y_stdev.camtool = self
set_device_alias(self.com_x_mean, name + " com x mean")
set_device_alias(self.com_y_mean, name + " com y mean")
set_device_alias(self.com_x_stdev, name + " com x stdev")
set_device_alias(self.com_y_stdev, name + " com y stdev")
def doInitialize(self):
if self.latch:
self.start()
else:
self.stop()
def start(self):
self.cam_run.write(-1)
def stop(self):
self.cam_run.write(0)
def capture(self):
timestamp = self.timestamp.read()
if not self.latch:
self.cam_run.write(1)
start = time.time()
while(True):
val = self.timestamp.read()
if timestamp != val:
self.setCache(val)
break
if self.latch:
self.cam_latch.write(1)
time.sleep(0.001)
if (time.time()-start) > self.grab_timeout:
if not self.latch:
self.cam_run.write(1)
else:
raise Exception("Grab Timeout")
def doUpdate(self):
self.com_x_samples, self.com_y_samples = [], []
for i in range(self.num_images):
self.capture()
self.com_x_samples.append(self.com_x.read())
self.com_y_samples.append(self.com_y.read())
def setNumberOfImages(self, value):
self.num_images = value
def enableBackground(self, value):
self.bg_en.write(1 if value else 0)
def captureBackground(self, images):
self.start()
self.bg_capture.write(images)
sleep(0.1)
while( self.bg_capture_remain.read() > 0):
time.sleep(0.001)
#self.stop()
#self.bg_capture.write(images)
#while( self.bg_capture_remain.read() > 0):
# self.capture()
self.doInitialize()
def doClose(self):
self.cam_run.close()
self.cam_latch.close()
self.timestamp.close()
self.com_x.close()
self.com_y.close()
self.profile_x.close()
self.profile_y.close()
self.data.close()
self.shape.close()
self.bg_image.close()
self.bg_en.close()
self.bg_capture.close()
self.bg_capture_remain.close()
self.image.close()
if __name__ == "__builtin__":
camera_tool = CamTool("camtool")
print camera_tool.com_x_mean.read()
camera_tool.enableBackground(False)
camera_tool.enableBackground(True)
camera_tool.captureBackground(5)
for i in range (100):
camera_tool.capture()
print camera_tool.take()
print camera_tool.com_x.read(), camera_tool.com_y.read()
add_device(camera_tool, True)