[chronojump] Added inertia-momentum calculation



commit d3e9d19dca58845ed148cb1e1bec4515cd77a82f
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Feb 3 20:36:03 2014 +0100

    Added inertia-momentum calculation

 encoder/Makefile.am             |    3 +-
 encoder/graph.R                 |   22 +--------
 encoder/inertia-momentum.R      |  107 +++++++++++++++++++++++++++++++++++++++
 glade/chronojump.glade          |   58 +++++++++++++++++++++
 src/constants.cs                |    1 +
 src/gui/encoderConfiguration.cs |    4 ++
 src/utilEncoder.cs              |   84 ++++++++++++++++++++++++++++++
 7 files changed, 257 insertions(+), 22 deletions(-)
---
diff --git a/encoder/Makefile.am b/encoder/Makefile.am
index a6d014d..d05d6f7 100644
--- a/encoder/Makefile.am
+++ b/encoder/Makefile.am
@@ -1,4 +1,5 @@
 encoderdatadir = @datadir@/@PACKAGE@/encoder
 
 dist_encoderdata_DATA = pyserial_pyper.py \
-            graph.R
+            graph.R \
+            inertia-momentum.R
diff --git a/encoder/graph.R b/encoder/graph.R
index 14b02af..2d78dcb 100644
--- a/encoder/graph.R
+++ b/encoder/graph.R
@@ -62,26 +62,6 @@
 #have data introduced always and have different encoders.
 #
 #
-#Finally need to add the code and explanation for the calculation of the inertia
-#momentum:
-#
-#     ----------
-#    /          \
-#   /         W  \
-#  /         /    \
-# |         /      |
-# |        o       |
-# |                |
-#  \              /
-#   \            /  
-#    \          /
-#      --------
-#
-#Weight has not to be on the top of the axis (has to be "sided")
-#Measure weight
-#Measure distance between centre of axis and centre of weight
-
-
 
 #concentric, eccentric-concentric, repetitions of eccentric-concentric
 #currently only used "c" and "ec". no need of ec-rep because c and ec are repetitive
@@ -116,7 +96,7 @@ optionsFile =args[1]
 
 print(optionsFile)
 
-options=getOptionsFromFile(optionsFile);
+options=getOptionsFromFile(optionsFile)
 
 print(options)
 
diff --git a/encoder/inertia-momentum.R b/encoder/inertia-momentum.R
new file mode 100644
index 0000000..7dd81f0
--- /dev/null
+++ b/encoder/inertia-momentum.R
@@ -0,0 +1,107 @@
+# 
+#  This file is part of ChronoJump
+# 
+#  ChronoJump is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or   
+#     (at your option) any later version.
+#     
+#  ChronoJump is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+#     GNU General Public License for more details.
+# 
+#  You should have received a copy of the GNU General Public License
+#   along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+# 
+#   Copyright (C) 2014   
+#   
+#   Xavier Padullés <x padulles gmail com>
+#   Xavier de Blas <xaviblas gmail com> 
+# 
+#
+#     ----------
+#    /          \
+#   /         W  \
+#  /         /    \
+# |         /      |
+# |        o       |
+# |                |
+#  \              /
+#   \            /  
+#    \          /
+#      --------
+#
+#Weight has not to be on the top of the axis (has to be "sided")
+#Measure weight
+#Measure distance between centre of axis and centre of weight
+
+library("EMD")
+
+calculate <- function (displacement, mass, length)
+{
+       #cumulative movement of the encoder
+       x <- cumsum(displacement)
+
+       #time in milliseconds
+       t <- seq(1,length(displacement))
+
+       #all the information about local maximums and minimums and crossings
+       ex <- extrema(x)
+
+       #times where the maximums are found in milliseconds
+       tmax <- rowMeans(ex$maxindex)
+
+       #the last maximum is discarded
+       tmax <- tmax[1:(length(tmax)-1)]
+
+       #Periods of the oscillations
+       T <- diff(tmax[1:length(tmax)])
+
+       #Coefficients of a Logarithmic regression
+       logT <- lm( log(T) ~ I(log(tmax[1:(length(tmax)-1)])))
+
+       #The final period of the oscillation in seconds
+       finalT <- exp(logT$coefficients[1] + logT$coefficients[2]*log(tmax[length(tmax)]))/1000
+
+       print(c("finalT: ", finalT, "; mass: ", mass))
+       
+       print ( (finalT / (2 * pi))^2 ) * mass
+
+       #Inertia momentum using the pendulus formula
+       I <- ( (finalT / (2 * pi))^2 ) * mass * 9.81 * length - (mass * length^2)
+
+       return(I)
+}
+
+getOptionsFromFile <- function(optionsFile,n) {
+       optionsCon <- file(optionsFile, 'r')
+       options=readLines(optionsCon,n=n)
+       close(optionsCon)
+       return (options)
+}
+
+
+args <- commandArgs(TRUE)
+print(args)
+
+optionsFile = args[1]
+print(optionsFile)
+
+options = getOptionsFromFile(optionsFile, 4)
+
+fileInput = options[1]
+fileOutput = options[2]
+mass = options[3]
+length = options[4]
+
+displacement = scan(file=fileInput, sep=",")
+
+inertia = calculate(displacement, mass, length)
+
+print (inertia)
+
+#TODO: use fileOutput
+
+
diff --git a/glade/chronojump.glade b/glade/chronojump.glade
index 2738e31..431e996 100644
--- a/glade/chronojump.glade
+++ b/glade/chronojump.glade
@@ -6336,6 +6336,9 @@ Second Chronopic to platforms.</property>
                                                         <child>
                                                           <placeholder/>
                                                         </child>
+                                                        <child>
+                                                          <placeholder/>
+                                                        </child>
                                                       </widget>
                                                       <packing>
                                                         <property name="expand">True</property>
@@ -6989,6 +6992,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             </child>
                                                             <child>
@@ -7778,6 +7784,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="position">2</property>
@@ -8326,6 +8335,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="position">4</property>
@@ -17179,6 +17191,9 @@ by you</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -17635,6 +17650,9 @@ by you</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -18561,6 +18579,12 @@ by you</property>
             <child>
               <placeholder/>
             </child>
+            <child>
+              <placeholder/>
+            </child>
+            <child>
+              <placeholder/>
+            </child>
           </widget>
           <packing>
             <property name="expand">True</property>
@@ -21415,6 +21439,7 @@ comments</property>
                                 <property name="receives_default">True</property>
                                 <property name="has_tooltip">True</property>
                                 <property name="tooltip" translatable="yes">Calcule inertial 
momentum</property>
+                                <signal name="clicked" handler="on_button_encoder_capture_inertial_clicked" 
swapped="no"/>
                               </widget>
                               <packing>
                                 <property name="expand">False</property>
@@ -25027,6 +25052,24 @@ options</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
@@ -26132,6 +26175,9 @@ options</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                       <packing>
                         <property name="left_attach">2</property>
@@ -26252,6 +26298,9 @@ options</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
@@ -26387,6 +26436,9 @@ options</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
@@ -28246,6 +28298,9 @@ show elevation as:</property>
                                 <child>
                                   <placeholder/>
                                 </child>
+                                <child>
+                                  <placeholder/>
+                                </child>
                               </widget>
                             </child>
                           </widget>
@@ -28761,6 +28816,9 @@ show elevation as:</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
diff --git a/src/constants.cs b/src/constants.cs
index be549a3..6ee91cc 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -589,6 +589,7 @@ public class Constants
        public static string EncoderScriptCaptureLinux = "pyserial_pyper.py";
        public static string EncoderScriptCaptureWindows = "pyserial_pyper_windows.exe";
        public static string EncoderScriptGraph = "graph.R";
+       public static string EncoderScriptInertiaMomentum = "inertia-momentum.R";
        //no longer used:
        //public static string EncoderScriptGraphCall = 
                //"/home/xavier/informatica/progs_meus/chronojump/chronojump/encoder/call_graph.py";
diff --git a/src/gui/encoderConfiguration.cs b/src/gui/encoderConfiguration.cs
index a46d6d4..f9a726f 100644
--- a/src/gui/encoderConfiguration.cs
+++ b/src/gui/encoderConfiguration.cs
@@ -180,6 +180,10 @@ public class EncoderConfigurationWindow {
                return ec;
        }
        
+       void on_button_encoder_capture_inertial_clicked (object o, EventArgs args) 
+       {
+               UtilEncoder.RunEncoderCalculeInertiaMomentum(20,10);
+       }
        
        private void on_button_cancel_clicked (object o, EventArgs args)
        {
diff --git a/src/utilEncoder.cs b/src/utilEncoder.cs
index 06e1175..7181f71 100644
--- a/src/utilEncoder.cs
+++ b/src/utilEncoder.cs
@@ -161,6 +161,11 @@ public class UtilEncoder
                                Util.GetDataDir(), "encoder", Constants.EncoderScriptGraph);
        }
        
+       private static string getEncoderScriptInertiaMomentum() {
+               return System.IO.Path.Combine(
+                               Util.GetDataDir(), "encoder", Constants.EncoderScriptInertiaMomentum);
+       }
+       
        
        /********** end of encoder paths ************/
 
@@ -356,6 +361,85 @@ public class UtilEncoder
 
                return true;
        }
+       
+       public static bool RunEncoderCalculeInertiaMomentum(double weight, double length) 
+       {
+               CancelRScript = false;
+
+               ProcessStartInfo pinfo;
+               Process p;
+               //If output file is not given, R will try to write in the running folder
+               //in which we may haven't got permissions
+               
+               string pBin="";
+               pinfo = new ProcessStartInfo();
+
+               string inputData = GetEncoderDataTempFileName();
+               string outputData = GetEncoderSpecialDataTempFileName();
+               string operatingSystem = "Linux";
+                       
+               pBin="Rscript";
+               if (UtilAll.IsWindows()) {
+                       //on Windows we need the \"str\" to call without problems in path with spaces
+                       pBin = "\"" + System.IO.Path.Combine(Util.GetPrefixDir(), "bin" + 
Path.DirectorySeparatorChar + "Rscript.exe") + "\"";
+                       Log.WriteLine("pBin:" + pBin);
+
+                       //On win32 R understands backlash as an escape character and 
+                       //a file path uses Unix-like path separator '/'         
+                       inputData = inputData.Replace("\\","/");
+                       operatingSystem = "Windows";
+               }
+               
+               //--- way A. passing options to a file
+               string scriptOptions = 
+                       inputData + "\n" + 
+                       outputData + "\n" + 
+                       Util.ConvertToPoint(weight) + "\n" + 
+                       Util.ConvertToPoint(length) + "\n";
+
+               string optionsFile = Path.GetTempPath() + "Roptions.txt";
+               TextWriter writer = File.CreateText(optionsFile);
+               writer.Write(scriptOptions);
+               writer.Flush();
+               ((IDisposable)writer).Dispose();
+               
+               if (UtilAll.IsWindows()) {
+                       //On win32 R understands backlash as an escape character and 
+                       //a file path uses Unix-like path separator '/'         
+                       optionsFile = optionsFile.Replace("\\","/");
+               }
+               
+               //on Windows we need the \"str\" to call without problems in path with spaces
+               pinfo.Arguments = "\"" + getEncoderScriptInertiaMomentum() + "\" " + optionsFile;
+               Log.WriteLine("Arguments:" + pinfo.Arguments);
+               
+               //Wait until this to update encoder gui (if don't wait then treeview will be outdated)
+               string outputFileCheck = outputData;
+
+               pinfo.FileName=pBin;
+
+               pinfo.CreateNoWindow = true;
+               pinfo.UseShellExecute = false;
+
+               //delete output file check(s)
+               Console.WriteLine("Deleting... " + outputFileCheck);
+               if (File.Exists(outputFileCheck))
+                       File.Delete(outputFileCheck);
+
+               try {   
+                       p = new Process();
+                       p.StartInfo = pinfo;
+                       p.Start();
+                       p.WaitForExit();
+
+                       while ( ! ( File.Exists(outputFileCheck) || CancelRScript) );
+               } catch {
+                       return false;
+               }
+
+               return true;
+       }
+
 
        private static string [] encoderFindPos(string contents, int start, int duration) {
                int startPos = 0;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]