]> git.itanic.dy.fi Git - rrdd/commitdiff
onewire_parser: Make step changes pass glitch detection
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Thu, 26 Jan 2017 18:28:32 +0000 (20:28 +0200)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 29 Jan 2017 14:41:38 +0000 (16:41 +0200)
The glitch detection code does not recognize step changes at
all. Currently it re-reads the temperature values until maximum glitch
threshold (two reads) and then just uses the last data. This is not
ideal on noisy lines as the last data might be a glitch itself.

Improve the logic so that after considering for a glitch, we compare
two consequent readings and see if the delta between those two is less
than the glitch threshold. If it is less, we consider the data
good. This is needed in case the temperature is changing quickly and
we don't get two identical values, but almost same.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
onewire_parser.c

index b33ee9f696850066e79623d9627cd30c55961d89..7df7aa1afbad411b74622bd49a162a9d79138e57 100644 (file)
@@ -33,24 +33,34 @@ static struct owparser_state *allocate_parser_state(const char **datastr)
        return calloc(sizeof(struct owparser_state), i);
 }
 
-static int might_be_glitch(double data, const struct owparser_state *s)
+static double max_glitch_delta(const struct owparser_state *s)
 {
-       double max_delta = 0, delta;
+       double max_delta = 0;
        int i;
 
        for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
                max_delta = max(s->prev_delta[i], max_delta);
 
+       return max_delta;
+}
+
+static int might_be_glitch(double data, const struct owparser_state *s)
+{
+       double max_delta, delta;
+
+       max_delta = max_glitch_delta(s);
+
        /* Probably no enough data yet, so no glitch detection */
-       if (!max_delta)
+       if (max_delta == 0)
                return 0;
 
        /*
         * Simple glitch detection. If delta to previous value is more
-        * than twice as larger as some older delta, we might have a
-        * glit
+        * than twice as larger as any of the older delta, we might
+        * have a glitch
         */
        delta = fabs(data - s->prev_data);
+
        return delta > max_delta * 2;
 }
 
@@ -277,11 +287,15 @@ undefined:
                        tmp = NULL;
 
                        /*
-                        * If we read the same value as previously,
-                        * it's not a glitch
+                        * If we read the almost same value as
+                        * previously, it's not a glitch
                         */
-                       if (glitches && prev_data == data)
-                               break;
+                       if (glitches && prev_data != 85) {
+                               double d = max_glitch_delta(&state[i]);
+
+                               if (fabs(data - prev_data) <= d * 2)
+                                       break;
+                       }
 
                        if (might_be_glitch(data, &state[i]) &&
                                glitches < 2 && retries < 7) {