mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-30 10:13:04 +05:00
1 parent
108594f2d9
commit
be6bbab16a
3 files changed
+110
-35
No files matched your search
+108
-33
@@ -3,11 +3,12 @@
|
||||
#include <app.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <GyverPID.h>
|
||||
#include <MycilaPID.h>
|
||||
|
||||
static GyverPID pidRegulator(0, 0, 0);
|
||||
static PID pidRegulator;
|
||||
|
||||
static float prevHeatingTarget = 0.0f;
|
||||
static float heatingTarget = 0.0f;
|
||||
static bool equithermEnabled = false;
|
||||
static float equithermResult = 0.0f;
|
||||
static float pidResult = 0.0f;
|
||||
static bool indoorSensorsConnected = false;
|
||||
@@ -53,33 +54,112 @@ static Task equithermTask("Equitherm", Task::Type::ONCE, [](void*) {
|
||||
});
|
||||
|
||||
static Task pidTask("PID", Task::Type::FOREVER, [](void*) {
|
||||
pidRegulator.Kp = settings.heating.turbo ? 0.0f : settings.pid.p_factor;
|
||||
pidRegulator.Ki = settings.pid.i_factor;
|
||||
pidRegulator.Kd = settings.pid.d_factor;
|
||||
// Reset before disabling
|
||||
if (!settings.pid.enabled) {
|
||||
if (pidRegulator.isEnabled()) {
|
||||
pidRegulator.pause();
|
||||
pidRegulator.reset();
|
||||
ESP_LOGI(L_REGULATOR_PID, "Disabled and reset");
|
||||
}
|
||||
|
||||
pidRegulator.setLimits(settings.pid.minTemp, settings.pid.maxTemp);
|
||||
// Use the same period for scheduling and the integral/derivative terms.
|
||||
pidRegulator.setDt(pidTask.interval());
|
||||
pidRegulator.input = vars.master.heating.indoorTemp;
|
||||
pidRegulator.setpoint = settings.heating.target;
|
||||
return;
|
||||
}
|
||||
|
||||
float error = pidRegulator.setpoint - pidRegulator.input;
|
||||
// Configure regulation modes
|
||||
pidRegulator.setProportionalMode(PID::ProportionalMode::ON_ERROR);
|
||||
pidRegulator.setIntegralCorrectionMode(PID::IntegralCorrectionMode::CLAMP);
|
||||
pidRegulator.setDerivativeMode(PID::DerivativeMode::ON_ERROR);
|
||||
pidRegulator.setReverse(false);
|
||||
pidRegulator.setTimeSampling(false);
|
||||
|
||||
// Update output limits
|
||||
// Updating limits also recalculates the I-term, even when the limits are unchanged.
|
||||
if (pidRegulator.getOutputMin() != settings.pid.minTemp || pidRegulator.getOutputMax() != settings.pid.maxTemp) {
|
||||
pidRegulator.setOutputLimits(settings.pid.minTemp, settings.pid.maxTemp);
|
||||
}
|
||||
|
||||
// Pause so resume(value) resets the PID instead of keeping the previous integral.
|
||||
if (equithermEnabled != settings.equitherm.enabled || std::isnan(pidRegulator.getITerm())) {
|
||||
pidRegulator.pause();
|
||||
ESP_LOGI(L_REGULATOR_PID, "Reinitialization");
|
||||
}
|
||||
|
||||
// Resume with the initial output
|
||||
if (!pidRegulator.isEnabled()) {
|
||||
// Initial output
|
||||
pidRegulator.resume(settings.equitherm.enabled
|
||||
? 0.0f
|
||||
: settings.heating.target
|
||||
);
|
||||
|
||||
pidResult = pidRegulator.getOutput();
|
||||
equithermEnabled = settings.equitherm.enabled;
|
||||
ESP_LOGI(L_REGULATOR_PID, "Resumed, initial output: %.2f, equitherm: %s", pidResult, equithermEnabled ? S_TRUE : S_FALSE);
|
||||
}
|
||||
|
||||
// Skip PID when heating is disabled
|
||||
if (!settings.heating.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Keep the emergency target outside PID control
|
||||
if (vars.emergency.state) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Let native OTC control the heating setpoint
|
||||
if (settings.opentherm.options.nativeOTC) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Require an available indoor temperature sensor
|
||||
if (!indoorSensorsConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare PID gains
|
||||
// Use the scheduled period for I/D, excluding time when the task is disabled.
|
||||
const float dt = pidTask.interval() / 1000.0f;
|
||||
float kp = settings.heating.turbo ? 0.0f : settings.pid.p_factor;
|
||||
float ki = settings.pid.i_factor * dt;
|
||||
float kd = settings.pid.d_factor / dt;
|
||||
|
||||
// Set room temperature target
|
||||
pidRegulator.setSetpoint(settings.heating.target);
|
||||
|
||||
// Check deadband
|
||||
float error = settings.heating.target - vars.master.heating.indoorTemp;
|
||||
bool hasDeadband = settings.pid.deadband.enabled &&
|
||||
(error > -(settings.pid.deadband.thresholdHigh)) &&
|
||||
(error < settings.pid.deadband.thresholdLow);
|
||||
|
||||
// Apply deadband gains
|
||||
if (hasDeadband) {
|
||||
pidRegulator.Kp *= settings.pid.deadband.p_multiplier;
|
||||
pidRegulator.Ki *= settings.pid.deadband.i_multiplier;
|
||||
pidRegulator.Kd *= settings.pid.deadband.d_multiplier;
|
||||
kp *= settings.pid.deadband.p_multiplier;
|
||||
ki *= settings.pid.deadband.i_multiplier;
|
||||
kd *= settings.pid.deadband.d_multiplier;
|
||||
}
|
||||
|
||||
float result = pidRegulator.getResult();
|
||||
if (std::isfinite(result) && isFloatDifferent(pidResult, result, 0.09f)) {
|
||||
pidResult = result;
|
||||
ESP_LOGI(L_REGULATOR_PID, "New result: %.2f", result);
|
||||
ESP_LOGD(L_REGULATOR_PID, "Integral: %.2f", pidRegulator.integral);
|
||||
}
|
||||
// Apply PID gains
|
||||
pidRegulator.setTunings(kp, ki, kd);
|
||||
|
||||
// Calculate result
|
||||
pidResult = pidRegulator.compute(vars.master.heating.indoorTemp);
|
||||
|
||||
// Log PID state
|
||||
ESP_LOGD(
|
||||
L_REGULATOR_PID,
|
||||
"Input: %.2f, setpoint: %.2f, error: %.2f, output: %.2f, P: %.2f, I: %.2f, D: %.2f",
|
||||
pidRegulator.getInput(), pidRegulator.getSetpoint(), pidRegulator.getError(), pidRegulator.getOutput(),
|
||||
pidRegulator.getPTerm(), pidRegulator.getITerm(), pidRegulator.getDTerm()
|
||||
);
|
||||
ESP_LOGD(
|
||||
L_REGULATOR_PID,
|
||||
"dt: %.0f s, Kp: %.3f, Ki*dt: %.6g, Kd/dt: %.6g, limits: [%.2f, %.2f], deadband: %s, turbo: %s",
|
||||
dt, pidRegulator.getKp(), pidRegulator.getKi(), pidRegulator.getKd(),
|
||||
pidRegulator.getOutputMin(), pidRegulator.getOutputMax(),
|
||||
hasDeadband ? S_TRUE : S_FALSE, settings.heating.turbo ? S_TRUE : S_FALSE
|
||||
);
|
||||
});
|
||||
|
||||
static void turbo() {
|
||||
@@ -132,8 +212,8 @@ static inline float getHeatingMaxSetpointTemp() {
|
||||
}
|
||||
|
||||
static float getHeatingSetpointTemp() {
|
||||
if (isFloatDifferent(prevHeatingTarget, settings.heating.target, 0.0001f)) {
|
||||
prevHeatingTarget = settings.heating.target;
|
||||
if (isFloatDifferent(heatingTarget, settings.heating.target, 0.0001f)) {
|
||||
heatingTarget = settings.heating.target;
|
||||
ESP_LOGI(L_REGULATOR, "New target: %.2f", settings.heating.target);
|
||||
}
|
||||
|
||||
@@ -199,12 +279,6 @@ static Task regulatorTask("Regulator", [](void*) {
|
||||
vars.master.heating.maxTemp = settings.heating.maxTemp;
|
||||
}
|
||||
|
||||
// This must still run when the PID calculation task is disabled.
|
||||
if (!settings.pid.enabled && fabsf(pidRegulator.integral) > 0.01f) {
|
||||
pidRegulator.integral = 0.0f;
|
||||
ESP_LOGI(L_REGULATOR_PID, "Integral sum has been reset");
|
||||
}
|
||||
|
||||
turbo();
|
||||
hysteresis();
|
||||
|
||||
@@ -227,11 +301,12 @@ static Task regulatorTask("Regulator", [](void*) {
|
||||
void initRegulator() {
|
||||
regulatorTask.setInterval(10000);
|
||||
|
||||
// Sensor availability is refreshed by the regulator before checking PID eligibility.
|
||||
// Pause so the first resume(value) initializes the integral for the selected mode.
|
||||
pidRegulator.pause();
|
||||
|
||||
// Keep the PID task eligible until it has processed its own shutdown.
|
||||
pidTask.setEnabledWhen([] {
|
||||
return settings.pid.enabled && settings.heating.enabled &&
|
||||
!vars.emergency.state && !settings.opentherm.options.nativeOTC &&
|
||||
indoorSensorsConnected;
|
||||
return settings.pid.enabled || pidRegulator.isEnabled();
|
||||
});
|
||||
pidTask.setInterval(10000);
|
||||
pidTask.resume(pidTask.interval());
|
||||
|
||||
Reference in new issue
Block a user