256 lines
11 KiB
Java
256 lines
11 KiB
Java
/*
|
|
* Copyright (c) 2014-2017 Paul Scherrer Institute. All rights reserved.
|
|
*/
|
|
|
|
import ch.psi.pshell.device.Device;
|
|
import ch.psi.pshell.ui.Panel;
|
|
import ch.psi.utils.State;
|
|
import ch.psi.utils.swing.SwingUtils;
|
|
import java.awt.Color;
|
|
import java.util.List;
|
|
import javax.swing.SwingUtilities;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public class Recovery extends Panel {
|
|
|
|
public Recovery() {
|
|
initComponents();
|
|
startTimer(5000, 200);
|
|
}
|
|
|
|
//Overridable callbacks
|
|
@Override
|
|
public void onInitialize(int runCount) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onStateChange(State state, State former) {
|
|
if (state==State.Ready){
|
|
SwingUtilities.invokeLater(()->{onTimer();});
|
|
}
|
|
textSegment.setEnabled(state == State.Ready);
|
|
textDistance.setEnabled(state == State.Ready);
|
|
textPosition.setEnabled(state == State.Ready);
|
|
updateButton();
|
|
}
|
|
|
|
void updateButton(){
|
|
buttonRecover.setEnabled((getContext().getState() == State.Ready) &&
|
|
(!textSegment.getText().trim().isEmpty()) &&
|
|
(textPosition.getText().trim().isEmpty()) );
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onExecutedFile(String fileName, Object result) {
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
protected void onTimer() {
|
|
Device robot = getContext().getDevicePool().getByName("robot", Device.class);
|
|
if ((robot==null) || (!robot.getState().isNormal())){
|
|
ledValidSegment.setColor(Color.BLACK);
|
|
textSegment.setText("");
|
|
ledKnownPosition.setColor(Color.BLACK);
|
|
textPosition.setText("");
|
|
} else {
|
|
if (getState()==State.Ready){
|
|
String point = null;
|
|
try{
|
|
point = (String) eval("robot.get_current_point()", true);
|
|
ledKnownPosition.setColor((point == null) ? Color.RED : Color.GREEN);
|
|
textPosition.setText((point == null) ? "": point);
|
|
} catch (Exception ex) {
|
|
System.out.println(ex);
|
|
ledKnownPosition.setColor(Color.BLACK);
|
|
textPosition.setText("");
|
|
}
|
|
try{
|
|
List segment = (List) eval("get_current_segment()", true);
|
|
ledValidSegment.setColor((segment == null) ? Color.RED : Color.GREEN);
|
|
textSegment.setText((segment == null) ? "": segment.get(0) + "->" + segment.get(1) + " [" + segment.get(2) + "mm]");
|
|
if ((segment == null)||(point!=null)){
|
|
textDistance.setText("");
|
|
} else {
|
|
try{
|
|
Object distance = eval("get_current_distance()", true);
|
|
textDistance.setText((distance == null) ? "" : String.format("%1.2f",((Number)distance).doubleValue()));
|
|
} catch (Exception ex) {
|
|
textDistance.setText("");
|
|
}
|
|
}
|
|
} catch (Exception ex) {
|
|
System.out.println(ex);
|
|
ledValidSegment.setColor(Color.BLACK);
|
|
textSegment.setText("");
|
|
textDistance.setText("");
|
|
}
|
|
}
|
|
}
|
|
updateButton();
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
|
|
private void initComponents() {
|
|
|
|
ledKnownPosition = new ch.psi.pshell.swing.Led();
|
|
jLabel6 = new javax.swing.JLabel();
|
|
ledValidSegment = new ch.psi.pshell.swing.Led();
|
|
jLabel7 = new javax.swing.JLabel();
|
|
buttonRecover = new javax.swing.JButton();
|
|
buttonAbort = new javax.swing.JButton();
|
|
textPosition = new javax.swing.JTextField();
|
|
textSegment = new javax.swing.JTextField();
|
|
textDistance = new javax.swing.JTextField();
|
|
jLabel8 = new javax.swing.JLabel();
|
|
|
|
jLabel6.setText("Known position");
|
|
|
|
jLabel7.setText("Valid segment");
|
|
|
|
buttonRecover.setText("Recover");
|
|
buttonRecover.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
buttonRecoverActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
buttonAbort.setText("Abort");
|
|
buttonAbort.setEnabled(false);
|
|
buttonAbort.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
buttonAbortActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
textPosition.setEditable(false);
|
|
|
|
textSegment.setEditable(false);
|
|
|
|
textDistance.setEditable(false);
|
|
textDistance.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
|
|
|
|
jLabel8.setText("Distance:");
|
|
|
|
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()
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addComponent(ledKnownPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(jLabel6)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
|
.addComponent(textPosition))
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addComponent(ledValidSegment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(jLabel7)
|
|
.addGap(16, 16, 16)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addGap(0, 0, Short.MAX_VALUE)
|
|
.addComponent(jLabel8)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
|
.addComponent(textDistance, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addComponent(textSegment))))
|
|
.addGap(8, 8, 8))
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addContainerGap(136, Short.MAX_VALUE)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
|
|
.addComponent(buttonAbort, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addComponent(buttonRecover))
|
|
.addContainerGap(144, Short.MAX_VALUE))
|
|
);
|
|
|
|
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonAbort, buttonRecover});
|
|
|
|
layout.setVerticalGroup(
|
|
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(layout.createSequentialGroup()
|
|
.addContainerGap()
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
|
.addComponent(ledKnownPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addComponent(jLabel6)
|
|
.addComponent(textPosition, 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.BASELINE)
|
|
.addComponent(ledValidSegment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addComponent(jLabel7)
|
|
.addComponent(textSegment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
|
.addComponent(jLabel8)
|
|
.addComponent(textDistance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
|
|
.addComponent(buttonRecover)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
|
.addComponent(buttonAbort)
|
|
.addContainerGap())
|
|
);
|
|
}// </editor-fold>//GEN-END:initComponents
|
|
|
|
private void buttonRecoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRecoverActionPerformed
|
|
try{
|
|
buttonAbort.setEnabled(true);
|
|
evalAsync("recover()", false).handle((ret, ex) -> {
|
|
if (ex != null){
|
|
showException((Exception)ex);
|
|
} else {
|
|
SwingUtils.showMessage(getTopLevel(), "Return", String.valueOf(ret));
|
|
}
|
|
buttonAbort.setEnabled(false);
|
|
return ret;
|
|
});
|
|
} catch (Exception ex) {
|
|
showException(ex);
|
|
}
|
|
}//GEN-LAST:event_buttonRecoverActionPerformed
|
|
|
|
private void buttonAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAbortActionPerformed
|
|
try {
|
|
abort();
|
|
try{
|
|
eval("robot.disable()", true);
|
|
} catch (Exception ex){
|
|
this.showException(ex);
|
|
}
|
|
try{
|
|
eval("stop_task()", true);
|
|
} catch (Exception ex){
|
|
this.showException(ex);
|
|
}
|
|
try{
|
|
eval("robot.reset_motion()", true);
|
|
} catch (Exception ex){
|
|
this.showException(ex);
|
|
}
|
|
} catch (InterruptedException ex) {
|
|
showException(ex);
|
|
}
|
|
}//GEN-LAST:event_buttonAbortActionPerformed
|
|
|
|
// Variables declaration - do not modify//GEN-BEGIN:variables
|
|
private javax.swing.JButton buttonAbort;
|
|
private javax.swing.JButton buttonRecover;
|
|
private javax.swing.JLabel jLabel6;
|
|
private javax.swing.JLabel jLabel7;
|
|
private javax.swing.JLabel jLabel8;
|
|
private ch.psi.pshell.swing.Led ledKnownPosition;
|
|
private ch.psi.pshell.swing.Led ledValidSegment;
|
|
private javax.swing.JTextField textDistance;
|
|
private javax.swing.JTextField textPosition;
|
|
private javax.swing.JTextField textSegment;
|
|
// End of variables declaration//GEN-END:variables
|
|
}
|