diff --git a/devices/CurrentCamera.properties b/devices/CurrentCamera.properties index fae1736..4552a47 100644 --- a/devices/CurrentCamera.properties +++ b/devices/CurrentCamera.properties @@ -1,13 +1,13 @@ -#Wed Sep 07 15:48:15 CEST 2016 -colormap=Temperature -colormapAutomatic=true -colormapMax=25.102 -colormapMin=12.575 +#Thu Sep 08 17:53:42 CEST 2016 +colormap=Grayscale +colormapAutomatic=false +colormapMax=0.0 +colormapMin=65535.0 flipHorizontally=false -flipVertically=true +flipVertically=false grayscale=false -imageHeight=1200 -imageWidth=1246 +imageHeight=1628 +imageWidth=1280 invert=false rescaleFactor=1.0 rescaleOffset=0.0 @@ -18,9 +18,9 @@ roiY=0 rotation=0.0 rotationCrop=true scale=1.0 -spatialCalOffsetX=-1191.0 -spatialCalOffsetY=-1176.0 -spatialCalScaleX=0.014285714285714285 -spatialCalScaleY=0.020833333333333332 +spatialCalOffsetX=-685.0 +spatialCalOffsetY=-874.0 +spatialCalScaleX=13.612279307222428 +spatialCalScaleY=12.708498808578236 spatialCalUnits=mm transpose=false diff --git a/plugins/Cameras.java b/plugins/Cameras.java index 5e41c56..6031fa0 100644 --- a/plugins/Cameras.java +++ b/plugins/Cameras.java @@ -12,25 +12,18 @@ import ch.psi.pshell.imaging.ImageListener; import ch.psi.utils.State; import ch.psi.utils.IO; import ch.psi.utils.swing.SwingUtils; -import ch.psi.utils.swing.ConfigDialog; -import ch.psi.utils.swing.StandardDialog; import ch.psi.utils.swing.TextEditor; import ch.psi.pshell.epics.PsiCamera; +import ch.psi.pshell.epics.Camtool; import ch.psi.pshell.core.JsonSerializer; -import ch.psi.pshell.device.Device; import ch.psi.pshell.epics.ArraySource; -import ch.psi.pshell.epics.ChannelDouble; -import ch.psi.pshell.epics.ChannelDoubleArray; -import ch.psi.pshell.epics.ChannelInteger; -import ch.psi.pshell.epics.ChannelIntegerArray; import ch.psi.pshell.ui.App; import ch.psi.pshell.imaging.Data; import ch.psi.pshell.imaging.Overlay; import ch.psi.pshell.imaging.Overlays; import ch.psi.pshell.imaging.Pen; -import ch.psi.pshell.imaging.Renderer; +import ch.psi.pshell.imaging.PointDouble; import ch.psi.pshell.imaging.Renderer.Profile; -import ch.psi.pshell.imaging.RendererListener; import ch.psi.pshell.imaging.Source; import ch.psi.pshell.imaging.Utils; import ch.psi.pshell.scripting.InterpreterResult; @@ -42,7 +35,7 @@ import ch.psi.utils.swing.Editor.EditorDialog; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; -import java.awt.Frame; +import java.awt.Point; import java.awt.image.BufferedImage; import java.nio.file.Files; import java.nio.file.Path; @@ -52,12 +45,10 @@ import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.IntStream; -import javax.script.ScriptException; -import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE; import org.apache.commons.math3.analysis.function.Gaussian; import org.apache.commons.math3.fitting.GaussianCurveFitter; import org.apache.commons.math3.fitting.WeightedObservedPoint; +import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; /** * */ @@ -66,12 +57,32 @@ public class Cameras extends Panel { public Cameras() { initComponents(); renderer.setPersistenceFile(Paths.get(getController().getSetup().getContextPath(), "Renderer_Cameras.bin")); + renderer.setProfileFactor(4); setPersistedComponents(new Component[]{checkCamtool}); comboCameras.setEnabled(false); - if (App.hasArgument("ct")) { - checkCamtool.setSelected(! App.getArgumentValue("ct").equals("0")); - } + if (App.hasArgument("poll")) { + try{ + polling = Integer.valueOf(App.getArgumentValue("poll")); + } catch (Exception ex){ + ex.printStackTrace(); + } + } + if (App.hasArgument("zoom")) { + try{ + renderer.setDefaultZoom(Double.valueOf(App.getArgumentValue("zoom"))); + } catch (Exception ex){ + ex.printStackTrace(); + } + } } + + @Override + public void onStart() { + super.onStart(); + if (App.hasArgument("ct")) { + checkCamtool.setSelected(!App.getArgumentValue("ct").equals("0") && !App.getArgumentValue("ct").equalsIgnoreCase("false")); + } + } final String configFolder = "/afs/psi.ch/intranet/SF/Applications/config/camtool"; File[] cameraConfigFiles = new File[0]; @@ -79,6 +90,7 @@ public class Cameras extends Panel { String cameraName; String cameraConfigJson; CameraConfig config; + int polling = 1000; //Overridable callbacks @Override @@ -89,7 +101,7 @@ public class Cameras extends Panel { renderer.setAutoScroll(true); ((Source)getDevice("image")).addListener(new ImageListener() { @Override - public void onImage(Object o, BufferedImage bi, Data data) { + public void onImage(Object o, BufferedImage bi, Data data) { if (bi == null) { fitOv = null; } else { @@ -129,7 +141,7 @@ public class Cameras extends Panel { } } } - startTimer(2000); + startTimer(1000); } @Override @@ -220,6 +232,7 @@ public class Cameras extends Panel { camera.close(); camera = null; renderer.setDevice(null); + renderer.setShowReticle(false); renderer.clear(); } try { @@ -229,8 +242,14 @@ public class Cameras extends Panel { //SwingUtils.showMessage(null, "", json); try { if (checkCamtool.isSelected()) { - camera = new Camtool("CurrentCamera", cameraName, false, true); - + camera = new Camtool("CurrentCamera", cameraName, false, true); + camera.getConfig().flipHorizontally = false; + camera.getConfig().flipVertically = false; + camera.getConfig().rotation = 0.0; + camera.getConfig().roiX = 0; + camera.getConfig().roiY = 0; + camera.getConfig().roiWidth = -1; + camera.getConfig().roiHeight = -1; } else { camera = new PsiCamera("CurrentCamera", cameraName); config = (CameraConfig) JsonSerializer.decode(cameraConfigJson, CameraConfig.class); @@ -262,31 +281,50 @@ public class Cameras extends Panel { } camera.initialize(); if (camera instanceof Camtool){ + try{ + camera.getConfig().spatialCalOffsetX = ((Camtool)camera).calOffX.read(); + camera.getConfig().spatialCalOffsetY = ((Camtool)camera).calOffY.read(); + camera.getConfig().spatialCalScaleX = ((Camtool)camera).calScaleX.read(); + camera.getConfig().spatialCalScaleY = ((Camtool)camera).calScaleY.read(); + } catch (Exception ex){ + ex.printStackTrace(); + camera.getConfig().spatialCalOffsetX = 0.0; + camera.getConfig().spatialCalOffsetY = 0.0; + camera.getConfig().spatialCalScaleX = 1.0; + camera.getConfig().spatialCalScaleY = 1.0; + } + /* double[] origin = ((Camtool)camera).origin.read(); if (origin.length>=2){ camera.getConfig().spatialCalOffsetX = origin[0]; camera.getConfig().spatialCalOffsetY = origin[1]; } + */ } camera.getConfig().save(); - camera.setPolling(-2000); - camera.setMonitored(false); - renderer.setDevice(camera); - renderer.setShowReticle(true); + if (polling<=0){ + camera.setMonitored(true); + } else { + camera.setPolling(-polling); + } + renderer.setDevice(camera); renderer.setAutoScroll(true); camera.addListener(new ImageListener() { @Override public void onImage(Object o, BufferedImage bi, Data data) { + if (bi == null) { renderer.removeOverlays(fitOv); fitOv = null; } else { - if (renderer.getReticle() != null) { - renderer.getReticle().setSize(new Dimension(bi.getWidth(), bi.getHeight())); - } + //if (renderer.getReticle() != null) { + // renderer.getReticle().setSize(new Dimension(bi.getWidth(), bi.getHeight())); + //} +//long start = System.currentTimeMillis(); Overlay[] profile = renderer.getProfileOverlays(); +//System.out.println(System.currentTimeMillis() - start); profile = ((profile != null) && (profile.length==4)) ? getFitOverlays(bi) : null; renderer.updateOverlays(profile, fitOv); fitOv = profile; @@ -300,6 +338,21 @@ public class Cameras extends Panel { }); } finally { + if (renderer.getZoom() > 1.0){ + renderer.resetZoom(); + } + if (camera != null) { + if (camera.getConfig().isCalibrated()){ +System.out.println("Calibrated"); + renderer.setCalibration(camera.getCalibration()); + renderer.configureReticle(new Dimension(800,800), 200); + renderer.setShowReticle(true); + } else{ +System.out.println("Not Calibrated"); + + } + } + onTimer(); } } @@ -309,7 +362,6 @@ public class Cameras extends Panel { textState.setText((camera == null) ? "" : camera.getState().toString()); buttonConfig.setEnabled(camera != null); if (App.hasArgument("s")){ - System.out.println("Refresh"); try { ((Source)getDevice("image")).initialize(); } catch (IOException ex) { @@ -320,30 +372,42 @@ public class Cameras extends Panel { } } - Pen fitPen = new Pen(new Color(0, 192, 0), 2); + Pen fitPen = new Pen(new Color(192, 105, 0), 1); + Pen crossPen = new Pen(new Color(192, 105, 0), 1); Overlay[] getFitOverlays(BufferedImage img){ Overlays.Polyline hpoly = null; Overlays.Polyline vpoly = null; - int factor = 2; + Double xMean = null; Double xSigma = null; + Double yMean = null; Double ySigma = null; Profile profile = renderer.getProfile(); if ((profile != Profile.None) && (img != null)) { img = Utils.grayscale(img); if (profile.hasVertical()) { try { - int[] sum = Utils.integrateVertically(img); - int[] x = Arr.indexesInt(sum.length); + double[] sum = (double[]) Convert.toDouble(Utils.integrateVertically(img)); + int[] x = Arr.indexesInt(sum.length); + DescriptiveStatistics stats = new DescriptiveStatistics(sum); + double min = stats.getMin(); + for (int i=0; i< sum.length; i++){ + sum[i] = sum[i] - min; + } double[] gaussian = fitGaussian(sum, x); if (gaussian!=null){ - System.out.println("Norm: " + gaussian[0] + " Mean: " + gaussian[1] + " Sigma: " + gaussian[2]); - - double[] fit = getFitFunction(gaussian, x); - int[] y = new int[x.length]; - for (int i = 0; i < x.length; i++) { - y[i] = (int) (img.getHeight() - 1 - (((double) fit[i]) / 255 / factor)); - } - vpoly = new Overlays.Polyline(fitPen, x, y); +// System.out.println("Norm: " + gaussian[0] + " Mean: " + gaussian[1] + " Sigma: " + gaussian[2]+ " Min: " + min); + //Only aknowledge beam fully inside the image and peak over 3% of min + if ((gaussian[2] < sum.length * 0.45) && (gaussian[0] > min * 0.03)){ + xMean = gaussian[1]; + xSigma = gaussian[2]; + gaussian[0]+=min; + double[] fit = getFitFunction(gaussian, x); + int[] y = new int[x.length]; + for (int i = 0; i < x.length; i++) { + y[i] = (int) (img.getHeight() - 1 - (((double) fit[i]) / 255 / renderer.getProfileFactor())); + } + vpoly = new Overlays.Polyline(fitPen, x, y); + } } else { - System.out.println("Fit failure"); + //System.out.println("Fit failure"); } } catch (Exception ex) { @@ -352,53 +416,67 @@ public class Cameras extends Panel { } if (profile.hasHorizontal()) { try { - int[] sum = Utils.integrateHorizontally(img); + double[] sum = (double[]) Convert.toDouble(Utils.integrateHorizontally(img)); int[] x = Arr.indexesInt(sum.length); + DescriptiveStatistics stats = new DescriptiveStatistics(sum); + double min = stats.getMin(); + for (int i=0; i< sum.length; i++){ + sum[i] = sum[i] - min; + } double[] gaussian = fitGaussian(sum, x); if (gaussian!=null){ - double[] fit = getFitFunction(gaussian, x); - - int[] y = new int[x.length]; - for (int i = 0; i < x.length; i++) { - y[i] = (int) (((double) fit[i]) / 255 / factor); - } - hpoly = new Overlays.Polyline(fitPen, y, x); + //Only aknowledge beam fully inside the image and peak over 3% of min + if ((gaussian[2] < sum.length * 0.45) && (gaussian[0] > min * 0.03)){ + yMean = gaussian[1]; + ySigma = gaussian[2]; + gaussian[0]+=min; + double[] fit = getFitFunction(gaussian, x); + + int[] y = new int[x.length]; + for (int i = 0; i < x.length; i++) { + y[i] = (int) (((double) fit[i]) / 255 / renderer.getProfileFactor()); + } + hpoly = new Overlays.Polyline(fitPen, y, x); + } } } catch (Exception ex) { ex.printStackTrace(); } } - return new Overlay[]{hpoly, vpoly}; + Overlays.Crosshairs cross= null; + if ((xMean!=null) && (yMean!=null)){ + cross = new Overlays.Crosshairs(crossPen, + new Point(xMean.intValue(), yMean.intValue()), + new Dimension(2*xSigma.intValue(),2*ySigma.intValue())); + } + return new Overlay[]{hpoly, vpoly, cross}; } return null; } + double[] fitGaussianScript(int[] y, int[] x){ ScriptManager sm = Controller.getInstance().getScriptManager(); ArrayProperties pY = ArrayProperties.get(y); sm.setVar("y", y); sm.setVar("x", x); - //InterpreterResult r = sm.eval("r = fit(y, x)"); - System.out.println("-> " + pY.maxIndex); InterpreterResult r = sm.eval("r = fit_gaussians(y, x, [" + pY.maxIndex+ ",])"); if (r.exception != null){ r.exception .printStackTrace(); } else { List ret = (List) sm.getVar("r"); - System.out.println(ret.size()); if ((ret!=null) && (ret.size()==1)&& (ret.get(0) instanceof List) && (((List)(ret.get(0))).size()==3)){ double norm = (Double) ((List)ret.get(0)).get(0); double mean = (Double) ((List)ret.get(0)).get(1); double sigma = (Double) ((List)ret.get(0)).get(2); - System.out.println("! " + norm + " " + mean + " " + sigma) ; return new double[]{norm, mean, sigma}; } } return null; } -double[]fitGaussian(int[] y, int[] x){ +double[]fitGaussian(double[] y, int[] x){ try{ ArrayProperties pY = ArrayProperties.get(y); GaussianCurveFitter fitter = GaussianCurveFitter.create().withStartPoint(new double[]{(pY.max-pY.min)/2,x[pY.maxIndex],1.0}).withMaxIterations(1000); @@ -423,174 +501,7 @@ double[]fitGaussian(int[] y, int[] x){ } return fit; } - - ///////// - 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 ChannelDoubleArray origin; - 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 + (roi ? "roi.output.shape" : "image.shape")); - origin = new ChannelDoubleArray(name + " origin X", roi ? (dataPrefix + "roi.origin_out") : (prefix+"origin")); - 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(); - } - - void safeInitialize(Device dev, int timeout) throws IOException, InterruptedException{ - for (int retries = 0; retries < 10; retries ++){ - try{ - dev.initialize(); - break; - } catch (IOException ex){ - if (retries==9){ - throw ex; - } - Thread.sleep(timeout / 10); - } - } - } - - @Override - protected void doInitialize() throws IOException, InterruptedException { - try { - safeInitialize (channelRun,3000); - channelLatch.initialize(); - if (latch){ - start(); - latch(); - } - safeInitialize (channelTimestamp,2000); - posX.initialize(); - posY.initialize(); - profileX.initialize(); - profileY.initialize(); - shape.initialize(); - origin.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 { - System.out.println("LATCH"); - channelLatch.write(1); - System.out.println("Ret:" + channelLatch.read()); - } - - 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); - } - } - } - + //////// @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents diff --git a/plugins/Camtool.java b/plugins/Camtool.java index 1823731..89839c8 100644 --- a/plugins/Camtool.java +++ b/plugins/Camtool.java @@ -11,238 +11,279 @@ import java.util.ArrayList; /** * */ - public class Camtool extends ArraySource { +public class Camtool extends ArraySource { - final String prefix; - final String dataPrefix; - final boolean latch; + final String prefix; + final String dataPrefix; + final boolean latch; - final public ChannelInteger channelRun; - final public ChannelInteger channelLatch; - final public ChannelDouble channelTimestamp; - final public ChannelDoubleArray origin; - 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; + final public ChannelInteger channelRun; + final public ChannelInteger channelLatch; + final public ChannelDouble channelTimestamp; + final public ChannelDoubleArray origin; + final public ChannelDouble posX, posY; + final public ChannelDoubleArray profileX, profileY; + final public ChannelIntegerArray shape; + final public ChannelInteger bgEnable, bgCapture, bgCaptureRemain; + final public ChannelDouble calOffX, calOffY, calScaleX, calScaleY; + 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) { + 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") + "."; + 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); + 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 + (roi ? "roi.output.shape" : "image.shape")); - - origin = new ChannelDoubleArray(name + " origin X", roi ? (dataPrefix + "roi.origin_out") : (prefix+"origin")); - 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"); + //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 + (roi ? "roi.output.shape" : "image.shape")); + origin = new ChannelDoubleArray(name + " origin X", roi ? (dataPrefix + "roi.origin_out") : (prefix + "origin")); + 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"); + calOffX = new ChannelDouble(name + " cal off x", this.prefix + "pipeline.egu.eoff_x"); + calOffY = new ChannelDouble(name + " cal off y", this.prefix + "pipeline.egu.eoff_y"); + calScaleX = new ChannelDouble(name + " cal scale x", this.prefix + "pipeline.egu.eslo_x"); + calScaleY = new ChannelDouble(name + " cal scale y", this.prefix + "pipeline.egu.eslo_y"); - posMeanX = new CamToolPosX(); - posMeanY = new CamToolPosY(); - posVarX = new CamToolVarX(); - posVarY = new CamToolVarY(); - } + 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 doSetMonitored(boolean value) { + super.doSetMonitored(value); + getDevice().setMonitored(value); + } - @Override - public void doUpdate() throws IOException, InterruptedException { - super.doUpdate(); - getDevice().update(); - } + @Override + public void doUpdate() throws IOException, InterruptedException { + super.doUpdate(); + getDevice().update(); + } - void safeInitialize(Device dev, int timeout) throws IOException, InterruptedException{ - for (int retries = 0; retries < 10; retries ++){ - try{ - dev.initialize(); - break; - } catch (IOException ex){ - if (retries==9){ - throw ex; - } - Thread.sleep(timeout / 10); - } - } - } - - @Override - protected void doInitialize() throws IOException, InterruptedException { + void safeInitialize(Device dev, int timeout) throws IOException, InterruptedException { + for (int retries = 0; retries < 10; retries++) { try { - safeInitialize (channelRun,3000); - channelLatch.initialize(); - if (latch){ - start(); - latch(); + dev.initialize(); + break; + } catch (IOException ex) { + if (retries == 9) { + throw ex; } - safeInitialize (channelTimestamp,2000); - posX.initialize(); - posY.initialize(); - profileX.initialize(); - profileY.initialize(); - shape.initialize(); - origin.initialize(); - bgEnable.initialize(); - bgCapture.initialize(); - bgCaptureRemain.initialize(); + Thread.sleep(timeout / 10); + } + } + } - 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; + @Override + protected void doInitialize() throws IOException, InterruptedException { + try { + safeInitialize(channelRun, 3000); + channelLatch.initialize(); + if (latch) { + start(); + latch(); + } + safeInitialize(channelTimestamp, 2000); + posX.initialize(); + posY.initialize(); + profileX.initialize(); + profileY.initialize(); + shape.initialize(); + origin.initialize(); + bgEnable.initialize(); + bgCapture.initialize(); + bgCaptureRemain.initialize(); + try { + calOffX.initialize(); + calOffY.initialize(); + calScaleX.initialize(); + calScaleY.initialize(); } catch (Exception ex) { - throw new IOException(ex); + ex.printStackTrace(); + } + + 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) { + ex.printStackTrace(); + 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 { + int retries = 3; + while (true) { + try { + double timestamp = channelTimestamp.read(); + if (latch) { + channelLatch.write(1); + } else { + channelRun.write(1); + } + long start = System.currentTimeMillis(); + while (true) { + double val = channelTimestamp.read(); + if (timestamp != val) { + return; + } + if ((System.currentTimeMillis() - start) > grabTimeout) { + throw new IOException("Frame timeout"); + } + Thread.sleep(5); + } + } catch (IOException ex) { + retries--; + if (--retries <= 0) { + throw ex; + } } } + } - int numImages = 1; + public void start() throws IOException, InterruptedException { + channelRun.write(-1); + } - public int getNumImages() { - return numImages; + 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); } + } - 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 { - System.out.println("LATCH"); - channelLatch.write(1); - System.out.println("Ret:" + channelLatch.read()); - } - - 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){ + } + } + + public double mean(ArrayList samples) { int count = 0; - double temp = 0; + double temp = 0; for (Double n : samples) { - if (!Double.isNaN(n)){ + if (!Double.isNaN(n)) { temp += n.doubleValue(); count++; } } - return count==0 ? Double.NaN : temp/count; + return count == 0 ? Double.NaN : temp / count; } - public double stdev(ArrayList samples){ + public double stdev(ArrayList samples) { int count = 0; - double temp = 0; + double temp = 0; double mean = mean(samples); for (Double n : samples) { - if (!Double.isNaN(n)){ - temp += Math.pow((mean - n), 2); + if (!Double.isNaN(n)) { + temp += Math.pow((mean - n), 2); } } - return count==0 ? Double.NaN : temp / count; + return count == 0 ? Double.NaN : temp / count; } - + }