-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilament_dryer_rp2040.ino
508 lines (460 loc) · 15.5 KB
/
filament_dryer_rp2040.ino
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**
* 3D Filament dryer using a Raspberry Pi Pico (RP2040).
*
* Uses a bed heating to remove moisture from the air in order to
* prevent filament degradation and improve 3D printing quality.
*
* 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 "TempSensors.h"
#include "HeaterController.h"
#include "RunTimer.h"
#include "menu_config.h"
#include "ParamStorage.h"
#include "Odometer.h"
#include "DoorLidSensor.h"
#define FIRMWARE_VERSION "1.1.4" // Version actual del firmware.
#define SAMPLE_TIMEOUT_100MS 100 // Refresh time for the sensor
/*
* NOTE: with arduinopico version 2.6.2 the resolution in 8 bits does not work
* when two pins are used. See issue:
* https://github.com/earlephilhower/arduino-pico/issues/955
*
* It is fixed with push #926 and can be used from the master branch using git.
* https://github.com/earlephilhower/arduino-pico/pull/962
*/
#define PWM_FREQUENCY 500 // Set a similar frequency of the Arduino Nano PWM (490Hz).
#define PWM_RESOLUTION 255 // Set 8 bits for PWM resolution (0-255).
#define MAX_HOURS 72
#define BED_MAX_TEMP 80.00
#define SPLASH_VERSION_TIME 5000 // Version screen display time in mS.
uint8_t refresh_display = 10;
unsigned long splash_timer;
bool factory_reset = false;
bool calib_thermistors = false;
ParamStorage param_storage(KP_DEFAULT, KI_DEFAULT, KD_DEFAULT, THERMS_DEFAULT,
SETPOINT_DEFAULT, TIME_DEFAULT, ODOM_MODE_DEFAULT,
ODOM_MINUTES_DEFAULT, ODOM_TURNS_DEFAULT,
ODOM_DIAMETER_DEFAULT, MOISTURE_CLOSE_DEFAULT,
MOISTURE_OPEN_DEFAULT, THERM_CALIB_FACTOR_DEFAULT,
THERM_CALIB_FACTOR_DEFAULT);
TempSensors sensors(SAMPLE_TIMEOUT_100MS, param_storage);
HeaterController heater(PWM_FREQUENCY, PWM_RESOLUTION, BED_MAX_TEMP, param_storage);
RunTimer timer(MAX_HOURS, param_storage);
UserInterface ui(menu_list, sizeof(menu_list));
Odometer odometer(TCRT5000_D0_PIN, param_storage);
DoorLidSensor lid_sensor(DOOR_LID_PIN, DOOR_LID_DEBOUNCE);
void bool_selection(char* str_buff, bool value) {
if (value) {
sprintf(str_buff, "ON");
} else {
sprintf(str_buff, "OFF");
}
}
void odom_mode_sel(char* str_buff, int mode) {
switch (mode) {
case ODOM_MODE_DISABLED:
sprintf(str_buff, "OFF");
break;
case ODOM_MODE_START:
sprintf(str_buff, "STAR");
break;
case ODOM_MODE_STOP:
sprintf(str_buff, "STOP");
break;
case ODOM_MODE_BOTH:
sprintf(str_buff, "BOTH");
break;
default:
sprintf(str_buff, "-");
}
}
/*
* Callback function that invokes the UI when it needs to update the
* parameters of each configuration menu item.
*/
char* callback_menu_get(char* str_buff, int item_id) {
switch (item_id) {
case MNU_TURN_ENABLE_ID:
bool_selection(str_buff, heater.get_mode() == MODE_RUN_PID);
break;
case MNU_BOX_TEMP_ID:
sprintf(str_buff, "%d", heater.get_setpoint());
break;
case MNU_REMAINING_TIME_ID:
sprintf(str_buff, "%d", timer.get_time());
break;
case MNU_TUNE_ENABLE_ID:
bool_selection(str_buff, heater.get_mode() == MODE_RUN_TUNE);
break;
case MNU_THERMISTORS_ID:
sprintf(str_buff, "%d", sensors.get_therms());
break;
case MNU_CALIB_THERMISTORS_ID:
bool_selection(str_buff, calib_thermistors);
break;
case MNU_HEATER_TEMP_ID:
sprintf(str_buff, "%02.0f", max(sensors.bed_left_celcius(), sensors.bed_right_celcius()));
break;
case MNU_ODOM_MODE_ID:
odom_mode_sel(str_buff, odometer.get_mode());
break;
case MNU_ODOM_MINUTES_ID:
sprintf(str_buff, "%d", odometer.get_minutes());
break;
case MNU_ODOM_TURNS_ID:
sprintf(str_buff, "%2.1f", odometer.get_turns());
break;
case MNU_ODOM_DIAMETER_ID:
sprintf(str_buff, "%d", odometer.get_diameter());
break;
case MNU_MOISTURE_CLOSE_ID:
sprintf(str_buff, "%d", param_storage.moisture_close_angle());
break;
case MNU_MOISTURE_OPEN_ID:
sprintf(str_buff, "%d", param_storage.moisture_open_angle());
break;
case MNU_KP_ID:
sprintf(str_buff, "%2.2f", param_storage.kp());
break;
case MNU_KI_ID:
sprintf(str_buff, "%2.2f", param_storage.ki());
break;
case MNU_KD_ID:
sprintf(str_buff, "%2.2f", param_storage.kd());
break;
case MNU_FACTORY_RESET_ID:
bool_selection(str_buff, factory_reset);
break;
case MNU_FIRMWARE_VERSION_ID:
sprintf(str_buff, "%s", FIRMWARE_VERSION);
break;
default:
sprintf(str_buff, "--");
break;
}
return str_buff;
}
/*
* Callback function that invokes the UI when editing is complete.
*/
void callback_menu_end_edit(int item_id) {
switch (item_id) {
case MNU_TURN_ENABLE_ID:
if (heater.get_mode() == MODE_RUN_PID) {
timer.start();
heater.start();
odometer.reset_timer();
} else if (heater.get_mode() == MODE_STOP) {
timer.reset();
heater.stop();
}
break;
case MNU_TUNE_ENABLE_ID:
if ((heater.get_mode() == MODE_RUN_TUNE)) {
timer.reset();
heater.start();
} else if (heater.get_mode() == MODE_STOP) {
timer.reset();
heater.stop();
}
break;
case MNU_THERMISTORS_ID:
case MNU_BOX_TEMP_ID:
case MNU_REMAINING_TIME_ID:
case MNU_ODOM_MODE_ID:
case MNU_ODOM_MINUTES_ID:
case MNU_ODOM_TURNS_ID:
case MNU_ODOM_DIAMETER_ID:
case MNU_MOISTURE_CLOSE_ID:
case MNU_MOISTURE_OPEN_ID:
param_storage.save();
break;
case MNU_FACTORY_RESET_ID:
if (factory_reset) {
factory_reset = false;
heater.set_mode(MODE_STOP);
param_storage.write_pid_const(KP_DEFAULT, KI_DEFAULT, KD_DEFAULT);
param_storage.write_therms(THERMS_DEFAULT);
param_storage.write_setpoint(SETPOINT_DEFAULT);
param_storage.write_time(TIME_DEFAULT);
param_storage.write_odom_mode(ODOM_MODE_DEFAULT);
param_storage.write_odom_minutes(ODOM_MINUTES_DEFAULT);
param_storage.write_odom_diameter(ODOM_DIAMETER_DEFAULT);
param_storage.write_moisture_close_angle(MOISTURE_CLOSE_DEFAULT);
param_storage.write_moisture_open_angle(MOISTURE_OPEN_DEFAULT);
param_storage.write_calib_factor_therm_1(THERM_CALIB_FACTOR_DEFAULT);
param_storage.write_calib_factor_therm_2(THERM_CALIB_FACTOR_DEFAULT);
param_storage.save();
heater.set_tunings();
}
break;
case MNU_CALIB_THERMISTORS_ID:
if (calib_thermistors) {
calib_thermistors = false;
if (sensors.calib_therms()) {
param_storage.save();
}
}
break;
}
}
/*
* Callback function that invokes the UI on exit.
*/
void callback_menu_exit(int item_id) {
refresh_display = 10;
}
/*
* Callback function that invokes the UI when it needs to change the
* parameters of one configuration menu item.
*/
bool callback_menu_set(int value, int item_id) {
switch (item_id) {
case MNU_TURN_ENABLE_ID:
if ((heater.get_setpoint() > 0) && (timer.get_time() > 0)) {
heater.set_mode((value > 0) ? MODE_RUN_PID : MODE_STOP);
}
break;
case MNU_BOX_TEMP_ID:
heater.inc_setpoint(value);
break;
case MNU_REMAINING_TIME_ID:
timer.inc_time(value);
break;
case MNU_TUNE_ENABLE_ID:
heater.set_mode((value > 0) ? MODE_RUN_TUNE : MODE_STOP);
break;
case MNU_THERMISTORS_ID:
sensors.set_therms(value);
break;
case MNU_CALIB_THERMISTORS_ID:
calib_thermistors = (value > 0) ? true : false;
break;
case MNU_FACTORY_RESET_ID:
factory_reset = (value > 0) ? true : false;
break;
case MNU_ODOM_MODE_ID:
odometer.set_mode(value);
break;
case MNU_ODOM_MINUTES_ID:
odometer.set_minutes(value);
break;
case MNU_ODOM_DIAMETER_ID:
odometer.set_diameter(value);
break;
case MNU_ODOM_TURNS_ID:
odometer.set_turns(value);
break;
case MNU_MOISTURE_CLOSE_ID:
heater.set_moisture_angle(false, value);
break;
case MNU_MOISTURE_OPEN_ID:
heater.set_moisture_angle(true, value);
break;
default:
return false;
}
return true;
}
/*
* turns on the PID, when it detects that the filament spool is moving
* and is not in tuning mode.
*/
void callback_odom_start(void) {
if (heater.get_mode() == MODE_STOP) {
heater.set_mode(MODE_RUN_PID);
timer.start();
heater.start();
odometer.reset_timer();
}
}
/*
* Turn off the PID when detects that the filament spool has not
* moved for the programmed time.
* As long as the PID has reached the programmed temperature.
*/
void callback_odom_stop(void) {
if (heater.setpoint_reached()) {
timer.reset();
heater.stop();
} else {
/*
* Reset the shutdown timer to prevent the stop callback from
* firing continuously while the desired temperature is reached
*/
odometer.reset_timer();
}
}
/*
* Displays the firmware version screen for 3 seconds or until the encoder is pressed.
*/
bool display_version() {
bool ret_val = (ui.clicks() == 0);
if (ret_val) {
ret_val = (splash_timer + SPLASH_VERSION_TIME > millis());
if (ret_val) {
ui.display.clearDisplay();
ui.display.setTextSize(2);
ui.display.setTextColor(WHITE);
ui.display.setCursor(15, 0);
ui.display.println("Filament");
ui.display.setCursor(35, 20);
ui.display.println("Dryer");
ui.display.setCursor(35, 40);
ui.display.print(FIRMWARE_VERSION);
ui.display.display();
}
}
return ret_val;
}
/*
* Shows the runing information.
*/
void update_display_info() {
char buff[100];
ui.display.clearDisplay();
ui.display.setTextSize(2);
ui.display.setTextColor(WHITE);
ui.display.setCursor(22, 0);
int setpoint = (heater.get_mode() == MODE_STOP) ? 0 : heater.get_setpoint();
sprintf(buff, "%02.0f/%02d C", sensors.box_celcius(), setpoint);
ui.display.print(buff);
if (lid_sensor.state()) {
ui.display.setCursor(15, 22);
sprintf(buff, "Lid Open");
}else {
if (heater.get_mode() == MODE_RUN_TUNE) {
ui.display.setCursor(10, 22);
sprintf(buff, "Bed %02.0f-%02.0f", sensors.bed_left_celcius(), sensors.bed_right_celcius());
} else {
ui.display.setCursor(15, 22);
sprintf(buff, "%02d:%02d:%02d", timer.get_hours(), timer.get_minutes(), timer.get_seconds());
}
}
ui.display.print(buff);
if (heater.get_mode() == MODE_RUN_TUNE) {
ui.display.setCursor(10, 42);
sprintf(buff, "Tune %02d %%", heater.tuning_percentage());
} else {
ui.display.setCursor(27, 42);
sprintf(buff, "%02.1f %%", sensors.box_humidity());
}
ui.display.print(buff);
ui.display.display();
}
/*
* It sends the controller parameters through the serial port to graph the behavior
* curve of the system, both in PID and Tune modes.
* Note: Since the system takes more than 20 minutes to stabilize, it sends a sample
* every 6 seconds so that it can be displayed in 500 samples.
*/
void plot_pid(float pwm_val, float bed_temp) {
static uint8_t plot_delay = 0;
if (Serial) {
if (heater.get_mode() == MODE_STOP) {
plot_delay = 0;
} else if ((plot_delay++ == 0) || (heater.get_mode() == MODE_RUN_TUNE)) {
pwm_val /= PWM_RESOLUTION;
pwm_val *= 100;
Serial.print(F("Setpoint:")); Serial.print(heater.get_setpoint()); Serial.print(F(", "));
Serial.print(F("BoxTemp:")); Serial.print(sensors.box_celcius()); Serial.print(F(", "));
Serial.print(F("BoxHumidity:")); Serial.print(sensors.box_humidity()); Serial.print(F(", "));
Serial.print(F("PWM:")); Serial.print(pwm_val); Serial.print(F(", "));
if (sensors.get_therms() > 0) {
Serial.print(F("BedTemp:")); Serial.print(bed_temp); Serial.print(F(", "));
Serial.print(F("BedMax:")); Serial.print(BED_MAX_TEMP);
}
Serial.println();
} else if (plot_delay >= 6) {
plot_delay = 0;
}
}
}
/*
* Show one time the introduction message when USB serial port is connected.
*/
void show_intro_msg(void) {
static bool one_time = true;
if (Serial && one_time) {
Serial.print("Filament dryer controller - version: ");
Serial.println(FIRMWARE_VERSION);
Serial.print("Kp: "); Serial.print(param_storage.kp());
Serial.print(" Ki: "); Serial.print(param_storage.ki());
Serial.print(" Kd: "); Serial.println(param_storage.kd());
Serial.print("Calibration factor for thermistor left: "); Serial.print(param_storage.calib_factor_therm_1());
Serial.print(" right: "); Serial.println(param_storage.calib_factor_therm_2());
Serial.print("Spool turns: "); Serial.println(odometer.get_turns(), 2);
one_time = false;
}
}
/**
*
*/
void setup() {
Serial.begin(115200);
param_storage.begin();
ui.begin(callback_menu_get, callback_menu_set,
callback_menu_end_edit, callback_menu_exit);
sensors.begin();
heater.begin();
odometer.begin(callback_odom_start, callback_odom_stop);
lid_sensor.begin();
splash_timer = millis();
}
void loop() {
show_intro_msg();
int menu_mode = ui.update();
lid_sensor.update();
// when the timer in hours expires or the lid is open, it stops the heater.
if (timer.update() || lid_sensor.state()) {
heater.stop();
timer.reset();
}
// Update sensor values every 100 mS.
if (sensors.update()) {
refresh_display++;
}
// The odometer functions only run in PID mode.
odometer.update((heater.get_mode() == MODE_RUN_PID), lid_sensor.state());
// Displays the firmware version screen for a seconds or until the encoder is pressed.
if (!display_version()) {
float bed_temp = max(sensors.bed_left_celcius(), sensors.bed_right_celcius());
float pwm_val = heater.update(sensors.box_celcius(), bed_temp);
// Once per second.
if (refresh_display >= 10) {
refresh_display = 0;
/*
* Shows status information, when not in setup mode.
*/
if (menu_mode == MENU_MODE_INFO) {
update_display_info();
}
/*
* Send the input output and setpoint values of the PID and tuner.
* Use the Arduino Plotter to plot the system response.
* Note: The output is converted to percentage.
*/
plot_pid(pwm_val, bed_temp);
}
}
}