From 33212d1abf45084c70d0da058684283129c698aa Mon Sep 17 00:00:00 2001 From: Carlos Gustavo Sarmiento Date: Thu, 9 Jul 2020 18:25:46 -0500 Subject: [PATCH] Add Integral Reset Action to PIDClimate (#1104) --- esphome/components/pid/climate.py | 13 +++++++++++++ esphome/components/pid/pid_climate.cpp | 2 ++ esphome/components/pid/pid_climate.h | 11 +++++++++++ esphome/components/pid/pid_controller.h | 2 ++ 4 files changed, 28 insertions(+) diff --git a/esphome/components/pid/climate.py b/esphome/components/pid/climate.py index a3e2299296..97a98efc20 100644 --- a/esphome/components/pid/climate.py +++ b/esphome/components/pid/climate.py @@ -7,6 +7,7 @@ from esphome.const import CONF_ID, CONF_SENSOR pid_ns = cg.esphome_ns.namespace('pid') PIDClimate = pid_ns.class_('PIDClimate', climate.Climate, cg.Component) PIDAutotuneAction = pid_ns.class_('PIDAutotuneAction', automation.Action) +PIDResetIntegralTermAction = pid_ns.class_('PIDResetIntegralTermAction', automation.Action) CONF_DEFAULT_TARGET_TEMPERATURE = 'default_target_temperature' @@ -64,6 +65,18 @@ def to_code(config): cg.add(var.set_default_target_temperature(config[CONF_DEFAULT_TARGET_TEMPERATURE])) +@automation.register_action( + 'climate.pid.reset_integral_term', + PIDResetIntegralTermAction, + automation.maybe_simple_id({ + cv.Required(CONF_ID): cv.use_id(PIDClimate), + }) +) +def pid_reset_integral_term(config, action_id, template_arg, args): + paren = yield cg.get_variable(config[CONF_ID]) + yield cg.new_Pvariable(action_id, template_arg, paren) + + @automation.register_action('climate.pid.autotune', PIDAutotuneAction, automation.maybe_simple_id({ cv.Required(CONF_ID): cv.use_id(PIDClimate), cv.Optional(CONF_NOISEBAND, default=0.25): cv.float_, diff --git a/esphome/components/pid/pid_climate.cpp b/esphome/components/pid/pid_climate.cpp index 0c777ffd8b..24fb0ec905 100644 --- a/esphome/components/pid/pid_climate.cpp +++ b/esphome/components/pid/pid_climate.cpp @@ -148,5 +148,7 @@ void PIDClimate::start_autotune(std::unique_ptr &&autotune) { }); } +void PIDClimate::reset_integral_term() { this->controller_.reset_accumulated_integral(); } + } // namespace pid } // namespace esphome diff --git a/esphome/components/pid/pid_climate.h b/esphome/components/pid/pid_climate.h index 3dae92af5f..12436e225c 100644 --- a/esphome/components/pid/pid_climate.h +++ b/esphome/components/pid/pid_climate.h @@ -39,6 +39,7 @@ class PIDClimate : public climate::Climate, public Component { default_target_temperature_ = default_target_temperature; } void start_autotune(std::unique_ptr &&autotune); + void reset_integral_term(); protected: /// Override control to change settings of the climate device. @@ -90,5 +91,15 @@ template class PIDAutotuneAction : public Action { PIDClimate *parent_; }; +template class PIDResetIntegralTermAction : public Action { + public: + PIDResetIntegralTermAction(PIDClimate *parent) : parent_(parent) {} + + void play(Ts... x) { this->parent_->reset_integral_term(); } + + protected: + PIDClimate *parent_; +}; + } // namespace pid } // namespace esphome diff --git a/esphome/components/pid/pid_controller.h b/esphome/components/pid/pid_controller.h index 7ec7724e15..4caad8dd8b 100644 --- a/esphome/components/pid/pid_controller.h +++ b/esphome/components/pid/pid_controller.h @@ -40,6 +40,8 @@ struct PIDController { return proportional_term + integral_term + derivative_term; } + void reset_accumulated_integral() { accumulated_integral_ = 0; } + /// Proportional gain K_p. float kp = 0; /// Integral gain K_i.