From 8f9584a8189a1a907c3ae80f931dc4c30144c543 Mon Sep 17 00:00:00 2001 From: Aleksander Belov Date: Fri, 6 Sep 2024 11:27:26 +0700 Subject: [PATCH] GyverPID --- hot_fermentation.ino | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/hot_fermentation.ino b/hot_fermentation.ino index b91eb81..6cecdf6 100644 --- a/hot_fermentation.ino +++ b/hot_fermentation.ino @@ -3,7 +3,6 @@ #define TempSensorDallas #include -#include #include #include "GyverEncoder.h" @@ -80,9 +79,12 @@ bool isInTransition = false; bool inSelectionMode = true; // PID Control variables +// double Kp = 0.5, Ki = 1, Kd = 0.05; +// PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); + +#include "GyverPID.h" double Setpoint, Input, Output; -double Kp = 0.5, Ki = 1, Kd = 0.05; -PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); +GyverPID regulator(1, 0.5, 0, 1000); // Timing variables long phaseStartTime; @@ -118,9 +120,9 @@ void setup() { oled.init(); // Initialize the OLED oled.clear(); // Clear the display readEEPROM(); - myPID.SetMode(AUTOMATIC); - myPID.SetOutputLimits(0, 100); - // myPID.SetSampleTime(1000); + regulator.setDirection(NORMAL); // направление регулирования (NORMAL/REVERSE). ПО УМОЛЧАНИЮ СТОИТ NORMAL + regulator.setLimits(0, 100); // пределы (ставим для 8 битного ШИМ). ПО УМОЛЧАНИЮ СТОЯТ 0 И 255 + regulator.setpoint = 0; #ifdef TempSensorDallas sensors.begin(); @@ -296,7 +298,10 @@ void handleExecution() { if (isnan(Input)) { Input = 0; } - myPID.Compute(); + regulator.input = (float)Input; + regulator.setpoint = Setpoint; + regulator.getResult(); + Output = regulator.output; if (isComplete && currentPhase >= activeProfile.numPhases && !finishTime) { finishTime = currentTime; @@ -311,8 +316,7 @@ void startExecution() { inSelectionMode = false; // Switch to execution mode phaseStartTime = totalStartTime = millis(); // Start the timer totalElapsedTime = 0; - myPID.SetMode(AUTOMATIC); - myPID.SetOutputLimits(0, 100); // SSR is either ON or OFF + regulator.setpoint = 0; // digitalWrite(ssrPin, HIGH); // Start with heater on ssrLastSwitchTime = millis(); calculateTotalTime();