From 310768d7acc8cbdfa9a81933dcea47352a5a409d Mon Sep 17 00:00:00 2001 From: sgdvetri Date: Mon, 18 Aug 2014 18:11:41 +0100 Subject: [PATCH] using matlabcontrol api we now can collect temperature log in matlab for further study. Matlab Lunches at the same time as the control panel is initiated --- .../app/ui/MachineStatusPanel.java | 195 ++++++++++++++---- 1 file changed, 155 insertions(+), 40 deletions(-) mode change 100644 => 100755 src/replicatorg/app/ui/MachineStatusPanel.java diff --git a/src/replicatorg/app/ui/MachineStatusPanel.java b/src/replicatorg/app/ui/MachineStatusPanel.java old mode 100644 new mode 100755 index 28d601342..bfcc09023 --- a/src/replicatorg/app/ui/MachineStatusPanel.java +++ b/src/replicatorg/app/ui/MachineStatusPanel.java @@ -5,9 +5,11 @@ import java.awt.Font; import java.awt.FontMetrics; import java.util.Vector; +//import java.util.Calendar; +//import java.io.FileWriter; import javax.swing.JLabel; -import javax.swing.JPanel; +//import javax.swing.JPanel; import javax.swing.SwingUtilities; import net.miginfocom.swing.MigLayout; @@ -20,6 +22,14 @@ import replicatorg.machine.MachineToolStatusEvent; import replicatorg.machine.model.ToolModel; +import matlabcontrol.MatlabConnectionException; +import matlabcontrol.MatlabInvocationException; +import matlabcontrol.MatlabProxy; +import matlabcontrol.MatlabProxyFactory; +import matlabcontrol.MatlabProxyFactoryOptions; +import matlabcontrol.extensions.MatlabNumericArray; +import matlabcontrol.extensions.MatlabTypeConverter; + /** * The MachineStatusPanel displays the current state of the connected machine, * or a message informing the user that no connected machine can be found. @@ -31,26 +41,29 @@ public class MachineStatusPanel extends BGPanel implements MachineListener { private static final long serialVersionUID = -6944931245041870574L; - + protected JLabel mainLabel= new JLabel(); - + /// small text label for ongoing actions protected JLabel smallLabel = new JLabel(); - + /// Temperature status string protected JLabel tempLabel = new JLabel(); /// Machine tyle/connection string protected JLabel machineLabel = new JLabel(); - + // Keep track of whether we are in a building state or not. private boolean isBuilding = false; - + static final private Color BG_ERROR = new Color(0xff, 0x80, 0x60); static final private Color BG_READY = new Color(0x80, 0xff, 0x60); static final private Color BG_BUILDING = new Color(0xff, 0xef, 0x00); // process yellow + + MatlabProxy proxy; + MatlabTypeConverter processor; + int count=0; - MachineStatusPanel() { Font statusFont = Base.getFontPref("status.font","SansSerif,plain,12"); Font smallFont = statusFont.deriveFont(10f); @@ -59,7 +72,7 @@ public class MachineStatusPanel extends BGPanel implements MachineListener { machineLabel.setFont(smallFont); mainLabel.setFont(statusFont); mainLabel.setText("Not Connected"); - + setLayout(new MigLayout("fill,novisualpadding, ins 5 10 5 10")); add(mainLabel, "top, left, growx, split"); add(machineLabel, "top, right, wrap"); @@ -76,6 +89,25 @@ public class MachineStatusPanel extends BGPanel implements MachineListener { int prefWidth = 80 * smallMetrics.charWidth('n'); setPreferredSize(new Dimension(prefWidth, height)); setBackground(BG_ERROR); + + //MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder() + //.setUsePreviouslyControlledSession(true) + //.setHidden(true) + // .setMatlabLocation(null).build(); + + MatlabProxyFactory factory = new MatlabProxyFactory(); + try { + proxy = factory.getProxy(); + } catch (MatlabConnectionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + processor = new MatlabTypeConverter(proxy); + + //count; + + + } @@ -85,9 +117,9 @@ private void updatePanel(Color panelColor, String text, String smallText, String smallLabel.setText(smallText); machineLabel.setText(machineText); } - + /** - * Creatss a one line string of machine info + * Creates a one line string of machine info * @return */ private String machineStatusString(String machineId, boolean connected) @@ -102,16 +134,16 @@ private String machineStatusString(String machineId, boolean connected) } return machineText; } - + /** * Display the current status of this machine. */ public void updateMachineStatus(MachineStateChangeEvent evt) { MachineState.State state = evt.getState().getState(); - + // Determine what color to use Color bgColor = null; - + switch (state) { case READY: bgColor = BG_READY; @@ -127,24 +159,22 @@ public void updateMachineStatus(MachineStateChangeEvent evt) { bgColor = BG_ERROR; break; } - + String text = evt.getMessage(); // Make up some text to describe the state if (text == null ) { text = state.toString(); } - + String machineText = machineStatusString(evt.getSource().getMachineName(), evt.getState().isConnected()); - + if( evt.getState().isConnected() == false ) tempLabel.setText(""); - boolean checkTempDuringBuild = Base.preferences.getBoolean("build.monitor_temp", true); - - if( evt.getState().isBuilding() && checkTempDuringBuild == false ) + if( evt.getState().isBuilding() && !(Base.preferences.getBoolean("build.monitor_temp", false) )) tempLabel.setText("Monitor build temp.: off"); - + // And mark which state we are in. switch (state) { case BUILDING: @@ -156,32 +186,32 @@ public void updateMachineStatus(MachineStateChangeEvent evt) { isBuilding = false; break; } - + updatePanel(bgColor, text, null, machineText); } - + public void updateBuildStatus(MachineProgressEvent event) { if (isBuilding) { /** Calculate the % of the build that is complete **/ double proportion = (double)event.getLines()/(double)event.getTotalLines(); double percentComplete = Math.round(proportion*10000.0)/100.0; - + double remaining= event.getEstimated() * (1.0 - proportion); if (event.getTotalLines() == 0) { remaining = 0; } - + final String s = String.format( "Commands: %1$7d / %2$7d (%3$3.2f%%) | Elapsed: %4$s | Est. done in: %5$s", event.getLines(), event.getTotalLines(), percentComplete, EstimationDriver.getBuildTimeString(event.getElapsed(), true), EstimationDriver.getBuildTimeString(remaining, true)); - + smallLabel.setText(s); } } - + public void machineStateChanged(MachineStateChangeEvent evt) { final MachineStateChangeEvent e = evt; SwingUtilities.invokeLater(new Runnable() { @@ -199,34 +229,119 @@ public void run() { } }); } - + public void toolStatusChanged(MachineToolStatusEvent event) { - + final MachineToolStatusEvent e = event; + + count++; + SwingUtilities.invokeLater(new Runnable() { + public void run() { - Vector tools = e.getSource().getModel().getTools(); - String tempString = ""; + Vector tools = e.getSource().getModel().getTools(); + String tempString = ""; +// String temps = ""; +// String ptemps = ""; +// for(ToolModel t : tools) +// { +// temps += t.getName() + ": " + Double.toString(t.getCurrentTemperature()) + ", "; +// +// if(t.hasHeatedPlatform()) +// { +// ptemps += "Platform: " +Double.toString(t.getPlatformCurrentTemperature()); +// } +// } +// try{ +// FileWriter out = new FileWriter("temperatureData.txt", true); +// Calendar now = Calendar.getInstance(); +// int hour = now.get(Calendar.HOUR_OF_DAY); +// int minute = now.get(Calendar.MINUTE); +// int seconds = now.get(Calendar.SECOND); +// out.write(Double.toString(hour)+":"+Double.toString(minute)+":"+Double.toString(seconds) + ", "); +// out.write(temps+" " +ptemps+ '\n'); +// out.close(); +// }catch(Exception e) +// { +// System.out.println("O GNOES"); +// } +// + + + /// TRICKY: we assume that the MachineThread is calling these functions /// for us, at least once every few seconds, to keep the local temperaure cache up to date. /// re-enable this if you want to test that, or if MachineThread stops checking temp - //e.getSource().getDriver().readAllPlatformTemperatures(); - //e.getSource().getDriver().readAllTemperatures(); + e.getSource().getDriver().readAllPlatformTemperatures(); + e.getSource().getDriver().readAllTemperatures(); +// for(ToolModel t : tools) +// { +// double temp= t.getCurrentTemperature(); +// //stream.write(temp); +// tempString += String.format(" "+t.getName()+": %1$3.1f\u00B0C ", temp); +// if(t.hasHeatedPlatform()) +// { +// double ptemp = t.getPlatformCurrentTemperature(); +// tempString += String.format("Platform: %1$3.1f\u00B0C", ptemp); +// } +// } +// + + //WRITE TO MATLAB + //double [][] array = new double [][] {{0,0,0,0}}; //create array of doubles in Java + + double [][] tarray = new double [1][4]; - for(ToolModel t : tools) + int i = 0; + + for(ToolModel t : tools) //for the total number of extruders defined in the Driver, Do: { - double temp= t.getCurrentTemperature(); + double temp = t.getCurrentTemperature(); //read temperature from extruder tempString += String.format(" "+t.getName()+": %1$3.1f\u00B0C ", temp); - if(t.hasHeatedPlatform()) - { - double ptemp = t.getPlatformCurrentTemperature(); + //if(t.hasHeatedPlatform()) + //{ + double ptemp = t.getPlatformCurrentTemperature(); //read platform temperature tempString += String.format("Platform: %1$3.1f\u00B0C", ptemp); + //} + + tarray[0][i+1] = temp; // tarray[0][1] and tarray[0][2] will hold extruder temperatures at any time + + if (i==0) + { + tarray[0][0] = count; // put time to array. + tarray[0][3] = ptemp; } + i++; } + + MatlabNumericArray tArray = new MatlabNumericArray(tarray,null); //converts to MatLab type + if (count <= 4) // if first cycle + { + try { + processor.setNumericArray("fullArray", tArray); + } catch (MatlabInvocationException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } // writes to MATLAB first line of array + + } + else // any other line of array + { + try { + processor.setNumericArray("tempArray", tArray); + proxy.eval("fullArray=vertcat(fullArray,tempArray);"); + } catch (MatlabInvocationException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } // writes to MATLAB + + } + + tempLabel.setText(tempString); } - }); - } -} + }); + } +} \ No newline at end of file