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>
return calloc(sizeof(struct owparser_state), i);
}
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;
int i;
for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
max_delta = max(s->prev_delta[i], max_delta);
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 */
/* Probably no enough data yet, so no glitch detection */
return 0;
/*
* Simple glitch detection. If delta to previous value is more
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);
*/
delta = fabs(data - s->prev_data);
return delta > max_delta * 2;
}
return delta > max_delta * 2;
}
- * 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) {
if (might_be_glitch(data, &state[i]) &&
glitches < 2 && retries < 7) {