mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 09:17:46 +01:00
Improves ct_clamp component accuracy (#2283)
Co-authored-by: Rafa Treviño <rafael.trevino@bbva.com>
This commit is contained in:
parent
34db9d9ef2
commit
c33077bc61
2 changed files with 15 additions and 8 deletions
|
@ -31,19 +31,20 @@ void CTClampSensor::update() {
|
|||
return;
|
||||
}
|
||||
|
||||
float dc = this->sample_sum_ / this->num_samples_;
|
||||
float var = (this->sample_squared_sum_ / this->num_samples_) - dc * dc;
|
||||
float ac = std::sqrt(var);
|
||||
ESP_LOGD(TAG, "'%s' - Got %d samples", this->name_.c_str(), this->num_samples_);
|
||||
ESP_LOGD(TAG, "'%s' - Raw AC Value: %.3fA", this->name_.c_str(), ac);
|
||||
this->publish_state(ac);
|
||||
const float rms_ac_dc_squared = this->sample_squared_sum_ / this->num_samples_;
|
||||
const float rms_dc = this->sample_sum_ / this->num_samples_;
|
||||
const float rms_ac = std::sqrt(rms_ac_dc_squared - rms_dc * rms_dc);
|
||||
ESP_LOGD(TAG, "'%s' - Raw AC Value: %.3fA after %d different samples (%d SPS)", this->name_.c_str(), rms_ac,
|
||||
this->num_samples_, 1000 * this->num_samples_ / this->sample_duration_);
|
||||
this->publish_state(rms_ac);
|
||||
});
|
||||
|
||||
// Set sampling values
|
||||
this->is_sampling_ = true;
|
||||
this->last_value_ = 0.0;
|
||||
this->num_samples_ = 0;
|
||||
this->sample_sum_ = 0.0f;
|
||||
this->sample_squared_sum_ = 0.0f;
|
||||
this->is_sampling_ = true;
|
||||
}
|
||||
|
||||
void CTClampSensor::loop() {
|
||||
|
@ -55,9 +56,14 @@ void CTClampSensor::loop() {
|
|||
if (std::isnan(value))
|
||||
return;
|
||||
|
||||
// Assuming a sine wave, avoid requesting values faster than the ADC can provide them
|
||||
if (this->last_value_ == value)
|
||||
return;
|
||||
this->last_value_ = value;
|
||||
|
||||
this->num_samples_++;
|
||||
this->sample_sum_ += value;
|
||||
this->sample_squared_sum_ += value * value;
|
||||
this->num_samples_++;
|
||||
}
|
||||
|
||||
} // namespace ct_clamp
|
||||
|
|
|
@ -43,6 +43,7 @@ class CTClampSensor : public sensor::Sensor, public PollingComponent {
|
|||
* https://en.wikipedia.org/wiki/Root_mean_square
|
||||
*/
|
||||
|
||||
float last_value_ = 0.0f;
|
||||
float sample_sum_ = 0.0f;
|
||||
float sample_squared_sum_ = 0.0f;
|
||||
uint32_t num_samples_ = 0;
|
||||
|
|
Loading…
Reference in a new issue