[chronojump] run double contacts manages better start. Need to refine code and read TODOs!



commit d1107f8d9bc766a14ffaa40648e35417f3a0039d
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Apr 12 17:16:33 2018 +0200

    run double contacts manages better start. Need to refine code and read TODOs!

 src/execute/run.cs        |   24 ++++++-
 src/execute/runObjects.cs |  153 ++++++++++++++++++++++++++++++++++++++-------
 src/gui/eventExecute.cs   |   52 ++++++++++++++--
 3 files changed, 198 insertions(+), 31 deletions(-)
---
diff --git a/src/execute/run.cs b/src/execute/run.cs
index 32efe9b..7e4740b 100644
--- a/src/execute/run.cs
+++ b/src/execute/run.cs
@@ -241,6 +241,7 @@ public class RunExecute : EventExecute
                                );
                runPTL = new RunPhaseTimeList();
 
+               bool firstFromChronopicReceived = false;
                bool exitWaitEventBucle = false;
                do {
                        if(simulated)
@@ -250,6 +251,12 @@ public class RunExecute : EventExecute
                        
                        if (ok && ! cancel && ! finish)
                        {
+                               if( ! firstFromChronopicReceived )
+                               {
+                                       runDC.SpeedStart = has_arrived();
+                                       firstFromChronopicReceived = true;
+                               }
+
                                onlyInterval_CalculateDistanceIntervalFixed();
 
                                //LogB.Information("timestamp:" + timestamp);
@@ -332,6 +339,7 @@ public class RunExecute : EventExecute
                                                        lastTc = timestamp / 1000.0;
                                                        runEI.ChangePhase(RunExecuteInspector.Phases.OUT,
                                                                string.Format("SpeedStartArrival, tc = {0}", 
Math.Round(lastTc, 3)));
+                                                       runDC.DoneTC(timestamp);
                                                        runPTL.AddTC(timestamp);
                                                }
 
@@ -406,6 +414,7 @@ public class RunExecute : EventExecute
                        {
                                LogB.Information("WAITING 100 MS TO EXIT BUCLE");
                                //TODO: checks what happens with cancel... in the pulse thread, will change 
this boolean? needCheckIfTrackEnded
+                               //TODO: also think on what happens if needCheckIfTrackEnded never gets 
negative
                                Thread.Sleep(100);
                        }
 
@@ -422,7 +431,6 @@ public class RunExecute : EventExecute
                        runDC.UpdateList();
        }
 
-       //this will be protected and in run simple execute class
        protected override bool lastTfCheckTimeEnded()
        {
                LogB.Information("In lastTfCheckTimeEnded()");
@@ -452,13 +460,23 @@ public class RunExecute : EventExecute
                        trackTime = lastTc + lastTf/1000.0;
                }
 
+               //check if start is double contact
+               if(trackTime < 0)
+               {
+                       if( ! runDC.SpeedStart )
+                               runPTL.FirstRPI = 0;
+                       else
+                               runPTL.FirstRPI = Convert.ToInt32(Math.Abs(trackTime)); //TODO: take care, is 
better GetTrackTimInSeconds... returns some int, read another value, or if trackTime == -1 then read a 
variable that will come with the FirstRPI value
+
+                       return;
+               }
+
                LogB.Information("trackTime: " + trackTime.ToString());
                //solve possible problems of bad copied data between threads on start
                if(trackTime == 0)
                        return;
 
-               //runEI.ChangePhase(RunExecuteInspector.Phases.IN, runEIString +
-               runEI.ChangePhase(RunExecuteInspector.Phases.IN, //runEIString +
+               runEI.ChangePhase(RunExecuteInspector.Phases.IN,
                                string.Format("; timestamp: {0}; <b>trackTime: {1}</b>",
                                        Math.Round(lastTf/1000.0, 3), Math.Round(trackTime, 3)));
 
diff --git a/src/execute/runObjects.cs b/src/execute/runObjects.cs
index 01aa892..35bf8c8 100644
--- a/src/execute/runObjects.cs
+++ b/src/execute/runObjects.cs
@@ -82,13 +82,76 @@ public class RunPhaseInfoManage
                        list.Add(listCaptureThread[i]);
        }
 
+       /*
+        * check double contacts at start:
+        * if ! speedStart: start at contact first contact will be TF (race starts at beginning)
+        * if   speedStart: start before contact (race starts related to biggest_TC
+        *      if(speedStartArrival)   first contact will be TC (start at beginning of biggest_TC)
+        *      else                    first contact will be TF (start at end of biggest_TC)
+        */
+       //return the double contact tc+tf at beginning
+       public int IsStartDoubleContact(bool speedStart, int checkTime)
+       {
+               if(! speedStart)
+                       return 0;
+
+               //--- from now on speedStart
+
+               bool first = true;
+               int startAt = 0;
+
+               double maxTCDuration = 0;
+               int maxTCPosition = 0;
+               bool speedStartArrival = true;
+
+               if(list.Count > 0)
+               {
+                       RunPhaseInfo firstRPI = (RunPhaseInfo) list[0];
+                       if(! firstRPI.IsContact()) //is TF
+                       {
+                               speedStartArrival = false;
+                               if (firstRPI.Duration < checkTime)
+                                       startAt = 1;
+                               else
+                                       return 0;
+                       }
+               }
+
+               RunPhaseInfo tcRPI;
+               RunPhaseInfo tfRPI;
+               for(int i = startAt +1; i < list.Count; i +=2)
+               {
+                       tcRPI = (RunPhaseInfo) list[i-1];
+                       tfRPI = (RunPhaseInfo) list[i];
+
+                       if(tcRPI.Duration > maxTCDuration)
+                       {
+                               maxTCDuration = tcRPI.Duration;
+                               maxTCPosition = i;
+                       }
+
+                       if(tcRPI.Duration + tfRPI.Duration >= checkTime)
+                       {
+                               if(speedStartArrival)
+                                       return maxTCPosition -1;
+                               else
+                                       return maxTCPosition;
+                       }
+               }
+
+               if(speedStartArrival)
+                       return maxTCPosition -1;
+               else
+                       return maxTCPosition;
+       }
+
        public int GetPosOfBiggestTC ()
        {
                LogB.Information("startPos at GetPosOfBiggestTC: " + startPos.ToString());
 
                //Read below message: "Message oneTCAfterTheTf"
                if(countTCs() == 1 && oneTCAfterTheTf())
-                       return 1;
+                       return startPos +1;
 
                //if there's no tc, return -1, track duration will be tf duration
                //if there's one tc, return -1, track duration will be tc+tf duration
@@ -160,6 +223,7 @@ public class RunPhaseInfoManage
                return str;
        }
 
+       //note it starts at startPos
        private int countTCs()
        {
                int countStart = 0;
@@ -179,7 +243,7 @@ public class RunPhaseInfoManage
         */
        private bool oneTCAfterTheTf()
        {
-               if(list.Count != 2)
+               if(list.Count - startPos != 2)
                        return false;
 
                RunPhaseInfo first = (RunPhaseInfo) list[0];
@@ -207,6 +271,7 @@ public class RunDoubleContact
 {
        private Constants.DoubleContact mode;
        private int checkTime;
+       public bool SpeedStart; //comes with speed or started in contact with the photocell
 
        private RunPhaseInfoManage rpim;
 
@@ -216,6 +281,7 @@ public class RunDoubleContact
        private double timeAcumulated;
 
        private List<RunPhaseInfo> listCaptureThread; //this list contains TCs and TFs from capture thread
+       private bool firstTrackDone;
 
        //constructor ------------------------------------------
        public RunDoubleContact (Constants.DoubleContact mode, int checkTime)
@@ -227,6 +293,7 @@ public class RunDoubleContact
                timeAcumulated = 0;
                rpim = new RunPhaseInfoManage();
                listCaptureThread = new List<RunPhaseInfo>();
+               firstTrackDone = false;
        }
 
        //public methods ---------------------------------------
@@ -275,17 +342,29 @@ public class RunDoubleContact
        //this wait will be done by C#
        public double GetTrackTimeInSecondsAndUpdateStartPos()
        {
-               double trackTime = 0;
-               /*
-               if(mode == Constants.FoubleContact.FIRST)
-                       timestamp = getDCFirst();
-               else if(mode == Constants.FoubleContact.LAST)
-                       timestamp = getDCLast();
-               else if(mode == Constants.FoubleContact.AVERAGE)
-                       timestamp = getDCAverage();
-               else // if(mode == Constants.FoubleContact.BIGGEST_TC)
-               */
-                       trackTime = getDCBiggestTC(); //superhardcoded
+               if(! firstTrackDone)
+               {
+                       //count TCs coming from double contacts at start
+                       int countDC_TCsAtStart = rpim.IsStartDoubleContact(SpeedStart, checkTime);
+                       //should work for many double contacts at start
+                       LogB.Information(string.Format("IsStartDoubleContact: {0}", countDC_TCsAtStart));
+
+                       if(countDC_TCsAtStart > 0)
+                       {
+                               rpim.UpdateStartPos(countDC_TCsAtStart);
+
+                               firstTrackDone = true;
+                               return -1 * countDC_TCsAtStart;
+                       }
+                       else if(! SpeedStart)
+                       {
+                               firstTrackDone = true;
+                               return -1000; //TODO: fix this, separate GetTrackTimeIn... in two functions, 
don't return negative positions as double
+                       }
+               }
+
+               double trackTime = getDCBiggestTC();
+               firstTrackDone = true;
 
                //in seconds
                if(trackTime > 0)
@@ -319,7 +398,6 @@ public class RunDoubleContact
                        }
                }
 
-               //rpim.EmptyListUntilPos(bigTCPosition);
                rpim.UpdateStartPos(bigTCPosition);
 
                return sum;
@@ -331,9 +409,13 @@ public class RunPhaseTimeList
 {
        private List<PhaseTime> listPhaseTime;
 
+       //if there are double contacts at start, first run phase info (0) will not be used
+       public int FirstRPI;
+
        public RunPhaseTimeList()
        {
                listPhaseTime = new List<PhaseTime>();
+               FirstRPI = 0;
        }
        
        public void AddTC(double timestamp)
@@ -385,25 +467,52 @@ public class RunPhaseTimeList
                                startedIn = true;
                }
 
+               //if FirstRPI is 1 then inverted the startedIn
+               if(FirstRPI == 1)
+                       startedIn = ! startedIn;
+
                // 3) add elements to the list
                LogB.Information("InListForPainting foreach:");
-               bool firstTrack = true;
+               int count = 0;
+               double negativeValues = 0; //double contacts times at start
                foreach(PhaseTime pt in listPhaseTimeShallowCloned)
                {
                        LogB.Information(pt.ToString());
+
+                       if(FirstRPI > count)
+                       {
+                               negativeValues += pt.Duration/1000.0;
+                               LogB.Information("InListForPaainting negativeValues = " + 
negativeValues.ToString());
+                       }
+
                        if(pt.IsContact)
                                startInMS = currentMS;
                        else if(startInMS >= 0)
-                       {
                                list_in.Add(startInMS/1000.0 + ":" + currentMS/1000.0); //in seconds
-                               if(startedIn && firstTrack)
-                               {
-                                       currentMS = 0; //reset currentMS in order to synchronize correctly
-                                       firstTrack = false;
-                               }
-                       }
 
                        currentMS += Convert.ToInt32(pt.Duration);
+
+                       LogB.Information(string.Format("End of iteration: {0}, pt.IsContact: {1}, startInMS: 
{2}, currentMS: {3}",
+                                               count, pt.IsContact, startInMS, currentMS));
+
+                       count ++;
+
+               }
+
+               //manage the negative values
+               if(negativeValues > 0)
+               {
+                       LogB.Information("Fixing negative values (double contacts times at start)");
+                       for (int i = 0; i < list_in.Count; i ++)
+                       {
+                               LogB.Information(string.Format("PRE i: {0}, list_in[{0}]: {1}", i, 
list_in[i]));
+
+                               string [] strFull = list_in[i].Split(new char[] {':'});
+                               list_in[i] = (Convert.ToDouble(strFull[0]) - negativeValues).ToString() + ":" 
+
+                                       (Convert.ToDouble(strFull[1]) - negativeValues).ToString();
+
+                               LogB.Information(string.Format("POST i: {0}, list_in[{0}]: {1}", i, 
list_in[i]));
+                       }
                }
 
                return list_in;
diff --git a/src/gui/eventExecute.cs b/src/gui/eventExecute.cs
index 0b70793..69a7f30 100644
--- a/src/gui/eventExecute.cs
+++ b/src/gui/eventExecute.cs
@@ -23,6 +23,7 @@ using Gtk;
 using Glade;
 using System.Text; //StringBuilder
 using System.Collections; //ArrayList
+using System.Collections.Generic; //List
 using Mono.Unix;
 using Gdk; //for the EventMask
 using LongoMatch.Gui;
@@ -1464,6 +1465,23 @@ public partial class ChronoJumpWindow
                        }
                        */
 
+                       double negativePTLTime = 0;
+                       double timeTotalWithNegativePTL = timeTotal;
+
+                       List<string> runPTLInListForPainting = runPTL.InListForPainting();
+                       if(runPTLInListForPainting.Count > 0)
+                       {
+                               string [] inPTL0Full = runPTLInListForPainting[0].Split(new char[] {':'});
+                               double firstValue = Convert.ToDouble(inPTL0Full[0]);
+                               if(firstValue < 0)
+                               {
+                                       negativePTLTime = Math.Abs(firstValue);
+                                       timeTotalWithNegativePTL += negativePTLTime;
+                               }
+                       }
+                       LogB.Information("negativePTLTime: " + negativePTLTime);
+
+
                        //paint reference guide black and green if needed
                        drawGuideOrAVG(pen_black_discont, eventGraphConfigureWin.BlackGuide, alto, ancho, 
topMargin, bottomMargin, maxValue, minValue, guideWidthEnum.FULL);
                        drawGuideOrAVG(pen_green_discont, eventGraphConfigureWin.GreenGuide, alto, ancho, 
topMargin, bottomMargin, maxValue, minValue, guideWidthEnum.FULL);
@@ -1479,16 +1497,17 @@ public partial class ChronoJumpWindow
                        double myValue = 0;
 
                        foreach (string inPTL in runPTL.InListForPainting())
+                       //foreach (string inPTL in runPTLInListForPainting)
                        {
                                string [] inPTLFull = inPTL.Split(new char[] {':'});
                                int xStart = event_execute_rightMargin + Convert.ToInt32((ancho - 
2*event_execute_rightMargin) *
-                                                       (Convert.ToDouble(inPTLFull[0]) / timeTotal));
+                                                       (Convert.ToDouble(inPTLFull[0]) + negativePTLTime) / 
timeTotalWithNegativePTL);
 
                                int xEnd = event_execute_rightMargin + Convert.ToInt32((ancho - 
2*event_execute_rightMargin) *
-                                               (Convert.ToDouble(inPTLFull[1]) / timeTotal));
+                                               (Convert.ToDouble(inPTLFull[1]) + negativePTLTime) / 
timeTotalWithNegativePTL);
 
                                //don't plot the TCs after current track
-                               if(Convert.ToDouble(inPTLFull[0]) < timeTotal)
+                               if(Convert.ToDouble(inPTLFull[0]) + negativePTLTime < 
timeTotalWithNegativePTL)
                                        event_execute_pixmap.DrawRectangle(pen_gris, true,
                                                        new Rectangle (xStart, alto-bottomMargin-4, 
xEnd-xStart, 4));
                        }
@@ -1513,14 +1532,35 @@ public partial class ChronoJumpWindow
                                myValue = myDistance / myTimeDouble;
 
                                int xStart = event_execute_rightMargin;
-                               if(myTimeDoubleAccumulated - myTimeDouble > 0)
+                               if(myTimeDoubleAccumulated - myTimeDouble > 0 || negativePTLTime > 0)
                                        xStart = event_execute_rightMargin + Convert.ToInt32((ancho - 
2*event_execute_rightMargin) * 
-                                                       ((myTimeDoubleAccumulated - myTimeDouble) / 
timeTotal));
+                                                       (myTimeDoubleAccumulated + negativePTLTime - 
myTimeDouble) / timeTotalWithNegativePTL);
 
                                int xEnd = event_execute_rightMargin;
                                if(myTimeDoubleAccumulated > 0)
                                        xEnd = event_execute_rightMargin + Convert.ToInt32((ancho - 
2*event_execute_rightMargin) * 
-                                                       (myTimeDoubleAccumulated / timeTotal));
+                                                       (myTimeDoubleAccumulated + negativePTLTime) / 
timeTotalWithNegativePTL);
+
+                               /*
+                               LogB.Information(string.Format("ancho: {0}, (ancho - 
2*event_execute_rightMargin): {1}, timeTotal: {2}, timeTotalWithNegativePTL: {3}",
+                                                       ancho, (ancho - 2*event_execute_rightMargin), 
timeTotal, timeTotalWithNegativePTL ));
+
+                               LogB.Information(string.Format("xStart: {0}, b: {1}, c: {2}, num: {3}, 
numDenom: {4}, numDenom*anchoright: {5}, xStartCalc: {6}, xStart: {7}",
+                                                       myTimeDoubleAccumulated, negativePTLTime, 
myTimeDouble,
+                                                       (myTimeDoubleAccumulated + negativePTLTime - 
myTimeDouble),
+                                                       (myTimeDoubleAccumulated + negativePTLTime - 
myTimeDouble) / timeTotalWithNegativePTL,
+                                                       Convert.ToInt32((ancho - 2*event_execute_rightMargin) 
* (myTimeDoubleAccumulated + negativePTLTime) / timeTotalWithNegativePTL),
+                                                       event_execute_rightMargin + Convert.ToInt32((ancho - 
2*event_execute_rightMargin) *
+                                                       (myTimeDoubleAccumulated + negativePTLTime - 
myTimeDouble) / timeTotalWithNegativePTL),
+                                                       xStart
+                                                       ));
+                               LogB.Information(string.Format("xEnd: {0}, b: {1}, num: {2}, numDenom: {3}, 
xEnd: {4}",
+                                                       myTimeDoubleAccumulated, negativePTLTime,
+                                                       (myTimeDoubleAccumulated + negativePTLTime),
+                                                       (myTimeDoubleAccumulated + negativePTLTime) / 
timeTotalWithNegativePTL,
+                                                       xEnd
+                                                       ));
+                               */
 
                                //LogB.Information("xStart: " + xStart.ToString() + "; xEnd: " + xEnd);
 


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