diff --git a/libraries/Filter/DerivativeFilter.cpp b/libraries/Filter/DerivativeFilter.cpp index 8a09aea938..43710dbc42 100644 --- a/libraries/Filter/DerivativeFilter.cpp +++ b/libraries/Filter/DerivativeFilter.cpp @@ -18,17 +18,35 @@ template void DerivativeFilter::update(T sample, uint32_t timestamp) { + uint8_t i = FilterWithBuffer::sample_index; + uint8_t i1; + if (i == 0) { + i1 = FILTER_SIZE-1; + } else { + i1 = i-1; + } + if (_timestamps[i1] == timestamp) { + // this is not a new timestamp - ignore + return; + } + // add timestamp before we apply to FilterWithBuffer - _timestamps[FilterWithBuffer::sample_index] = timestamp; + _timestamps[i] = timestamp; // call parent's apply function to get the sample into the array FilterWithBuffer::apply(sample); + + _new_data = true; } template float DerivativeFilter::slope(void) { + if (!_new_data) { + return _last_slope; + } + float result = 0; // use f() to make the code match the maths a bit better. Note @@ -74,6 +92,9 @@ float DerivativeFilter::slope(void) break; } + _new_data = false; + _last_slope = result; + return result; } diff --git a/libraries/Filter/DerivativeFilter.h b/libraries/Filter/DerivativeFilter.h index 1267a9ba8b..ad71e0aa7d 100644 --- a/libraries/Filter/DerivativeFilter.h +++ b/libraries/Filter/DerivativeFilter.h @@ -36,7 +36,8 @@ class DerivativeFilter : public FilterWithBuffer virtual void reset(); private: - uint32_t _last_time; + bool _new_data; + float _last_slope; // microsecond timestamps for samples. This is needed // to cope with non-uniform time spacing of the data