Skip to content

Commit

Permalink
Use Kalman (simple) filter for temperature.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Basov committed Jan 22, 2021
1 parent 07b6011 commit 724a189
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion PETCTL.ino
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,20 @@ float getTemp() {
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert absolute temp to C

return steinhart;
return simpleKalman(steinhart);
}

// Простой “Калман”
// https://alexgyver.ru/lessons/filters/
float _err_measure = 0.8; // примерный шум измерений
float _q = 0.1; // скорость изменения значений 0.001-1, варьировать самому
float simpleKalman(float newVal) {
float _kalman_gain, _current_estimate;
static float _err_estimate = _err_measure;
static float _last_estimate;
_kalman_gain = (float)_err_estimate / (_err_estimate + _err_measure);
_current_estimate = _last_estimate + (float)_kalman_gain * (newVal - _last_estimate);
_err_estimate = (1.0 - _kalman_gain) * _err_estimate + fabs(_last_estimate - _current_estimate) * _q;
_last_estimate = _current_estimate;
return _current_estimate;
}

0 comments on commit 724a189

Please sign in to comment.