Fix for noise in pulse_counter and duty_cycle components (#2646)

This commit is contained in:
Carlos Garcia Saura 2021-11-01 20:27:57 +01:00 committed by GitHub
parent d8b3af3815
commit d54b4e7c44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 17 deletions

View file

@ -12,7 +12,6 @@ void DutyCycleSensor::setup() {
this->pin_->setup();
this->store_.pin = this->pin_->to_isr();
this->store_.last_level = this->pin_->digital_read();
this->last_update_ = micros();
this->store_.last_interrupt = micros();
this->pin_->attach_interrupt(DutyCycleSensorStore::gpio_intr, &this->store_, gpio::INTERRUPT_ANY_EDGE);
@ -24,19 +23,20 @@ void DutyCycleSensor::dump_config() {
}
void DutyCycleSensor::update() {
const uint32_t now = micros();
const bool level = this->store_.last_level;
const uint32_t last_interrupt = this->store_.last_interrupt;
uint32_t on_time = this->store_.on_time;
if (this->last_update_ != 0) {
const bool level = this->store_.last_level;
const uint32_t last_interrupt = this->store_.last_interrupt;
uint32_t on_time = this->store_.on_time;
if (level)
on_time += now - last_interrupt;
if (level)
on_time += now - last_interrupt;
const float total_time = float(now - this->last_update_);
const float value = (on_time / total_time) * 100.0f;
ESP_LOGD(TAG, "'%s' Got duty cycle=%.1f%%", this->get_name().c_str(), value);
this->publish_state(value);
const float total_time = float(now - this->last_update_);
const float value = (on_time / total_time) * 100.0f;
ESP_LOGD(TAG, "'%s' Got duty cycle=%.1f%%", this->get_name().c_str(), value);
this->publish_state(value);
}
this->store_.on_time = 0;
this->store_.last_interrupt = now;
this->last_update_ = now;

View file

@ -30,7 +30,7 @@ class DutyCycleSensor : public sensor::Sensor, public PollingComponent {
InternalGPIOPin *pin_;
DutyCycleSensorStore store_{};
uint32_t last_update_;
uint32_t last_update_{0};
};
} // namespace duty_cycle

View file

@ -155,16 +155,20 @@ void PulseCounterSensor::dump_config() {
void PulseCounterSensor::update() {
pulse_counter_t raw = this->storage_.read_raw_value();
float value = (60000.0f * raw) / float(this->get_update_interval()); // per minute
ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
this->publish_state(value);
uint32_t now = millis();
if (this->last_time_ != 0) {
uint32_t interval = now - this->last_time_;
float value = (60000.0f * raw) / float(interval); // per minute
ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
this->publish_state(value);
}
if (this->total_sensor_ != nullptr) {
current_total_ += raw;
ESP_LOGD(TAG, "'%s': Total : %i pulses", this->get_name().c_str(), current_total_);
this->total_sensor_->publish_state(current_total_);
}
this->last_time_ = now;
}
} // namespace pulse_counter

View file

@ -65,7 +65,8 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
protected:
InternalGPIOPin *pin_;
PulseCounterStorage storage_;
uint32_t current_total_ = 0;
uint32_t last_time_{0};
uint32_t current_total_{0};
sensor::Sensor *total_sensor_;
};