[chronojump] Encoder big change, now height approval is done in R



commit 18039ee19a150caee55d15556add44492b68d309
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri May 22 21:10:09 2015 +0200

    Encoder big change, now height approval is done in R

 encoder/capture.R   |   71 +++++++++++++++++++++++++++---------------------
 src/encoderRProc.cs |   12 ++------
 src/gui/encoder.cs  |   75 +++++++++++++++++---------------------------------
 src/utilEncoder.cs  |   41 ----------------------------
 4 files changed, 69 insertions(+), 130 deletions(-)
---
diff --git a/encoder/capture.R b/encoder/capture.R
index 4a39406..2cbba6f 100644
--- a/encoder/capture.R
+++ b/encoder/capture.R
@@ -39,6 +39,9 @@ calcule <- function(displacement, op, curveNum)
        if(length(displacement) < 4)
                return (curveNum)
 
+       if(abs(sum(displacement)) < op$MinHeight)
+               return (curveNum)
+
 
        #read AnalysisOptions
        #if is propulsive and rotatory inertial is: "p;ri" 
@@ -91,15 +94,6 @@ calcule <- function(displacement, op, curveNum)
        return(curveNum +1)
 }
                
-getPositionStart <- function(input) 
-{
-       inputVector = unlist(strsplit(input, " "))
-       if( length(inputVector) == 2 && inputVector[1] == "ps" )
-               return (as.numeric(inputVector[2]))
-       else
-               return (0)
-}
-
 
 #converts data: "0*5 1 0 -1*3 2"
 #into: 0  0  0  0  0  1  0 -1 -1 -1  2
@@ -119,6 +113,7 @@ uncompress <- function(curveSent)
        }
        return (as.numeric(ints))
 }
+       
 
 doProcess <- function(options) 
 {
@@ -131,6 +126,10 @@ doProcess <- function(options)
        #print (op)
 
        curveNum = 0
+       inertialPositionCurveSentStart = 0
+       inertialPositionCurveSentEnd = 0
+       inertialCapturingFirstPhase = TRUE
+       
        input <- readLines(f, n = 1L)
        while(input[1] != "Q") {
                if(debug) {
@@ -148,6 +147,9 @@ doProcess <- function(options)
                        op <- assignOptions(options)
 
                        curveNum = 0
+                       inertialPositionCurveSentStart = 0
+                       inertialPositionCurveSentEnd = 0
+                       inertialCapturingFirstPhase = TRUE
                        input <- readLines(f, n = 1L)
        
                        if(input[1] == "Q")
@@ -157,13 +159,8 @@ doProcess <- function(options)
                #Sys.sleep(4) #just to test how Chronojump reacts if process takes too long
                #cat(paste("input is:", input, "\n"))
 
-               #from Chronojump first it's send the eg: "ps -1000", meaning curve starts at -1000
-               #then it's send the displacement
-               positionStart = getPositionStart(input)
-
                #-- read the curve (from some lines that finally end on an 'E')
                readingCurve = TRUE
-               input = NULL
                while(readingCurve) {
                        inputLine <- readLines(f, n = 1L)
                        if(inputLine[1] == "E")
@@ -172,6 +169,7 @@ doProcess <- function(options)
                                input = c(input, inputLine)
                }
                #-- curve readed
+
                
                if(debug)
                        write("doProcess input", stderr())
@@ -186,15 +184,19 @@ doProcess <- function(options)
                #this removes all NAs
                displacement  = displacement[!is.na(displacement)]
 
-
                if(debug)
                        write("doProcess 2", stderr())
+                       
                if(isInertial(op$EncoderConfigurationName))
                {
                  diametersPerTick = getInertialDiametersPerMs(displacement, op$diameter)
                        displacement = getDisplacementInertial(displacement, op$EncoderConfigurationName, 
diametersPerTick, op$diameterExt)
 
-                       displacement = getDisplacementInertialBody(positionStart, displacement, FALSE, 
op$Title) #draw: FALSE
+                       #need to do this before getDisplacementInertialBody cuts the curve:  /|\
+                       positionTemp = cumsum(displacement)
+                       inertialPositionCurveSentEnd = inertialPositionCurveSentStart + 
positionTemp[length(positionTemp)]
+
+                       displacement = getDisplacementInertialBody(inertialPositionCurveSentStart, 
displacement, FALSE, op$Title) #draw: FALSE
                } else {
                        displacement = getDisplacement(op$EncoderConfigurationName, displacement, 
op$diameter, op$diameterExt)
                }
@@ -223,25 +225,32 @@ doProcess <- function(options)
                #if isInertial: getDisplacementInertialBody separate phases using initial height of full 
extended person
                #so now there will be two different curves to process
 
+               position = cumsum(displacement)
+
                if(isInertial(op$EncoderConfigurationName)) 
                {
-                       position = cumsum(displacement)
-                       positionTop <- floor(mean(which(position == max(position))))
-                       displacement1 = displacement[1:positionTop]
-                       displacement2 = displacement[(positionTop+1):length(displacement)]
-
-                       if(op$Eccon == "c")
-                               curveNum <- calcule(displacement1, op, curveNum)
-                       else {
-                               curveNum <- calcule(displacement1, op, curveNum)
-                               curveNum <- calcule(displacement2, op, curveNum)
+                       if(abs(max(position) - min(position)) >= op$MinHeight) {
+                               if(inertialCapturingFirstPhase)
+                                       inertialCapturingFirstPhase = FALSE
+                               else {
+                                       positionTop <- floor(mean(which(position == max(position))))
+                                       displacement1 = displacement[1:positionTop]
+                                       displacement2 = displacement[(positionTop+1):length(displacement)]
+
+                                       if(op$Eccon == "c") {
+                                               curveNum <- calcule(displacement1, op, curveNum)
+                                       } else {
+                                               curveNum <- calcule(displacement1, op, curveNum)
+                                               curveNum <- calcule(displacement2, op, curveNum)
+                                       }
+                               }
                        }
+               } else {
+                       curveNum <- calcule(displacement, op, curveNum)
+               }
+                       
+               inertialPositionCurveSentStart = inertialPositionCurveSentEnd
 
-                       #write(c("positionTop", positionTop), stderr())
-                       #write(c("length(displacement)", length(displacement)), stderr())
-               } else
-                               curveNum <- calcule(displacement, op, curveNum)
-               
                if(debug)
                        write("doProcess 4", stderr())
 
diff --git a/src/encoderRProc.cs b/src/encoderRProc.cs
index f5ea988..edc6ed2 100644
--- a/src/encoderRProc.cs
+++ b/src/encoderRProc.cs
@@ -227,20 +227,14 @@ public class EncoderRProcCapture : EncoderRProc
        }
        
        //here curve is sent compressed (string. eg: "0*5 1 0 -1*3 2")
-       public void SendCurve(double heightAtStart, string curveCompressed)
+       public void SendCurve(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);   //this will send some lines because compressed data 
comes with '\n's
+               LogB.Debug("curveSend [displacement array]",curveCompressed);
+               p.StandardInput.WriteLine(curveCompressed);     //this will send some lines because 
compressed data comes with '\n's
                p.StandardInput.WriteLine("E");         //this will mean the 'E'nd of the curve. Then data 
can be uncompressed on R
                
                LogB.Debug("<-- writen line 1");
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 77400b7..e788e96 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -198,7 +198,8 @@ public partial class ChronoJumpWindow
        private ArrayList encoderCompareInterperson;    //personID:personName
        private ArrayList encoderCompareIntersession;   //sessionID:sessionDate
 
-       private static double [] encoderReaded;         //data coming from encoder and converted (can be 
double)
+       //private static double [] encoderReaded;               //data coming from encoder and converted (can 
be double)
+       private static int [] encoderReaded;            //data coming from encoder and converted
        private static int encoderCaptureCountdown;
        private static Gdk.Point [] encoderCapturePoints;               //stored to be realtime displayed
        private static int encoderCapturePointsCaptured;                //stored to be realtime displayed
@@ -2021,15 +2022,10 @@ public partial class ChronoJumpWindow
                //int recordingTime = es.Ep.Time * 1000;
                int recordingTime = time * 1000;
                
-               //this is what's readed from encoder, as it's linear (non-inverted, not inertial, ...)
-               //it's stored in file like this
-               int byteReadedRaw;
-               //this it's converted applying encoderConfigurationConversions: inverted, inertial, diameter, 
gearedDown, ...
-               double byteReaded;
+               int byteReaded;
                
                //initialize
-               int [] encoderReadedRaw = new int[recordingTime]; //stored to file in this method
-               encoderReaded = new double[recordingTime];
+               encoderReaded = new int[recordingTime];
        
                double sum = 0;
                string dataString = "";
@@ -2068,11 +2064,6 @@ public partial class ChronoJumpWindow
                int previousEnd = 0;
                int lastNonZero = 0;
                
-               //used to send the heightAtCurveStart to R
-               //this is need on inertial to convert on direction curve (recorded by encoder) to a con-ecc 
curve (done by the person)
-               double heightAtCurveStart = 0;
-               double heightAccumulated = 0;
-
                //this will be used to stop encoder automatically       
                int consecutiveZeros = -1;              
                int consecutiveZerosMax = (int) 
encoderCaptureOptionsWin.spin_encoder_capture_inactivity_end_time.Value * 1000;
@@ -2097,7 +2088,7 @@ public partial class ChronoJumpWindow
 
                do {
                        try {
-                               byteReadedRaw = sp.ReadByte();
+                               byteReaded = sp.ReadByte();
                        } catch {
                                LogB.Error("Maybe encoder cable is disconnected");
                                encoderProcessCancel = true;
@@ -2105,13 +2096,12 @@ public partial class ChronoJumpWindow
                        }
 
 
-                       if(byteReadedRaw > 128)
-                               byteReadedRaw = byteReadedRaw - 256;
+                       if(byteReaded > 128)
+                               byteReaded = byteReaded - 256;
 
                        if(inertialCaptureDirectionInverted)
-                               byteReadedRaw *= -1;
+                               byteReaded *= -1;
 
-                       byteReaded = UtilEncoder.GetDisplacement(byteReadedRaw, encoderConfigurationCurrent);
 
                        i=i+1;
                        if(i >= 0) {
@@ -2140,7 +2130,6 @@ public partial class ChronoJumpWindow
 
                                sum += byteReaded;
                                encoderReaded[i] = byteReaded;
-                               encoderReadedRaw[i] = byteReadedRaw;
 
                                encoderCapturePoints[i] = new Gdk.Point(
                                                Convert.ToInt32(widthG * i / recordingTime),
@@ -2178,7 +2167,6 @@ public partial class ChronoJumpWindow
                                                sum *= -1;
                                                for(int j=0; j <= i; j ++) {
                                                        encoderReaded[j] *= -1;
-                                                       encoderReadedRaw[j] *= -1;
                                                }
                                                double sum2=0;
                                                for(int j=0; j <= i; j ++) {
@@ -2266,22 +2254,15 @@ public partial class ChronoJumpWindow
                                                LogB.Debug("curve stuff" + ecc.startFrame + ":" + 
ecc.endFrame + ":" + encoderReaded.Length);
                                                if(ecc.endFrame - ecc.startFrame > 0 ) 
                                                {
-                                                       heightAtCurveStart = heightAccumulated;
-
-                                                       double heightCurve = 0;
                                                        double [] curve = new double[ecc.endFrame - 
ecc.startFrame];
                                                        for(int k=0, j=ecc.startFrame; j < ecc.endFrame ; j 
++) {
-                                                               heightCurve += encoderReaded[j];
                                                                curve[k]=encoderReaded[j];
                                                                k++;
                                                        }
 
                                                        previousEnd = ecc.endFrame;
 
-                                                       heightAccumulated += heightCurve;
-
-                                                       heightCurve = Math.Abs(heightCurve / 10); //mm -> cm
-                                                       LogB.Information(" height: " + 
heightCurve.ToString());
+                                                       //22-may-2015: This is done in R now
 
                                                        //1) check heightCurve in a fast way first to discard 
curves soon
                                                        //   only process curves with height >= min_height
@@ -2325,30 +2306,26 @@ public partial class ChronoJumpWindow
 
                                                        if(! inertiaMomentCalculation) {        
                                                                bool sendCurve = true;
-                                                               if(heightCurve >= 
encoderSelectedMinimumHeight)         //1
-                                                               {
-                                                                       
if(encoderConfigurationCurrent.has_inertia) {
-                                                                               if(capturingFirstPhase)
-                                                                                       sendCurve = false;
-                                                                       } else { // ! 
encoderConfigurationCurrent.has_inertia
-                                                                               if( eccon == "c" && ! ecc.up )
-                                                                                       sendCurve = false;
-                                                                               if( (eccon == "ec" || eccon 
== "ecS") && ecc.up && capturingFirstPhase ) //3
-                                                                                       sendCurve = false;
-                                                                               if( 
-                                                                                               (eccon == 
"ec" || eccon == "ecS") && 
-                                                                                               
ecca.curvesAccepted > 0 &&
-                                                                                               
lastDirectionStoredIsUp == ecc.up ) //4
-                                                                                       sendCurve = false;
-                                                                       }
-                                                                       capturingFirstPhase = false;
-                                                               } else {
-                                                                       sendCurve = false;
+                                                               
+                                                               if(encoderConfigurationCurrent.has_inertia) {
+                                                                       //22 may 2015: send'it because we 
need to know the heigth change in order to do the cuts
+                                                                       //if(capturingFirstPhase)
+                                                                       //      sendCurve = false;
+                                                               } else { // ! 
encoderConfigurationCurrent.has_inertia
+                                                                       if( eccon == "c" && ! ecc.up )
+                                                                               sendCurve = false;
+                                                                       if( (eccon == "ec" || eccon == "ecS") 
&& ecc.up && capturingFirstPhase ) //3
+                                                                               sendCurve = false;
+                                                                       if( 
+                                                                                       (eccon == "ec" || 
eccon == "ecS") && 
+                                                                                       ecca.curvesAccepted > 
0 &&
+                                                                                       
lastDirectionStoredIsUp == ecc.up ) //4
+                                                                               sendCurve = false;
                                                                }
+                                                               capturingFirstPhase = false;
 
                                                                if(sendCurve) {
                                                                        encoderRProcCapture.SendCurve(
-                                                                                       heightAtCurveStart, 
                                                                                        
UtilEncoder.CompressData(curve, 25)     //compressed
                                                                                        );
 
@@ -2394,7 +2371,7 @@ public partial class ChronoJumpWindow
 
                sep = "";
                for(int j=0; j < i ; j ++) {
-                       writer.Write(sep + encoderReadedRaw[j]); //store the raw file (before 
encoderConfigurationConversions)
+                       writer.Write(sep + encoderReaded[j]); //store the raw file (before 
encoderConfigurationConversions)
                        sep = ", ";
                }
 
diff --git a/src/utilEncoder.cs b/src/utilEncoder.cs
index 8a6220c..8cee3fe 100644
--- a/src/utilEncoder.cs
+++ b/src/utilEncoder.cs
@@ -755,46 +755,5 @@ public class UtilEncoder
                return list;
        }
 
-       /* -------- EncoderConfiguration, kinematics and Dynamics ---- 
-        *
-        *              this is the same than graph.R
-        * -------------------------------------------------------- */
-
-       /*
-        * in signals and curves, need to do conversions (invert, inertiaMomentum, diameter)
-        * we use 'data' variable because can be position or displacement
-        */
-
-       public static double GetDisplacement(int byteReaded, EncoderConfiguration ec) {
-               /* no change:
-                * WEIGHTEDMOVPULLEYLINEARONPERSON1, WEIGHTEDMOVPULLEYLINEARONPERSON1INV,
-                * WEIGHTEDMOVPULLEYLINEARONPERSON2, WEIGHTEDMOVPULLEYLINEARONPERSON2INV,
-                * LINEARONPLANE, LINEARONPLANEWEIGHTDIFFANGLE, ROTARYFRICTIONSIDE, 
WEIGHTEDMOVPULLEYROTARYFRICTION
-                */
-
-               double data = byteReaded;
-               if(
-                               ec.name == Constants.EncoderConfigurationNames.LINEARINVERTED ||
-                               ec.name == 
Constants.EncoderConfigurationNames.WEIGHTEDMOVPULLEYLINEARONPERSON1INV ||
-                               ec.name == 
Constants.EncoderConfigurationNames.WEIGHTEDMOVPULLEYLINEARONPERSON2INV ) {
-                       data *= -1;
-               } else if(ec.name == Constants.EncoderConfigurationNames.WEIGHTEDMOVPULLEYONLINEARENCODER) {
-                       //default is: gearedDown = 2. Future maybe this will be a parameter
-                       data *= 2;
-               } else if(ec.name == Constants.EncoderConfigurationNames.ROTARYFRICTIONAXIS) {
-                       data = data * ec.D / ec.d;
-               } else if(
-                               ec.name == Constants.EncoderConfigurationNames.ROTARYAXIS || 
-                               ec.name == Constants.EncoderConfigurationNames.WEIGHTEDMOVPULLEYROTARYAXIS) 
-               {
-                       int ticksRotaryEncoder = 200; //our rotary axis encoder send 200 ticks by turn
-                       //diameter m -> mm
-                       data = ( data / ticksRotaryEncoder ) * 2 * Math.PI * ( ec.d * 1000 / 2 );
-               }
-               return data;
-       }
-
-
-       /* ----end of EncoderConfiguration, kinematics and Dynamics ---- */ 
 
 }


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