fix throttle average nan handling (#6275)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2024-02-25 10:26:35 -08:00 committed by Jesse Hills
parent e66e135a63
commit f3174c58bc
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
2 changed files with 7 additions and 2 deletions

View file

@ -252,7 +252,9 @@ ThrottleAverageFilter::ThrottleAverageFilter(uint32_t time_period) : time_period
optional<float> ThrottleAverageFilter::new_value(float value) {
ESP_LOGVV(TAG, "ThrottleAverageFilter(%p)::new_value(value=%f)", this, value);
if (!std::isnan(value)) {
if (std::isnan(value)) {
this->have_nan_ = true;
} else {
this->sum_ += value;
this->n_++;
}
@ -262,12 +264,14 @@ void ThrottleAverageFilter::setup() {
this->set_interval("throttle_average", this->time_period_, [this]() {
ESP_LOGVV(TAG, "ThrottleAverageFilter(%p)::interval(sum=%f, n=%i)", this, this->sum_, this->n_);
if (this->n_ == 0) {
this->output(NAN);
if (this->have_nan_)
this->output(NAN);
} else {
this->output(this->sum_ / this->n_);
this->sum_ = 0.0f;
this->n_ = 0;
}
this->have_nan_ = false;
});
}
float ThrottleAverageFilter::get_setup_priority() const { return setup_priority::HARDWARE; }

View file

@ -245,6 +245,7 @@ class ThrottleAverageFilter : public Filter, public Component {
uint32_t time_period_;
float sum_{0.0f};
unsigned int n_{0};
bool have_nan_{false};
};
using lambda_filter_t = std::function<optional<float>(float)>;