[chronojump] Encoder capture method: using a file for each curve



commit 28ad1165ac66cf22bbb42ee7c951291ea5575a68
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Mar 11 12:23:44 2015 +0100

    Encoder capture method: using a file for each curve

 encoder/capture.R  |   38 +++++++++++++++++++++++++++---------
 src/constants.cs   |    2 +-
 src/gui/encoder.cs |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 11 deletions(-)
---
diff --git a/encoder/capture.R b/encoder/capture.R
index 469dd4e..ae008f9 100644
--- a/encoder/capture.R
+++ b/encoder/capture.R
@@ -51,11 +51,19 @@ g = 9.81
 
 debug = FALSE
                    
-filename = options[1]
-file.create(filename)
+filenameBegins = options[1] #comes ".../chronojump-captured". will be ".../chronojump-captured-000.txt", 001 
... 999
 
+filenameCompose <- function(curveNum)
+{
+       if(curveNum > 99)
+               return(paste(filenameBegins, "-", curveNum, sep=""))    #eg. "filename-123"
+       else if(curveNum > 9)
+               return(paste(filenameBegins, "-0", curveNum, sep=""))   #eg. "filename-023"
+       else #(curveNum <= 9)
+               return(paste(filenameBegins, "-00", curveNum, sep=""))  #eg. "filename-003"
+}
 
-calcule <- function(displacement, start, end, op) 
+calcule <- function(displacement, start, end, op, curveNum) 
 {
        if(debug)
                write("At calcule", stderr())
@@ -104,12 +112,16 @@ calcule <- function(displacement, start, end, op)
        #         paf$meanForce, paf$maxForce, paf$maxForceT,
        #         sep=", "))
        #cat("\n") #mandatory to read this from C#, but beware, there we will need a trim to remove the 
windows \r\n
-       write(paste(#start, #start is not used because we have no data of the initial zeros
+
+       filename <- filenameCompose(curveNum)
+       con <- file(filename, "w")
+       cat(paste(#start, #start is not used because we have no data of the initial zeros
                  0, 0, 
                  paf$meanSpeed, paf$maxSpeed, paf$maxSpeedT, 
                  paf$meanPower, paf$peakPower, paf$peakPowerT, paf$pp_ppt, 
                  paf$meanForce, paf$maxForce, paf$maxForceT,
-                 sep=", "), filename, append=TRUE)
+                 sep=", "), file = con)
+       close(con)
        if(debug)
                write("ended calcule", stderr())
 }
@@ -152,7 +164,8 @@ doProcess <- function()
 
        #print ("----op----")
        #print (op)
-       
+
+       curveNum = 0
        input <- readLines(f, n = 1L)
        while(input[1] != "Q") {
                if(debug)
@@ -233,16 +246,21 @@ doProcess <- function()
                        displacement2 = displacement[(positionTop+1):length(displacement)]
 
                        if(op$Eccon == "c") {
-                               calcule(displacement1, start, end, op) #TODO: check this start, end
+                               calcule(displacement1, start, end, op, curveNum) #TODO: check this start, end
+                               curveNum = curveNum +1
                        } else {
-                               calcule(displacement1, start, end, op) #TODO: check this start, end
-                               calcule(displacement2, start, end, op) #TODO: check this start, end
+                               calcule(displacement1, start, end, op, curveNum) #TODO: check this start, end
+                               curveNum = curveNum +1
+                               
+                               calcule(displacement2, start, end, op, curveNum) #TODO: check this start, end
+                               curveNum = curveNum +1
                        }
 
                        #write(c("positionTop", positionTop), stderr())
                        #write(c("length(displacement)", length(displacement)), stderr())
                } else {
-                       calcule(displacement, start, end, op) #TODO: check this start, end
+                       calcule(displacement, start, end, op, curveNum) #TODO: check this start, end
+                       curveNum = curveNum +1
                }
                if(debug)
                        write("doProcess 4", stderr())
diff --git a/src/constants.cs b/src/constants.cs
index 3da3323..f01740e 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -712,7 +712,7 @@ public class Constants
        //public static string EncoderScriptGraphCall = 
                //"/home/xavier/informatica/progs_meus/chronojump/chronojump/encoder/call_graph.py";
 
-       public static string EncoderCaptureTemp = "chronojump-captured-data.txt";
+       public static string EncoderCaptureTemp = "chronojump-captured"; //will be 
"chronojump-captured-000.txt", 001 ... 999
        public static string EncoderDataTemp = "chronojump-last-encoder-data.txt";
        public static string EncoderCurvesTemp = "chronojump-last-encoder-curves.txt";
        public static string EncoderAnalyzeTableTemp = "chronojump-last-encoder-analyze-table.txt";
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 71ebe7e..6984c21 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -4736,6 +4736,8 @@ public partial class ChronoJumpWindow
                                                File.Delete(filename);
 
                                        encoderCaptureReadedLines = 0;
+                                       deleteAllCapturedCurveFiles();
+
                                        capturingCsharp = encoderCaptureProcess.CAPTURING;
 
                                        massDisplacedEncoder = UtilEncoder.GetMassByEncoderConfiguration( 
encoderConfigurationCurrent, 
@@ -5062,12 +5064,40 @@ LogB.Debug("D");
        }
        */
 
+       /*
+        * History
+        * 1) In the beginning we used RDotNet for C# - R communication. But it was buggy, complex, problems 
with try catch, ...
+        * 2) Then we used stdin,stdout,stderr communication. Worked fine on Linux and Windows but not in Mac
+        * 3) Then we used a capture.txt file created by R with a row for each curve. But reading it on 
windows from C# gives file access problems
+        * 4) Now we try to create one file for each curve and read it here with a try/catch
+        */
+
+       private void deleteAllCapturedCurveFiles()
+       {
+               foreach (var f in new DirectoryInfo(Path.GetTempPath()).GetFiles(
+                                       Constants.EncoderCaptureTemp + "-*")) {
+                           f.Delete();
+               }
+               Util.FileDelete(UtilEncoder.GetEncoderCaptureTempFileName() + "-*");
+       }       
+       private string readingCurveFromRFilenameCompose(int curveNum)
+       {
+               string filenameBegins = UtilEncoder.GetEncoderCaptureTempFileName();
+               if(curveNum > 99)
+                       return(filenameBegins + "-" + curveNum.ToString());     //eg. "filename-123"
+               else if(curveNum > 9)
+                       return(filenameBegins + "-0" + curveNum.ToString());    //eg. "filename-023"
+               else //(curveNum <= 9)
+                       return(filenameBegins + "-00" + curveNum.ToString());   //eg. "filename-003"
+       }
 
        static bool needToRefreshTreeviewCapture;
        static int encoderCaptureReadedLines;
        //private void readingCurveFromR (object sendingProcess, DataReceivedEventArgs curveFromR)
        private void readingCurveFromR ()
        {
+               /*
+                * 3) method ----
                string filename = UtilEncoder.GetEncoderCaptureTempFileName();
                if(! File.Exists(filename))
                        return;
@@ -5083,6 +5113,29 @@ LogB.Debug("D");
                        //http://stackoverflow.com/a/1262985
                        line = File.ReadLines(filename).Skip(encoderCaptureReadedLines ++).Take(1).First();
                }
+                * ---- end of 3) method
+                */
+
+               //4) method ----
+               string line = "";
+               string filename = readingCurveFromRFilenameCompose(encoderCaptureReadedLines);
+               //LogB.Debug("filename = ",filename);
+               
+               if(! File.Exists(filename))
+                       return;
+
+               try {
+                       StreamReader reader = File.OpenText(filename);
+                       line = reader.ReadLine(); //just read first line
+                       reader.Close();
+                       encoderCaptureReadedLines ++;
+               }
+               catch {
+                       LogB.Debug("catched - open later",encoderCaptureReadedLines.ToString());
+                       return;
+               }
+               //---- end of 4) method
+
 
 
                //if (!String.IsNullOrEmpty(curveFromR.Data))


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