/* * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved. */ import ch.psi.pshell.plot.LinePlotErrorSeries; import ch.psi.pshell.plot.LinePlotJFree; import ch.psi.pshell.plot.Plot; import ch.psi.pshell.scan.Scan; import ch.psi.pshell.scan.ScanListener; import ch.psi.pshell.scan.ScanRecord; import ch.psi.pshell.scan.ScanResult; import ch.psi.pshell.ui.Panel; import ch.psi.pshell.ui.Plugin; import ch.psi.utils.Convert; import ch.psi.utils.State; import java.util.HashMap; import java.util.List; import java.util.logging.Level; /** * */ public class GunSolenoidAlignment extends Panel { public GunSolenoidAlignment() { initComponents(); centroidPlot.setStyle(LinePlotJFree.Style.ErrorXY); } boolean running; ScanListener scanListener = new ScanListener() { public void onNewRecord(Scan scan, ScanRecord record) { try { if (running) { double error_x = 0.2; double error_y = 0.4; scanSeries.appendData((Double) record.getValues()[0], (Double) record.getValues()[1], error_x, error_y); progressBar.setValue((int) (1000.0 * record.getIndex() / scan.getNumberOfRecords())); } } catch (Exception ex) { getLogger().log(Level.WARNING, null, ex); } } }; //Overridable callbacks @Override public void onInitialize(int runCount) { progressBar.setValue(0); getController().addScanListener(scanListener); centroidPlot.getAxis(Plot.AxisId.X).setLabel("Centroid Excursion"); } @Override public void onStateChange(State state, State former) { buttonAbort.setEnabled(state == State.Busy); buttonRun.setEnabled(state == State.Ready); if (state != State.Busy) { running = false; } } @Override public void onExecutedFile(String fileName, Object result) { } //Callback to perform update - in event thread @Override protected void doUpdate() { } LinePlotErrorSeries scanSeries; int index = 1; void onEndScan(ScanResult sr, List hx, List hy) { progressBar.setValue(1000); //double[] x =(double[]) Convert.toDouble(sr.getReadable(0)); //double[] y =(double[]) Convert.toDouble(sr.getReadable(1)); double[] x = (double[]) Convert.toDouble(hx); double[] y = (double[]) Convert.toDouble(hy); LinePlotErrorSeries hull = new LinePlotErrorSeries(scanSeries.getName() + "H", scanSeries.getColor()); centroidPlot.addSeries(hull); hull.setData((double[]) Convert.toDouble(x), (double[]) Convert.toDouble(y)); } @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { buttonGroupBack = new javax.swing.ButtonGroup(); buttonRun = new javax.swing.JButton(); buttonAbort = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); spinnerI1 = new javax.swing.JSpinner(); jLabel2 = new javax.swing.JLabel(); spinnerI2 = new javax.swing.JSpinner(); jLabel3 = new javax.swing.JLabel(); spinnerStep = new javax.swing.JSpinner(); jLabel4 = new javax.swing.JLabel(); spinnerSettlingTime = new javax.swing.JSpinner(); checkSaveImages = new javax.swing.JCheckBox(); spinnerNumBackgrounds = new javax.swing.JSpinner(); jLabel5 = new javax.swing.JLabel(); checkBackground = new javax.swing.JCheckBox(); radioBackSimple = new javax.swing.JRadioButton(); radioBackMultiple = new javax.swing.JRadioButton(); jLabel6 = new javax.swing.JLabel(); spinnerNumImages = new javax.swing.JSpinner(); checkElog = new javax.swing.JCheckBox(); checkZeroSolenoids = new javax.swing.JCheckBox(); centroidPlot = new ch.psi.pshell.plot.LinePlotJFree(); buttonClear = new javax.swing.JButton(); progressBar = new javax.swing.JProgressBar(); buttonRun.setText("Run"); buttonRun.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonRunActionPerformed(evt); } }); buttonAbort.setText("Abort"); buttonAbort.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonAbortActionPerformed(evt); } }); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Parameters")); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel1.setText("I1:"); spinnerI1.setModel(new javax.swing.SpinnerNumberModel(95.0d, 50.0d, 150.0d, 1.0d)); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel2.setText("I2:"); spinnerI2.setModel(new javax.swing.SpinnerNumberModel(100.0d, 50.0d, 150.0d, 1.0d)); jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel3.setText("Step size:"); spinnerStep.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.1d, 10.0d, 1.0d)); spinnerStep.setName("spinnerStep"); // NOI18N jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel4.setText("Settling time:"); spinnerSettlingTime.setModel(new javax.swing.SpinnerNumberModel(0.1d, 0.0d, 10.0d, 1.0d)); checkSaveImages.setText("Save images"); spinnerNumBackgrounds.setModel(new javax.swing.SpinnerNumberModel(5, 1, 10, 1)); jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel5.setText("Number of backgrounds:"); checkBackground.setSelected(true); checkBackground.setText("Background:"); checkBackground.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { checkBackgroundActionPerformed(evt); } }); buttonGroupBack.add(radioBackSimple); radioBackSimple.setText("Simple"); buttonGroupBack.add(radioBackMultiple); radioBackMultiple.setSelected(true); radioBackMultiple.setText("Multiple"); jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel6.setText("Number of images:"); spinnerNumImages.setModel(new javax.swing.SpinnerNumberModel(0, 0, 10, 1)); checkElog.setSelected(true); checkElog.setText("ELOG"); checkZeroSolenoids.setSelected(true); checkZeroSolenoids.setText("Zero solenoids"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(spinnerI1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(spinnerI2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(spinnerStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(spinnerSettlingTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(spinnerNumBackgrounds, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(spinnerNumImages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(radioBackSimple) .addComponent(checkBackground)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(36, 36, 36) .addComponent(radioBackMultiple)) .addComponent(checkSaveImages) .addComponent(checkElog) .addComponent(checkZeroSolenoids)) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {spinnerI1, spinnerI2, spinnerNumBackgrounds, spinnerNumImages, spinnerSettlingTime, spinnerStep}); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4, jLabel5, jLabel6}); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(spinnerI1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(spinnerI2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(spinnerStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(spinnerSettlingTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(4, 4, 4) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(spinnerNumImages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(checkBackground) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(radioBackSimple) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(radioBackMultiple) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(spinnerNumBackgrounds, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(57, 57, 57) .addComponent(checkSaveImages) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(checkElog) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(checkZeroSolenoids) .addContainerGap(34, Short.MAX_VALUE)) ); centroidPlot.setLegendVisible(true); centroidPlot.setTitle(""); centroidPlot.setTitleFont(new java.awt.Font("DejaVu Sans", 0, 14)); // NOI18N buttonClear.setText("Clear"); buttonClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonClearActionPerformed(evt); } }); progressBar.setMaximum(1000); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonRun, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(buttonAbort, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(12, 12, 12) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(12, 12, 12) .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE) .addGap(18, 18, 18) .addComponent(buttonClear)) .addComponent(centroidPlot, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonRun) .addComponent(buttonAbort)) .addGap(18, 18, 18) .addComponent(centroidPlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(buttonClear) .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); }// //GEN-END:initComponents //((Plugin)this) private void buttonAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAbortActionPerformed try { abort(); } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_buttonAbortActionPerformed private void buttonRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRunActionPerformed try { progressBar.setValue(0); scanSeries = new LinePlotErrorSeries(String.valueOf(index++)); centroidPlot.addSeries(scanSeries); scanSeries.setLinesVisible(false); scanSeries.setPointSize(4); running = true; HashMap args = new HashMap(); args.put("I1", spinnerI1.getValue()); args.put("I2", spinnerI2.getValue()); args.put("dI", spinnerStep.getValue()); args.put("settling_time", spinnerSettlingTime.getValue()); args.put("plot_image", checkSaveImages.isSelected()); args.put("number_images", spinnerNumImages.getValue()); args.put("number_backgrounds", spinnerNumBackgrounds.getValue()); args.put("use_background", checkBackground.isSelected()); args.put("multiple_background", radioBackMultiple.isSelected()); args.put("zero_solenoids", checkZeroSolenoids.isSelected()); args.put("do_elog", checkElog.isSelected()); ((Plugin) this).runAsync("Alignment/Gun_solenoid_alignment", args).thenAccept((Object t) -> { List ret = (List) t; onEndScan((ScanResult) ret.get(0), (List) ret.get(1), (List) ret.get(2)); }); } catch (Exception ex) { running = false; showException(ex); } }//GEN-LAST:event_buttonRunActionPerformed private void checkBackgroundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBackgroundActionPerformed radioBackMultiple.setEnabled(checkBackground.isSelected()); radioBackSimple.setEnabled(checkBackground.isSelected()); spinnerNumBackgrounds.setEnabled(checkBackground.isSelected()); }//GEN-LAST:event_checkBackgroundActionPerformed private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed try { index = 1; centroidPlot.clear(); } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_buttonClearActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton buttonAbort; private javax.swing.JButton buttonClear; private javax.swing.ButtonGroup buttonGroupBack; private javax.swing.JButton buttonRun; private ch.psi.pshell.plot.LinePlotJFree centroidPlot; private javax.swing.JCheckBox checkBackground; private javax.swing.JCheckBox checkElog; private javax.swing.JCheckBox checkSaveImages; private javax.swing.JCheckBox checkZeroSolenoids; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JProgressBar progressBar; private javax.swing.JRadioButton radioBackMultiple; private javax.swing.JRadioButton radioBackSimple; private javax.swing.JSpinner spinnerI1; private javax.swing.JSpinner spinnerI2; private javax.swing.JSpinner spinnerNumBackgrounds; private javax.swing.JSpinner spinnerNumImages; private javax.swing.JSpinner spinnerSettlingTime; private javax.swing.JSpinner spinnerStep; // End of variables declaration//GEN-END:variables }