This commit is contained in:
sfop
2016-09-01 09:29:30 +02:00
parent 22d5cfffa6
commit 0f160bc67b
6 changed files with 321 additions and 113 deletions

View File

@@ -16,9 +16,16 @@ import ch.psi.utils.swing.StandardDialog;
import ch.psi.utils.swing.TextEditor;
import ch.psi.pshell.epics.PsiCamera;
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.utils.swing.Editor.EditorDialog;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.image.BufferedImage;
@@ -38,35 +45,36 @@ public class Cameras extends Panel {
public Cameras() {
initComponents();
renderer.setPersistenceFile(Paths.get(getController().getSetup().getContextPath(), "Renderer_Cameras.bin"));
renderer.setPersistenceFile(Paths.get(getController().getSetup().getContextPath(), "Renderer_Cameras.bin"));
setPersistedComponents(new Component[]{});
}
final String configFolder= "/afs/psi.ch/intranet/SF/Applications/config/camtool";
final String configFolder = "/afs/psi.ch/intranet/SF/Applications/config/camtool";
File[] cameraConfigFiles = new File[0];
PsiCamera camera;
ArraySource camera;
String cameraName;
String cameraConfigJson;
CameraConfig config;
//Overridable callbacks
//Overridable callbacks
@Override
public void onInitialize(int runCount) {
cameraConfigFiles = IO.listFiles(configFolder, new String[]{"json"});
Arrays.sort(cameraConfigFiles, (a, b) -> a.compareTo(b));
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (File file:cameraConfigFiles) {
for (File file : cameraConfigFiles) {
String prefix = IO.getPrefix(file);
if (!prefix.startsWith("#")){
if (!prefix.startsWith("#")) {
model.addElement(prefix);
}
}
comboCameras.setModel(model);
comboCameras.setSelectedItem(-1);
if (model.getSize()>0){
if (model.getSize() > 0) {
try {
//setCamera((String)comboCameras.getSelectedItem());
if (App.hasArgument("cam")){
setCamera(App.getArgument("cam"));
if (App.hasArgument("cam")) {
setCamera(App.getArgumentValue("cam"));
}
} catch (Exception ex) {
}
@@ -83,144 +91,324 @@ public class Cameras extends Panel {
public void onExecutedFile(String fileName, Object result) {
}
//Callback to perform update - in event thread
@Override
protected void doUpdate() {
}
public static class CameraConfig {
public static class CameraConfig{
public HashMap kwargs;
public ArrayList args;
public ArrayList links;
public String type;
public HashMap<String, Object> state;
public CameraConfig image_source;
public HashMap<String,CameraConfig> subcomponents;
public ArrayList<Integer> getCalibration(){
public HashMap<String, CameraConfig> subcomponents;
public ArrayList<Integer> getCalibration() {
return (ArrayList<Integer>) state.get("calibration");
}
public Double getCalibrationHeight(){
public Double getCalibrationHeight() {
return (Double) state.get("calibration_height");
}
public Double getCalibrationWidth(){
public Double getCalibrationWidth() {
return (Double) state.get("calibration_width");
}
public Double getCalibrationHorizontalAngle(){
public Double getCalibrationHorizontalAngle() {
return (Double) state.get("calibration_horizontal_angle");
}
public Double getCalibrationVerticalAngle(){
public Double getCalibrationVerticalAngle() {
return (Double) state.get("calibration_vertical_angle");
}
public Boolean getMirrorX(){
public Boolean getMirrorX() {
return (Boolean) state.get("mirror_x");
}
public Boolean getMirrorY(){
public Boolean getMirrorY() {
return (Boolean) state.get("mirror_y");
}
public Integer getRotate(){
public Integer getRotate() {
return (Integer) state.get("rotate");
}
public ArrayList<Integer> getOrigin(){
public ArrayList<Integer> getOrigin() {
return (ArrayList<Integer>) state.get("origin");
}
public ArrayList<Integer> getRoi(){
public ArrayList<Integer> getRoi() {
return (ArrayList<Integer>) state.get("roi");
}
public Boolean getRoiEnable(){
if ((state.get("roi_enable")==null) ||(state.get("roi")==null)){
public Boolean getRoiEnable() {
if ((state.get("roi_enable") == null) || (state.get("roi") == null)) {
return false;
}
}
return (Boolean) state.get("roi_enable");
}
public ArrayList<Double> getUnitSize(){
}
public ArrayList<Double> getUnitSize() {
return (ArrayList<Double>) state.get("unit_size");
}
public Integer getRun(){
public Integer getRun() {
return (Integer) state.get("run");
}
}
}
void setCamera(String cameraName) throws IOException, InterruptedException{
if (camera!=null){
void setCamera(String cameraName) throws IOException, InterruptedException {
if (camera != null) {
camera.close();
camera = null;
renderer.setDevice(null);
renderer.setDevice(null);
renderer.clear();
}
try{
Path configFile= Paths.get(configFolder, cameraName + ".json");
try {
Path configFile = Paths.get(configFolder, cameraName + ".json");
cameraConfigJson = new String(Files.readAllBytes(configFile));
this.cameraName=cameraName;
this.cameraName = cameraName;
//SwingUtils.showMessage(null, "", json);
camera = new PsiCamera("CurrentCamera", cameraName);
try{
config = (CameraConfig) JsonSerializer.decode(cameraConfigJson, CameraConfig.class);
camera.getConfig().flipHorizontally = config.getMirrorX();
camera.getConfig().flipVertically = config.getMirrorY();
camera.getConfig().rotation = config.getRotate();
camera.getConfig().rotationCrop = true;
camera.getConfig().roiX = config.getRoiEnable() ? config.getRoi().get(0) : 0;
camera.getConfig().roiY = config.getRoiEnable() ? config.getRoi().get(1) : 0;
camera.getConfig().roiWidth = config.getRoiEnable() ?config.getRoi().get(2) : -1;
camera.getConfig().roiHeight = config.getRoiEnable() ?config.getRoi().get(3): -1;
//camera.getConfig().spatialCalOffsetX = (config.getOrigin()!=null) ? config.getOrigin().get(0) : Double.NaN;
//camera.getConfig().spatialCalOffsetY = (config.getOrigin()!=null) ? config.getOrigin().get(1) : Double.NaN;
//camera.getConfig().spatialCalScaleX = (config.getUnitSize()!=null) && (config.getOrigin().get(0)!=0) ? 1.0/(config.getOrigin().get(0)*1000) : Double.NaN;
//camera.getConfig().spatialCalScaleY= (config.getUnitSize()!=null) && (config.getOrigin().get(1)!=0) ? 1.0/(config.getOrigin().get(1)*1000) : Double.NaN;
ArrayList<Integer> calibration = config.getCalibration();
if ((calibration!=null) && (calibration.size()!=4)){
calibration=null;
try {
if (checkCamtool.isSelected()) {
camera = new Camtool("CurrentCamera", cameraName, true, true);
} else {
camera = new PsiCamera("CurrentCamera", cameraName);
config = (CameraConfig) JsonSerializer.decode(cameraConfigJson, CameraConfig.class);
camera.getConfig().flipHorizontally = config.getMirrorX();
camera.getConfig().flipVertically = config.getMirrorY();
camera.getConfig().rotation = config.getRotate();
camera.getConfig().rotationCrop = true;
camera.getConfig().roiX = config.getRoiEnable() ? config.getRoi().get(0) : 0;
camera.getConfig().roiY = config.getRoiEnable() ? config.getRoi().get(1) : 0;
camera.getConfig().roiWidth = config.getRoiEnable() ? config.getRoi().get(2) : -1;
camera.getConfig().roiHeight = config.getRoiEnable() ? config.getRoi().get(3) : -1;
//camera.getConfig().spatialCalOffsetX = (config.getOrigin()!=null) ? config.getOrigin().get(0) : Double.NaN;
//camera.getConfig().spatialCalOffsetY = (config.getOrigin()!=null) ? config.getOrigin().get(1) : Double.NaN;
//camera.getConfig().spatialCalScaleX = (config.getUnitSize()!=null) && (config.getOrigin().get(0)!=0) ? 1.0/(config.getOrigin().get(0)*1000) : Double.NaN;
//camera.getConfig().spatialCalScaleY= (config.getUnitSize()!=null) && (config.getOrigin().get(1)!=0) ? 1.0/(config.getOrigin().get(1)*1000) : Double.NaN;
ArrayList<Integer> calibration = config.getCalibration();
if ((calibration != null) && (calibration.size() != 4)) {
calibration = null;
}
camera.getConfig().spatialCalOffsetX = (calibration != null) ? -calibration.get(2) : Double.NaN;
camera.getConfig().spatialCalOffsetY = (calibration != null) ? -calibration.get(3) : Double.NaN;
camera.getConfig().spatialCalScaleX = (calibration != null) && (calibration.get(0) != 0) ? 1.0 / (calibration.get(0)) : Double.NaN;
camera.getConfig().spatialCalScaleY = (calibration != null) && (calibration.get(1) != 0) ? 1.0 / (calibration.get(1)) : Double.NaN;
}
camera.getConfig().spatialCalOffsetX = (calibration!=null) ? - calibration.get(2) : Double.NaN;
camera.getConfig().spatialCalOffsetY = (calibration!=null) ? - calibration.get(3) : Double.NaN;
camera.getConfig().spatialCalScaleX = (calibration!=null) && (calibration.get(0)!=0) ? 1.0/(calibration.get(0)) : Double.NaN;
camera.getConfig().spatialCalScaleY= (calibration!=null) && (calibration.get(1)!=0) ? 1.0/(calibration.get(1)) : Double.NaN;
} catch (Exception ex){
} catch (Exception ex) {
config = null;
showException(ex);
}
camera.getConfig().save();
camera.initialize();
camera.setMonitored(true);
}
camera.getConfig().save();
camera.initialize();
renderer.setDevice(camera);
renderer.setShowReticle(true);
//camera.setMonitored(true);
camera.setPolling(500);
renderer.setDevice(camera);
renderer.setShowReticle(true);
renderer.setAutoScroll(true);
camera.addListener(new ImageListener() {
@Override
public void onImage(Object o, BufferedImage bi, Data data) {
if (renderer.getReticle()!=null){
if (bi==null){
if (renderer.getReticle() != null) {
if (bi == null) {
return;
}
renderer.getReticle().setSize(new Dimension(bi.getWidth(), bi.getHeight()));
}
renderer.getReticle().setSize(new Dimension(bi.getWidth(), bi.getHeight()));
}
camera.removeListener(this);
}
@Override
public void onError(Object o, Exception excptn) {
}
});
} finally{
} finally {
onTimer();
}
}
}
@Override
protected void onTimer() {
textState.setText((camera==null) ? "" : camera.getState().toString());
buttonConfig.setEnabled(camera!=null);
}
textState.setText((camera == null) ? "" : camera.getState().toString());
buttonConfig.setEnabled(camera != null);
}
/////////
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, true, 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"));
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();
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
@@ -232,6 +420,7 @@ public class Cameras extends Panel {
textState = new javax.swing.JTextField();
buttonConfig = new javax.swing.JButton();
buttonSetup = new javax.swing.JButton();
checkCamtool = new javax.swing.JCheckBox();
jLabel1.setText("Camera:");
@@ -262,18 +451,23 @@ public class Cameras extends Panel {
}
});
checkCamtool.setSelected(true);
checkCamtool.setText("Camtool");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(checkCamtool)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(comboCameras, 0, 236, Short.MAX_VALUE)
.addComponent(comboCameras, 0, 218, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonConfig)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
@@ -297,9 +491,10 @@ public class Cameras extends Panel {
.addComponent(jLabel2)
.addComponent(textState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonConfig)
.addComponent(buttonSetup))
.addGap(18, 18, 18)
.addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
.addComponent(buttonSetup)
.addComponent(checkCamtool))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
.addGap(6, 6, 6))
);
@@ -308,16 +503,16 @@ public class Cameras extends Panel {
}// </editor-fold>//GEN-END:initComponents
private void comboCamerasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCamerasActionPerformed
try{
try {
setCamera((String) comboCameras.getSelectedItem());
} catch(Exception ex){
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_comboCamerasActionPerformed
private void buttonConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonConfigActionPerformed
try{
if (camera!=null){
try {
if (camera != null) {
TextEditor editor = new TextEditor();
editor.setText(cameraConfigJson);
editor.setReadOnly(true);
@@ -327,17 +522,17 @@ public class Cameras extends Panel {
dlg.setVisible(true);
SwingUtils.centerComponent(this, dlg);
}
} catch(Exception ex){
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonConfigActionPerformed
private void buttonSetupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSetupActionPerformed
try{
if (camera!=null){
try {
if (camera != null) {
this.showDeviceConfigDialog(camera, false);
}
} catch(Exception ex){
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonSetupActionPerformed
@@ -345,6 +540,7 @@ public class Cameras extends Panel {
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonConfig;
private javax.swing.JButton buttonSetup;
private javax.swing.JCheckBox checkCamtool;
private javax.swing.JComboBox comboCameras;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;