fix: replace GyverPID with MycilaPID

fix #292
This commit is contained in:
Yurii committed 2026-11-29 10:23:23 +03:00
1 parent 108594f2d9
commit be6bbab16a
3 files changed
+110 -35

No files matched your search

+1 -1
View File
@@ -133,7 +133,7 @@ Anyone is welcome to add their native language as a [new localization](https://h
</a>
## Gratitude
* To the developers of the libraries used: [ArduinoJson](https://github.com/bblanchon/ArduinoJson), [NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino), [MycilaMQTT](https://github.com/mathieucarbou/MycilaMQTT), [MycilaHADiscovery](https://github.com/mathieucarbou/MycilaHADiscovery), [AsyncTCP](https://github.com/ESP32Async/AsyncTCP), [ESPAsyncWebServer](https://github.com/ESP32Async/ESPAsyncWebServer), [MycilaWebSerial](https://github.com/mathieucarbou/MycilaWebSerial), [MycilaTaskManager](https://github.com/mathieucarbou/MycilaTaskManager), [FileData](https://github.com/GyverLibs/FileData), [GyverPID](https://github.com/GyverLibs/GyverPID), [GyverBlinker](https://github.com/GyverLibs/GyverBlinker), [OneWireNg](https://github.com/pstolarz/OneWireNg), [DallasTemperature](https://github.com/milesburton/Arduino-Temperature-Control-Library) & [esp32DHT](https://github.com/bertmelis/esp32DHT)
* To the developers of the libraries used: [ArduinoJson](https://github.com/bblanchon/ArduinoJson), [NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino), [MycilaMQTT](https://github.com/mathieucarbou/MycilaMQTT), [MycilaHADiscovery](https://github.com/mathieucarbou/MycilaHADiscovery), [AsyncTCP](https://github.com/ESP32Async/AsyncTCP), [ESPAsyncWebServer](https://github.com/ESP32Async/ESPAsyncWebServer), [MycilaWebSerial](https://github.com/mathieucarbou/MycilaWebSerial), [MycilaTaskManager](https://github.com/mathieucarbou/MycilaTaskManager), [FileData](https://github.com/GyverLibs/FileData), [MycilaUtilities](https://github.com/mathieucarbou/MycilaUtilities), [GyverBlinker](https://github.com/GyverLibs/GyverBlinker), [OneWireNg](https://github.com/pstolarz/OneWireNg), [DallasTemperature](https://github.com/milesburton/Arduino-Temperature-Control-Library) & [esp32DHT](https://github.com/bertmelis/esp32DHT)
* To the [PlatformIO](https://platformio.org/) Team
* To the team and contributors of the [pioarduino](https://github.com/pioarduino/platform-espressif32) project
* To the [BrowserStack](https://www.browserstack.com/) team. This project is tested with BrowserStack.
+1 -1
View File
@@ -12,6 +12,7 @@ lib_deps = ESP32Async/AsyncTCP@^3.5.0
https://github.com/mathieucarbou/MycilaMQTT
https://github.com/mathieucarbou/MycilaHADiscovery
mathieucarbou/MycilaWebSerial@^8.2.3
mathieucarbou/MycilaUtilities@^5.0.0
laxilef/ESP32OT@^1.0.0
;pstolarz/OneWireNg@^0.14.1
https://github.com/pstolarz/OneWireNg.git
@@ -19,7 +20,6 @@ lib_deps = ESP32Async/AsyncTCP@^3.5.0
https://github.com/Laxilef/esp32DHT#idf5
bblanchon/ArduinoJson@^7.4.3
gyverlibs/FileData@^1.0.3
gyverlibs/GyverPID@^3.3.2
gyverlibs/GyverBlinker@^1.1.1
lib_ignore = OneWire
build_type = ${secrets.build_type}
+108 -33
View File
@@ -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());