[chronojump] encoder inertial capture (calibrated) can be simulated from guiTests.cs



commit 4e6bd6ebcb6c9798c600c800ea380cab2c74ad11
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Apr 10 19:09:52 2017 +0200

    encoder inertial capture (calibrated) can be simulated from guiTests.cs

 src/encoderCapture.cs           |   25 ++++++++++----
 src/encoderCaptureInertialBG.cs |   68 ++++++++++++++++++++++++++++++++++----
 src/gui/chronojump.cs           |    3 +-
 src/gui/encoder.cs              |   44 +++++++++++++++++++------
 src/gui/guiTests.cs             |   57 +++++++++++++++++++++++++++++++--
 5 files changed, 167 insertions(+), 30 deletions(-)
---
diff --git a/src/encoderCapture.cs b/src/encoderCapture.cs
index f559a86..17c0818 100644
--- a/src/encoderCapture.cs
+++ b/src/encoderCapture.cs
@@ -100,9 +100,9 @@ public abstract class EncoderCapture
        protected bool inertialCalibrated;
 
        //capture is simulated (a signal file is readed)
-       private static bool simulated = false;
-       private int [] simulatedInts;
-       private int simulatedCount;
+       private bool simulated = false;
+       //private int [] simulatedInts;
+       //private int simulatedCount;
 
        
        // ---- private stuff ----
@@ -110,7 +110,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, bool capturingInertialBG, bool showOnlyBars)
+       public void InitGlobal (int widthG, int heightG, int time, int timeEnd, bool cont, string eccon, 
string port, bool capturingInertialBG, bool showOnlyBars, bool simulated)
        {
                this.widthG = widthG;
                this.heightG = heightG;
@@ -118,7 +118,8 @@ public abstract class EncoderCapture
                this.eccon = eccon;
                this.capturingInertialBG = capturingInertialBG;
                this.showOnlyBars = showOnlyBars;
-               
+               this.simulated = simulated;
+
                //---- a) open port -----
                if(! simulated && ! capturingInertialBG) {
                        LogB.Debug("runEncoderCaptureCsharp start port:", port);
@@ -210,11 +211,14 @@ public abstract class EncoderCapture
 
        public bool Capture(string outputData1, EncoderRProcCapture encoderRProcCapture, bool compujump)
        {
+               /*
+                * removed at 1.7.0
                if(simulated) {
                        bool success = initSimulated();
                        if(! success)
                                return false;
                }
+               */
 
                inertialCalibratedFirstCross0Pos = 0;
 
@@ -528,6 +532,8 @@ public abstract class EncoderCapture
                return true;
        }
 
+       /*
+        * removed at 1.7.0
        private bool initSimulated()
        {
                if(! File.Exists(UtilAll.GetECapSimSignalFileName()))
@@ -537,18 +543,22 @@ public abstract class EncoderCapture
                simulatedInts = Util.ReadFileAsInts(filename);
                return true;
        }
+       */
 
 
        protected int readByte()
        {
+               /*
+                * removed at 1.7.0
                if(simulated) {
                        return simulatedInts[simulatedCount ++];
                } else {
+               */
                        if(capturingInertialBG)
                                return EncoderCaptureInertialBackgroundStatic.GetNext();
                        else
                                return sp.ReadByte();
-               }
+               //}
        }
        protected int convertByte(int b)
        {
@@ -700,7 +710,6 @@ public abstract class EncoderCapture
        public void Finish() {
                finish = true;
        }
-       
 }
 
 
@@ -896,9 +905,11 @@ public class EncoderCaptureInertial : EncoderCapture
                else
                        encoderReadedInertialDisc = trimInitialZeros(encoderReadedInertialDisc);
 
+               LogB.Information("Saving to disk");
                string sep = "";
                foreach(int k in encoderReadedInertialDisc) {
                        writer.Write(sep + k); //store the raw file (before encoderConfigurationConversions)
+                       //LogB.Information(sep + k);
                        sep = ", ";
                }
 
diff --git a/src/encoderCaptureInertialBG.cs b/src/encoderCaptureInertialBG.cs
index e76e431..409cfdf 100644
--- a/src/encoderCaptureInertialBG.cs
+++ b/src/encoderCaptureInertialBG.cs
@@ -31,7 +31,9 @@ public class EncoderCaptureInertialBackground
        private bool finishBG;
        public bool StoreData;
        private SerialPort sp;
+       private Random rand;
 
+       //send port == "" for simulated
        public EncoderCaptureInertialBackground(string port)
        {
                angleNow = 0;
@@ -39,21 +41,34 @@ public class EncoderCaptureInertialBackground
                StoreData = false;
                EncoderCaptureInertialBackgroundStatic.Start();
 
-               sp = new SerialPort(port);
-               sp.BaudRate = 115200;
-               LogB.Information("sp created");
+               if(port != "")
+               {
+                       sp = new SerialPort(port);
+                       sp.BaudRate = 115200;
+                       LogB.Information("sp created");
+               }
        }
 
-       public bool CaptureBG()
+       public bool CaptureBG(bool simulated)
        {
                LogB.Information("CaptureBG!");
-               sp.Open();
-               LogB.Information("sp opened");
+               if(simulated)
+               {
+                       rand = new Random(40);
+                       SimulatedReset();
+               }
+               else {
+                       sp.Open();
+                       LogB.Information("sp opened");
+               }
 
                int byteReaded;
                do {
                        try {
-                               byteReaded = readByte();
+                               if(simulated)
+                                       byteReaded = simulateByte();
+                               else
+                                       byteReaded = readByte();
                        } catch {
                                LogB.Error("ERROR at InertialCaptureBackground: Maybe encoder cable is 
disconnected");
                                return false;
@@ -67,7 +82,9 @@ public class EncoderCaptureInertialBackground
                        //LogB.Information("angleNow = " + angleNow.ToString());
                } while (! finishBG);
 
-               sp.Close();
+               if(! simulated)
+                       sp.Close();
+
                return true;
        }
 
@@ -76,6 +93,41 @@ public class EncoderCaptureInertialBackground
                return sp.ReadByte();
        }
 
+       private bool simulatedGoingUp = false;
+       private int simulatedMaxValue = 400;
+       private int simulatedLength;
+       private int simulatedMaxLength = 4000; //when signal stops
+
+       public void SimulatedReset()
+       {
+               simulatedLength = 0;
+       }
+
+       private int simulateByte()
+       {
+               System.Threading.Thread.Sleep(1);
+
+               //return 0's to end if signal needs to end
+               simulatedLength ++;
+               if(simulatedLength > simulatedMaxLength)
+                       return 0;
+
+               //get new value
+               int simValue;
+               if(simulatedGoingUp)
+                       simValue = rand.Next(0, 4);
+               else
+                       simValue = rand.Next(-4, 0);
+
+               //change direction if needed
+               if(simulatedGoingUp && angleNow > simulatedMaxValue)
+                       simulatedGoingUp = false;
+               else if(! simulatedGoingUp && angleNow < -1 * simulatedMaxValue)
+                       simulatedGoingUp = true;
+
+               return simValue;
+       }
+
        private int convertByte(int b)
        {
                if(b > 128)
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index ccaea3f..56d7af4 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -6255,7 +6255,8 @@ LogB.Debug("X");
        private void on_button_gui_tests_clicked (object o, EventArgs args) {
                chronojumpWindowTestsStart(Convert.ToInt32(spin_gui_tests.Value),
                                //CJTests.SequenceChangeMultitest
-                               CJTests.SequenceRJsSimulatedFinishCancel
+                               //CJTests.SequenceRJsSimulatedFinishCancel
+                               CJTests.SequenceEncoderInertialCapture
                                );
        }
        
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 0e667df..09f1eee 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -626,6 +626,9 @@ public partial class ChronoJumpWindow
 
        bool canCaptureEncoder()
        {
+               if(currentSession.Name == Constants.SessionSimulatedName && testsActive)
+                       return true;
+
                chronopicRegisterUpdate(false);
                int numEncoders = chronopicRegister.NumConnectedOfType(ChronopicRegisterPort.Types.ENCODER);
                LogB.Information("numEncoders: " + numEncoders);
@@ -2193,7 +2196,9 @@ public partial class ChronoJumpWindow
        //I suppose reading gtk is ok, changing will be the problem
        private void encoderDoCaptureBG ()
        {
-               eCaptureInertialBG.CaptureBG();
+               eCaptureInertialBG.CaptureBG(
+                       currentSession.Name == Constants.SessionSimulatedName && testsActive
+                       );
        }
 
        private void stopCapturingInertialBG()
@@ -2218,6 +2223,10 @@ public partial class ChronoJumpWindow
                //wait to ensure capture thread has ended
                Thread.Sleep(50);       
                
+               //on simulated sleep more to ensure data is written to disc
+               if(currentSession.Name == Constants.SessionSimulatedName && testsActive)
+                       Thread.Sleep(1500);
+
                LogB.Debug("Going to stop");            
                capturingCsharp = encoderCaptureProcess.STOPPING;
 
@@ -4718,8 +4727,12 @@ public partial class ChronoJumpWindow
                        calledCaptureInertial = false;
                        timeCalibrated = DateTime.Now;
 
-                       eCaptureInertialBG = new EncoderCaptureInertialBackground(
-                                       
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port);
+                       if(currentSession.Name == Constants.SessionSimulatedName && testsActive)
+                               eCaptureInertialBG = new EncoderCaptureInertialBackground("");
+                       else
+                               eCaptureInertialBG = new EncoderCaptureInertialBackground(
+                                               
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port);
+
                        encoderThreadBG = new Thread(new ThreadStart(encoderDoCaptureBG));
                        GLib.Idle.Add (new GLib.IdleHandler (pulseGTKEncoderCaptureBG));
 
@@ -4793,6 +4806,10 @@ public partial class ChronoJumpWindow
                                        encoderProcessFinishContMode = false; //will be true when finish 
button is pressed
                                }
 
+                               string portName = "";
+                               if( ! (currentSession.Name == Constants.SessionSimulatedName && testsActive))
+                                       portName = 
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port;
+
                                eCapture.InitGlobal(
                                                encoder_capture_signal_drawingarea.Allocation.Width,
                                                encoder_capture_signal_drawingarea.Allocation.Height,
@@ -4800,15 +4817,19 @@ public partial class ChronoJumpWindow
                                                preferences.encoderCaptureInactivityEndTime,
                                                radio_encoder_capture_cont.Active,
                                                findEccon(true),
-                                               
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port,
+                                               portName,
                                                (encoderConfigurationCurrent.has_inertia && 
eCaptureInertialBG != null),
-                                               configChronojump.EncoderCaptureShowOnlyBars
+                                               configChronojump.EncoderCaptureShowOnlyBars,
+                                               currentSession.Name == Constants.SessionSimulatedName && 
testsActive
                                                );
 
                                if(encoderConfigurationCurrent.has_inertia && eCaptureInertialBG != null)
                                {
                                        eCaptureInertialBG.StoreData = true;
                                        eCapture.InitCalibrated(eCaptureInertialBG.AngleNow);
+
+                                       if(currentSession.Name == Constants.SessionSimulatedName && 
testsActive)
+                                               eCaptureInertialBG.SimulatedReset();
                                }
 
                                encoderThread = new Thread(new ThreadStart(encoderDoCaptureCsharp));
@@ -4826,6 +4847,7 @@ public partial class ChronoJumpWindow
                                                findEccon(true),
                                                
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ENCODER).Port,
                                                false,
+                                               false,
                                                false
                                                );
 
@@ -5911,10 +5933,7 @@ public partial class ChronoJumpWindow
                                restTime.AddOrModify(currentPerson.UniqueID, true);
                                updateRestTimes();
                        }
-               
-                       if(action == encoderActions.CURVES_AC && radio_encoder_capture_cont.Active && ! 
encoderProcessFinishContMode)
-                               on_button_encoder_capture_clicked_do (false);
-                       
+
                        //on inertial, check after capture if string was not fully extended and was corrected
                        if(getMenuItemMode() == Constants.Menuitem_modes.POWERINERTIAL && 
                                        action == encoderActions.CURVES_AC && 
@@ -5925,7 +5944,6 @@ public partial class ChronoJumpWindow
                                        new DialogMessage(Constants.MessageTypes.WARNING, 
                                                Catalog.GetString("Set corrected. string was not fully 
extended at the beginning."));
                        }
-               
                } else { //ANALYZE
                        if(encoderProcessCancel) {
                                encoder_pulsebar_analyze.Text = Catalog.GetString("Cancelled");
@@ -6027,11 +6045,15 @@ public partial class ChronoJumpWindow
                Util.FileDelete(UtilEncoder.GetEncoderStatusTempBaseFileName() + "5.txt");
                Util.FileDelete(UtilEncoder.GetEncoderStatusTempBaseFileName() + "6.txt");
                        
+               if(action == encoderActions.CURVES_AC && radio_encoder_capture_cont.Active && ! 
encoderProcessFinishContMode)
+                       on_button_encoder_capture_clicked_do (false);
+
                //for chronojumpWindowTests
                LogB.Information("finishPulseBar DONE: " + action.ToString());
                if(
                                action == encoderActions.LOAD ||        //load 
-                               action == encoderActions.CURVES )       //recalculate
+                               action == encoderActions.CURVES ||      //recalculate
+                               action == encoderActions.CURVES_AC)     //curves after capture
                        chronojumpWindowTestsNext();
        }
 
diff --git a/src/gui/guiTests.cs b/src/gui/guiTests.cs
index a9fd5be..a06ec48 100644
--- a/src/gui/guiTests.cs
+++ b/src/gui/guiTests.cs
@@ -58,6 +58,7 @@ public class CJTests
                CONTACTS_EXECUTE_TEST,
                ENCODER_SIGNAL_LOAD,
                ENCODER_ECC_CON_INVERT,
+               ENCODER_CAPTURE,
                ENCODER_RECALCULATE,
                ENCODER_SET_SAVE_REPS,
                ENCODER_SET_SAVE_REPS_BUCLE,
@@ -159,6 +160,38 @@ public class CJTests
                CJTests.Types.END
        };
 
+       /*
+        * howto simulate inertial capture
+        * open chronojump, inertial mode, load simulated session, preferences / debug,
+        * main menu: 70 (simulated session), click on guiTests
+        * go to inertial mode, click on calibrate
+        */
+       public static List<Types> SequenceEncoderInertialCapture = new List<Types>
+       {
+               CJTests.Types.MODE_POWERINERTIAL,
+//             CJTests.Types.SESSION_LOAD,
+//             CJTests.Types.BUCLE_1_ON,
+//                     CJTests.Types.PERSON_SELECT, //bucle1startPos //repeat from here //when all persons 
have done a jumps/runs test, will end
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+                       CJTests.Types.ENCODER_CAPTURE,
+//             CJTests.Types.BUCLE_1_OFF,
+               CJTests.Types.END
+       };
+
        public CJTests(List<Types> sequence)
        {
                this.sequence = sequence;
@@ -267,29 +300,35 @@ public partial class ChronoJumpWindow
        
        public void chronojumpWindowTestsNext() 
        {
+               LogB.Information("At chronojumpWindowTestsNext 0");
                if(! testsActive)
                        return;
 
+               LogB.Information("At chronojumpWindowTestsNext 1");
                //needed for SequenceContactsExecute50_10 (but doesn't work properly). Find other solution:
                //http://www.mono-project.com/docs/gui/gtksharp/responsive-applications/
                //
                //force GUI update
                //System.Threading.Thread.Sleep(250); //ok there's an sleep, but screen is not updated. Maybe 
need to open a new thread before each next call to ensure gui is updated
                //http://mono.1490590.n4.nabble.com/Force-UI-Update-td3488126.html
-               while (Gtk.Application.EventsPending ())
-                               Gtk.Application.RunIteration ();
-               //System.Threading.Thread.Sleep(250); //ok there's an sleep, but screen is not updated. Maybe 
need to open a new thread before each next call to ensure gui is updated
+               //this works, and is used on 1.7.0, but does not work after ENCODER_CAPTURE test
+               //while (Gtk.Application.EventsPending ())
+               //              Gtk.Application.RunIteration ();
+               System.Threading.Thread.Sleep(250); //ok there's an sleep, but screen is not updated. Maybe 
need to open a new thread before each next call to ensure gui is updated
 
+               LogB.Information("At chronojumpWindowTestsNext 2");
                if(cjTest.Next())
                        chronojumpWindowTestsDo();
                else {
                        testsActive = false;
                        testsReport();
                }
+               LogB.Information("At chronojumpWindowTestsNext 3");
        }
 
        private void chronojumpWindowTestsDo() 
        {
+               LogB.Information("At chronojumpWindowTestsDo");
                //if process is very fast (no threads, no GUI problems) just call next from this method
                bool callNext = false;
                
@@ -347,6 +386,9 @@ public partial class ChronoJumpWindow
                                chronojumpWindowTestsEncoderEccConInvert();
                                callNext = true;
                                break;
+                       case CJTests.Types.ENCODER_CAPTURE:
+                               chronojumpWindowTestsEncoderCapture();
+                               break;
                        case CJTests.Types.ENCODER_RECALCULATE:
                                chronojumpWindowTestsEncoderRecalculate();
                                break;
@@ -539,6 +581,15 @@ public partial class ChronoJumpWindow
                LogB.TestEnd("chronojumpWindowTestsEncoderEccConInvert");
        }
        
+       private void chronojumpWindowTestsEncoderCapture()
+       {
+               LogB.TestStart("chronojumpWindowTestsEncoderCapture");
+
+               on_button_encoder_capture_clicked (new Object (), new EventArgs ());
+
+               LogB.TestEnd("chronojumpWindowTestsEncoderCapture");
+       }
+
        private void chronojumpWindowTestsEncoderRecalculate()
        {
                LogB.TestStart("chronojumpWindowTestsEncoderRecalculate");


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