From 5628fd00da981b564aabe81ad22b42f8dfe972d2 Mon Sep 17 00:00:00 2001 From: Alexandre Gobbo Date: Mon, 26 Aug 2019 11:45:14 +0200 Subject: [PATCH] Startup --- devices/AthosCamera.properties | 26 +++++ plugins/AthosCameras.form | 59 ++++++++-- plugins/AthosCameras.java | 204 +++++++++++++++++++++++++++------ 3 files changed, 242 insertions(+), 47 deletions(-) create mode 100644 devices/AthosCamera.properties diff --git a/devices/AthosCamera.properties b/devices/AthosCamera.properties new file mode 100644 index 0000000..0f080b7 --- /dev/null +++ b/devices/AthosCamera.properties @@ -0,0 +1,26 @@ +#Mon Aug 26 09:42:13 CEST 2019 +colormap=Flame +colormapAutomatic=true +colormapLogarithmic=false +colormapMax=NaN +colormapMin=NaN +flipHorizontally=false +flipVertically=false +grayscale=false +invert=false +rescaleFactor=1.0 +rescaleOffset=0.0 +roiHeight=-1 +roiWidth=-1 +roiX=0 +roiY=0 +rotation=0.0 +rotationCrop=false +scale=1.0 +serverURL=localhost\:8889 +spatialCalOffsetX=-637.4980411378614 +spatialCalOffsetY=-483.5036425564949 +spatialCalScaleX=-35.21126887460907 +spatialCalScaleY=-48.38709552593848 +spatialCalUnits=mm +transpose=false diff --git a/plugins/AthosCameras.form b/plugins/AthosCameras.form index edacaa3..92f5089 100644 --- a/plugins/AthosCameras.form +++ b/plugins/AthosCameras.form @@ -16,18 +16,28 @@ - + - - - - - - - + + + + + + + + + + + + + + + + + + - @@ -51,7 +61,12 @@ - + + + + + + @@ -79,12 +94,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/AthosCameras.java b/plugins/AthosCameras.java index 56760af..fb5e133 100644 --- a/plugins/AthosCameras.java +++ b/plugins/AthosCameras.java @@ -1,34 +1,54 @@ import ch.psi.pshell.bs.PipelineServer; +import ch.psi.pshell.bs.StreamValue; +import ch.psi.pshell.device.Device; +import ch.psi.pshell.device.DeviceAdapter; +import ch.psi.pshell.device.DeviceListener; import ch.psi.pshell.imaging.ColormapSource; import ch.psi.pshell.imaging.Overlay; import ch.psi.pshell.imaging.Overlays; +import ch.psi.pshell.scan.MonitorScan; import ch.psi.pshell.ui.App; import ch.psi.pshell.ui.Panel; import ch.psi.utils.State; +import ch.psi.utils.Threading; +import ch.psi.utils.swing.MainFrame; import java.awt.Font; import java.awt.Point; import java.io.IOException; +import java.nio.file.Paths; import java.util.HashMap; +import java.util.concurrent.Future; import java.util.logging.Level; import java.util.logging.Logger; +import javax.swing.ImageIcon; /** * */ public class AthosCameras extends Panel { - PipelineServer server; - ColormapSource camera; + PipelineServer imagePipeline; + PipelineServer dataPipeline; String serverUrl; Overlay errorOverlay; - String instanceName; + String imageInstanceName; + String dataInstanceName; String cameraName; + String persistFile = "{context}/AthosCameras"; final String CAMERA_DEVICE_NAME = "AthosCamera"; - String pipelineSuffix = "_sp"; + String pipelineSuffixData = "_acd"; + String pipelineSuffixImage = "_aci"; + + boolean persisting; public AthosCameras() { initComponents(); + + } + + ImageIcon getIcon(String name){ + return new ImageIcon(ch.psi.pshell.ui.App.class.getResource("/ch/psi/pshell/ui/" + (MainFrame.isDark() ? "dark/": "") + name + ".png")); } //Overridable callbacks @@ -41,6 +61,7 @@ public class AthosCameras extends Panel { Logger.getLogger(AthosCameras.class.getName()).log(Level.SEVERE, null, ex); } } + renderer.setPersistenceFile(Paths.get(getContext().getSetup().expandPath(persistFile))); } @Override @@ -62,12 +83,12 @@ public class AthosCameras extends Panel { PipelineServer newServer() throws IOException { if (serverUrl != null) { System.out.println("Connecting to server: " + serverUrl); - server = new PipelineServer(CAMERA_DEVICE_NAME, serverUrl); + imagePipeline = new PipelineServer(CAMERA_DEVICE_NAME, serverUrl); } else { System.out.println("Connecting to server"); - server = new PipelineServer(CAMERA_DEVICE_NAME); + imagePipeline = new PipelineServer(CAMERA_DEVICE_NAME); } - return server; + return imagePipeline; } void setCamera(String cameraName) throws IOException, InterruptedException { @@ -87,33 +108,60 @@ public class AthosCameras extends Panel { System.out.println("Setting camera: " + cameraName ); try{ - camera = newServer(); - 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; - camera.initialize(); - camera.assertInitialized(); - System.out.println("Camera initialization OK"); + imagePipeline = newServer(); + imagePipeline.getConfig().flipHorizontally = false; + imagePipeline.getConfig().flipVertically = false; + imagePipeline.getConfig().rotation = 0.0; + imagePipeline.getConfig().roiX = 0; + imagePipeline.getConfig().roiY = 0; + imagePipeline.getConfig().roiWidth = -1; + imagePipeline.getConfig().roiHeight = -1; + imagePipeline.getConfig().save(); + imagePipeline.initialize(); + imagePipeline.assertInitialized(); + System.out.println("Image pipeline initialization OK"); - String pipelineName = cameraName + pipelineSuffix; - instanceName = cameraName + pipelineSuffix + "1"; - if (!server.getPipelines().contains(pipelineName)) { + String pipelineName = cameraName + pipelineSuffixImage; + imageInstanceName = cameraName + pipelineSuffixImage + "1"; + if (!imagePipeline.getPipelines().contains(pipelineSuffixImage)) { System.out.println("Creating pipeline: " + pipelineName); HashMap config = new HashMap<>(); config.put("camera_name", cameraName); + //config.put("include", new String[]{"image", "width", "height"}); + config.put("function", "transparent"); + config.put("max_frame_rate" , 2.1); //server.createFromConfig(config, pipelineName); - server.savePipelineConfig(pipelineName, config); + imagePipeline.savePipelineConfig(pipelineName, config); } - server.start(pipelineName, instanceName); - - camera.getConfig().save(); - renderer.setDevice(camera); - renderer.setAutoScroll(true); - renderer.setSource(camera); + imagePipeline.start(pipelineName, imageInstanceName); + renderer.setDevice(imagePipeline); + + + dataPipeline = newServer(); + //dataPipeline.getConfig().copyFrom(imagePipeline.getConfig()); + dataPipeline.initialize(); + dataPipeline.assertInitialized(); + System.out.println("Data pipeline initialization OK"); + + pipelineName = cameraName + pipelineSuffixData; + dataInstanceName = cameraName + pipelineSuffixData + "1"; + if (!dataPipeline.getPipelines().contains(pipelineSuffixData)) { + System.out.println("Creating pipeline: " + pipelineName); + HashMap config = new HashMap<>(); + config.put("camera_name", cameraName); + config.put("include", new String[]{"x_center_of_mass", "y_center_of_mass"}); + //server.createFromConfig(config, pipelineName); + dataPipeline.savePipelineConfig(pipelineName, config); + } + dataPipeline.start(pipelineName, dataInstanceName); + + dataPipeline.getStream().addListener(new DeviceAdapter() { + @Override + public void onCacheChanged(Device device, Object value, Object former, long timestamp, boolean arg4) { + updateData((StreamValue)value); + } + }); + } catch (Exception ex) { showException(ex); renderer.clearOverlays(); @@ -131,6 +179,37 @@ public class AthosCameras extends Panel { } } + String getDoubleStr(StreamValue sv, String id){ + return String.format("%1.4f", sv.getValue(id)); + } + + void updateData(StreamValue value){ + //System.exit(0); + try{ + edit_x_center_of_mass.setText(getDoubleStr(value, "x_center_of_mass")); + } catch (Exception ex){ + edit_x_center_of_mass.setText(""); + } + try{ + edit_y_center_of_mass.setText(getDoubleStr(value, "y_center_of_mass")); + } catch (Exception ex){ + edit_y_center_of_mass.setText(""); + } + } + + MonitorScan scan; + + void startRecording() throws IOException, InterruptedException{ + System.out.println("startRecording"); + scan= new MonitorScan(dataPipeline.getStream(), dataPipeline.getStream().getReadables().toArray(new ch.psi.pshell.device.Readable[0]), -1, -1); + Threading.getFuture(() ->scan.start()); + } + + void stopRecording() throws InterruptedException{ + System.out.println("stopRecording"); + scan.abort(); + } + @SuppressWarnings("unchecked") @@ -142,29 +221,56 @@ public class AthosCameras extends Panel { jLabel2 = new javax.swing.JLabel(); edit_y_center_of_mass = new javax.swing.JTextField(); edit_x_center_of_mass = new javax.swing.JTextField(); + buttonRec = new javax.swing.JToggleButton(); + buttonStop = new javax.swing.JButton(); jLabel1.setText("x_center_of_mass:"); jLabel2.setText("y_center_of_mass:"); edit_y_center_of_mass.setEditable(false); + edit_y_center_of_mass.setHorizontalAlignment(javax.swing.JTextField.TRAILING); edit_x_center_of_mass.setEditable(false); + edit_x_center_of_mass.setHorizontalAlignment(javax.swing.JTextField.TRAILING); + + buttonRec.setIcon(getIcon("Rec")); + buttonRec.setToolTipText("Start Data Recording"); + buttonRec.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonRecActionPerformed(evt); + } + }); + + buttonStop.setIcon(getIcon("Stop")); + buttonStop.setEnabled(false); + buttonStop.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonStopActionPerformed(evt); + } + }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createSequentialGroup() .addContainerGap(503, Short.MAX_VALUE) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(jLabel2) - .addComponent(jLabel1)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(edit_y_center_of_mass, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(edit_x_center_of_mass, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGap(72, 72, 72)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(jLabel2) + .addComponent(jLabel1)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(edit_y_center_of_mass, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(edit_x_center_of_mass, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(72, 72, 72)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(buttonRec) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(buttonStop) + .addGap(92, 92, 92)))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() @@ -182,7 +288,11 @@ public class AthosCameras extends Panel { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(edit_y_center_of_mass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addContainerGap(303, Short.MAX_VALUE)) + .addGap(123, 123, 123) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(buttonRec) + .addComponent(buttonStop)) + .addContainerGap(151, Short.MAX_VALUE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() @@ -191,7 +301,27 @@ public class AthosCameras extends Panel { ); }// //GEN-END:initComponents + private void buttonRecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRecActionPerformed + try{ + buttonStop.setEnabled(buttonRec.isSelected()); + if (buttonRec.isSelected()){ + startRecording(); + } else { + stopRecording(); + } + } catch (Exception ex){ + this.showException(ex); + } + }//GEN-LAST:event_buttonRecActionPerformed + + private void buttonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopActionPerformed + buttonRec.setSelected(false); + buttonRecActionPerformed(null); + }//GEN-LAST:event_buttonStopActionPerformed + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JToggleButton buttonRec; + private javax.swing.JButton buttonStop; private javax.swing.JTextField edit_x_center_of_mass; private javax.swing.JTextField edit_y_center_of_mass; private javax.swing.JLabel jLabel1;