Files
sf-op/plugins/GunSolenoidAlignment.java
2016-11-04 13:39:40 +01:00

444 lines
23 KiB
Java

/*
* 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.awt.Component;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JPanel;
/**
*
*/
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 x = (Double) record.getValues()[0];
double y = (Double) record.getValues()[1];
double stdev_x = (Double) record.getValues()[2];
double stdev_y = (Double) record.getValues()[3];
scanSeries.appendData(x, y, stdev_x, stdev_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;
}
progressBar.setIndeterminate(!state.isInitialized());
for (Component c : panelParameters.getComponents()) {
if (!(c instanceof JPanel)){
c.setEnabled(state == State.Ready);
}
}
if (state == State.Ready){
checkBackgroundActionPerformed(null);
}
}
@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(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));
//Add ELOG entry
if (checkElog.isSelected()) {
String file = Paths.get(getController().getSetup().getContextPath(), "CentroidPlot.jpg").toString();
try {
Thread.sleep(250); //Some time to plot update
centroidPlot.saveSnapshot(file , "jpg");
evalAsync("elog('Gun solenoid current scan', gsa_log_msg, ['"+ file +"',])", true).handle((ret, ex) -> {
if (ex != null){
getLogger().log(Level.WARNING, null, ex);
} else {
getLogger().info("Success generating ELOG entry");
}
return null;
});
} catch (Exception ex) {
showException(ex);
}
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
buttonGroupBack = new javax.swing.ButtonGroup();
buttonRun = new javax.swing.JButton();
buttonAbort = new javax.swing.JButton();
panelParameters = 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();
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);
}
});
panelParameters.setBorder(javax.swing.BorderFactory.createTitledBorder("Parameters"));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel1.setText("I1:");
spinnerI1.setModel(new javax.swing.SpinnerNumberModel(95.0d, 0.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(1, 1, 100, 1));
checkElog.setSelected(true);
checkElog.setText("ELOG");
javax.swing.GroupLayout panelParametersLayout = new javax.swing.GroupLayout(panelParameters);
panelParameters.setLayout(panelParametersLayout);
panelParametersLayout.setHorizontalGroup(
panelParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelParametersLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelParametersLayout.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(panelParametersLayout.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(panelParametersLayout.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(panelParametersLayout.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, panelParametersLayout.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(panelParametersLayout.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(panelParametersLayout.createSequentialGroup()
.addGroup(panelParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(radioBackSimple)
.addComponent(checkBackground))
.addGroup(panelParametersLayout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(radioBackMultiple))
.addComponent(checkSaveImages)
.addComponent(checkElog))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
panelParametersLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {spinnerI1, spinnerI2, spinnerNumBackgrounds, spinnerNumImages, spinnerSettlingTime, spinnerStep});
panelParametersLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4, jLabel5, jLabel6});
panelParametersLayout.setVerticalGroup(
panelParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelParametersLayout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(panelParametersLayout.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(panelParametersLayout.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(panelParametersLayout.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(panelParametersLayout.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(panelParametersLayout.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(panelParametersLayout.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(50, 50, 50)
.addComponent(checkElog)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(checkSaveImages)
.addContainerGap(69, 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()
.addContainerGap()
.addComponent(panelParameters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.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)
.addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(buttonClear))
.addComponent(centroidPlot, javax.swing.GroupLayout.DEFAULT_SIZE, 443, 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(panelParameters, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
}// </editor-fold>//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("do_elog", checkElog.isSelected());
runAsync("Alignment/Gun_solenoid_alignment", args).thenAccept((Object ret) -> {
List l = (List) ret;
onEndScan((ScanResult) l.get(0), (List) l.get(1), (List) l.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.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 panelParameters;
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
}