PX4 Firmware
PX4 Autopilot Software http://px4.io
data_validator.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file data_validator.c
36  *
37  * A data validation class to identify anomalies in data streams
38  *
39  * @author Lorenz Meier <lorenz@px4.io>
40  */
41 
42 #include "data_validator.h"
43 
44 #include <ecl.h>
45 
46 void
47 DataValidator::put(uint64_t timestamp, float val, uint64_t error_count_in, int priority_in)
48 {
49  float data[dimensions] = { val }; //sets the first value and all others to 0
50 
51  put(timestamp, data, error_count_in, priority_in);
52 }
53 
54 void
55 DataValidator::put(uint64_t timestamp, const float val[dimensions], uint64_t error_count_in, int priority_in)
56 {
57  _event_count++;
58 
59  if (error_count_in > _error_count) {
60  _error_density += (error_count_in - _error_count);
61 
62  } else if (_error_density > 0) {
64  }
65 
66  _error_count = error_count_in;
67  _priority = priority_in;
68 
69  for (unsigned i = 0; i < dimensions; i++) {
70  if (_time_last == 0) {
71  _mean[i] = 0;
72  _lp[i] = val[i];
73  _M2[i] = 0;
74 
75  } else {
76  float lp_val = val[i] - _lp[i];
77 
78  float delta_val = lp_val - _mean[i];
79  _mean[i] += delta_val / _event_count;
80  _M2[i] += delta_val * (lp_val - _mean[i]);
81  _rms[i] = sqrtf(_M2[i] / (_event_count - 1));
82 
83  if (fabsf(_value[i] - val[i]) < 0.000001f) {
85 
86  } else {
88  }
89  }
90 
91  _vibe[i] = _vibe[i] * 0.99f + 0.01f * fabsf(val[i] - _lp[i]);
92 
93  // XXX replace with better filter, make it auto-tune to update rate
94  _lp[i] = _lp[i] * 0.99f + 0.01f * val[i];
95 
96  _value[i] = val[i];
97  }
98 
99  _time_last = timestamp;
100 }
101 
102 float
103 DataValidator::confidence(uint64_t timestamp)
104 {
105  float ret = 1.0f;
106 
107  /* check if we have any data */
108  if (_time_last == 0) {
110  ret = 0.0f;
111 
112  } else if (timestamp - _time_last > _timeout_interval) {
113  /* timed out - that's it */
115  ret = 0.0f;
116 
118  /* we got the exact same sensor value N times in a row */
120  ret = 0.0f;
121 
122  } else if (_error_count > NORETURN_ERRCOUNT) {
123  /* check error count limit */
125  ret = 0.0f;
126 
127  } else if (_error_density > ERROR_DENSITY_WINDOW) {
128  /* cap error density counter at window size */
131 
132  }
133 
134  /* no critical errors */
135  if (ret > 0.0f) {
136  /* return local error density for last N measurements */
137  ret = 1.0f - (_error_density / ERROR_DENSITY_WINDOW);
138 
139  if (ret > 0.0f) {
141  }
142  }
143 
144  return ret;
145 }
146 
147 void
149 {
150  if (_time_last == 0) {
151  ECL_INFO("\tno data");
152  return;
153  }
154 
155  for (unsigned i = 0; i < dimensions; i++) {
156  ECL_INFO("\tval: %8.4f, lp: %8.4f mean dev: %8.4f RMS: %8.4f conf: %8.4f",
157  (double) _value[i], (double)_lp[i], (double)_mean[i],
158  (double)_rms[i], (double)confidence(ecl_absolute_time()));
159  }
160 }
static constexpr uint32_t ERROR_FLAG_NO_ERROR
Data validator error states.
Adapter / shim layer for system calls needed by ECL.
static constexpr uint32_t ERROR_FLAG_TIMEOUT
float _lp[dimensions]
low pass value
float _value[dimensions]
last value
float _vibe[dimensions]
vibration level, in sensor unit
static constexpr uint32_t ERROR_FLAG_STALE_DATA
#define ecl_absolute_time()
Definition: ecl.h:85
int _priority
sensor nominal priority
unsigned _value_equal_count
equal values in a row
static constexpr uint32_t ERROR_FLAG_HIGH_ERRCOUNT
float _rms[dimensions]
root mean square error
float _M2[dimensions]
RMS component value.
void print()
Print the validator value.
uint8_t * data
Definition: dataman.cpp:149
uint32_t _timeout_interval
interval in which the datastream times out in us
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
uint32_t _error_mask
sensor error state
uint64_t _event_count
total data counter
float _mean[dimensions]
mean of value
unsigned _value_equal_count_threshold
when to consider an equal count as a problem
static constexpr uint32_t ERROR_FLAG_NO_DATA
static constexpr uint32_t ERROR_FLAG_HIGH_ERRDENSITY
uint64_t _error_count
error count
int _error_density
ratio between successful reads and errors
#define ECL_INFO
Definition: ecl.h:90
void put(uint64_t timestamp, float val, uint64_t error_count, int priority)
Put an item into the validator.
uint64_t _time_last
last timestamp
A data validation class to identify anomalies in data streams.
static const constexpr unsigned NORETURN_ERRCOUNT
if the error count reaches this value, return sensor as invalid
float confidence(uint64_t timestamp)
Get the confidence of this validator.
static const constexpr float ERROR_DENSITY_WINDOW
window in measurement counts for errors
static const unsigned dimensions