[chronojump] Encoder curve is sent to R compressed to not fill buffer



commit bda74284e76b6a83a769295e54f33667764b43cb
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Feb 23 19:31:11 2015 +0100

    Encoder curve is sent to R compressed to not fill buffer

 encoder/capture.R  |   41 +++++++++++++++++++++++++++++-
 src/gui/encoder.cs |    7 ++++-
 src/utilEncoder.cs |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 115 insertions(+), 3 deletions(-)
---
diff --git a/encoder/capture.R b/encoder/capture.R
index 6cb0929..c37b187 100644
--- a/encoder/capture.R
+++ b/encoder/capture.R
@@ -31,6 +31,7 @@ g = 9.81
 
 calcule <- function(displacement, start, end, op) 
 {
+write("At calcule", stderr())
        #read AnalysisOptions
        #if is propulsive and rotatory inertial is: "p;ri" 
        #if nothing: "-;-"
@@ -53,6 +54,7 @@ calcule <- function(displacement, start, end, op)
        }
 
 
+write("At calcule calling kinematics", stderr())
        kinematicsResult <- kinematicsF(displacement, 
                    op$MassBody, op$MassExtra, op$ExercisePercentBodyWeight,
                    op$EncoderConfigurationName, op$diameter, op$diameterExt, op$anglePush, op$angleWeight, 
op$inertiaMomentum, op$gearedDown,
@@ -73,6 +75,7 @@ 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("ended calcule", stderr())
 }
                
 getPositionStart <- function(input) 
@@ -84,8 +87,32 @@ getPositionStart <- function(input)
                return (0)
 }
 
+
+#converts data: "0*5 1 0 -1*3 2"
+#into: 0  0  0  0  0  1  0 -1 -1 -1  2
+uncompress <- function(curveSent)
+{
+       chunks = unlist(strsplit(curveSent, " "))
+       s = NULL
+       ints = NULL
+       for(i in 1:length(chunks)) 
+       {
+               if(grepl("\\*",chunks[i])) {
+                       chunk = as.numeric(unlist(strsplit(chunks[i], "\\*"))) #from "0*1072" to: 0 1072 (as 
integers)
+                       chunk = rep(chunk[1],chunk[2])
+               } else {
+                       chunk=chunks[i]
+               }
+               ints = c(ints,chunk)
+       }
+       return (as.numeric(ints))
+}
+
+
+
 doProcess <- function() 
 {
+       write("doProcess", stderr())
        op <- assignOptions(options)
 
        #print ("----op----")
@@ -93,6 +120,8 @@ doProcess <- function()
        
        input <- readLines(f, n = 1L)
        while(input[1] != "Q") {
+               write("doProcess main while", stderr())
+               
                #Sys.sleep(4) #just to test how Chronojump reacts if process takes too long
                #cat(paste("input is:", input, "\n"))
 
@@ -100,13 +129,21 @@ doProcess <- function()
                #then it's send the displacement
                positionStart = getPositionStart(input)
                input <- readLines(f, n = 1L)
+               
+               write("doProcess input", stderr())
+               #write(input, stderr())
+
+               #when data is sent uncompressed
+               #displacement = as.numeric(unlist(strsplit(input, " ")))
+               #when data is sent compressed
+               displacement = uncompress(input)
 
-               displacement = as.numeric(unlist(strsplit(input, " ")))
                #if data file ends with comma. Last character will be an NA. remove it
                #this removes all NAs
                displacement  = displacement[!is.na(displacement)]
 
 
+               write("doProcess 2", stderr())
                if(isInertial(op$EncoderConfigurationName)) 
                {
                        displacement = fixDisplacementInertial(displacement, op$EncoderConfigurationName, 
op$diameter, op$diameterExt)
@@ -134,6 +171,7 @@ doProcess <- function()
 
                        displacement = displacement[start:end]
                }
+               write("doProcess 3", stderr())
 
                #if isInertial: getDisplacementInertialBody separate phases using initial height of full 
extended person
                #so now there will be two different curves to process
@@ -156,6 +194,7 @@ doProcess <- function()
                } else {
                        calcule(displacement, start, end, op) #TODO: check this start, end
                }
+               write("doProcess 4", stderr())
 
                input <- readLines(f, n = 1L)
        }
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 62ce670..e4b348c 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -2343,7 +2343,12 @@ public partial class ChronoJumpWindow
 
                                                                if(sendCurve) {
                                                                        
UtilEncoder.RunEncoderCaptureNoRDotNetSendCurve(
-                                                                                       pCaptureNoRDotNet, 
heightAtCurveStart, curve);
+                                                                               pCaptureNoRDotNet, 
+                                                                               heightAtCurveStart, 
+                                                                               //curve);                     
  //uncompressed
+                                                                               
UtilEncoder.CompressData(curve) //compressed
+                                                                                       );
+
                                                                        ecca.curvesDone ++;
                                                                        ecca.curvesAccepted ++;
                                                                        ecca.ecc.Add(ecc);
diff --git a/src/utilEncoder.cs b/src/utilEncoder.cs
index 46cdafb..3cfd6f0 100644
--- a/src/utilEncoder.cs
+++ b/src/utilEncoder.cs
@@ -474,6 +474,26 @@ public class UtilEncoder
 
 
 
+       //here curve is sent compressed (string. eg: "0*5 1 0 -1*3 2")
+       public static void RunEncoderCaptureNoRDotNetSendCurve(Process p, double heightAtStart, string 
curveCompressed)
+       {
+               LogB.Debug("writing line 1 -->");
+               
+               string curveSend = "ps " + Util.ConvertToPoint(heightAtStart);
+               LogB.Debug("curveSend [heightAtStart]",curveSend);
+               p.StandardInput.WriteLine(curveSend);
+
+               curveSend = curveCompressed;
+               
+               //TODO convert comma to point in this doubles
+
+               LogB.Debug("curveSend [displacement array]",curveSend);
+               p.StandardInput.WriteLine(curveSend);
+               
+               LogB.Debug("<-- writen line 1");
+       }
+       /* unused
+        * here curve is sent uncompressed ([] double)
        public static void RunEncoderCaptureNoRDotNetSendCurve(Process p, double heightAtStart, double [] d)
        {
                LogB.Debug("writing line 1 -->");
@@ -491,6 +511,8 @@ public class UtilEncoder
                
                LogB.Debug("<-- writen line 1");
        }
+       */
+       
        public static void RunEncoderCaptureNoRDotNetSendEnd(Process p)
        {
                LogB.Debug("sending end line");
@@ -705,9 +727,54 @@ public class UtilEncoder
         * in order to be shorter if has to be sended by network
         * this compression reduces size six times aproximately
         */
+       
+       public static string CompressData(double [] curve)
+       {
+               string compressed = "";
+               
+               bool start = true;
+               int digit = -10000;
+               int digitPre = -10000; //just an impossible mark
+               int rep = 0;
+               for(int i=0; i < curve.Length; i++) 
+               {
+                       digit = Convert.ToInt32(curve[i]);
+                       if(start) {
+                               rep ++;
+                               start = false;
+                       } else if(digit == digitPre)
+                               rep ++;
+                       else {
+                               if(rep == 1)
+                                       compressed += digitPre.ToString() + " ";
+                               else {
+                                       compressed += digitPre.ToString() + "*" + rep.ToString() + " ";
+                                       rep = 1;
+                               }
+                       }
+
+                       digitPre = digit;
+               }
+
+               if(rep == 0)
+                       compressed += "";
+               else if(rep == 1)
+                       compressed += digit.ToString();
+               else
+                       compressed += digit.ToString() + "*" + rep.ToString();
+
+               return compressed;
+       }
+
+
+       /* unused
        public static string CompressSignal(string fileNameSignal)
        {
-               string contents = Util.ReadFile(fileNameSignal, false);
+               return CompressData(Util.ReadFile(fileNameSignal, false));
+       }
+       
+       public static string CompressData(string contents)
+       {
                string compressed = "";
                
                bool start = true;
@@ -746,6 +813,7 @@ public class UtilEncoder
 
                return compressed;
        }
+       */
 
 
        private static string [] encoderFindPos(string contents, int start, int duration) {


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