mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-30 10:13:04 +05:00
317 lines
10 KiB
C++
317 lines
10 KiB
C++
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
|
|
|
#include <app.h>
|
|
|
|
#include <cmath>
|
|
#include <MycilaPID.h>
|
|
|
|
static PID pidRegulator;
|
|
|
|
static float heatingTarget = 0.0f;
|
|
static bool equithermEnabled = false;
|
|
static float equithermResult = 0.0f;
|
|
static float pidResult = 0.0f;
|
|
static bool indoorSensorsConnected = false;
|
|
|
|
static Task equithermTask("Equitherm", Task::Type::ONCE, [](void*) {
|
|
float etResult = settings.heating.target + settings.equitherm.shift;
|
|
|
|
if (settings.heating.maxTemp > settings.heating.target) {
|
|
float tempDelta = settings.heating.target - vars.master.heating.outdoorTemp;
|
|
float maxPoint = settings.heating.target - (
|
|
settings.heating.maxTemp - settings.heating.target
|
|
) / settings.equitherm.slope;
|
|
|
|
float sf = (settings.heating.maxTemp - settings.heating.target) / pow(
|
|
settings.heating.target - maxPoint,
|
|
1.0f / settings.equitherm.exponent
|
|
);
|
|
etResult = settings.heating.target + settings.equitherm.shift + sf * (
|
|
tempDelta >= 0
|
|
? pow(tempDelta, 1.0f / settings.equitherm.exponent)
|
|
: -(pow(-(tempDelta), 1.0f / settings.equitherm.exponent))
|
|
);
|
|
}
|
|
|
|
if (indoorSensorsConnected && !settings.pid.enabled && !settings.heating.turbo) {
|
|
etResult += constrain(
|
|
settings.heating.target - vars.master.heating.indoorTemp,
|
|
-3.0f,
|
|
3.0f
|
|
) * settings.equitherm.targetDiffFactor;
|
|
}
|
|
|
|
if (!std::isfinite(etResult)) {
|
|
etResult = settings.heating.target + settings.equitherm.shift;
|
|
}
|
|
|
|
etResult = constrain(etResult, settings.heating.minTemp, settings.heating.maxTemp);
|
|
|
|
if (isFloatDifferent(equithermResult, etResult, 0.05f)) {
|
|
equithermResult = etResult;
|
|
ESP_LOGI(L_REGULATOR_EQUITHERM, "New result: %.2f", etResult);
|
|
}
|
|
});
|
|
|
|
static Task pidTask("PID", Task::Type::FOREVER, [](void*) {
|
|
// Reset before disabling
|
|
if (!settings.pid.enabled) {
|
|
if (pidRegulator.isEnabled()) {
|
|
pidRegulator.pause();
|
|
pidRegulator.reset();
|
|
ESP_LOGI(L_REGULATOR_PID, "Disabled and reset");
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
// 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) {
|
|
kp *= settings.pid.deadband.p_multiplier;
|
|
ki *= settings.pid.deadband.i_multiplier;
|
|
kd *= settings.pid.deadband.d_multiplier;
|
|
}
|
|
|
|
// 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() {
|
|
if (settings.heating.turbo) {
|
|
if (!settings.heating.enabled || vars.emergency.state || !indoorSensorsConnected) {
|
|
settings.heating.turbo = false;
|
|
|
|
} else if (!settings.pid.enabled && !settings.equitherm.enabled) {
|
|
settings.heating.turbo = false;
|
|
|
|
} else if (fabsf(settings.heating.target - vars.master.heating.indoorTemp) <= 1.0f) {
|
|
settings.heating.turbo = false;
|
|
}
|
|
|
|
if (!settings.heating.turbo) {
|
|
ESP_LOGI(L_REGULATOR, "Turbo mode auto disabled");
|
|
}
|
|
}
|
|
}
|
|
|
|
static void hysteresis() {
|
|
// Hysteresis also clears blocking when it is no longer applicable.
|
|
const bool useHysteresis =
|
|
settings.heating.hysteresis.enabled && indoorSensorsConnected &&
|
|
(settings.equitherm.enabled || settings.pid.enabled || settings.opentherm.options.nativeOTC);
|
|
|
|
if (useHysteresis) {
|
|
if (!vars.master.heating.blocking && vars.master.heating.indoorTemp - settings.heating.target + 0.0001f >= settings.heating.hysteresis.value) {
|
|
vars.master.heating.blocking = true;
|
|
|
|
} else if (vars.master.heating.blocking && vars.master.heating.indoorTemp - settings.heating.target - 0.0001f <= -(settings.heating.hysteresis.value)) {
|
|
vars.master.heating.blocking = false;
|
|
}
|
|
|
|
} else if (vars.master.heating.blocking) {
|
|
vars.master.heating.blocking = false;
|
|
}
|
|
}
|
|
|
|
static inline float getHeatingMinSetpointTemp() {
|
|
return settings.opentherm.options.nativeOTC
|
|
? vars.master.heating.minTemp
|
|
: settings.heating.minTemp;
|
|
}
|
|
|
|
static inline float getHeatingMaxSetpointTemp() {
|
|
return settings.opentherm.options.nativeOTC
|
|
? vars.master.heating.maxTemp
|
|
: settings.heating.maxTemp;
|
|
}
|
|
|
|
static float getHeatingSetpointTemp() {
|
|
if (isFloatDifferent(heatingTarget, settings.heating.target, 0.0001f)) {
|
|
heatingTarget = settings.heating.target;
|
|
ESP_LOGI(L_REGULATOR, "New target: %.2f", settings.heating.target);
|
|
}
|
|
|
|
if (vars.emergency.state) {
|
|
return settings.emergency.target;
|
|
|
|
} else if (settings.opentherm.options.nativeOTC) {
|
|
return settings.heating.target;
|
|
}
|
|
|
|
float setpoint = settings.heating.target;
|
|
if (settings.equitherm.enabled || settings.pid.enabled) {
|
|
setpoint = 0.0f;
|
|
|
|
if (settings.equitherm.enabled) {
|
|
equithermTask.forceRun();
|
|
setpoint += equithermResult;
|
|
}
|
|
|
|
// Keep the last PID result when heating or the indoor sensor is unavailable.
|
|
if (settings.pid.enabled) {
|
|
setpoint += pidResult;
|
|
}
|
|
|
|
if (settings.heating.turbo) {
|
|
setpoint += constrain(
|
|
settings.heating.target - vars.master.heating.indoorTemp,
|
|
-3.0f,
|
|
3.0f
|
|
) * settings.heating.turboFactor;
|
|
}
|
|
}
|
|
|
|
if (vars.master.heating.freezing) {
|
|
setpoint = fmaxf(
|
|
setpoint,
|
|
static_cast<float>(settings.heating.freezeProtection.lowTemp)
|
|
);
|
|
}
|
|
|
|
return setpoint;
|
|
}
|
|
|
|
static Task regulatorTask("Regulator", [](void*) {
|
|
indoorSensorsConnected = Sensors::existsConnectedSensorsByPurpose(Sensors::Purpose::INDOOR_TEMP);
|
|
|
|
if (pidTask.enabled()) {
|
|
const uint32_t pidInterval = settings.pid.dt * 1000u;
|
|
|
|
if (pidTask.interval() != pidInterval) {
|
|
pidTask.setInterval(pidInterval);
|
|
}
|
|
}
|
|
|
|
if (settings.equitherm.enabled || settings.pid.enabled || settings.opentherm.options.nativeOTC) {
|
|
vars.master.heating.smartControl = true;
|
|
vars.master.heating.minTemp = THERMOSTAT_INDOOR_MIN_TEMP;
|
|
vars.master.heating.maxTemp = THERMOSTAT_INDOOR_MAX_TEMP;
|
|
|
|
} else {
|
|
vars.master.heating.smartControl = false;
|
|
vars.master.heating.minTemp = settings.heating.minTemp;
|
|
vars.master.heating.maxTemp = settings.heating.maxTemp;
|
|
}
|
|
|
|
turbo();
|
|
hysteresis();
|
|
|
|
const float setpointTemp = getHeatingSetpointTemp();
|
|
vars.master.heating.targetTemp = settings.opentherm.options.nativeOTC
|
|
? setpointTemp
|
|
: settings.heating.target;
|
|
vars.master.heating.setpointTemp = roundf(constrain(
|
|
setpointTemp,
|
|
getHeatingMinSetpointTemp(),
|
|
getHeatingMaxSetpointTemp()
|
|
), 0);
|
|
|
|
Sensors::setValueByType(
|
|
Sensors::Type::HEATING_SETPOINT_TEMP, vars.master.heating.setpointTemp,
|
|
Sensors::ValueType::PRIMARY, true, true
|
|
);
|
|
});
|
|
|
|
void initRegulator() {
|
|
// 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.setInterval(10000)
|
|
.setEnabledWhen([] {
|
|
return settings.pid.enabled || pidRegulator.isEnabled();
|
|
})
|
|
.resume(pidTask.interval());
|
|
|
|
// Apply mode changes and turbo auto-disable before a PID calculation due on the same pass.
|
|
appTaskManager.addTask(regulatorTask.setInterval(10000));
|
|
appTaskManager.addTask(pidTask);
|
|
appTaskManager.addTask(equithermTask.pause());
|
|
}
|