[chronojump] Initial code for calibration on inertial capture



commit e9034f2c5cf4e46acf4c2de515738c0def2581ad
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Jan 19 19:08:51 2017 +0100

    Initial code for calibration on inertial capture

 src/encoderCapture.cs |   90 ++++++++++++++++++++++++++++++++++++++++++++-----
 src/gui/encoder.cs    |   56 +++++++++++++++++++++++++++++--
 2 files changed, 134 insertions(+), 12 deletions(-)
---
diff --git a/src/encoderCapture.cs b/src/encoderCapture.cs
index 42ed751..9675076 100644
--- a/src/encoderCapture.cs
+++ b/src/encoderCapture.cs
@@ -58,7 +58,7 @@ public abstract class EncoderCapture
         * on inertial we need both
         */
        protected double sum;   
-       protected double sumInertialDisc;
+       protected int sumInertialDisc;
 
        protected int i;
        protected int msCount;
@@ -82,6 +82,9 @@ public abstract class EncoderCapture
        protected bool lastDirectionStoredIsUp;
        protected bool capturingFirstPhase;
 
+       protected static SerialPort sp;
+       protected bool finish;
+
        //capture is simulated (a signal file is readed)
        private static bool simulated = false;
        private int [] simulatedInts;
@@ -89,11 +92,8 @@ public abstract class EncoderCapture
 
        
        // ---- private stuff ----
-               
-       private static SerialPort sp;
        private bool cancel;
-       private bool finish;
-       
+
        /*
        public static bool CheckPort(string port)
        {
@@ -121,6 +121,7 @@ public abstract class EncoderCapture
        }
        */
 
+
        //if cont (continuous mode), then will not end when too much time passed before start
        public void InitGlobal (int widthG, int heightG, int time, int timeEnd, bool cont, string eccon, 
string port)
        {
@@ -198,11 +199,15 @@ public abstract class EncoderCapture
                cancel = false;
                finish = false;
        }
-       
+
        protected virtual void initSpecific()
        {
        }
 
+       public virtual void InitCalibrated(int angleNow)
+       {
+       }
+
        public bool Capture(string outputData1, EncoderRProcCapture encoderRProcCapture)
        {
                if(simulated) {
@@ -211,6 +216,8 @@ public abstract class EncoderCapture
                                return false;
                }
 
+               LogB.Information("sum = " + sum.ToString());
+               LogB.Information("sumInertialDisc = " + sumInertialDisc.ToString());
                do {
                        try {
                                byteReaded = readByte();
@@ -224,6 +231,7 @@ public abstract class EncoderCapture
                        }
 
                        byteReaded = convertByte(byteReaded);
+                       //LogB.Information(" byte: " + byteReaded);
 
                        i = i+1;
                        if(i >= 0) 
@@ -296,6 +304,10 @@ public abstract class EncoderCapture
                                if(directionNow != directionLastMSecond) {
                                        directionLastMSecond = directionNow;
                                        directionChangeCount = 0;
+
+               LogB.Information("sum = " + sum.ToString());
+               LogB.Information("sumInertialDisc = " + sumInertialDisc.ToString());
+
                                } 
                                else if(directionNow != directionCompleted) {
                                        //we are in a different direction than the last completed
@@ -456,7 +468,8 @@ public abstract class EncoderCapture
                return true;
        }
 
-       private int readByte() 
+
+       protected virtual int readByte()
        {
                if(simulated) {
                        return simulatedInts[simulatedCount ++];
@@ -464,7 +477,7 @@ public abstract class EncoderCapture
                        return sp.ReadByte();
                }
        }
-       private int convertByte(int b) 
+       protected int convertByte(int b)
        {
                if(simulated) {
                        if(b >= 48)
@@ -648,7 +661,18 @@ public class EncoderCaptureInertial : EncoderCapture
 
                inertialFirstEccPhaseDone = false;
        }
-       
+
+       public override void InitCalibrated(int angleNow)
+       {
+               sum = angleNow;
+               sumInertialDisc = angleNow;
+
+               if(inertialShouldCheckStartDirection)
+               {
+                       inertialCheckIfInverted();
+               }
+       }
+
        protected override void inertialCheckIfInverted() 
        {
                /*
@@ -810,3 +834,51 @@ public class EncoderCaptureIMCalc : EncoderCapture
        }
        
 }
+
+public class EncoderCaptureInertialBackground : EncoderCapture
+{
+       /*
+        * this class allows reading always in order to know the angle of the string and the change of 
direction
+        */
+       private int angleNow;
+
+       public EncoderCaptureInertialBackground(string port)
+       {
+               angleNow = 0;
+               finish = false;
+
+               sp = new SerialPort(port);
+               sp.BaudRate = 115200;
+               LogB.Information("sp created");
+       }
+
+       public bool CaptureBG()
+       {
+               LogB.Information("CaptureBG!");
+               sp.Open();
+               LogB.Information("sp opened");
+
+               int myByteReaded;
+               do {
+                       try {
+                               myByteReaded = readByte();
+                       } catch {
+                               LogB.Error("ERROR at InertialCaptureBackground: Maybe encoder cable is 
disconnected");
+                               return false;
+                       }
+
+                       myByteReaded = convertByte(myByteReaded);
+                       angleNow += myByteReaded;
+                       //LogB.Information("angleNow = " + angleNow.ToString());
+               } while (! finish);
+
+               sp.Close();
+               return true;
+       }
+
+       public int AngleNow
+       {
+               get { return angleNow; }
+       }
+
+}
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 3d33071..97263f4 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -261,6 +261,7 @@ public partial class ChronoJumpWindow
        Gtk.ListStore encoderAnalyzeListStore; //can be EncoderCurves or EncoderNeuromuscularData
 
        Thread encoderThread;
+       //Thread encoderThreadBG;
        
 
        int image_encoder_width;
@@ -320,7 +321,7 @@ public partial class ChronoJumpWindow
         * CAPTURE_IM records to get the inertia moment but does not calculate curves in R and not updates 
the treeview
         * CURVES_AC (After Capture) is like curves but does not start a new thread (uses same pulse as 
capture)
         */
-       enum encoderActions { CAPTURE, CAPTURE_EXTERNAL, CURVES, CURVES_AC, LOAD, ANALYZE, CAPTURE_IM, 
CURVES_IM } 
+       enum encoderActions { CAPTURE_BG, CAPTURE, CAPTURE_EXTERNAL, CURVES, CURVES_AC, LOAD, ANALYZE, 
CAPTURE_IM, CURVES_IM }
        
        //STOPPING is used to stop the camera. It has to be called only one time
        enum encoderCaptureProcess { CAPTURING, STOPPING, STOPPED } 
@@ -610,9 +611,19 @@ public partial class ChronoJumpWindow
                return true;
        }
 
+       EncoderCaptureInertialBackground eCaptureInertialBG; //only created one time
+       void on_button_encoder_inertial_calibrate_clicked (object o, EventArgs args)
+       {
+               //TODO: At the moment, button_encoder_inertial_calibrate can only be sensitive while not 
capturing
+               encoderThreadStart(encoderActions.CAPTURE_BG);
+       }
+
        double maxPowerIntersessionOnCapture;
        void on_button_encoder_capture_clicked (object o, EventArgs args) 
        {
+               if(eCaptureInertialBG != null)
+                       eCaptureInertialBG.Finish();
+
                maxPowerIntersessionOnCapture = findMaxPowerIntersession();
                //LogB.Information("maxPower: " + maxPowerIntersessionOnCapture);
 
@@ -2082,6 +2093,13 @@ public partial class ChronoJumpWindow
 
        //this is called by non gtk thread. Don't do gtk stuff here
        //I suppose reading gtk is ok, changing will be the problem
+       private void encoderDoCaptureBG ()
+       {
+               eCaptureInertialBG.CaptureBG();
+       }
+
+       //this is called by non gtk thread. Don't do gtk stuff here
+       //I suppose reading gtk is ok, changing will be the problem
        private void encoderDoCaptureCsharp () 
        {
                bool capturedOk = eCapture.Capture(
@@ -4531,7 +4549,20 @@ public partial class ChronoJumpWindow
        {
                encoderProcessCancel = false;
                                        
-               if(action == encoderActions.CAPTURE || action == encoderActions.CAPTURE_IM)
+               if(action == encoderActions.CAPTURE_BG)
+               {
+                       eCaptureInertialBG = new EncoderCaptureInertialBackground(
+                                       
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port);
+                       //encoderThreadBG = new Thread(new ThreadStart(encoderDoCaptureBG));
+                       encoderThread = new Thread(new ThreadStart(encoderDoCaptureBG));
+                       GLib.Idle.Add (new GLib.IdleHandler (pulseGTKEncoderCaptureBG));
+
+                       LogB.ThreadStart();
+                       //encoderThreadBG.Start();
+                       encoderThread.Start();
+               }
+
+               else if(action == encoderActions.CAPTURE || action == encoderActions.CAPTURE_IM)
                {
                        //encoder_pulsebar_capture.Text = Catalog.GetString("Please, wait.");
                        LogB.Information("encoderThreadStart begins");
@@ -4585,8 +4616,9 @@ public partial class ChronoJumpWindow
                                needToRefreshTreeviewCapture = false;
 
                                if(encoderConfigurationCurrent.has_inertia)
+                               {
                                        eCapture = new EncoderCaptureInertial();
-                               else
+                               } else
                                        eCapture = new EncoderCaptureGravitatory();
 
                                int recordingTime = preferences.encoderCaptureTime;
@@ -4605,6 +4637,11 @@ public partial class ChronoJumpWindow
                                                
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port
                                                );
 
+                               if(encoderConfigurationCurrent.has_inertia && eCaptureInertialBG != null)
+                               {
+                                       eCapture.InitCalibrated(eCaptureInertialBG.AngleNow);
+                               }
+
                                encoderThread = new Thread(new ThreadStart(encoderDoCaptureCsharp));
                                GLib.Idle.Add (new GLib.IdleHandler (pulseGTKEncoderCaptureAndCurves));
                        }
@@ -4957,6 +4994,19 @@ public partial class ChronoJumpWindow
                        needToRefreshTreeviewCapture = true;
                }
        }
+
+       private bool pulseGTKEncoderCaptureBG ()
+       {
+               //if(! encoderThreadBG.IsAlive) {
+               if(! encoderThread.IsAlive) {
+                       return false;
+               }
+
+               Thread.Sleep (100);
+               //LogB.Information(" CapBG:"+ encoderThreadBG.ThreadState.ToString());
+               LogB.Information(" CapBG:"+ encoderThread.ThreadState.ToString());
+               return true;
+       }
                                
        static bool needToCallPrepareEncoderGraphs; //this will not erase them
        private bool pulseGTKEncoderCaptureAndCurves ()


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