[chronojump] Always capture on inertial (calibrated) 75%



commit 2cb8f8c2583530af3bcf1ef6c267ae9c24dec119
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sun Jan 22 15:08:35 2017 +0100

    Always capture on inertial (calibrated) 75%

 chronojump.csproj               |    3 +-
 glade/app1.glade                |   75 ++++++++++++++--------
 src/Makefile.am                 |    1 +
 src/encoderCapture.cs           |   94 ++++-----------------------
 src/encoderCaptureInertialBG.cs |  137 +++++++++++++++++++++++++++++++++++++++
 src/gui/chronojump.cs           |   12 ++-
 src/gui/encoder.cs              |   62 ++++++++++++------
 7 files changed, 250 insertions(+), 134 deletions(-)
---
diff --git a/chronojump.csproj b/chronojump.csproj
index 8dbe78c..4eb82a8 100644
--- a/chronojump.csproj
+++ b/chronojump.csproj
@@ -1003,6 +1003,7 @@
     <Compile Include="src\platform.cs" />
     <Compile Include="src\gui\movingStartWindow.cs" />
     <Compile Include="src\Mini\chronojump_mini.cs" />
+    <Compile Include="src\encoderCaptureInertialBG.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="src\" />
@@ -2049,4 +2050,4 @@
       </Properties>
     </MonoDevelop>
   </ProjectExtensions>
-</Project>
+</Project>
\ No newline at end of file
diff --git a/glade/app1.glade b/glade/app1.glade
index 8007e98..08c9c9c 100644
--- a/glade/app1.glade
+++ b/glade/app1.glade
@@ -13730,6 +13730,49 @@ Concentric</property>
                                                           <widget class="GtkHBox" 
id="hbox_chronopic_encoder_top">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
+                                                            <property name="spacing">6</property>
+                                                            <child>
+                                                            <widget class="GtkHBox" 
id="hbox_encoder_inertial_calibrate">
+                                                            <property name="can_focus">False</property>
+                                                            <property name="spacing">2</property>
+                                                            <child>
+                                                            <widget class="GtkButton" 
id="button_encoder_inertial_calibrate">
+                                                            <property name="label" 
translatable="yes">Calibrate</property>
+                                                            <property name="visible">True</property>
+                                                            <property name="can_focus">True</property>
+                                                            <property name="receives_default">True</property>
+                                                            <signal name="clicked" 
handler="on_button_encoder_inertial_calibrate_clicked" swapped="no"/>
+                                                            </widget>
+                                                            <packing>
+                                                            <property name="expand">False</property>
+                                                            <property name="fill">False</property>
+                                                            <property name="position">0</property>
+                                                            </packing>
+                                                            </child>
+                                                            <child>
+                                                            <widget class="GtkHScale" 
id="hscale_encoder_capture_inertial_angle_now">
+                                                            <property name="width_request">150</property>
+                                                            <property name="visible">True</property>
+                                                            <property name="can_focus">True</property>
+                                                            <property name="adjustment">0 -100 100 1 1 
0</property>
+                                                            <property 
name="restrict_to_fill_level">False</property>
+                                                            <property name="fill_level">0</property>
+                                                            <property name="digits">0</property>
+                                                            <property name="value_pos">right</property>
+                                                            </widget>
+                                                            <packing>
+                                                            <property name="expand">False</property>
+                                                            <property name="fill">False</property>
+                                                            <property name="position">1</property>
+                                                            </packing>
+                                                            </child>
+                                                            </widget>
+                                                            <packing>
+                                                            <property name="expand">False</property>
+                                                            <property name="fill">False</property>
+                                                            <property name="position">0</property>
+                                                            </packing>
+                                                            </child>
                                                             <child>
                                                             <widget class="GtkHBox" 
id="hbox_chronopic_encoder_detecting">
                                                             <property name="can_focus">False</property>
@@ -13779,7 +13822,7 @@ Concentric</property>
                                                             <packing>
                                                             <property name="expand">False</property>
                                                             <property name="fill">False</property>
-                                                            <property name="position">0</property>
+                                                            <property name="position">1</property>
                                                             </packing>
                                                             </child>
                                                             <child>
@@ -13907,7 +13950,7 @@ Concentric</property>
                                                             <packing>
                                                             <property name="expand">True</property>
                                                             <property name="fill">True</property>
-                                                            <property name="position">1</property>
+                                                            <property name="position">2</property>
                                                             </packing>
                                                             </child>
                                                           </widget>
@@ -14628,24 +14671,6 @@ Concentric</property>
                                                             <property name="can_focus">False</property>
                                                             <property name="spacing">2</property>
                                                             <child>
-                                                            <widget class="GtkHBox" id="hbox73">
-                                                            <property name="visible">True</property>
-                                                            <property name="can_focus">False</property>
-                                                            <child>
-                                                            <widget class="GtkButton" 
id="button_encoder_inertial_calibrate">
-                                                            <property name="label" 
translatable="yes">Calibrate</property>
-                                                            <property name="visible">True</property>
-                                                            <property name="can_focus">True</property>
-                                                            <property name="receives_default">True</property>
-                                                            <signal name="clicked" 
handler="on_button_encoder_inertial_calibrate_clicked" swapped="no"/>
-                                                            </widget>
-                                                            <packing>
-                                                            <property name="expand">False</property>
-                                                            <property name="fill">False</property>
-                                                            <property name="position">0</property>
-                                                            </packing>
-                                                            </child>
-                                                            <child>
                                                             <widget class="GtkHBox" 
id="hbox_encoder_capture_1_or_cont">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
@@ -14687,13 +14712,6 @@ Concentric</property>
                                                             <packing>
                                                             <property name="expand">True</property>
                                                             <property name="fill">True</property>
-                                                            <property name="position">1</property>
-                                                            </packing>
-                                                            </child>
-                                                            </widget>
-                                                            <packing>
-                                                            <property name="expand">False</property>
-                                                            <property name="fill">False</property>
                                                             <property name="position">0</property>
                                                             </packing>
                                                             </child>
@@ -18485,6 +18503,9 @@ Concentric</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
diff --git a/src/Makefile.am b/src/Makefile.am
index fa0dd69..584f3ff 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -132,6 +132,7 @@ SOURCES = \
        config.cs\
        encoder.cs\
        encoderCapture.cs\
+       encoderCaptureInertialBG.cs\
        encoderRProc.cs\
        executeAuto.cs\
        event.cs\
diff --git a/src/encoderCapture.cs b/src/encoderCapture.cs
index 9675076..a69a6de 100644
--- a/src/encoderCapture.cs
+++ b/src/encoderCapture.cs
@@ -84,6 +84,7 @@ public abstract class EncoderCapture
 
        protected static SerialPort sp;
        protected bool finish;
+       protected bool capturingInertialBG;
 
        //capture is simulated (a signal file is readed)
        private static bool simulated = false;
@@ -94,44 +95,18 @@ public abstract class EncoderCapture
        // ---- private stuff ----
        private bool cancel;
 
-       /*
-       public static bool CheckPort(string port)
-       {
-               if(File.Exists(UtilAll.GetECapSimSignalFileName())) { //simulatedEncoder
-                       simulated = true;
-                       return true;
-               }
-
-               simulated = false;
-
-               LogB.Information("testing encoder port: ", port);
-               sp = new SerialPort(port);
-               sp.BaudRate = 115200;
-               LogB.Information("testing 1: sp created");
-               try {
-                       sp.Open();
-                       LogB.Information("testing 2: sp opened");
-                       sp.Close();
-                       LogB.Information("testing 3: sp closed. Success!");
-               } catch {
-                       LogB.Error("testing encoder port failed");
-                       return false;
-               }
-               return true;
-       }
-       */
-
 
        //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)
+       public void InitGlobal (int widthG, int heightG, int time, int timeEnd, bool cont, string eccon, 
string port, bool capturingInertialBG)
        {
                this.widthG = widthG;
                this.heightG = heightG;
                this.cont = cont;
                this.eccon = eccon;
+               this.capturingInertialBG = capturingInertialBG;
                
                //---- a) open port -----
-               if(! simulated) {
+               if(! simulated && ! capturingInertialBG) {
                        LogB.Debug("runEncoderCaptureCsharp start port:", port);
                        sp = new SerialPort(port);
                        sp.BaudRate = 115200;
@@ -445,7 +420,7 @@ public abstract class EncoderCapture
                //leave some time to capture.R be able to paint data, and to create two Roptions.txt file 
correctly
                if(simulated)
                        System.Threading.Thread.Sleep(2000);
-               else 
+               else if(! capturingInertialBG)
                        sp.Close();
 
                if(cancel)
@@ -469,12 +444,15 @@ public abstract class EncoderCapture
        }
 
 
-       protected virtual int readByte()
+       protected int readByte()
        {
                if(simulated) {
                        return simulatedInts[simulatedCount ++];
                } else {
-                       return sp.ReadByte();
+                       if(capturingInertialBG)
+                               return EncoderCaptureInertialBackgroundStatic.GetNext();
+                       else
+                               return sp.ReadByte();
                }
        }
        protected int convertByte(int b)
@@ -630,7 +608,7 @@ public class EncoderCaptureGravitatory : EncoderCapture
        public EncoderCaptureGravitatory() 
        {
        }
-       
+
        protected override void initSpecific()
        {
                realHeightG = 2 * 1000 ; //1 meter up / 1 meter down
@@ -652,7 +630,7 @@ public class EncoderCaptureInertial : EncoderCapture
        public EncoderCaptureInertial() 
        {
        }
-       
+
        protected override void initSpecific()
        {
                realHeightG = 2 * 5000 ; //5 meters up / 5 meters down
@@ -834,51 +812,3 @@ 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/encoderCaptureInertialBG.cs b/src/encoderCaptureInertialBG.cs
new file mode 100644
index 0000000..59487c3
--- /dev/null
+++ b/src/encoderCaptureInertialBG.cs
@@ -0,0 +1,137 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or   
+ *    (at your option) any later version.
+ *    
+ * ChronoJump is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ *    GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  Copyright (C) 2017   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System;
+using System.IO.Ports;
+using System.Collections.Generic; //List<T>
+
+/*
+ * this class allows reading always in order to know the angle of the string and the change of direction
+ */
+public class EncoderCaptureInertialBackground
+{
+       private int angleNow;
+       private bool finishBG;
+       public bool StoreData;
+       private SerialPort sp;
+
+       public EncoderCaptureInertialBackground(string port)
+       {
+               angleNow = 0;
+               finishBG = false;
+               StoreData = false;
+               EncoderCaptureInertialBackgroundStatic.Start();
+
+               sp = new SerialPort(port);
+               sp.BaudRate = 115200;
+               LogB.Information("sp created");
+       }
+
+       public bool CaptureBG()
+       {
+               LogB.Information("CaptureBG!");
+               sp.Open();
+               LogB.Information("sp opened");
+
+               int byteReaded;
+               do {
+                       try {
+                               byteReaded = readByte();
+                       } catch {
+                               LogB.Error("ERROR at InertialCaptureBackground: Maybe encoder cable is 
disconnected");
+                               return false;
+                       }
+
+                       byteReaded = convertByte(byteReaded);
+                       angleNow += byteReaded;
+
+                       if(StoreData)
+                               EncoderCaptureInertialBackgroundStatic.ListCaptured.Add(byteReaded);
+                       //LogB.Information("angleNow = " + angleNow.ToString());
+               } while (! finishBG);
+
+               sp.Close();
+               return true;
+       }
+
+       private int readByte()
+       {
+               return sp.ReadByte();
+       }
+
+       private int convertByte(int b)
+       {
+               if(b > 128)
+                       b = b - 256;
+
+               return b;
+       }
+
+       public int AngleNow
+       {
+               get { return angleNow; }
+       }
+
+       public void FinishBG()
+       {
+               finishBG = true;
+       }
+
+}
+
+/*
+ * Readed by EncoderCaptureInertial when calibration is done
+ */
+public static class EncoderCaptureInertialBackgroundStatic
+{
+       public static List<int> ListCaptured;
+       private static int pos;
+
+       //abort allow to finish the capture process and don't get waiting GetNext forever
+       private static bool abort;
+
+       public static void Start()
+       {
+               ListCaptured = new List<int> ();
+               pos = 0;
+               abort = false;
+       }
+
+       //TODO: write nicer
+       public static int GetNext()
+       {
+               if(abort)
+                       return 0;
+
+               do {
+                       if(ListCaptured.Count > pos)
+                               return ListCaptured[pos ++];
+
+                       System.Threading.Thread.Sleep(25);
+               } while (! abort);
+
+               return 0;
+       }
+
+       public static void Abort()
+       {
+               abort = true;
+       }
+}
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 798c464..5ab0a21 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2282,11 +2282,13 @@ public partial class ChronoJumpWindow
 
                updatingRestTimes = false;
 
-               /*
-               if(chronopicWin.Connected == true) {
-                       chronopicWin.SerialPortsCloseIfNeeded();
+               //if capturing on the background finish it
+               if(eCaptureInertialBG != null)
+               {
+                       eCaptureInertialBG.FinishBG();
+                       EncoderCaptureInertialBackgroundStatic.Abort();
                }
-               */
+
                cp2016.SerialPortsCloseIfNeeded(true);
 
                //exit start ping if has not ended
@@ -3101,6 +3103,7 @@ public partial class ChronoJumpWindow
                                        radiobutton_encoder_analyze_neuromuscular_profile.Visible = true;
                                }
                                hbox_encoder_capture_1_or_cont.Visible = true;
+                               hbox_encoder_inertial_calibrate.Visible = false;
                        } else {
                                menuitem_mode_selected_power_inertial.Visible = true;
 
@@ -3123,6 +3126,7 @@ public partial class ChronoJumpWindow
                                
                                radio_encoder_capture_1set.Active = true;
                                hbox_encoder_capture_1_or_cont.Visible = false;
+                               hbox_encoder_inertial_calibrate.Visible = true;
                        }
                        encoderGuiChangesAfterEncoderConfigurationWin(true);
                        if(changed) {
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 97263f4..c2b2814 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -60,8 +60,10 @@ public partial class ChronoJumpWindow
        
        [Widget] Gtk.Box hbox_encoder_capture_wait;
        [Widget] Gtk.Box vbox_encoder_capture_doing;
+       [Widget] Gtk.HScale hscale_encoder_capture_inertial_angle_now;
        
        [Widget] Gtk.Box hbox_encoder_capture_1_or_cont;
+       [Widget] Gtk.Box hbox_encoder_inertial_calibrate;
        [Widget] Gtk.RadioButton radio_encoder_capture_1set;
        [Widget] Gtk.RadioButton radio_encoder_capture_cont;
        [Widget] Gtk.Button button_encoder_capture;
@@ -261,7 +263,7 @@ public partial class ChronoJumpWindow
        Gtk.ListStore encoderAnalyzeListStore; //can be EncoderCurves or EncoderNeuromuscularData
 
        Thread encoderThread;
-       //Thread encoderThreadBG;
+       Thread encoderThreadBG;
        
 
        int image_encoder_width;
@@ -615,20 +617,24 @@ public partial class ChronoJumpWindow
        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
+               //check if chronopics have changed
+               if(! canCaptureEncoder())
+                       return;
+
                encoderThreadStart(encoderActions.CAPTURE_BG);
        }
 
        double maxPowerIntersessionOnCapture;
        void on_button_encoder_capture_clicked (object o, EventArgs args) 
        {
-               if(eCaptureInertialBG != null)
-                       eCaptureInertialBG.Finish();
+//             if(eCaptureInertialBG != null)
+//                     eCaptureInertialBG.Finish();
 
                maxPowerIntersessionOnCapture = findMaxPowerIntersession();
                //LogB.Information("maxPower: " + maxPowerIntersessionOnCapture);
 
-               //check if chronopics have changed
-               if(! canCaptureEncoder())
+               //if we are not capturing on the background, check if chronopics have changed
+               if( ! (encoderThreadBG != null && encoderThreadBG.IsAlive) && ! canCaptureEncoder() )
                        return;
 
                if(encoderConfigurationCurrent.has_inertia)
@@ -4553,13 +4559,11 @@ public partial class ChronoJumpWindow
                {
                        eCaptureInertialBG = new EncoderCaptureInertialBackground(
                                        
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port);
-                       //encoderThreadBG = new Thread(new ThreadStart(encoderDoCaptureBG));
-                       encoderThread = new Thread(new ThreadStart(encoderDoCaptureBG));
+                       encoderThreadBG = new Thread(new ThreadStart(encoderDoCaptureBG));
                        GLib.Idle.Add (new GLib.IdleHandler (pulseGTKEncoderCaptureBG));
 
                        LogB.ThreadStart();
-                       //encoderThreadBG.Start();
-                       encoderThread.Start();
+                       encoderThreadBG.Start();
                }
 
                else if(action == encoderActions.CAPTURE || action == encoderActions.CAPTURE_IM)
@@ -4634,11 +4638,13 @@ public partial class ChronoJumpWindow
                                                preferences.encoderCaptureInactivityEndTime,
                                                radio_encoder_capture_cont.Active,
                                                findEccon(true),
-                                               
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port
+                                               
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port,
+                                               (encoderConfigurationCurrent.has_inertia && 
eCaptureInertialBG != null)
                                                );
 
                                if(encoderConfigurationCurrent.has_inertia && eCaptureInertialBG != null)
                                {
+                                       eCaptureInertialBG.StoreData = true;
                                        eCapture.InitCalibrated(eCaptureInertialBG.AngleNow);
                                }
 
@@ -4655,7 +4661,8 @@ public partial class ChronoJumpWindow
                                                preferences.encoderCaptureInactivityEndTime,
                                                false,
                                                findEccon(true),
-                                               
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port
+                                               
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port,
+                                               false
                                                );
 
                                encoderThread = new Thread(new ThreadStart(encoderDoCaptureCsharpIM));
@@ -4997,14 +5004,24 @@ public partial class ChronoJumpWindow
 
        private bool pulseGTKEncoderCaptureBG ()
        {
-               //if(! encoderThreadBG.IsAlive) {
-               if(! encoderThread.IsAlive) {
+               if(! encoderThreadBG.IsAlive) {
                        return false;
                }
 
-               Thread.Sleep (100);
-               //LogB.Information(" CapBG:"+ encoderThreadBG.ThreadState.ToString());
-               LogB.Information(" CapBG:"+ encoderThread.ThreadState.ToString());
+               //resize hscale if needed
+               int newValue = eCaptureInertialBG.AngleNow;
+               int lower = Convert.ToInt32(hscale_encoder_capture_inertial_angle_now.Adjustment.Lower);
+               int upper = Convert.ToInt32(hscale_encoder_capture_inertial_angle_now.Adjustment.Upper);
+
+               if(newValue < lower || newValue > upper)
+                       hscale_encoder_capture_inertial_angle_now.SetRange(lower * 2, upper *2);
+
+               //update hscale value
+               hscale_encoder_capture_inertial_angle_now.Value = eCaptureInertialBG.AngleNow;
+
+               Thread.Sleep (50);
+               LogB.Information(" CapBG:"+ encoderThreadBG.ThreadState.ToString());
+
                return true;
        }
                                
@@ -5019,12 +5036,17 @@ public partial class ChronoJumpWindow
                        prepareEncoderGraphs(false, false); //do not erase them
                        needToCallPrepareEncoderGraphs = false;
                }
-                       
-               if(! encoderThread.IsAlive || encoderProcessCancel) {
+
+               if(! encoderThread.IsAlive || encoderProcessCancel)
+               {
                        LogB.Information("End from capture"); 
-                       LogB.ThreadEnding(); 
+                       LogB.ThreadEnding();
+
+                       if(eCaptureInertialBG != null)
+                               eCaptureInertialBG.StoreData = false;
+
                        finishPulsebar(encoderActions.CURVES_AC);
-                       
+
                        if(encoderProcessCancel) {
                                //stop video            
                                encoderStopVideoRecord();


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