[chronojump] Fixed encoder ecca bug. Improved encoderMode



commit 76b33e0735b777ecdb435660a49fb5963c30f65b
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Jan 16 18:38:43 2014 +0100

    Fixed encoder ecca bug. Improved encoderMode

 encoder/graph.R        |   22 +++++++++++-
 glade/chronojump.glade |    2 +-
 src/constants.cs       |    6 ++-
 src/encoder.cs         |   14 ++------
 src/gui/encoder.cs     |   85 +++++++++++++++++++++++++----------------------
 src/sqlite/encoder.cs  |    6 ++--
 src/utilEncoder.cs     |   37 ++++----------------
 7 files changed, 85 insertions(+), 87 deletions(-)
---
diff --git a/encoder/graph.R b/encoder/graph.R
index 61a9c7b..300a17a 100644
--- a/encoder/graph.R
+++ b/encoder/graph.R
@@ -1599,6 +1599,19 @@ find.yrange <- function(singleFile, rawdata, curves) {
        return (c(y.min,y.max))
 }
 
+#encoderMode conversions
+#in signals and curves, need to do conversions (invert, inertiaMomentum, diameter)
+encoderModeConversions <- function(rawdata, encoderMode, diameter) {
+       if(encoderMode == "LINEARINVERTED")
+               rawdata = -rawdata
+       if(encoderMode == "ROTARYAXIS") {
+               ticksRotaryEncoder = 200 #our rotary axis encoder send 200 ticks by turn
+               rawdata = ( rawdata / ticksRotaryEncoder ) * 2 * pi * ( diameter / 2 )
+       }
+       #write(rawdata, "debug-file.txt")
+       return(rawdata)
+}
+
 quitIfNoData <- function(n, curves, outputData1) {
        #if not found curves with this data, plot a "sorry" message and exit
        if(n == 1 & curves[1,1] == 0 & curves[1,2] == 0) {
@@ -1725,7 +1738,8 @@ doProcess <- function(options) {
        #declare here
        SmoothingsEC = 0
 
-       if(! singleFile) {
+
+       if(! singleFile) {      #reads CSV with curves to analyze
                #this produces a rawdata, but note that a cumsum(rawdata) cannot be done because:
                #this are separated movements
                #maybe all are concentric (there's no returning to 0 phase)
@@ -1759,6 +1773,8 @@ doProcess <- function(options) {
                        #this removes all NAs on a curve
                        dataTempFile  = dataTempFile[!is.na(dataTempFile)]
 
+                       dataTempFile = encoderModeConversions(dataTempFile, encoderMode, diameter)
+
                        dataTempPhase=dataTempFile
                        processTimes = 1
                        changePos = 0
@@ -1843,12 +1859,14 @@ doProcess <- function(options) {
                
                #find SmoothingsEC
                SmoothingsEC = findSmoothingsEC(rawdata, curves, Eccon, SmoothingOneC)
-       } else {
+       } else {        #singleFile == True. reads a signal file
                rawdata=scan(file=File,sep=",")
                        
                #if data file ends with comma. Last character will be an NA. remove it
                #this removes all NAs
                rawdata  = rawdata[!is.na(rawdata)]
+                       
+               rawdata = encoderModeConversions(rawdata, encoderMode, diameter)
 
                if(length(rawdata)==0) {
                        plot(0,0,type="n",axes=F,xlab="",ylab="")
diff --git a/glade/chronojump.glade b/glade/chronojump.glade
index 553df2d..fd2a5e1 100644
--- a/glade/chronojump.glade
+++ b/glade/chronojump.glade
@@ -12213,7 +12213,7 @@ on current Chronojump version.</property>
                                                         <property 
name="secondary_icon_activatable">False</property>
                                                         <property 
name="primary_icon_sensitive">True</property>
                                                         <property 
name="secondary_icon_sensitive">True</property>
-                                                        <property name="adjustment">4 0 40 1 10 0</property>
+                                                        <property name="adjustment">4 0 80 1 10 0</property>
                                                         <property name="climb_rate">1</property>
                                                         <property name="digits">1</property>
                                                         <property name="snap_to_ticks">True</property>
diff --git a/src/constants.cs b/src/constants.cs
index c41e90b..777ab3c 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -609,10 +609,12 @@ public class Constants
        public static string ConcentricEccentric = "Concentric-eccentric";
 
        public enum EncoderCheckFileOp { ANALYZE_EXPORT_ALL_CURVES, ANALYZE_SAVE_IMAGE, ANALYZE_SAVE_TABLE}
-       public enum EncoderSignalMode { 
-               LINEAR, LINEARINVERTED, ROTARY, 
+
+       public enum EncoderSignalMode { //this names are used on graph.R change there also if needed
+               LINEAR, LINEARINVERTED, ROTARYFRICTION, ROTARYAXIS,
                LINEARINERTIAL, LINEARINVERTEDINERTIAL, ROTARYINERTIAL 
        }
+
        public enum Encoder1RMMethod { NONWEIGHTED, WEIGHTED, WEIGHTED2, WEIGHTED3 }
        public enum ContextMenu { NONE, EDITDELETE, DELETE }
 
diff --git a/src/encoder.cs b/src/encoder.cs
index c875da7..8e4ed3b 100644
--- a/src/encoder.cs
+++ b/src/encoder.cs
@@ -36,11 +36,8 @@ public class EncoderParams
        private string analysisVariables;
        private string analysisOptions;         //p: propulsive
                                        
-       //Constants.EncoderSignalMode
-       //in signals and in curves
-       //in signals need to do conversions (invert)
-       //in curves they are already done, is just informative
-       //same for inertiaMomentum and diameter
+       //encoderMode conversions
+       //in signals and curves, need to do conversions (invert, inertiaMomentum, diameter)
        private string encoderMode;     
        private int inertiaMomentum; 
        private double diameter;
@@ -285,11 +282,8 @@ public class EncoderSQL
        public string status;   //active or inactive curves
        public string videoURL; //URL of video of signals
        
-       //Constants.EncoderSignalMode
-       //in signals and in curves
-       //in signals need to do conversions (invert)
-       //in curves they are already done, is just informative
-       //same for inertiaMomentum and diameter
+       //encoderMode conversions
+       //in signals and curves, need to do conversions (invert, inertiaMomentum, diameter)
        public string encoderMode;
        public int inertiaMomentum; //kg*cm^2
        public double diameter;
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index af6e65e..35932da 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -172,8 +172,6 @@ public partial class ChronoJumpWindow
        bool encoderPropulsive;
        double encoderSmoothCon;
 
-       bool lastRecalculateWasInverted;
-       bool dataHasBeenInverted; //useful to save info of curve in SQL if no curves are found after 
inverting and recalculating
        //bool capturingRotaryInertial;
                
        EncoderCaptureOptionsWindow encoderCaptureOptionsWin;
@@ -322,8 +320,6 @@ public partial class ChronoJumpWindow
                                "",                                     //SpecialData
                                ep);                            
                                
-               lastRecalculateWasInverted = checkbutton_encoder_capture_inverted.Active;
-
                //Update inertia momentum of encoder if needed
                SqlitePreferences.Update("inertialmomentum", 
                                Util.ConvertToPoint((double) spin_encoder_capture_inertial.Value), false);
@@ -524,16 +520,6 @@ public partial class ChronoJumpWindow
        {
                if (File.Exists(UtilEncoder.GetEncoderDataTempFileName())) 
                {
-                       dataHasBeenInverted = false;    
-                       if(saveOrLoad) {
-                               //change sign on signal file if checkbutton_encoder_capture_inverted.Active 
changed
-                               if(lastRecalculateWasInverted != checkbutton_encoder_capture_inverted.Active) 
{
-                                       UtilEncoder.ChangeSign(UtilEncoder.GetEncoderDataTempFileName());
-                                       dataHasBeenInverted = true;
-                               }
-                       }
-                       lastRecalculateWasInverted = checkbutton_encoder_capture_inverted.Active;
-       
                        //calculate and recalculate saves the curve at end
                        //load does not save the curve 
                        if(saveOrLoad)          
@@ -609,8 +595,12 @@ public partial class ChronoJumpWindow
                else { //(radiobutton_encoder_capture_rotary.Active)
                        if(checkbutton_encoder_capture_inertial.Active)
                                data.Add(Constants.EncoderSignalMode.ROTARYINERTIAL.ToString());
-                       else
-                               data.Add(Constants.EncoderSignalMode.ROTARY.ToString());
+                       else {
+                               if(radiobutton_encoder_capture_rotary_friction.Active)
+                                       data.Add(Constants.EncoderSignalMode.ROTARYFRICTION.ToString());
+                               else
+                                       data.Add(Constants.EncoderSignalMode.ROTARYAXIS.ToString());
+                       }
                }
                        
                if(checkbutton_encoder_capture_inertial.Active)
@@ -624,7 +614,6 @@ public partial class ChronoJumpWindow
        }
 
 
-       //TODO: add diameter here       
        private void setEncoderCombos(EncoderSQL eSQL) {
                //TODO diferentiate both rotary encoders
                if (
@@ -644,16 +633,23 @@ public partial class ChronoJumpWindow
                if(eSQL.encoderMode == Constants.EncoderSignalMode.LINEARINVERTED.ToString()) {
                        radiobutton_encoder_capture_linear.Active = true;
                        checkbutton_encoder_capture_inverted.Active = true;
-               } else if(eSQL.encoderMode == Constants.EncoderSignalMode.ROTARY.ToString()) {
+               } else if(
+                               eSQL.encoderMode == Constants.EncoderSignalMode.ROTARYFRICTION.ToString() ||
+                               eSQL.encoderMode == Constants.EncoderSignalMode.ROTARYAXIS.ToString() ) {
                        radiobutton_encoder_capture_rotary.Active = true;
                        checkbutton_encoder_capture_inverted.Active = false;
+                       
+                       if(eSQL.encoderMode == Constants.EncoderSignalMode.ROTARYFRICTION.ToString())
+                               radiobutton_encoder_capture_rotary_friction.Active = true;
+                       else
+                               radiobutton_encoder_capture_rotary_axis.Active = true;
+
                } else { //default to linear: (eSQL.encoderMode == 
Constants.EncoderSignalMode.LINEAR.ToString()) 
                        radiobutton_encoder_capture_linear.Active = true;
                        checkbutton_encoder_capture_inverted.Active = false;
                }
 
-               //TODO: fix this to use diameter if needed
-               //eSQL.diameter...
+               spin_encoder_capture_diameter.Value = Convert.ToDouble(eSQL.diameter);  
        }
 
 
@@ -1653,16 +1649,10 @@ public partial class ChronoJumpWindow
                eSQL.url = path;
                eSQL.description = desc;
 
-               if(mode == "signal") {
-                       ArrayList encoderTypeArray = getEncoderTypeByCombos();
-                       eSQL.encoderMode =      encoderTypeArray[0].ToString();
-                       eSQL.inertiaMomentum =  Convert.ToInt32(encoderTypeArray[1]);
-                       eSQL.diameter =         Convert.ToDouble(encoderTypeArray[2]);
-               } else {
-                       eSQL.encoderMode = "";
-                       eSQL.inertiaMomentum = 0;
-                       eSQL.diameter = 0; 
-               }
+               ArrayList encoderTypeArray = getEncoderTypeByCombos();
+               eSQL.encoderMode =      encoderTypeArray[0].ToString();
+               eSQL.inertiaMomentum =  Convert.ToInt32(encoderTypeArray[1]);
+               eSQL.diameter =         Convert.ToDouble(encoderTypeArray[2]);
 
                
                //if is a signal that we just loaded, then don't insert, do an update
@@ -1860,9 +1850,21 @@ 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 encoderModeConversions: inverted, inertial, diameter, demult, 
...
                int byteReaded;
+               
                //initialize
-               encoderReaded = new int[recordingTime];
+               int [] encoderReadedRaw = new int[recordingTime]; //stored to file in this method
+               encoderReaded = new int[recordingTime];           //readed from drawing process: 
updateEncoderCaptureGraphRCalc() 
+               
+               ArrayList encoderTypeArray = getEncoderTypeByCombos();
+               string encoderMode = encoderTypeArray[0].ToString();
+               int inertiaMomentum = Convert.ToInt32(encoderTypeArray[1]);
+               double diameter = Convert.ToDouble(encoderTypeArray[2]);
+
 
                int sum = 0;
                string dataString = "";
@@ -1906,23 +1908,20 @@ public partial class ChronoJumpWindow
                        ecca = new EncoderCaptureCurveArray();
                        eccaCreated = true;
                }
-               ecca.curvesDone = 0;
-               ecca.curvesAccepted = 0;
 
 
                do {
-                       byteReaded = sp.ReadByte();
-                       if(byteReaded > 128)
-                               byteReaded = byteReaded - 256;
+                       byteReadedRaw = sp.ReadByte();
+                       if(byteReadedRaw > 128)
+                               byteReadedRaw = byteReadedRaw - 256;
 
-                       //invert sign if inverted is selected
-                       if(checkbutton_encoder_capture_inverted.Active)
-                               byteReaded *= -1;
+                       byteReaded = UtilEncoder.EncoderModeConversions(byteReadedRaw, encoderMode, 
inertiaMomentum, diameter);
 
                        i=i+1;
                        if(i >= 0) {
                                sum += byteReaded;
                                encoderReaded[i] = byteReaded;
+                               encoderReadedRaw[i] = byteReadedRaw;
 
                                encoderCapturePoints[i] = new Gdk.Point(
                                                Convert.ToInt32(width*i/recordingTime),
@@ -2022,7 +2021,7 @@ public partial class ChronoJumpWindow
 
                sep = "";
                for(int j=0; j < i ; j ++) {
-                       writer.Write(sep + encoderReaded[j]);
+                       writer.Write(sep + encoderReadedRaw[j]); //store the raw file (before 
encoderModeConversions)
                        sep = ", ";
                }
 
@@ -2074,6 +2073,9 @@ public partial class ChronoJumpWindow
                        analysisVariables = getAnalysisVariables(sendAnalysis);
 
                ArrayList encoderTypeArray = getEncoderTypeByCombos();
+               string encoderMode = encoderTypeArray[0].ToString();
+               int inertiaMomentum = Convert.ToInt32(encoderTypeArray[1]);
+               double diameter = Convert.ToDouble(encoderTypeArray[2]);
 
                if(radiobutton_encoder_analyze_data_user_curves.Active) {
                        string myEccon = "ec";
@@ -3872,6 +3874,8 @@ Log.WriteLine(str);
        
        private void updateEncoderCaptureGraphRCalc() 
        {
+               if(! eccaCreated)
+                       return;
                if(ecca.ecc.Count <= ecca.curvesDone) 
                        return;
 
@@ -4219,6 +4223,7 @@ Log.WriteLine(str);
                                encoderCaptureStringR = 
",series,exercise,mass,start,width,height,meanSpeed,maxSpeed,maxSpeedT,meanPower,peakPower,peakPowerT,pp_ppt,NA,NA,NA";
                                
                                capturingCsharp = true;
+                               eccaCreated = false;
 
                                encoderThreadCapture = new Thread(new ThreadStart(captureCsharp));
                                GLib.Idle.Add (new GLib.IdleHandler (pulseGTKEncoderCapture));
diff --git a/src/sqlite/encoder.cs b/src/sqlite/encoder.cs
index f147644..aec4edf 100644
--- a/src/sqlite/encoder.cs
+++ b/src/sqlite/encoder.cs
@@ -78,9 +78,9 @@ class SqliteEncoder : Sqlite
                        "description TEXT, " +
                        "status TEXT, " +       //"active", "inactive"
                        "videoURL TEXT, " +     //URL of video of signals
-                       "mode TEXT, " +         //Constants.EncoderSignalMode (only on signals??)
-                       "inertiaMomentum INT, " + 
-                       "diameter FLOAT, " + 
+                       "mode TEXT, " +         //Constants.EncoderSignalMode (signals, and curves)
+                       "inertiaMomentum INT, " +       //signals and curves
+                       "diameter FLOAT, " +            //signals and curves
                        "future1 TEXT, " + 
                        "future2 TEXT, " + 
                        "future3 TEXT )";
diff --git a/src/utilEncoder.cs b/src/utilEncoder.cs
index dd881c1..6acc321 100644
--- a/src/utilEncoder.cs
+++ b/src/utilEncoder.cs
@@ -441,33 +441,12 @@ public class UtilEncoder
                return fileCurve;
        }
 
-       //used on encoder inverted-noninverted
-       public static void ChangeSign(string fileName) 
-       {
-               string data = Util.ReadFile(fileName, true);
-               if(data != null) {
-                       TextWriter writer;
-                       try {
-                               writer = File.CreateText(fileName);
-                       } catch {return;}
-       
-                       using (StringReader reader = new StringReader (data)) {
-                               do {
-                                       string line = reader.ReadLine ();
-                                       if (line == null)
-                                               break;
-                                       string [] ints = line.Split(new char[] {','});
-                                       string sep = "";
-                                       foreach(string istr in ints) {
-                                               int i = Convert.ToInt32(istr);
-                                               i *= -1;
-                                               writer.Write(sep + i.ToString());
-                                               sep = ", ";
-                                       }
-                               } while(true);
-                       }
-                       writer.Flush();
-                       ((IDisposable)writer).Dispose();
-               }
-       }
+       public static int EncoderModeConversions(int byteReaded, string encoderMode, int inertiaMomentum, 
double diameter) {
+               //invert sign if inverted is selected
+               if(encoderMode == Constants.EncoderSignalMode.LINEARINVERTED.ToString())
+                       byteReaded *= -1;
+
+               return byteReaded;
+       }       
+
 }


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