Repository navigation
Expand file tree
/
Copy pathHeatControl.cpp
More file actions
319 lines (265 loc) · 10.1 KB
/
Copy pathHeatControl.cpp
File metadata and controls
319 lines (265 loc) · 10.1 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <Arduino.h>
#include "SensorSampler.h"
#include "HeatControl.h"
#include "HomeAssistant.h"
#include "MqttParamManager.h"
#include "PID.h"
#include "IO.h"
#include "Debug.h"
#include "hardware.h"
#include "config.h"
namespace Defaults {
// How often PID is calculated
// NOTE: If this is changed, the coefficients will need to be updated too
static const unsigned long UpdatePeriodMs = 1000;
static float SetPoint = CONFIG_BOILER_TEMPERATURE_C;
static const float Kp = 20.0f;
static const float Ki = 0.10f; //Kp / 20.0f; // Ki = Kp / Tn
static const float Kd = 10.0f; //Kp * 1.0f; // Kd = Kp * Tv
static const float Kd2 = 10.0f; //Kp * 1.0f;
// Probably dependent on thermal losses to environment
// This was emperically determined with the cover off, ambient temperature ~20C
static const float PlantOffset = 10.0f;
static const float MaxBoilerTemperature = CONFIG_MAX_BOILER_TEMPERATURE_C;
static const float RegulationRange = CONFIG_BOILER_PID_RANGE_C;
}
// TODO: May need to incorporate another temperature probe for steam water,
// as currently the control loop doesn't respond to a drop in main boiler temperature
// until far too late.
// Or, we could use a second pressure transducer which would respond very quickly,
// and also let us detect whether steam is being used.
static fPID pid;
static fPID pid2;
const float MAX_BOILER_TEMPERATURE = CONFIG_MAX_BOILER_TEMPERATURE_C;
namespace HeatControl {
static BoilerProfile operating_profile = BoilerProfile::Off;
void updatePidCoefficients();
// Configuration parameters to expose to MQTT
MqttParam::Parameter<float> param_kp("pid/boiler/kp", Defaults::Kp, [] (float val) { updatePidCoefficients(); });
MqttParam::Parameter<float> param_ki("pid/boiler/ki", Defaults::Ki, [] (float val) { updatePidCoefficients(); });
MqttParam::Parameter<float> param_kd("pid/boiler/kd", Defaults::Kd, [] (float val) { updatePidCoefficients(); });
MqttParam::Parameter<float> param_kd2("pid/boiler/kd2", Defaults::Kd2, [] (float val) { updatePidCoefficients(); });
MqttParam::Parameter<float> param_po("pid/boiler/po", Defaults::PlantOffset, [] (float val) { updatePidCoefficients(); });
MqttParam::Parameter<float> param_boilerTemp("brew/boiler_temp", CONFIG_BOILER_TEMPERATURE_C, [] (float val) { setProfile(operating_profile); });
MqttParam::Parameter<float> param_steamTemp("brew/steam_temp", CONFIG_BOILER_STEAM_TEMPERATURE_C, [] (float val) { setProfile(operating_profile); });
// DEBUG
ValueArray<float, numSamples> pid_i;
ValueArray<float, numSamples> pid_d;
ValueArray<float, numSamples> pid_d2;
void initControlLoop()
{
pid.reset();
pid.setOutputLimits(0.0f, 100.0f);
pid.setParameters(Defaults::Kp, Defaults::Ki, Defaults::Kd);
pid.setSetpoint(Defaults::SetPoint);
pid.setSampleTime(Defaults::UpdatePeriodMs);
pid.setPlantOffset(Defaults::PlantOffset);
pid.setRegulationRange(Defaults::RegulationRange);
// D term only, to account for main boiler transients
pid2.reset();
//pid2.setOutputLimits(-20.0f, 20.0f);
pid2.setParameters(0.0f, 0.0f, Defaults::Kd2);
pid2.setSampleTime(Defaults::UpdatePeriodMs);
updatePidCoefficients();
// TODO: Prevent integral from going below 0, as it can
// take a LOT longer to cool down than it will to warm up,
// so the integral term will work against us.
setProfile(BoilerProfile::Brew);
}
void updatePidCoefficients() {
float kp = param_kp.value();
float ki = param_ki.value();
float kd = param_kd.value();
float kd2 = param_kd2.value();
float po = param_po.value();
pid.setParameters(kp, ki, kd);
pid.setPlantOffset(po);
pid.reset();
pid2.setParameters(0.0f, 0.0f, kd2);
pid2.reset();
Debug.printf("Boiler PID Parameters:\n\tKp: %.4f\n\tKi: %.4f\n\tKd: %.4f\n\tKd2: %.4f\n\tOf: %.4f\n",
pid.getKp(),
pid.getKi(),
pid.getKd(),
pid2.getKd(),
po
);
}
void publishTuningData(float pid_input, float pid_output) {
float t_sec = millis() * 0.001f;
char s[50];
snprintf(s, sizeof(s), "%.1f,%.2f,%.1f",
t_sec,
pid_input,
pid_output
);
Debug.printf("Tuning: %s\n", s);
HomeAssistant::publishData("lupa/tuning/boiler", s);
}
const float tuningPhaseSetpoint[] = {
20.0f,
40.0f,
60.0f,
80.0f,
100.0f,
0.0f
};
float calculateTuningTick(float pid_input) {
static float output = 0.0f;
static unsigned long tuning_interval_ms = 1*60*1000;
static float last_input = 0.0f;
static unsigned long t_last = 0;
static int tuning_phase = 0;
const int n_phases = sizeof(tuningPhaseSetpoint)/sizeof(tuningPhaseSetpoint[0]) * 2;
if (t_last == 0) {
t_last = millis();
last_input = pid_input;
}
// // 100% if below setpoint, 0% if above, with 1C hysteresis
// if (pid_input < (CONFIG_BOILER_TUNING_TEMPERATURE_C + 1.0f)) {
// pid_output = 100.0f;
// }
// else if (pid_input > (CONFIG_BOILER_TUNING_TEMPERATURE_C - 1.0f)) {
// pid_output = 0.0f;
// }
if ((millis() - t_last) > tuning_interval_ms) {
t_last = millis();
if (tuning_phase == n_phases) {
output = 0.0f;
Debug.println("[ TUNING DONE ]");
}
else {
tuning_phase++;
Debug.printf("[ TUNING PHASE: %d ]\n", tuning_phase);
if ((tuning_phase & 1) == 0) {
// odd numbers
output = 0.0f;
tuning_interval_ms = 2*60*1000; // cool
}
else {
// even numbers
output = tuningPhaseSetpoint[tuning_phase >> 1];
tuning_interval_ms = 2*60*1000; // heat
}
Debug.printf("Setpoint: %.1f\n", output);
Debug.printf("Interval: %dms\n", tuning_interval_ms);
}
}
return output;
}
void processControlLoop()
{
// TODO: Watchdog for safety.
static unsigned long t_last = 0;
static unsigned long t_last_pid = 0;
static float pid_output = 0.0f;
static int tuning_phase = 0;
if (!SensorSampler::isTemperatureValid() ||
IO::isWaterTankLow() ||
IO::isBoilerTankLow() ||
(operating_profile == BoilerProfile::Off))
{
//IO::setHeat(false);
IO::setHeatPower(0.0f);
}
else {
// Regulate the heat exchange (HX) loop temperature by applying PID
// to the HX temperature sensor.
// Optionally add a differential term from the main boiler temperature
// sensor to help respond quicker (since the main boiler is closer to the heating element).
// Typically there is about a 5C difference between HX and the boiler.
// TODO: Temperature2 should be optional
float pid_input = SensorSampler::getTemperature(); // HX
float pid_input_2 = SensorSampler::getTemperature2(); // Boiler
if (pid_input > Defaults::MaxBoilerTemperature) {
//IO::setHeat(false);
IO::setHeatPower(0.0f);
pid.reset();
pid2.reset();
return;
}
// Not yet near the pid_setpoint, 100% duty until we get close
if (pid_input < (pid.getSetpoint() - Defaults::RegulationRange)) {
if (IO::getHeatPower() < 1.0f) {
Debug.printf("PREHEAT: %.1f\n", pid_input);
}
//IO::setHeat(true);
IO::setHeatPower(1.0f);
pid.reset();
pid2.reset();
return;
}
else {
#if false
// Override PID when water is flowing
const float flow_perturbation_coeff = 1.0f;
float offset = 0.0f;
if (SensorSampler::isFlowRateValid()) {
auto flow = SensorSampler::getFlowRate();
if (flow > 1.0f) {
// Perturb the PID controller error to predict
// the fact that the temperature will start to decrease soon.
// Flow ranges from 0 to 15, let's lazily convert this to a DegC offset
// so faster flows result in a larger offset...
offset = flow * flow_perturbation_coeff;
}
}
pid.setPerturbationOffset(offset);
#endif
auto t_now = millis();
if ((t_now - t_last_pid) >= Defaults::UpdatePeriodMs) {
t_last_pid = t_now;
if (operating_profile == BoilerProfile::Tuning) {
pid_output = calculateTuningTick(pid_input);
publishTuningData(pid_input, pid_output);
}
else {
float dterm = pid2.calculateTick(pid_input_2);
pid_output = pid.calculateTick(pid_input) + dterm;
// For debug:
pid_i.add(pid.getI());
pid_d.add(pid.getD());
pid_d2.add(pid2.getD());
}
//Debug.printf("PID: I=%.1f, S=%.1f, O=%.1f\n", pid_input, pid_setpoint, pid_output);
IO::setHeatPower(pid_output * 0.01f);
}
}
}
}
void setProfile(BoilerProfile mode) {
operating_profile = mode;
Debug.print("Boiler heat profile: ");
switch (mode) {
case BoilerProfile::Off:
Debug.println("Off");
pid.reset();
pid2.reset();
break;
case BoilerProfile::Brew:
Debug.println("Brew");
//pid.setSetpoint(CONFIG_BOILER_TEMPERATURE_C);
pid.setSetpoint(param_boilerTemp.value());
break;
case BoilerProfile::Steam:
Debug.println("Steam");
//pid.setSetpoint(CONFIG_BOILER_STEAM_TEMPERATURE_C);
pid.setSetpoint(param_steamTemp.value());
break;
case BoilerProfile::Idle:
Debug.println("Idle");
pid.setSetpoint(CONFIG_BOILER_IDLE_TEMPERATURE_C);
break;
case BoilerProfile::Tuning:
Debug.println("Tuning");
pid.setSetpoint(CONFIG_BOILER_TUNING_TEMPERATURE_C);
break;
}
}
BoilerProfile getProfile() {
return operating_profile;
}
float getSetpoint() {
return pid.getSetpoint();
}
}