7 namespace kalman_combinator {
10 ESP_LOGCONFIG(
"kalman_combinator",
"Kalman Combinator:");
11 ESP_LOGCONFIG(
"kalman_combinator",
" Update variance: %f per ms", this->update_variance_value_);
12 ESP_LOGCONFIG(
"kalman_combinator",
" Sensors:");
13 for (
const auto &sensor : this->sensors_) {
14 auto &entity = *sensor.first;
15 ESP_LOGCONFIG(
"kalman_combinator",
" - %s", entity.get_name().c_str());
20 for (
const auto &sensor : this->sensors_) {
21 const auto stddev = sensor.second;
22 sensor.first->add_on_state_callback([
this, stddev](
float x) ->
void { this->correct_(x, stddev(x)); });
27 this->sensors_.emplace_back(sensor, stddev);
31 this->
add_source(sensor, std::function<
float(
float)>{[stddev](
float x) ->
float {
return stddev; }});
34 void KalmanCombinatorComponent::update_variance_() {
38 auto dt = now - this->last_update_;
39 auto dv = this->update_variance_value_ * dt;
40 this->variance_ += dv;
41 this->last_update_ = now;
44 void KalmanCombinatorComponent::correct_(
float value,
float stddev) {
45 if (std::isnan(value) || std::isinf(stddev)) {
49 if (std::isnan(this->state_) || std::isinf(this->variance_)) {
51 this->variance_ = stddev * stddev;
52 if (this->std_dev_sensor_ !=
nullptr) {
58 this->update_variance_();
62 const bool this_first = this->variance_ < (stddev * stddev);
63 const float mu1 = this_first ? this->state_ : value;
64 const float mu2 = this_first ? value : this->state_;
66 const float var1 = this_first ? this->variance_ : stddev * stddev;
67 const float var2 = this_first ? stddev * stddev : this->variance_;
69 const float mu = mu1 + var1 * (mu2 - mu1) / (var1 + var2);
70 const float var = var1 - (var1 * var1) / (var1 + var2);
74 this->variance_ = var;
77 if (this->std_dev_sensor_ !=
nullptr) {
void dump_config() override
uint32_t IRAM_ATTR HOT millis()
void publish_state(float state)
Publish a new state to the front-end.
void add_source(Sensor *sensor, std::function< float(float)> const &stddev)
Implementation of SPI Controller mode.