-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathpid_config.cpp
189 lines (172 loc) · 6.63 KB
/
pid_config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
//
//
#include "pid_config.h"
byte PIDsetSave;
unsigned long timePidPause;
void initPID()
{
HTTP.on("/pidMashingSet", handlePidSet); // Настройка коэффициентов Kp, Ki, Kd
}
void EEPROM_float_write_pid(int addr, float val) {
byte *x = (byte *)&val;
for (byte i = 0; i < 4; i++) EEPROM.write(i + addr, x[i]);
}
float EEPROM_float_read_pid(int addr) {
byte x[4];
for (byte i = 0; i < 4; i++) x[i] = EEPROM.read(i + addr);
float *y = (float *)&x;
return y[0];
}
void loadEepromPid() {
EEPROM.begin(2048);
uint16_t index = 1600;
if (EEPROM.read(index) == 4) {
index++;
Kp = EEPROM_float_read_pid(index); index += 4;
Ki = EEPROM_float_read_pid(index); index += 4;
Kd = EEPROM_float_read_pid(index); index += 4;
setTempForPID = EEPROM_float_read_pid(index);
}
EEPROM.end();
}
void handlePidSet() {
String arg;
//arg = "process";
arg = "save";
PIDsetSave = HTTP.arg(arg).toInt();
if (PIDsetSave == 0) {
arg = "process";
uint8_t processModeOld = processMode.allow;
processMode.allow = HTTP.arg(arg + "[allow]").toInt();
if (processMode.allow != processModeOld) processMode.step = 0;
}
arg = "Kp";
setKp = HTTP.arg(arg + "[userSetValue]").toFloat();
arg = "Ki";
setKi = HTTP.arg(arg + "[userSetValue]").toFloat();
arg = "Kd";
setKd = HTTP.arg(arg + "[userSetValue]").toFloat();
arg = "t1";
setTempForPID = HTTP.arg(arg + "[userSetValue]").toFloat();
//Kp = setKp;
//Ki = setKi;
//Kd = setKd;
if (PIDsetSave != 0) {
Kp = setKp;
Ki = setKi;
Kd = setKd;
// сохраним в EEPROM
EEPROM.begin(2048);
uint16_t index = 1600;
EEPROM.write(index, 0x04); index++;
EEPROM_float_write_pid(index, Kp); index += 4;
EEPROM_float_write_pid(index, Ki); index += 4;
EEPROM_float_write_pid(index, Kd); index += 4;
EEPROM_float_write_pid(index, setTempForPID);
////EEPROM.commit();
////delay(100);
EEPROM.end();
}
//else processMode.step = 0;
String dataForWeb = "{";
dataForWeb += "\"process\":{\"allow\":" + String(processMode.allow) + ",\"save\":" + String(PIDsetSave) + "},";
dataForWeb += "\"settings\":[{\"Kp\":{\"userSetValue\":" + String(setKp) + ",\"deviceOutValue\":" + String(Kp) + "}},";
dataForWeb += "{\"Ki\":{\"userSetValue\":" + String(setKi) + ",\"deviceOutValue\":" + String(Ki) + "}},";
dataForWeb += "{\"Kd\":{\"userSetValue\":" + String(setKd) + ",\"deviceOutValue\":" + String(Kd) + "}},";
// dataForWeb += "\"settings\":[{\"Kp\":{\"userSetValue\":" + String(setKp) + ",\"deviceOutValue\":" + String(setKp) + "}},";
// dataForWeb += "{\"Ki\":{\"userSetValue\":" + String(setKi) + ",\"deviceOutValue\":" + String(setKi) + "}},";
// dataForWeb += "{\"Kd\":{\"userSetValue\":" + String(setKd) + ",\"deviceOutValue\":" + String(setKd) + "}},";
dataForWeb += "{\"t1\":{\"userSetValue\":" + String(setTempForPID) + ",\"deviceOutValue\":" + String(temperatureSensor[numSenseMashBrew].data) + "}}]}";
HTTP.send(200, "text/json", dataForWeb);
//Serial.println(PIDsetSave);
myPID.SetTunings(setKp, setKi, setKd);
Setpoint = setTempForPID;
}
void pidSetLoop() {
switch (processMode.step) {
case 0: {
loadEepromMashing();
loadEepromPid();
// если при выборе нет приоритета берем первый из выбранных
if (temperatureSensor[DS_Cube].member == 1) numSenseMashBrew = DS_Cube;
else if (temperatureSensor[DS_Tube].member == 1) numSenseMashBrew = DS_Tube;
else if (temperatureSensor[DS_Out].member == 1) numSenseMashBrew = DS_Out;
else if (temperatureSensor[DS_Def].member == 1) numSenseMashBrew = DS_Def;
else if (temperatureSensor[DS_Res1].member == 1) numSenseMashBrew = DS_Res1;
else if (temperatureSensor[DS_Res2].member == 1) numSenseMashBrew = DS_Res2;
else if (temperatureSensor[DS_Res3].member == 1) numSenseMashBrew = DS_Res3;
else if (temperatureSensor[DS_Res4].member == 1) numSenseMashBrew = DS_Res4;
// если есть приоритет
if (temperatureSensor[DS_Cube].priority == 1) numSenseMashBrew = DS_Cube;
else if (temperatureSensor[DS_Tube].priority == 1) numSenseMashBrew = DS_Tube;
else if (temperatureSensor[DS_Out].priority == 1) numSenseMashBrew = DS_Out;
else if (temperatureSensor[DS_Def].priority == 1) numSenseMashBrew = DS_Def;
else if (temperatureSensor[DS_Res1].priority == 1) numSenseMashBrew = DS_Res1;
else if (temperatureSensor[DS_Res2].priority == 1) numSenseMashBrew = DS_Res2;
else if (temperatureSensor[DS_Res3].priority == 1) numSenseMashBrew = DS_Res3;
else if (temperatureSensor[DS_Res4].priority == 1) numSenseMashBrew = DS_Res4;
processMode.timeStart = time(nullptr);
// подготовка данных для вывода на TFT
#if defined TFT_Display
csOn(TFT_CS);
graphOutInterval = Display_out_temp;
//tft.fillScreen(ILI9341_BLACK);
tftStartForGraph();
displayTimeInterval = millis() + 1000;
DefCubOut = Display_out_temp;
csOff(TFT_CS);
#endif
tempBigOut = 1;
myPID.SetTunings(setKp, setKi, setKd);
if (powerType <= 1) myPID.SetOutputLimits(0, WindowSize); // временной интервал реагирования для PID
else {
myPID.SetSampleTime(1000);
myPID.SetOutputLimits(0, 100);
}
myPID.SetMode(AUTOMATIC);
Setpoint = setTempForPID;
timePauseOff = millis();
processMode.step = 1;
break;
}
case 1: {
Input = temperatureSensor[numSenseMashBrew].data;
if (powerType <= 1) {
if (timePidPause < millis()) {
myPID.Compute(); // расчет времени для PID регулировки
if (millis() > WindowSize + windowStartTime) {
windowStartTime += WindowSize;
if (windowStartTime > millis()) windowStartTime = 0; // защита от переполнения
}
// Если идет предварительный нагрев (до температуры поддержания Т стабилизации минус 4 градуса)
if (Input < Setpoint - 4) {
digitalWrite(heater, HIGH);
power.heaterPower = 100;
}
else if (Input > Setpoint + 2) {
digitalWrite(heater, LOW);
power.heaterPower = 0;
}
// включить или выключить ТЭН в зависимости от расчетов временного PID регулирования
else {
if (Output < millis() - windowStartTime) {
digitalWrite(heater, LOW);
power.heaterPower = 0;
}
else {
digitalWrite(heater, HIGH);
power.heaterPower = 100;
}
}
timePidPause = millis() + 100;
}
}
else {
myPID.Compute();
power.heaterPower = (uint8_t)Output;
}
break;
}
}
}