-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHeaterController.cpp
310 lines (262 loc) · 8.87 KB
/
HeaterController.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
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
/**
* Controls the beds that are responsible for heating the filament box.
* Encapsulates the PID and Tunner library.
*
* MIT License
*
* Copyright (c) 2022 Juan Schiavoni
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "HeaterController.h"
HeaterController::HeaterController(int pwm_freq, int pwm_res, float max_bed, ParamStorage& storage) :
pstorage(storage),
pid(&pid_input, &pid_output, &pid_setpoint),
tuner(&tune_input, &tune_output, tuner.ZN_PID, tuner.directIP, tuner.printOFF) {
_mode = MODE_STOP;
pid_status = ST_DISABLED;
tune_status = ST_DISABLED;
pwm_frequency = pwm_freq;
pwm_resolution = pwm_res;
max_bed_temp = max_bed;
tune_settle_time_sec = 10;
tune_test_time_sec = 250; // runPid interval = testTimeSec / samples (500 mS)
tune_samples = 500;
tune_input_span = 60;
tune_output_span = pwm_res;
tune_output_start = 0;
tune_output_step = (pwm_res * 40) / 100; // Set initial step in % depending on pwm resolution;
tune_temp_limit = 60;
tune_debounce = 1;
tune_samples_count = 0;
pid_input = 0;
pid_output = 0;
tune_input = 0;
tune_output = 0;
tune_setpoint = 50;
// The sample time of the PID has to match that of the tuner, and uses uS as the unit.
pid_sample_timeout = (tune_test_time_sec * 1000000) / tune_samples;
}
bool HeaterController::begin(void) {
tuner.Configure(tune_input_span, tune_output_span, tune_output_start,
tune_output_step, tune_test_time_sec, tune_settle_time_sec, tune_samples);
tuner.SetEmergencyStop(tune_temp_limit);
pid.SetSampleTimeUs(pid_sample_timeout); // 500 mS en uS
pid.SetOutputLimits(0, pwm_resolution);
pid.SetProportionalMode(pid.pMode::pOnMeas);
pid.SetAntiWindupMode(pid.iAwMode::iAwClamp);
pid_setpoint = pstorage.setpoint();
set_tunings();
analogWriteFreq(pwm_frequency);
analogWriteRange(pwm_resolution);
pinMode(FAN_PIN, OUTPUT);
fan_cooler(OUT_OFF);
pwm(OUT_OFF);
/* attaches the servo on PIN GI28.
* min pulse width in microseconds, corresponding to 0 degree angle (defaults to 544)
* max pulse width in microseconds, corresponding to 180 degree angle (defaults to 2400)
* See: https://github.com/earlephilhower/arduino-pico/issues/776
*/
moisture_servo.attach(MOISTURE_SERVO_PIN, 544, 2400);
return moisture_door(false);
}
float HeaterController::update(float box_temp, float bed_temp) {
float output = 0;
if (_mode == MODE_RUN_PID) {
output = pid_controller(box_temp, bed_temp);
} else if (_mode == MODE_RUN_TUNE){
output = tune_controller(box_temp, bed_temp);
}
pwm(output);
return output;
}
void HeaterController::inc_setpoint(int count) {
int new_val = pstorage.setpoint() + count;
// The temperature can be regulated between 40 and 60 degrees. 0 = disabled
if ((pstorage.setpoint() == 0) && (new_val < MIN_SETPOINT) && (count > 0)) {
pstorage.write_setpoint(MIN_SETPOINT);
} else if(new_val < MIN_SETPOINT) {
pstorage.write_setpoint(0);
} else if (new_val <= MAX_SETPOINT) {
pstorage.write_setpoint(new_val);
}
// It is necessary to keep a local copy of the setpoint because the PID library uses it as a reference.
pid_setpoint = pstorage.setpoint();
}
int HeaterController::get_setpoint(void) {
return pstorage.setpoint();
}
int HeaterController::get_mode(void) {
return _mode;
}
void HeaterController::set_mode(int new_mode) {
_mode = new_mode;
}
void HeaterController::start(void) {
if (_mode == MODE_RUN_TUNE) {
tune_status = ST_INITIALICE;
pid_status = ST_DISABLED;
} else if (_mode == MODE_RUN_PID) {
pid_status = ST_INITIALICE;
tune_status = ST_DISABLED;
}
}
void HeaterController::stop(void) {
fan_cooler(OUT_OFF);
moisture_door(false);
pwm(OUT_OFF);
pid_status = ST_DISABLED;
tune_status = ST_DISABLED;
_mode = MODE_STOP;
}
void HeaterController::pwm(int output) {
analogWrite(HOT_BED_LEFT_PIN, output);
analogWrite(HOT_BED_RIGHT_PIN, output);
}
void HeaterController::fan_cooler(int output) {
digitalWrite(FAN_PIN, (output == OUT_ON) ? HIGH : LOW );
}
float HeaterController::pid_controller(float box_temp, float bed_temp) {
pid_input = box_temp;
switch (pid_status) {
case ST_DISABLED:
pid.SetMode(pid.Control::manual);
fan_cooler(OUT_OFF);
moisture_door(false);
break;
case ST_INITIALICE:
pid.SetMode(pid.Control::manual);
pid_output = 0;
pid.SetMode(pid.Control::automatic);
fan_cooler(OUT_ON);
moisture_door(true);
pid_status = ST_RUN_PID;
break;
case ST_RUN_PID:
if (bed_temp > max_bed_temp) {
pid.SetMode(pid.Control::manual);
pid_status = ST_WAIT_BED_TEMP_DROP;
}
break;
case ST_WAIT_BED_TEMP_DROP:
if (bed_temp < (max_bed_temp - 5)) {
pid_status = ST_RUN_PID;
pid_output = 0;
pid.SetMode(pid.Control::automatic);
}
break;
default:
pid_status = ST_DISABLED;
break;
}
pid.Compute();
return (pid_status == ST_RUN_PID) ? pid_output : 0;
}
float HeaterController::tune_controller(float input, float bed_temp) {
switch (tune_status) {
case ST_INITIALICE:
pid_status = ST_DISABLED;
pid.SetMode(pid.Control::manual);
pid_output = 0;
fan_cooler(OUT_ON);
moisture_door(true);
tuner.Reset();
tune_samples_count = 0;
tune_status = ST_RUN_PID;
break;
case ST_RUN_PID: {
if (bed_temp > max_bed_temp) {
tune_status = ST_WAIT_BED_TEMP_DROP;
} else {
switch (tuner.Run()) {
case tuner.sample:
tune_input = input;
tune_samples_count++;
break;
// update PID with the new tunings
case tuner.tunings:
float kp, ki, kd;
tuner.GetAutoTunings(&kp, &ki, &kd);
pstorage.write_pid_const(kp, ki, kd);
pstorage.save();
set_tunings();
stop();
break;
}
}
}
break;
case ST_WAIT_BED_TEMP_DROP:
if (bed_temp < (max_bed_temp - 5)) {
tune_status = ST_RUN_PID;
}
break;
default:
tune_status = ST_DISABLED;
break;
}
return (tune_status == ST_RUN_PID) ? tune_output : 0;
}
void HeaterController::set_tunings(void) {
pid.SetTunings(pstorage.kp(), pstorage.ki(), pstorage.kd());
}
bool HeaterController::setpoint_reached(void) {
if (pid_status == ST_RUN_PID) {
float diff = get_setpoint() - pid_input;
if ((diff >= -0.5) && (diff <= 0.5)) {
return true;
}
}
return false;
}
int HeaterController::tuning_percentage(void) {
/*
* The tuner takes a sample every 1000 mS, both in the setup time and in the
* computation time. In order for the percentage to match the number of samples,
* the waiting seconds must be added to the total.
*/
float total = tune_samples_count;
total /= (tune_samples + tune_settle_time_sec);
total *= 100;
return total;
}
bool HeaterController::moisture_door(bool state) {
// state control the gate, true when open and false when closed.
int angle = state ? pstorage.moisture_open_angle() : pstorage.moisture_close_angle();
return moisture_move(angle);
}
void HeaterController::set_moisture_angle(bool state, int value) {
// state control the gate, true when open and false when closed.
int angle = state ? pstorage.moisture_open_angle() : pstorage.moisture_close_angle();
angle += value;
if (moisture_move(angle)) {
if (state) {
pstorage.write_moisture_open_angle(angle);
} else {
pstorage.write_moisture_close_angle(angle);
}
}
}
bool HeaterController::moisture_move(int angle) {
// Control the position of the moisture vent door.
if ((angle >= MOISTURE_DOOR_MIN) && (angle <= MOISTURE_DOOR_MAX)) {
moisture_servo.write(angle);
return (moisture_servo.read() == angle);
}
return false;
}