forked from mriscoc/Ender3V2S1
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
temperature.cpp
4855 lines (4140 loc) · 168 KB
/
temperature.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* temperature.cpp - temperature control
*/
// Useful when debugging thermocouples
//#define IGNORE_THERMOCOUPLE_ERRORS
#include "../MarlinCore.h"
#include "../HAL/shared/Delay.h"
#include "../lcd/marlinui.h"
#include "../gcode/gcode.h"
#include "temperature.h"
#include "endstops.h"
#include "planner.h"
#include "printcounter.h"
#if ANY(HAS_COOLER, LASER_COOLANT_FLOW_METER)
#include "../feature/cooler.h"
#include "../feature/spindle_laser.h"
#endif
#if ENABLED(USE_CONTROLLER_FAN)
#include "../feature/controllerfan.h"
#endif
#if ENABLED(EMERGENCY_PARSER)
#include "motion.h"
#endif
#if ENABLED(DWIN_LCD_PROUI)
#include "../lcd/e3v2/proui/dwin.h"
#endif
#if ENABLED(EXTENSIBLE_UI)
#include "../lcd/extui/ui_api.h"
#endif
#if ENABLED(HOST_PROMPT_SUPPORT)
#include "../feature/host_actions.h"
#endif
#if ENABLED(NOZZLE_PARK_FEATURE)
#include "../libs/nozzle.h"
#endif
#if LASER_SAFETY_TIMEOUT_MS > 0
#include "../feature/spindle_laser.h"
#endif
// MAX TC related macros
#define TEMP_SENSOR_IS_MAX(n, M) (ENABLED(TEMP_SENSOR_##n##_IS_MAX##M) || (ENABLED(TEMP_SENSOR_REDUNDANT_IS_MAX##M) && REDUNDANT_TEMP_MATCH(SOURCE, E##n)))
#define TEMP_SENSOR_IS_ANY_MAX_TC(n) (TEMP_SENSOR_IS_MAX_TC(n) || (TEMP_SENSOR_IS_MAX_TC(REDUNDANT) && REDUNDANT_TEMP_MATCH(SOURCE, E##n)))
// LIB_MAX6675 can be added to the build_flags in platformio.ini to use a user-defined library
// If LIB_MAX6675 is not on the build_flags then raw SPI reads will be used.
#if HAS_MAX6675 && USE_LIB_MAX6675
#include <max6675.h>
#define HAS_MAX6675_LIBRARY 1
#endif
// LIB_MAX31855 can be added to the build_flags in platformio.ini to use a user-defined library.
// If LIB_MAX31855 is not on the build_flags then raw SPI reads will be used.
#if HAS_MAX31855 && USE_ADAFRUIT_MAX31855
#include <Adafruit_MAX31855.h>
#define HAS_MAX31855_LIBRARY 1
typedef Adafruit_MAX31855 MAX31855;
#endif
#if HAS_MAX31865
#if USE_ADAFRUIT_MAX31865
#include <Adafruit_MAX31865.h>
typedef Adafruit_MAX31865 MAX31865;
#else
#include "../libs/MAX31865.h"
#endif
#endif
#if HAS_MAX6675_LIBRARY || HAS_MAX31855_LIBRARY || HAS_MAX31865
#define HAS_MAXTC_LIBRARIES 1
#endif
// If we have a MAX TC with SCK and MISO pins defined, it's either on a separate/dedicated Hardware
// SPI bus, or some pins for Software SPI. Alternate Hardware SPI buses are not supported yet, so
// your SPI options are:
//
// 1. Only CS pin(s) defined: Hardware SPI on the default bus (usually the SD card SPI).
// 2. CS, MISO, and SCK pins defined: Software SPI on a separate bus, as defined by MISO, SCK.
// 3. CS, MISO, and SCK pins w/ FORCE_HW_SPI: Hardware SPI on the default bus, ignoring MISO, SCK.
//
#if TEMP_SENSOR_IS_ANY_MAX_TC(0) && TEMP_SENSOR_0_HAS_SPI_PINS && DISABLED(TEMP_SENSOR_FORCE_HW_SPI)
#define TEMP_SENSOR_0_USES_SW_SPI 1
#endif
#if TEMP_SENSOR_IS_ANY_MAX_TC(1) && TEMP_SENSOR_1_HAS_SPI_PINS && DISABLED(TEMP_SENSOR_FORCE_HW_SPI)
#define TEMP_SENSOR_1_USES_SW_SPI 1
#endif
#if TEMP_SENSOR_IS_ANY_MAX_TC(2) && TEMP_SENSOR_2_HAS_SPI_PINS && DISABLED(TEMP_SENSOR_FORCE_HW_SPI)
#define TEMP_SENSOR_2_USES_SW_SPI 1
#endif
#if (TEMP_SENSOR_0_USES_SW_SPI || TEMP_SENSOR_1_USES_SW_SPI || TEMP_SENSOR_2_USES_SW_SPI) && !HAS_MAXTC_LIBRARIES
#include "../libs/private_spi.h"
#define HAS_MAXTC_SW_SPI 1
// Define pins for SPI-based sensors
#if TEMP_SENSOR_0_USES_SW_SPI
#define SW_SPI_SCK_PIN TEMP_0_SCK_PIN
#define SW_SPI_MISO_PIN TEMP_0_MISO_PIN
#if PIN_EXISTS(TEMP_0_MOSI)
#define SW_SPI_MOSI_PIN TEMP_0_MOSI_PIN
#endif
#elif TEMP_SENSOR_1_USES_SW_SPI
#define SW_SPI_SCK_PIN TEMP_1_SCK_PIN
#define SW_SPI_MISO_PIN TEMP_1_MISO_PIN
#if PIN_EXISTS(TEMP_1_MOSI)
#define SW_SPI_MOSI_PIN TEMP_1_MOSI_PIN
#endif
#elif TEMP_SENSOR_2_USES_SW_SPI
#define SW_SPI_SCK_PIN TEMP_2_SCK_PIN
#define SW_SPI_MISO_PIN TEMP_2_MISO_PIN
#if PIN_EXISTS(TEMP_2_MOSI)
#define SW_SPI_MOSI_PIN TEMP_2_MOSI_PIN
#endif
#endif
#ifndef SW_SPI_MOSI_PIN
#define SW_SPI_MOSI_PIN SD_MOSI_PIN
#endif
#endif
#if ENABLED(MPCTEMP)
#include <math.h>
#include "probe.h"
#endif
#if ANY(MPCTEMP, PID_EXTRUSION_SCALING)
#include "stepper.h"
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
#include "../feature/filwidth.h"
#endif
#if HAS_POWER_MONITOR
#include "../feature/power_monitor.h"
#endif
#if ENABLED(EMERGENCY_PARSER)
#include "../feature/e_parser.h"
#endif
#if ENABLED(PRINTER_EVENT_LEDS)
#include "../feature/leds/printer_event_leds.h"
#endif
#if ENABLED(JOYSTICK)
#include "../feature/joystick.h"
#endif
#if ENABLED(SINGLENOZZLE)
#include "tool_change.h"
#endif
#if HAS_BEEPER
#include "../libs/buzzer.h"
#endif
#if HAS_SERVOS
#include "servo.h"
#endif
#if ANY(TEMP_SENSOR_0_IS_THERMISTOR, TEMP_SENSOR_1_IS_THERMISTOR, TEMP_SENSOR_2_IS_THERMISTOR, TEMP_SENSOR_3_IS_THERMISTOR, \
TEMP_SENSOR_4_IS_THERMISTOR, TEMP_SENSOR_5_IS_THERMISTOR, TEMP_SENSOR_6_IS_THERMISTOR, TEMP_SENSOR_7_IS_THERMISTOR )
#define HAS_HOTEND_THERMISTOR 1
#endif
#if HAS_HOTEND_THERMISTOR
#define NEXT_TEMPTABLE(N) ,TEMPTABLE_##N
#define NEXT_TEMPTABLE_LEN(N) ,TEMPTABLE_##N##_LEN
static const temp_entry_t* heater_ttbl_map[HOTENDS] = ARRAY_BY_HOTENDS(TEMPTABLE_0 REPEAT_S(1, HOTENDS, NEXT_TEMPTABLE));
static constexpr uint8_t heater_ttbllen_map[HOTENDS] = ARRAY_BY_HOTENDS(TEMPTABLE_0_LEN REPEAT_S(1, HOTENDS, NEXT_TEMPTABLE_LEN));
#endif
Temperature thermalManager;
PGMSTR(str_t_thermal_runaway, STR_T_THERMAL_RUNAWAY);
PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
//
// Initialize MAX TC objects/SPI
//
#if HAS_MAX_TC
#if HAS_MAXTC_SW_SPI
// Initialize SoftSPI for non-lib Software SPI; Libraries take care of it themselves.
template<uint8_t MisoPin, uint8_t MosiPin, uint8_t SckPin>
SoftSPI<MisoPin, MosiPin, SckPin> SPIclass<MisoPin, MosiPin, SckPin>::softSPI;
SPIclass<SW_SPI_MISO_PIN, SW_SPI_MOSI_PIN, SW_SPI_SCK_PIN> max_tc_spi;
#endif
#define MAXTC_INIT(n, M) \
MAX##M max##M##_##n = MAX##M( \
TEMP_##n##_CS_PIN \
OPTARG(_MAX31865_##n##_SW, TEMP_##n##_MOSI_PIN) \
OPTARG(TEMP_SENSOR_##n##_USES_SW_SPI, TEMP_##n##_MISO_PIN, TEMP_##n##_SCK_PIN) \
OPTARG(LARGE_PINMAP, HIGH) \
)
#if HAS_MAX6675_LIBRARY
#if TEMP_SENSOR_IS_MAX(0, 6675)
MAXTC_INIT(0, 6675);
#endif
#if TEMP_SENSOR_IS_MAX(1, 6675)
MAXTC_INIT(1, 6675);
#endif
#if TEMP_SENSOR_IS_MAX(2, 6675)
MAXTC_INIT(2, 6675);
#endif
#endif
#if HAS_MAX31855_LIBRARY
#if TEMP_SENSOR_IS_MAX(0, 31855)
MAXTC_INIT(0, 31855);
#endif
#if TEMP_SENSOR_IS_MAX(1, 31855)
MAXTC_INIT(1, 31855);
#endif
#if TEMP_SENSOR_IS_MAX(2, 31855)
MAXTC_INIT(2, 31855);
#endif
#endif
// MAX31865 always uses a library, unlike '55 & 6675
#if HAS_MAX31865
#define _MAX31865_0_SW TEMP_SENSOR_0_USES_SW_SPI
#define _MAX31865_1_SW TEMP_SENSOR_1_USES_SW_SPI
#define _MAX31865_2_SW TEMP_SENSOR_2_USES_SW_SPI
#if TEMP_SENSOR_IS_MAX(0, 31865)
MAXTC_INIT(0, 31865);
#endif
#if TEMP_SENSOR_IS_MAX(1, 31865)
MAXTC_INIT(1, 31865);
#endif
#if TEMP_SENSOR_IS_MAX(2, 31865)
MAXTC_INIT(2, 31865);
#endif
#undef _MAX31865_0_SW
#undef _MAX31865_1_SW
#undef _MAX31865_2_SW
#endif
#undef MAXTC_INIT
#endif
/**
* public:
*/
#if ENABLED(TEMP_TUNING_MAINTAIN_FAN)
bool Temperature::adaptive_fan_slowing = true;
#endif
#if HAS_HOTEND
hotend_info_t Temperature::temp_hotend[HOTENDS];
#if ENABLED(MPCTEMP)
bool MPC::e_paused; // = false
int32_t MPC::e_position; // = 0
#endif
#if PROUI_EX
celsius_t Temperature::hotend_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP, HEATER_5_MAXTEMP, HEATER_6_MAXTEMP, HEATER_7_MAXTEMP);
#else
constexpr celsius_t Temperature::hotend_maxtemp[HOTENDS];
#endif // PROUI_EX
// Sanity-check max readable temperatures
#define CHECK_MAXTEMP_(N,M,S) static_assert( \
S >= 998 || M <= _MAX(TT_NAME(S)[0].celsius, TT_NAME(S)[COUNT(TT_NAME(S)) - 1].celsius) - HOTEND_OVERSHOOT, \
"HEATER_" STRINGIFY(N) "_MAXTEMP (" STRINGIFY(M) ") is too high for thermistor_" STRINGIFY(S) ".h with HOTEND_OVERSHOOT=" STRINGIFY(HOTEND_OVERSHOOT) ".");
#define CHECK_MAXTEMP(N) TERN(TEMP_SENSOR_##N##_IS_THERMISTOR, CHECK_MAXTEMP_, CODE_0)(N, HEATER_##N##_MAXTEMP, TEMP_SENSOR_##N)
REPEAT(HOTENDS, CHECK_MAXTEMP)
#if HAS_PREHEAT
#define CHECK_PREHEAT__(N,P,T,M) static_assert(T <= M - HOTEND_OVERSHOOT, "PREHEAT_" STRINGIFY(P) "_TEMP_HOTEND (" STRINGIFY(T) ") must be less than HEATER_" STRINGIFY(N) "_MAXTEMP (" STRINGIFY(M) ") - " STRINGIFY(HOTEND_OVERSHOOT) ".");
#define CHECK_PREHEAT_(N,P) CHECK_PREHEAT__(N, P, PREHEAT_##P##_TEMP_HOTEND, HEATER_##N##_MAXTEMP)
#define CHECK_PREHEAT(P) REPEAT2(HOTENDS, CHECK_PREHEAT_, P)
#if PREHEAT_COUNT >= 1
CHECK_PREHEAT(1)
#endif
#if PREHEAT_COUNT >= 2
CHECK_PREHEAT(2)
#endif
#if PREHEAT_COUNT >= 3
CHECK_PREHEAT(3)
#endif
#if PREHEAT_COUNT >= 4
CHECK_PREHEAT(4)
#endif
#if PREHEAT_COUNT >= 5
CHECK_PREHEAT(5)
#endif
#if PREHEAT_COUNT >= 6
CHECK_PREHEAT(6)
#endif
#if PREHEAT_COUNT >= 7
CHECK_PREHEAT(7)
#endif
#if PREHEAT_COUNT >= 8
CHECK_PREHEAT(8)
#endif
#if PREHEAT_COUNT >= 9
CHECK_PREHEAT(9)
#endif
#if PREHEAT_COUNT >= 10
CHECK_PREHEAT(10)
#endif
#endif // HAS_PREHEAT
#endif // HAS_HOTEND
#if HAS_TEMP_REDUNDANT
redundant_info_t Temperature::temp_redundant;
#endif
#if ANY(AUTO_POWER_E_FANS, HAS_FANCHECK)
uint8_t Temperature::autofan_speed[HOTENDS] = ARRAY_N_1(HOTENDS, FAN_OFF_PWM);
#endif
#if ENABLED(AUTO_POWER_CHAMBER_FAN)
uint8_t Temperature::chamberfan_speed = FAN_OFF_PWM;
#endif
#if ENABLED(AUTO_POWER_COOLER_FAN)
uint8_t Temperature::coolerfan_speed = FAN_OFF_PWM;
#endif
#if ALL(FAN_SOFT_PWM, USE_CONTROLLER_FAN)
uint8_t Temperature::soft_pwm_controller_speed = FAN_OFF_PWM;
#endif
// Init fans according to whether they're native PWM or Software PWM
#ifdef BOARD_OPENDRAIN_MOSFETS
#define _INIT_SOFT_FAN(P) OUT_WRITE_OD(P, FAN_INVERTING ? LOW : HIGH)
#else
#define _INIT_SOFT_FAN(P) OUT_WRITE(P, FAN_INVERTING ? LOW : HIGH)
#endif
#if ENABLED(FAN_SOFT_PWM)
#define _INIT_FAN_PIN(P) _INIT_SOFT_FAN(P)
#else
#define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0)
#endif
#if ENABLED(FAST_PWM_FAN)
#define SET_FAST_PWM_FREQ(P) hal.set_pwm_frequency(pin_t(P), FAST_PWM_FAN_FREQUENCY)
#else
#define SET_FAST_PWM_FREQ(P) NOOP
#endif
#define INIT_FAN_PIN(P) do{ _INIT_FAN_PIN(P); SET_FAST_PWM_FREQ(P); }while(0)
// HAS_FAN does not include CONTROLLER_FAN
#if HAS_FAN
uint8_t Temperature::fan_speed[FAN_COUNT] = ARRAY_N_1(FAN_COUNT, FAN_OFF_PWM);
#if ENABLED(EXTRA_FAN_SPEED)
Temperature::extra_fan_t Temperature::extra_fan_speed[FAN_COUNT] = ARRAY_N_1(FAN_COUNT, FAN_OFF_PWM);
/**
* Handle the M106 P<fan> T<speed> command:
* T1 = Restore fan speed saved on the last T2
* T2 = Save the fan speed, then set to the last T<3-255> value
* T<3-255> = Set the "extra fan speed"
*/
void Temperature::set_temp_fan_speed(const uint8_t fan, const uint16_t command_or_speed) {
switch (command_or_speed) {
case 1:
set_fan_speed(fan, extra_fan_speed[fan].saved);
break;
case 2:
extra_fan_speed[fan].saved = fan_speed[fan];
set_fan_speed(fan, extra_fan_speed[fan].speed);
break;
default:
extra_fan_speed[fan].speed = _MIN(command_or_speed, 255U);
break;
}
}
#endif
#if ANY(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
bool Temperature::fans_paused; // = false;
uint8_t Temperature::saved_fan_speed[FAN_COUNT] = ARRAY_N_1(FAN_COUNT, FAN_OFF_PWM);
#endif
#if ENABLED(ADAPTIVE_FAN_SLOWING)
uint8_t Temperature::fan_speed_scaler[FAN_COUNT] = ARRAY_N_1(FAN_COUNT, 128);
#endif
/**
* Set the print fan speed for a target extruder
*/
void Temperature::set_fan_speed(uint8_t fan, uint16_t speed) {
NOMORE(speed, 255U);
#if ENABLED(SINGLENOZZLE_STANDBY_FAN)
if (fan != active_extruder) {
if (fan < EXTRUDERS) singlenozzle_fan_speed[fan] = speed;
return;
}
#endif
TERN_(SINGLENOZZLE, if (fan < EXTRUDERS) fan = 0); // Always fan 0 for SINGLENOZZLE E fan
if (fan >= FAN_COUNT) return;
fan_speed[fan] = speed;
#if NUM_REDUNDANT_FANS
if (fan == 0) {
for (uint8_t f = REDUNDANT_PART_COOLING_FAN; f < REDUNDANT_PART_COOLING_FAN + NUM_REDUNDANT_FANS; ++f)
thermalManager.set_fan_speed(f, speed);
}
#endif
TERN_(REPORT_FAN_CHANGE, report_fan_speed(fan));
}
#if ENABLED(REPORT_FAN_CHANGE)
/**
* Report print fan speed for a target extruder
*/
void Temperature::report_fan_speed(const uint8_t fan) {
if (fan >= FAN_COUNT) return;
PORT_REDIRECT(SerialMask::All);
SERIAL_ECHOLNPGM("M106 P", fan, " S", fan_speed[fan]);
}
#endif
#if ANY(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
void Temperature::set_fans_paused(const bool p) {
if (p != fans_paused) {
fans_paused = p;
if (p)
FANS_LOOP(i) { saved_fan_speed[i] = fan_speed[i]; fan_speed[i] = 0; }
else
FANS_LOOP(i) fan_speed[i] = saved_fan_speed[i];
}
}
#endif
#endif // HAS_FAN
#if WATCH_HOTENDS
hotend_watch_t Temperature::watch_hotend[HOTENDS]; // = { { 0 } }
#endif
#if HEATER_IDLE_HANDLER
Temperature::heater_idle_t Temperature::heater_idle[NR_HEATER_IDLE]; // = { { 0 } }
#endif
#if HAS_HEATED_BED
bed_info_t Temperature::temp_bed; // = { 0 }
// Init min and max temp with extreme values to prevent false errors during startup
raw_adc_t Temperature::mintemp_raw_BED = TEMP_SENSOR_BED_RAW_LO_TEMP,
Temperature::maxtemp_raw_BED = TEMP_SENSOR_BED_RAW_HI_TEMP;
#if WATCH_BED
bed_watch_t Temperature::watch_bed; // = { 0 }
#endif
#if DISABLED(PIDTEMPBED)
millis_t Temperature::next_bed_check_ms;
#endif
#endif
#if HAS_TEMP_CHAMBER
chamber_info_t Temperature::temp_chamber; // = { 0 }
#if HAS_HEATED_CHAMBER
millis_t next_cool_check_ms = 0;
celsius_float_t old_temp = 9999;
raw_adc_t Temperature::mintemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_LO_TEMP,
Temperature::maxtemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_HI_TEMP;
#if WATCH_CHAMBER
chamber_watch_t Temperature::watch_chamber; // = { 0 }
#endif
#if DISABLED(PIDTEMPCHAMBER)
millis_t Temperature::next_chamber_check_ms;
#endif
#endif
#endif
#if HAS_TEMP_COOLER
cooler_info_t Temperature::temp_cooler; // = { 0 }
#if HAS_COOLER
bool flag_cooler_state;
//bool flag_cooler_excess = false;
celsius_float_t previous_temp = 9999;
raw_adc_t Temperature::mintemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_LO_TEMP,
Temperature::maxtemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_HI_TEMP;
#if WATCH_COOLER
cooler_watch_t Temperature::watch_cooler; // = { 0 }
#endif
millis_t Temperature::next_cooler_check_ms, Temperature::cooler_fan_flush_ms;
#endif
#endif
#if HAS_TEMP_PROBE
probe_info_t Temperature::temp_probe; // = { 0 }
#endif
#if HAS_TEMP_BOARD
board_info_t Temperature::temp_board; // = { 0 }
#if ENABLED(THERMAL_PROTECTION_BOARD)
raw_adc_t Temperature::mintemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_LO_TEMP,
Temperature::maxtemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_HI_TEMP;
#endif
#endif
#if HAS_TEMP_SOC
soc_info_t Temperature::temp_soc; // = { 0 }
raw_adc_t Temperature::maxtemp_raw_SOC = TEMP_SENSOR_SOC_RAW_HI_TEMP;
#endif
#if ALL(HAS_MARLINUI_MENU, PREVENT_COLD_EXTRUSION) && E_MANUAL > 0
bool Temperature::allow_cold_extrude_override = false;
#else
constexpr bool Temperature::allow_cold_extrude_override;
#endif
#if ENABLED(PREVENT_COLD_EXTRUSION)
bool Temperature::allow_cold_extrude = false;
celsius_t Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
#else
constexpr bool Temperature::allow_cold_extrude;
constexpr celsius_t Temperature::extrude_min_temp;
#endif
#if HAS_ADC_BUTTONS
uint32_t Temperature::current_ADCKey_raw = HAL_ADC_RANGE;
uint16_t Temperature::ADCKey_count = 0;
#endif
#if ENABLED(PID_EXTRUSION_SCALING)
int16_t Temperature::lpq_len; // Initialized in settings.cpp
#endif
/**
* private:
*/
volatile bool Temperature::raw_temps_ready = false;
#define TEMPDIR(N) ((TEMP_SENSOR_##N##_RAW_LO_TEMP) < (TEMP_SENSOR_##N##_RAW_HI_TEMP) ? 1 : -1)
#define TP_CMP(S,A,B) (TEMPDIR(S) < 0 ? ((A)<(B)) : ((A)>(B)))
#if HAS_HOTEND
// Init mintemp and maxtemp with extreme values to prevent false errors during startup
constexpr temp_range_t sensor_heater_0 { TEMP_SENSOR_0_RAW_LO_TEMP, TEMP_SENSOR_0_RAW_HI_TEMP, 0, 16383 },
sensor_heater_1 { TEMP_SENSOR_1_RAW_LO_TEMP, TEMP_SENSOR_1_RAW_HI_TEMP, 0, 16383 },
sensor_heater_2 { TEMP_SENSOR_2_RAW_LO_TEMP, TEMP_SENSOR_2_RAW_HI_TEMP, 0, 16383 },
sensor_heater_3 { TEMP_SENSOR_3_RAW_LO_TEMP, TEMP_SENSOR_3_RAW_HI_TEMP, 0, 16383 },
sensor_heater_4 { TEMP_SENSOR_4_RAW_LO_TEMP, TEMP_SENSOR_4_RAW_HI_TEMP, 0, 16383 },
sensor_heater_5 { TEMP_SENSOR_5_RAW_LO_TEMP, TEMP_SENSOR_5_RAW_HI_TEMP, 0, 16383 },
sensor_heater_6 { TEMP_SENSOR_6_RAW_LO_TEMP, TEMP_SENSOR_6_RAW_HI_TEMP, 0, 16383 },
sensor_heater_7 { TEMP_SENSOR_7_RAW_LO_TEMP, TEMP_SENSOR_7_RAW_HI_TEMP, 0, 16383 };
temp_range_t Temperature::temp_range[HOTENDS] = ARRAY_BY_HOTENDS(sensor_heater_0, sensor_heater_1, sensor_heater_2, sensor_heater_3, sensor_heater_4, sensor_heater_5, sensor_heater_6, sensor_heater_7);
#endif
#if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
#define MULTI_MAX_CONSECUTIVE_LOW_TEMP_ERR 1
uint8_t Temperature::consecutive_low_temperature_error[HOTENDS]; // = { 0 }
#endif
#if PREHEAT_TIME_HOTEND_MS > 0
millis_t Temperature::preheat_end_ms_hotend[HOTENDS]; // = { 0 };
#endif
#if HAS_HEATED_BED && PREHEAT_TIME_BED_MS > 0
millis_t Temperature::preheat_end_ms_bed = 0;
#endif
#if HAS_FAN_LOGIC
constexpr millis_t Temperature::fan_update_interval_ms;
millis_t Temperature::fan_update_ms = 0;
#endif
#if ENABLED(FAN_SOFT_PWM)
uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
Temperature::soft_pwm_count_fan[FAN_COUNT];
#endif
#if ENABLED(SINGLENOZZLE_STANDBY_TEMP)
celsius_t Temperature::singlenozzle_temp[EXTRUDERS];
#endif
#if ENABLED(SINGLENOZZLE_STANDBY_FAN)
uint8_t Temperature::singlenozzle_fan_speed[EXTRUDERS];
#endif
#if ENABLED(PROBING_HEATERS_OFF)
bool Temperature::paused_for_probing;
#endif
/**
* public:
* Class and Instance Methods
*/
#if HAS_PID_HEATING
inline void say_default_() { SERIAL_ECHOPGM("#define DEFAULT_"); }
/**
* PID Autotuning (M303)
*
* Alternately heat and cool the nozzle, observing its behavior to
* determine the best PID values to achieve a stable temperature.
* Needs sufficient heater power to make some overshoot at target
* temperature to succeed.
*/
void Temperature::PID_autotune(const celsius_t target, const heater_id_t heater_id, const int8_t ncycles, const bool set_result/*=false*/) {
celsius_float_t current_temp = 0.0;
int cycles = 0;
bool heating = true;
millis_t next_temp_ms = millis(), t1 = next_temp_ms, t2 = next_temp_ms;
long t_high = 0, t_low = 0;
raw_pid_t tune_pid = { 0, 0, 0 };
celsius_float_t maxT = 0, minT = 10000;
const bool isbed = (heater_id == H_BED),
ischamber = (heater_id == H_CHAMBER);
#if ENABLED(PIDTEMPCHAMBER)
#define C_TERN(T,A,B) ((T) ? (A) : (B))
#else
#define C_TERN(T,A,B) (B)
#endif
#if ENABLED(PIDTEMPBED)
#define B_TERN(T,A,B) ((T) ? (A) : (B))
#else
#define B_TERN(T,A,B) (B)
#endif
#define GHV(C,B,H) C_TERN(ischamber, C, B_TERN(isbed, B, H))
#define SHV(V) C_TERN(ischamber, temp_chamber.soft_pwm_amount = V, B_TERN(isbed, temp_bed.soft_pwm_amount = V, temp_hotend[heater_id].soft_pwm_amount = V))
#define ONHEATINGSTART() C_TERN(ischamber, printerEventLEDs.onChamberHeatingStart(), B_TERN(isbed, printerEventLEDs.onBedHeatingStart(), printerEventLEDs.onHotendHeatingStart()))
#define ONHEATING(S,C,T) C_TERN(ischamber, printerEventLEDs.onChamberHeating(S,C,T), B_TERN(isbed, printerEventLEDs.onBedHeating(S,C,T), printerEventLEDs.onHotendHeating(S,C,T)))
#define WATCH_PID DISABLED(NO_WATCH_PID_TUNING) && (ALL(WATCH_CHAMBER, PIDTEMPCHAMBER) || ALL(WATCH_BED, PIDTEMPBED) || ALL(WATCH_HOTENDS, PIDTEMP))
#if WATCH_PID
#if ALL(THERMAL_PROTECTION_CHAMBER, PIDTEMPCHAMBER)
#define C_GTV(T,A,B) ((T) ? (A) : (B))
#else
#define C_GTV(T,A,B) (B)
#endif
#if ALL(THERMAL_PROTECTION_BED, PIDTEMPBED)
#define B_GTV(T,A,B) ((T) ? (A) : (B))
#else
#define B_GTV(T,A,B) (B)
#endif
#define GTV(C,B,H) C_GTV(ischamber, C, B_GTV(isbed, B, H))
const uint16_t watch_temp_period = GTV(WATCH_CHAMBER_TEMP_PERIOD, WATCH_BED_TEMP_PERIOD, WATCH_TEMP_PERIOD);
const uint8_t watch_temp_increase = GTV(WATCH_CHAMBER_TEMP_INCREASE, WATCH_BED_TEMP_INCREASE, WATCH_TEMP_INCREASE);
const celsius_float_t watch_temp_target = celsius_float_t(target - (watch_temp_increase + GTV(TEMP_CHAMBER_HYSTERESIS, TEMP_BED_HYSTERESIS, TEMP_HYSTERESIS) + 1));
millis_t temp_change_ms = next_temp_ms + SEC_TO_MS(watch_temp_period);
celsius_float_t next_watch_temp = 0.0;
bool heated = false;
#endif
TERN_(HAS_FAN_LOGIC, fan_update_ms = next_temp_ms + fan_update_interval_ms);
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_STARTED));
TERN_(DWIN_LCD_PROUI, DWIN_PidTuning(isbed ? PID_BED_START : PID_EXTR_START));
if (target > GHV(CHAMBER_MAX_TARGET, BED_MAX_TARGET, temp_range[heater_id].maxtemp - (HOTEND_OVERSHOOT))) {
SERIAL_ECHOPGM(STR_PID_AUTOTUNE); SERIAL_ECHOLNPGM(STR_PID_TEMP_TOO_HIGH);
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_TEMP_TOO_HIGH));
TERN_(DWIN_LCD_PROUI, DWIN_PidTuning(PID_TEMP_TOO_HIGH));
TERN_(HOST_PROMPT_SUPPORT, hostui.notify(GET_TEXT_F(MSG_PID_TEMP_TOO_HIGH)));
return;
}
SERIAL_ECHOPGM(STR_PID_AUTOTUNE); SERIAL_ECHOLNPGM(STR_PID_AUTOTUNE_START);
disable_all_heaters();
TERN_(AUTO_POWER_CONTROL, powerManager.power_on());
long bias = GHV(MAX_CHAMBER_POWER, MAX_BED_POWER, PID_MAX) >> 1, d = bias;
SHV(bias);
#if ENABLED(PRINTER_EVENT_LEDS)
const celsius_float_t start_temp = GHV(degChamber(), degBed(), degHotend(heater_id));
LEDColor color = ONHEATINGSTART();
#endif
TERN_(TEMP_TUNING_MAINTAIN_FAN, adaptive_fan_slowing = false);
LCD_MESSAGE(MSG_HEATING);
// PID Tuning loop
wait_for_heatup = true;
while (wait_for_heatup) { // Can be interrupted with M108
const millis_t ms = millis();
if (updateTemperaturesIfReady()) { // temp sample ready
// Get the current temperature and constrain it
current_temp = GHV(degChamber(), degBed(), degHotend(heater_id));
NOLESS(maxT, current_temp);
NOMORE(minT, current_temp);
#if ENABLED(PRINTER_EVENT_LEDS)
ONHEATING(start_temp, current_temp, target);
#endif
TERN_(HAS_FAN_LOGIC, manage_extruder_fans(ms));
if (heating && current_temp > target && ELAPSED(ms, t2 + 5000UL)) {
heating = false;
SHV((bias - d) >> 1);
t1 = ms;
t_high = t1 - t2;
maxT = target;
}
if (!heating && current_temp < target && ELAPSED(ms, t1 + 5000UL)) {
heating = true;
t2 = ms;
t_low = t2 - t1;
if (cycles > 0) {
const long max_pow = GHV(MAX_CHAMBER_POWER, MAX_BED_POWER, PID_MAX);
bias += (d * (t_high - t_low)) / (t_low + t_high);
LIMIT(bias, 20, max_pow - 20);
d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias;
SERIAL_ECHOPGM(STR_BIAS, bias, STR_D_COLON, d, STR_T_MIN, minT, STR_T_MAX, maxT);
if (cycles > 2) {
const float Ku = (4.0f * d) / (float(M_PI) * (maxT - minT) * 0.5f),
Tu = float(t_low + t_high) * 0.001f,
pf = (ischamber || isbed) ? 0.2f : 0.6f,
df = (ischamber || isbed) ? 1.0f / 3.0f : 1.0f / 8.0f;
tune_pid.p = Ku * pf;
tune_pid.i = tune_pid.p * 2.0f / Tu;
tune_pid.d = tune_pid.p * Tu * df;
SERIAL_ECHOLNPGM(STR_KU, Ku, STR_TU, Tu);
if (ischamber || isbed)
SERIAL_ECHOLNPGM(" No overshoot");
else
SERIAL_ECHOLNPGM(STR_CLASSIC_PID);
SERIAL_ECHOLNPGM(STR_KP, tune_pid.p, STR_KI, tune_pid.i, STR_KD, tune_pid.d);
}
}
SHV((bias + d) >> 1);
TERN_(HAS_STATUS_MESSAGE, ui.status_printf(0, F(S_FMT " %i/%i"), GET_TEXT(MSG_PID_CYCLE), cycles, ncycles));
cycles++;
minT = target;
}
}
// Did the temperature overshoot very far?
#ifndef MAX_OVERSHOOT_PID_AUTOTUNE
#define MAX_OVERSHOOT_PID_AUTOTUNE 30
#endif
if (current_temp > target + MAX_OVERSHOOT_PID_AUTOTUNE) {
SERIAL_ECHOPGM(STR_PID_AUTOTUNE); SERIAL_ECHOLNPGM(STR_PID_TEMP_TOO_HIGH);
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_TEMP_TOO_HIGH));
TERN_(DWIN_LCD_PROUI, DWIN_PidTuning(PID_TEMP_TOO_HIGH));
TERN_(HOST_PROMPT_SUPPORT, hostui.notify(GET_TEXT_F(MSG_PID_TEMP_TOO_HIGH)));
break;
}
// Report heater states every 2 seconds
if (ELAPSED(ms, next_temp_ms)) {
#if HAS_TEMP_SENSOR
print_heater_states(heater_id < 0 ? active_extruder : (int8_t)heater_id);
SERIAL_EOL();
#endif
next_temp_ms = ms + 2000UL;
// Make sure heating is actually working
#if WATCH_PID
if (ALL(WATCH_BED, WATCH_HOTENDS) || isbed == DISABLED(WATCH_HOTENDS) || ischamber == DISABLED(WATCH_HOTENDS)) {
if (!heated) { // If not yet reached target...
if (current_temp > next_watch_temp) { // Over the watch temp?
next_watch_temp = current_temp + watch_temp_increase; // - set the next temp to watch for
temp_change_ms = ms + SEC_TO_MS(watch_temp_period); // - move the expiration timer up
if (current_temp > watch_temp_target) heated = true; // - Flag if target temperature reached
}
else if (ELAPSED(ms, temp_change_ms)) // Watch timer expired
_TEMP_ERROR(heater_id, FPSTR(str_t_heating_failed), MSG_ERR_HEATING_FAILED, current_temp);
}
else if (current_temp < target - (MAX_OVERSHOOT_PID_AUTOTUNE)) // Heated, then temperature fell too far?
_TEMP_ERROR(heater_id, FPSTR(str_t_thermal_runaway), MSG_ERR_THERMAL_RUNAWAY, current_temp);
}
#endif
} // every 2 seconds
// Timeout after MAX_CYCLE_TIME_PID_AUTOTUNE minutes since the last undershoot/overshoot cycle
#ifndef MAX_CYCLE_TIME_PID_AUTOTUNE
#define MAX_CYCLE_TIME_PID_AUTOTUNE 20L
#endif
if ((ms - _MIN(t1, t2)) > (MAX_CYCLE_TIME_PID_AUTOTUNE * 60L * 1000L)) {
TERN_(DWIN_LCD_PROUI, DWIN_PidTuning(PID_TUNING_TIMEOUT));
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_TUNING_TIMEOUT));
TERN_(HOST_PROMPT_SUPPORT, hostui.notify(GET_TEXT_F(MSG_PID_TIMEOUT)));
SERIAL_ECHOPGM(STR_PID_AUTOTUNE); SERIAL_ECHOLNPGM(STR_PID_TIMEOUT);
break;
}
if (cycles > ncycles && cycles > 2) {
SERIAL_ECHOPGM(STR_PID_AUTOTUNE); SERIAL_ECHOLNPGM(STR_PID_AUTOTUNE_FINISHED);
TERN_(HOST_PROMPT_SUPPORT, hostui.notify(GET_TEXT_F(MSG_PID_AUTOTUNE_DONE)));
#if ANY(PIDTEMPBED, PIDTEMPCHAMBER)
FSTR_P const estring = GHV(F("chamber"), F("bed"), FPSTR(NUL_STR));
say_default_(); SERIAL_ECHO(estring, F("Kp "), tune_pid.p);
say_default_(); SERIAL_ECHO(estring, F("Ki "), tune_pid.i);
say_default_(); SERIAL_ECHO(estring, F("Kd "), tune_pid.d);
#else
say_default_(); SERIAL_ECHOLNPGM("Kp ", tune_pid.p);
say_default_(); SERIAL_ECHOLNPGM("Ki ", tune_pid.i);
say_default_(); SERIAL_ECHOLNPGM("Kd ", tune_pid.d);
#endif
auto _set_hotend_pid = [](const uint8_t tool, const raw_pid_t &in_pid) {
#if ENABLED(PIDTEMP)
#if ENABLED(PID_PARAMS_PER_HOTEND)
thermalManager.temp_hotend[tool].pid.set(in_pid);
#else
HOTEND_LOOP() thermalManager.temp_hotend[e].pid.set(in_pid);
#endif
updatePID();
#endif
UNUSED(tool); UNUSED(in_pid);
};
#if ENABLED(PIDTEMPBED)
auto _set_bed_pid = [](const raw_pid_t &in_pid) {
temp_bed.pid.set(in_pid);
};
#endif
#if ENABLED(PIDTEMPCHAMBER)
auto _set_chamber_pid = [](const raw_pid_t &in_pid) {
temp_chamber.pid.set(in_pid);
};
#endif
// Use the result? (As with "M303 U1")
if (set_result)
GHV(_set_chamber_pid(tune_pid), _set_bed_pid(tune_pid), _set_hotend_pid(heater_id, tune_pid));
TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(color));
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_DONE));
TERN_(DWIN_LCD_PROUI, DWIN_PidTuning(AUTOTUNE_DONE));
goto EXIT_M303;
}
// Run HAL idle tasks
hal.idletask();
// Run UI update
ui.update();
// Run Beeper queue update
TERN_(HAS_BEEPER, buzzer.tick());
}
wait_for_heatup = false;
disable_all_heaters();
TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(color));
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_DONE));
TERN_(DWIN_LCD_PROUI, DWIN_PidTuning(AUTOTUNE_DONE));
EXIT_M303:
TERN_(TEMP_TUNING_MAINTAIN_FAN, adaptive_fan_slowing = true);
return;
}
#endif // HAS_PID_HEATING
#if ENABLED(MPC_AUTOTUNE)
#if ANY(MPC_FAN_0_ALL_HOTENDS, MPC_FAN_0_ACTIVE_HOTEND)
#define SINGLEFAN 1
#endif
#define DEBUG_MPC_AUTOTUNE 1
millis_t Temperature::MPC_autotuner::curr_time_ms, Temperature::MPC_autotuner::next_report_ms;
celsius_float_t Temperature::MPC_autotuner::temp_samples[16];
uint8_t Temperature::MPC_autotuner::sample_count;
uint16_t Temperature::MPC_autotuner::sample_distance;
// Parameters from differential analysis
celsius_float_t Temperature::MPC_autotuner::temp_fastest;
#if HAS_FAN
float Temperature::MPC_autotuner::power_fan255;
#endif
Temperature::MPC_autotuner::MPC_autotuner(const uint8_t extruderIdx) : e(extruderIdx) {
TERN_(TEMP_TUNING_MAINTAIN_FAN, adaptive_fan_slowing = false);
}
Temperature::MPC_autotuner::~MPC_autotuner() {
wait_for_heatup = false;
ui.reset_status();
temp_hotend[e].target = 0.0f;
temp_hotend[e].soft_pwm_amount = 0;
#if HAS_FAN
set_fan_speed(TERN(SINGLEFAN, 0, e), 0);
planner.sync_fan_speeds(fan_speed);
#endif
do_z_clearance(MPC_TUNING_END_Z, false);
TERN_(TEMP_TUNING_MAINTAIN_FAN, adaptive_fan_slowing = true);
}
Temperature::MPC_autotuner::MeasurementState Temperature::MPC_autotuner::measure_ambient_temp() {
init_timers();
const millis_t test_interval_ms = 10000UL;
millis_t next_test_ms = curr_time_ms + test_interval_ms;
ambient_temp = current_temp = degHotend(e);
wait_for_heatup = true;
for (;;) { // Can be interrupted with M108
if (housekeeping() == CANCELLED) return CANCELLED;
if (ELAPSED(curr_time_ms, next_test_ms)) {
if (current_temp >= ambient_temp) {
ambient_temp = (ambient_temp + current_temp) / 2.0f;
break;
}
ambient_temp = current_temp;
next_test_ms += test_interval_ms;
}
}
wait_for_heatup = false;
#if ENABLED(DEBUG_MPC_AUTOTUNE)
SERIAL_ECHOLNPGM("MPC_autotuner::measure_ambient_temp() Completed");
SERIAL_ECHOLNPGM("=====");
SERIAL_ECHOLNPGM("ambient_temp ", get_ambient_temp());
#endif
return SUCCESS;
}
Temperature::MPC_autotuner::MeasurementState Temperature::MPC_autotuner::measure_heatup() {
init_timers();
constexpr millis_t test_interval_ms = 1000UL;
millis_t next_test_time_ms = curr_time_ms + test_interval_ms;
MPCHeaterInfo &hotend = temp_hotend[e];
current_temp = degHotend(e);
millis_t heat_start_time_ms = curr_time_ms;
sample_count = 0;
sample_distance = 1;
t1_time = 0;