Files
sf-op/plugins/Camtool.java
2016-08-31 16:58:44 +02:00

219 lines
6.9 KiB
Java

/*
* 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);
}
public Camtool(String name, String prefix, boolean latch) {
super(name, prefix + (latch ? ":latch": ":pipeline") + ".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<Double> posXSamples;
ArrayList<Double> posYSamples;
public void updateStats() throws IOException, InterruptedException {
posXSamples.clear();
posYSamples.clear();
for (int i=0; i<getNumImages();i++){
capture();
posXSamples.add(posX.read());
posXSamples.add(posY.read());
}
}
class CamToolPosX implements Readable{
@Override
public Object read() throws IOException, InterruptedException {
return mean(posXSamples);
}
}
class CamToolPosY implements Readable{
@Override
public Object read() throws IOException, InterruptedException {
return mean(posYSamples);
}
}
class CamToolVarX implements Readable{
@Override
public Object read() throws IOException, InterruptedException {
return stdev(posXSamples);
}
}
class CamToolVarY implements Readable{
@Override
public Object read() throws IOException, InterruptedException {
return stdev(posYSamples);
}
}
public double mean(ArrayList<Double> 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<Double> 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;
}
}