/* * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved. */ import ch.psi.pshell.device.Readable; import ch.psi.pshell.epics.*; import java.io.IOException; import java.util.ArrayList; /** * */ public class Camtool extends ArraySource { final String prefix; final String dataPrefix; final boolean latch; final public ChannelInteger channelRun; final public ChannelInteger channelLatch; final public ChannelDouble channelTimestamp; final public ChannelDouble posX, posY; final public ChannelDoubleArray profileX, profileY; final public ChannelIntegerArray shape; final public ChannelInteger bgEnable, bgCapture, bgCaptureRemain; final public CamToolPosX posMeanX; final public CamToolPosY posMeanY; final public CamToolVarX posVarX; final public CamToolVarY posVarY; public Camtool(String name, String prefix) { this(name, prefix, false, true); } public Camtool(String name, String prefix, boolean latch, boolean roi) { super(name, prefix + (latch ? ":latch": ":pipeline") + (roi ? ".roi.output": ".image")); this.prefix = prefix+":"; this.latch = latch; dataPrefix = this.prefix + (latch ? "latch": "pipeline") + "."; channelRun = new ChannelInteger(name + " run", this.prefix + "camera.run"); channelLatch = new ChannelInteger(name + " latch", this.prefix + "latch.capture"); channelTimestamp = new ChannelDouble(name + " timestamp", dataPrefix + "timestamp"); channelTimestamp.setMonitored(true); //posX = new ChannelDouble(name + " com x", dataPrefix + "x_stats.com"); //posY = new ChannelDouble(name + " com y", dataPrefix + "y_stats.com"); posX = new ChannelDouble(name + " com x", dataPrefix + "x_stats.com_egu"); posY = new ChannelDouble(name + " com y", dataPrefix + "y_stats.com_egu"); profileX = new ChannelDoubleArray(name + " profile x", dataPrefix + "profile.x"); profileY = new ChannelDoubleArray(name + " profile y", dataPrefix + "profile.y"); shape = new ChannelIntegerArray(name + " shape", dataPrefix + "image.shape"); bgEnable = new ChannelInteger(name + " bg enable", this.prefix + "pipeline.bg.enabled"); bgCapture = new ChannelInteger(name + " bg capture", this.prefix + "pipeline.bg.capture"); bgCaptureRemain = new ChannelInteger(name + " bg capture remain", this.prefix + "pipeline.bg.capture_remain"); posMeanX = new CamToolPosX(); posMeanY = new CamToolPosY(); posVarX = new CamToolVarX(); posVarY = new CamToolVarY(); } @Override public void doSetMonitored(boolean value){ super.doSetMonitored(value); getDevice().setMonitored(value); } @Override public void doUpdate() throws IOException, InterruptedException{ super.doUpdate(); getDevice().update(); } @Override protected void doInitialize() throws IOException, InterruptedException { try { channelRun.initialize(); channelLatch.initialize(); channelTimestamp.initialize(); posX.initialize(); posY.initialize(); profileX.initialize(); profileY.initialize(); shape.initialize(); bgEnable.initialize(); bgCapture.initialize(); bgCaptureRemain.initialize(); int[] s = shape.read(); getConfig().imageHeight = s[0]; getConfig().imageWidth = s[1]; getConfig().save(); getDevice().setSize(s[0] * s[1]); super.doInitialize(); } catch (InterruptedException ex) { throw ex; } catch (Exception ex) { throw new IOException(ex); } } int numImages = 1; public int getNumImages(){ return numImages; } public void setNumImages(int value){ numImages = value; } double grabTimeout = 3.0; public double getGrabTimeout(){ return grabTimeout; } public void setGrabTimeou(double value){ grabTimeout = value; } public void capture() throws IOException, InterruptedException { } public void start() throws IOException, InterruptedException { channelRun.write(-1); } public void stop() throws IOException, InterruptedException { channelRun.write(0); } public void grabSingle() throws IOException, InterruptedException { channelRun.write(1); } public void latch() throws IOException, InterruptedException { channelLatch.write(1); } public void enableBackground(boolean value) throws IOException, InterruptedException { bgEnable.write(value ? 1 : 0); } public void captureBackground(int images) throws IOException, InterruptedException { start(); bgCapture.write(images); Thread.sleep(200); while( bgCaptureRemain.read() > 0){ Thread.sleep(10); } } //Statisticss pseudo devices ArrayList posXSamples; ArrayList posYSamples; public void updateStats() throws IOException, InterruptedException { posXSamples.clear(); posYSamples.clear(); for (int i=0; i samples){ int count = 0; double temp = 0; for (Double n : samples) { if (!Double.isNaN(n)){ temp += n.doubleValue(); count++; } } return count==0 ? Double.NaN : temp/count; } public double stdev(ArrayList samples){ int count = 0; double temp = 0; double mean = mean(samples); for (Double n : samples) { if (!Double.isNaN(n)){ temp += Math.pow((mean - n), 2); } } return count==0 ? Double.NaN : temp / count; } }