[chronojump] RunEncoder: Trigger with spurious management



commit 0d5eb9caecb75d86b88bf99aef3176df97f4ddda
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Feb 3 15:17:25 2020 +0100

    RunEncoder: Trigger with spurious management

 src/encoderCapture.cs |  2 +-
 src/gui/runEncoder.cs | 17 +++++++++-----
 src/trigger.cs        | 65 +++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 67 insertions(+), 17 deletions(-)
---
diff --git a/src/encoderCapture.cs b/src/encoderCapture.cs
index 42c7c1e6..f8a08b56 100644
--- a/src/encoderCapture.cs
+++ b/src/encoderCapture.cs
@@ -292,7 +292,7 @@ public abstract class EncoderCapture
                                }
 
                                Trigger trigger = new Trigger(Trigger.Modes.ENCODER, i, true);
-                               if(triggerList.IsSpurious(trigger))
+                               if(triggerList.IsSpurious(trigger, TriggerList.SpuriousType.ON, 50))
                                {
                                        triggerList.RemoveLastOff();
                                        continue;
diff --git a/src/gui/runEncoder.cs b/src/gui/runEncoder.cs
index 485d3c8a..5fdcd7c3 100644
--- a/src/gui/runEncoder.cs
+++ b/src/gui/runEncoder.cs
@@ -481,12 +481,17 @@ public partial class ChronoJumpWindow
                        writer.WriteLine(string.Format("{0};{1};{2}", encoderDisplacement, time, force));
                        if(encoderOrRCA == 0)
                                rowsCount ++; //note this is not used right now, and maybe it will need to be 
for all cases, not just for encoderOrRCA
-                       else if(encoderOrRCA == 1)
-                               triggerListRunEncoder.Add(new Trigger(Trigger.Modes.RACEANALYZER, time, 
false));
-                       else if(encoderOrRCA == 2)
-                               triggerListRunEncoder.Add(new Trigger(Trigger.Modes.RACEANALYZER, time, 
true));
-
-                       //TODO: manage spurious like on encoderCapture.cs
+                       else {
+                               Trigger trigger;
+                               if(encoderOrRCA == 1)
+                                       trigger = new Trigger(Trigger.Modes.RACEANALYZER, time, false);
+                               else //(encoderOrRCA == 2)
+                                       trigger = new Trigger(Trigger.Modes.RACEANALYZER, time, true);
+
+                               if(! triggerListRunEncoder.NewSameTypeThanBefore(trigger) &&
+                                               ! triggerListRunEncoder.IsSpurious(trigger, 
TriggerList.SpuriousType.BOTH, 50))
+                                       triggerListRunEncoder.Add(trigger);
+                       }
                }
 
                LogB.Information(string.Format("FINISHED WITH conditions: {0}-{1}-{2}",
diff --git a/src/trigger.cs b/src/trigger.cs
index adc0c3b7..68606655 100644
--- a/src/trigger.cs
+++ b/src/trigger.cs
@@ -214,6 +214,16 @@ public class TriggerList
                return countOn;
        }
 
+       private int countOff()
+       {
+               int countOff = 0;
+               foreach(Trigger t in l)
+                       if(! t.InOut)
+                               countOff ++;
+
+               return countOff;
+       }
+
        //see encoderCapture.MinimumOneTriggersOn()
        public bool MinimumOneOn()
        {
@@ -223,14 +233,18 @@ public class TriggerList
                return false;
        }
 
-       //if inOut == true will return last "in"
-       private Trigger last(bool inOut)
+       public enum SpuriousType { ON, OFF, BOTH }
+       //if ON will return last "on"
+       private Trigger last(SpuriousType spt)
        {
                int i = 0;
                int lastPos = 0;
                foreach(Trigger t in l)
                {
-                       if(t.InOut == inOut)
+                       if(
+                                       spt == SpuriousType.BOTH ||
+                                       spt == SpuriousType.ON && t.InOut ||
+                                       spt == SpuriousType.OFF && ! t.InOut)
                                lastPos = i;
                        i ++;
                }
@@ -238,24 +252,55 @@ public class TriggerList
                return l[lastPos];
        }
 
-       //this newTrigger is an On trigger, compare with last On
-       public bool IsSpurious(Trigger newTrigger)
+       //on runEncoder .ino the management of interruptions/triggers could be better
+       //just check if two triggers ON or two OFF arrive to discard the second (newTrigger)
+       public bool NewSameTypeThanBefore(Trigger newTrigger)
        {
-               //cannot be spurious if is the first On
-               if(countOn() == 0)
+               //cannot be an old trigger if this is the first Of this type
+               if(newTrigger.InOut && countOn() == 0)
+                       return false;
+               else if(! newTrigger.InOut && countOff() == 0)
                        return false;
 
-               //last(true) will check last On trigger
-               if( (newTrigger.Ms - last(true).Ms) < 50 )
+               if(last(SpuriousType.BOTH).InOut == newTrigger.InOut)
                        return true;
 
                return false;
        }
 
+       /*
+        * this newTrigger is an On trigger, compare with last
+        * encoder: spt.ON, 50ms
+        * runEncoder: spt.BOTH, 50ms
+        */
+       public bool IsSpurious(Trigger newTrigger, SpuriousType spt, int ms)
+       {
+               //cannot be spurious if is the first of this type
+               if(spt == SpuriousType.ON && countOn() == 0)
+                       return false;
+               else if(spt == SpuriousType.BOTH && (
+                               newTrigger.InOut && countOn() == 0 ||
+                               ! newTrigger.InOut && countOff() == 0) )
+                       return false;
+
+               if(spt == SpuriousType.BOTH && (newTrigger.Ms - last(SpuriousType.BOTH).Ms) < ms )
+                       return true;
+               else if(spt == SpuriousType.ON && (newTrigger.Ms - last(SpuriousType.ON).Ms) < ms )
+                       return true;
+
+               return false;
+       }
+
+       /*
+        * used on encoder (where we also use triggers to cut repetitions)
+        * if on forceSensor we use triggers to cut repetitions, will use this too
+        * not used on RunEncoder
+        */
        public void RemoveLastOff()
        {
-               l.Remove(last(false));
+               l.Remove(last(SpuriousType.OFF));
        }
+
        /*
         * end of spurious management
         */


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