-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extruder.cpp
2905 lines (2814 loc) · 111 KB
/
Extruder.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
/*
This file is part of Repetier-Firmware.
Repetier-Firmware 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.
Repetier-Firmware 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 Repetier-Firmware. If not, see <http://www.gnu.org/licenses/>.
This firmware is a nearly complete rewrite of the sprinter firmware
by kliment (https://github.com/kliment/Sprinter)
which based on Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
*/
#include "Repetier.h"
uint8_t manageMonitor = 0; ///< Temp. we want to monitor with our host. 1+NUM_EXTRUDER is heated bed
unsigned int counterPeriodical = 0;
volatile uint8_t executePeriodical = 0;
uint8_t counter500ms = 5;
#if FEATURE_DITTO_PRINTING
uint8_t Extruder::dittoMode = 0;
#endif
#if MIXING_EXTRUDER > 0
int Extruder::mixingS;
uint8_t Extruder::mixingDir = 10;
uint8_t Extruder::activeMixingExtruder = 0;
#endif // MIXING_EXTRUDER
#ifdef SUPPORT_MAX6675
extern int16_t read_max6675(uint8_t ss_pin, fast8_t idx);
#endif
#ifdef SUPPORT_MAX31855
extern int16_t read_max31855(uint8_t ss_pin, fast8_t idx);
#endif
#if ANALOG_INPUTS > 0
const uint8 osAnalogInputChannels[] PROGMEM = ANALOG_INPUT_CHANNELS;
volatile uint osAnalogInputValues[ANALOG_INPUTS];
#endif
#ifdef USE_GENERIC_THERMISTORTABLE_1
short temptable_generic1[GENERIC_THERM_NUM_ENTRIES][2];
#endif
#ifdef USE_GENERIC_THERMISTORTABLE_2
short temptable_generic2[GENERIC_THERM_NUM_ENTRIES][2];
#endif
#ifdef USE_GENERIC_THERMISTORTABLE_3
short temptable_generic3[GENERIC_THERM_NUM_ENTRIES][2];
#endif
/** Makes updates to temperatures and heater state every call.
Is called every 100ms.
*/
static uint8_t extruderTempErrors = 0;
static uint8_t extrSecondFlag = 0;
void Extruder::manageTemperatures() {
extrSecondFlag++;
if(extrSecondFlag == 10)
extrSecondFlag = 0;
Com::writeToAll = true;
#if FEATURE_WATCHDOG
HAL::pingWatchdog();
#endif // FEATURE_WATCHDOG
uint8_t errorDetected = 0;
#ifdef RED_BLUE_STATUS_LEDS
bool hot = false;
#endif
bool newDefectFound = false;
millis_t time = HAL::timeInMilliseconds(); // compare time for decouple tests
#if NUM_TEMPERATURE_LOOPS > 0
for(uint8_t controller = 0; controller < NUM_TEMPERATURE_LOOPS; controller++) {
TemperatureController *act = tempController[controller];
// Get Temperature
act->updateCurrentTemperature();
#if FAN_THERMO_PIN > -1
// Special case thermistor controlled fan
if(act == &thermoController) {
if(act->currentTemperatureC < Printer::thermoMinTemp)
pwm_pos[PWM_FAN_THERMO] = 0;
else if(act->currentTemperatureC > Printer::thermoMaxTemp)
pwm_pos[PWM_FAN_THERMO] = FAN_THERMO_MAX_PWM;
else {
// Interpolate target speed
float out = FAN_THERMO_MIN_PWM + (FAN_THERMO_MAX_PWM - FAN_THERMO_MIN_PWM) * (act->currentTemperatureC - Printer::thermoMinTemp) / (Printer::thermoMaxTemp - Printer::thermoMinTemp);
if(out > 255)
pwm_pos[PWM_FAN_THERMO] = FAN_THERMO_MAX_PWM;
else
pwm_pos[PWM_FAN_THERMO] = static_cast<uint8_t>(out);
}
continue;
}
#endif
// Handle automatic cooling of extruders
if(controller < NUM_EXTRUDER) {
#if SHARED_EXTRUDER_HEATER
if(controller > 0)
continue;
#endif
#if ((SHARED_COOLER && NUM_EXTRUDER >= 2 && EXT0_EXTRUDER_COOLER_PIN == EXT1_EXTRUDER_COOLER_PIN) || SHARED_COOLER_BOARD_EXT) && EXT0_EXTRUDER_COOLER_PIN > -1
if(controller == 0) {
bool enable = false;
for(uint8_t j = 0; j < NUM_EXTRUDER; j++) {
if(tempController[j]->currentTemperatureC >= EXTRUDER_FAN_COOL_TEMP || tempController[j]->targetTemperatureC >= EXTRUDER_FAN_COOL_TEMP) {
enable = true;
break;
}
}
#if SHARED_COOLER_BOARD_EXT
if(pwm_pos[PWM_BOARD_FAN] == BOARD_FAN_SPEED) enable = true;
#endif
extruder[0].coolerPWM = (enable ? extruder[0].coolerSpeed : 0);
} // controller == 0
#else
if(act->currentTemperatureC < EXTRUDER_FAN_COOL_TEMP && act->targetTemperatureC < EXTRUDER_FAN_COOL_TEMP)
extruder[controller].coolerPWM = 0;
else
extruder[controller].coolerPWM = extruder[controller].coolerSpeed;
#endif // NUM_EXTRUDER
} // extruder controller
// do skip temperature control while auto tuning is in progress
if(controller == autotuneIndex) continue;
#if MIXING_EXTRUDER
if(controller > 0 && controller < NUM_EXTRUDER) continue; // Mixing extruder only test for ext 0
#endif // MIXING_EXTRUDER
if(controller == autotuneIndex) // Ignore heater we are currently testing
continue;
// Check for obvious sensor errors
if((act->currentTemperatureC < MIN_DEFECT_TEMPERATURE || act->currentTemperatureC > MAX_DEFECT_TEMPERATURE) &&
act->targetTemperatureC > 0 /*is heating*/ &&
(act->preheatTime() == 0 || act->preheatTime() >= MILLISECONDS_PREHEAT_TIME /*preheating time is over*/)) { // no temp sensor or short in sensor, disable heater
errorDetected = 1;
if(extruderTempErrors < 10) // Ignore short temporary failures
extruderTempErrors++;
else {
act->flags |= TEMPERATURE_CONTROLLER_FLAG_SENSDEFECT;
if(!Printer::isAnyTempsensorDefect()) {
newDefectFound = true;
Printer::setAnyTempsensorDefect();
reportTempsensorError();
UI_MESSAGE(2);
}
EVENT_HEATER_DEFECT(controller);
}
}
#if HAVE_HEATED_BED
else if(controller == HEATED_BED_INDEX && Extruder::getHeatedBedTemperature() > HEATED_BED_MAX_TEMP + 5) {
errorDetected = 1;
if(extruderTempErrors < 10) // Ignore short temporary failures
extruderTempErrors++;
else {
act->flags |= TEMPERATURE_CONTROLLER_FLAG_SENSDEFECT;
Com::printErrorFLN(PSTR("Heated bed exceeded max temperature!"));
if(!Printer::isAnyTempsensorDefect()) {
newDefectFound = true;
Printer::setAnyTempsensorDefect();
reportTempsensorError();
UI_MESSAGE(2);
}
EVENT_HEATER_DEFECT(controller);
}
}
#endif // HAVE_HEATED_BED
#ifdef RED_BLUE_STATUS_LEDS
if(act->currentTemperatureC > 50)
hot = true;
#endif // RED_BLUE_STATUS_LEDS
if(Printer::isAnyTempsensorDefect()) continue;
uint8_t on = act->currentTemperatureC >= act->targetTemperatureC ? LOW : HIGH;
// Make a sound if alarm was set on reaching target temperature
if(!on && act->isAlarm()) {
beep(50 * (controller + 1), 3);
act->setAlarm(false); //reset alarm
}
// Run test if heater and sensor are decoupled
bool decoupleTestRequired = !errorDetected && act->decoupleTestPeriod > 0 && (time - act->lastDecoupleTest) > act->decoupleTestPeriod; // time enough for temperature change?
if(decoupleTestRequired && act->isDecoupleFullOrHold() && Printer::isPowerOn()) { // Only test when powered
if(act->isDecoupleFull()) { // Phase 1: Heating fully until target range is reached
if(act->currentTemperatureC - act->lastDecoupleTemp < DECOUPLING_TEST_MIN_TEMP_RISE) { // failed test
extruderTempErrors++;
errorDetected = 1;
if(extruderTempErrors > 10) { // Ignore short temporary failures
act->flags |= TEMPERATURE_CONTROLLER_FLAG_SENSDECOUPLED;
if(!Printer::isAnyTempsensorDefect()) {
Printer::setAnyTempsensorDefect();
newDefectFound = true;
UI_MESSAGE(3);
}
UI_ERROR_P(Com::tHeaterDecoupled);
Com::printErrorFLN(Com::tHeaterDecoupledWarning);
Com::printF(PSTR("Error:Temp. raised to slow. Rise = "), act->currentTemperatureC - act->lastDecoupleTemp);
Com::printF(PSTR(" after "), (int32_t)(time - act->lastDecoupleTest));
Com::printFLN(PSTR(" ms"));
EVENT_HEATER_DECOUPLED(controller);
}
} else {
act->stopDecouple();
act->startFullDecouple(time);
}
} else { // Phase 2: Holding temperature inside a target corridor
if(fabs(act->currentTemperatureC - act->targetTemperatureC) > DECOUPLING_TEST_MAX_HOLD_VARIANCE) { // failed test
extruderTempErrors++;
errorDetected = 1;
if(extruderTempErrors > 10) { // Ignore short temporary failures
act->flags |= TEMPERATURE_CONTROLLER_FLAG_SENSDECOUPLED;
if(!Printer::isAnyTempsensorDefect()) {
Printer::setAnyTempsensorDefect();
newDefectFound = true;
UI_MESSAGE(3);
}
UI_ERROR_P(Com::tHeaterDecoupled);
Com::printErrorFLN(Com::tHeaterDecoupledWarning);
Com::printF(PSTR("Error:Could not hold temperature "), act->lastDecoupleTemp);
Com::printF(PSTR(" measured "), act->currentTemperatureC);
Com::printFLN(PSTR(" deg. C"));
EVENT_HEATER_DECOUPLED(controller);
}
} else {
act->lastDecoupleTest = time - act->decoupleTestPeriod + 1000; // once running test every second
}
}
}
uint8_t output = 0;
float error = act->targetTemperatureC - act->currentTemperatureC;
if(act->targetTemperatureC < 20.0f) { // heating is off
output = 0; // off is off, even if damping term wants a heat peak!
act->stopDecouple();
} else if(error > PID_CONTROL_RANGE) { // Phase 1: full heating until control range reached
output = act->pidMax;
act->startFullDecouple(time);
act->tempIState = act->tempIStateLimitMin;
if(act->heatManager == HTR_DEADTIME) {
act->tempIStateLimitMax = act->pidDriveMax;
act->tempIStateLimitMin = 0;
}
} else if(error < -PID_CONTROL_RANGE) // control range left upper side!
output = 0;
else { // control range handle by heat manager
if(act->heatManager == HTR_PID) {
act->startHoldDecouple(time);
// Com::printF(PSTR(" CUR:"),act->currentTemperatureC); Com::printFLN(PSTR(" IST:"),(act->pidIGain * act->tempIState * 0.1),1);
float pidTerm = act->pidPGain * error;
act->tempIState = constrain(act->tempIState + error, act->tempIStateLimitMin, act->tempIStateLimitMax);
pidTerm += act->pidIGain * act->tempIState * 0.1; // 0.1 = 10Hz
// float dgain = act->pidDGain * (act->tempArray[act->tempPointer] - act->currentTemperatureC) * 3.333f;
float dgain = act->pidDGain * (act->lastTemperatureC - act->temperatureC);
pidTerm += dgain;
#if SCALE_PID_TO_MAX == 1
pidTerm = (pidTerm * act->pidMax) * 0.0039215;
#endif // SCALE_PID_TO_MAX
output = constrain((int)pidTerm, 0, act->pidMax);
} else if(act->heatManager == HTR_DEADTIME) { // dead-time control
act->startHoldDecouple(time);
// output = (act->currentTemperatureC + act->tempIState * act->deadTime > act->targetTemperatureC ? 0 : act->pidDriveMax);
float raising = (act->temperatureC - act->lastTemperatureC); // raising dT/dt from 0.5 seconds
// act->tempIState = 0.25 * (3.0 * act->tempIState + raising); // damp raising
#ifndef SKIP_DEADTIME_ADJUSTMENT
if(raising < 0 && act->tempIState >= 0) { // peak reached
if(error < -0.5)
act->tempIStateLimitMax = constrain(act->tempIStateLimitMax - 10, act->tempIStateLimitMin, act->pidDriveMax);
else
act->tempIStateLimitMax = constrain(act->tempIStateLimitMax + 10, act->tempIStateLimitMin, act->pidDriveMax);
// Com::printF(PSTR("Raise:"), raising);Com::printF(PSTR(" er:"),error,2);Com::printFLN(PSTR(" LimitMax:"),act->tempIStateLimitMax,0);
} else if(raising > 0 && act->tempIState <= 0) { // bottom reached
if(error > 0.5)
act->tempIStateLimitMin = constrain(act->tempIStateLimitMin + 10, 0, act->tempIStateLimitMax - 20);
else
act->tempIStateLimitMin = constrain(act->tempIStateLimitMin - 10, 0, act->tempIStateLimitMax - 20);
// Com::printFLN(PSTR("LimitMin:"),act->tempIStateLimitMin,0);
}
// Com::printFLN(PSTR("Raise:"), raising);
#endif
output = static_cast<uint8_t>(act->currentTemperatureC + raising * act->deadTime > act->targetTemperatureC ? act->tempIStateLimitMin : act->tempIStateLimitMax /* pidDriveMax */);
act->tempIState = raising;
} else // bang bang and slow bang bang
if(act->heatManager == HTR_SLOWBANG) { // Bang-bang with reduced change frequency to save relays life
if (time - act->lastTemperatureUpdate > HEATED_BED_SET_INTERVAL) {
output = (on ? act->pidMax : 0);
act->lastTemperatureUpdate = time;
if(on) act->startFullDecouple(time);
else act->stopDecouple();
} else continue;
} else if(act->heatManager == HTR_OFF) { // Fast Bang-Bang fall back
output = (on ? act->pidMax : 0);
if(on) act->startFullDecouple(time);
else act->stopDecouple();
}
} // Temperature control
#ifdef MAXTEMP
if(act->currentTemperatureC > MAXTEMP) // Force heater off if MAXTEMP is exceeded
output = 0;
#endif // MAXTEMP
pwm_pos[act->pwmIndex] = output; // set pwm signal
if(extrSecondFlag == 0 /*|| (act->heatManager == HTR_DEADTIME && extrSecondFlag == 5)*/) {
act->lastTemperatureC = act->temperatureC;
act->temperatureC = act->currentTemperatureC;
}
#if LED_PIN > -1
if(act == &Extruder::current->tempControl)
WRITE(LED_PIN, on);
#endif // LED_PIN
} // for controller
#ifdef RED_BLUE_STATUS_LEDS
if(Printer::isAnyTempsensorDefect()) {
WRITE(BLUE_STATUS_LED, HIGH);
WRITE(RED_STATUS_LED, HIGH);
} else {
WRITE(BLUE_STATUS_LED, !hot);
WRITE(RED_STATUS_LED, hot);
}
#endif // RED_BLUE_STATUS_LEDS
if(errorDetected == 0 && extruderTempErrors > 0)
extruderTempErrors--;
if(newDefectFound) {
Com::printFLN(PSTR("Disabling all heaters due to detected sensor defect."));
for(uint8_t i = 0; i < NUM_TEMPERATURE_LOOPS; i++) {
tempController[i]->targetTemperatureC = 0;
pwm_pos[tempController[i]->pwmIndex] = 0;
}
#if defined(KILL_IF_SENSOR_DEFECT) && KILL_IF_SENSOR_DEFECT > 0
if(!Printer::debugDryrun() && PrintLine::linesCount > 0) { // kill printer if actually printing
Printer::stopPrint();
Printer::kill(false);
}
#endif // KILL_IF_SENSOR_DEFECT
Printer::debugSet(8); // Go into dry mode
GCode::fatalError(PSTR("Heater/sensor error"));
} // any sensor defect
#endif // NUM_TEMPERATURE_LOOPS
// Report temperatures every second, so we do not need to send M105
if(Printer::isAutoreportTemp()) {
millis_t now = HAL::timeInMilliseconds();
if(now - Printer::lastTempReport > 1000) {
Printer::lastTempReport = now;
Commands::printTemperatures();
}
}
}
void TemperatureController::waitForTargetTemperature() {
if(targetTemperatureC < 30) return;
if(Printer::debugDryrun()) return;
bool oldReport = Printer::isAutoreportTemp();
Printer::setAutoreportTemp(true);
//millis_t time = HAL::timeInMilliseconds();
while(true) {
/*if( (HAL::timeInMilliseconds() - time) > 1000 ) //Print Temp Reading every 1 second while heating up.
{
Commands::printTemperatures();
time = HAL::timeInMilliseconds();
}*/
Commands::checkForPeriodicalActions(true);
GCode::keepAlive(WaitHeater);
if(fabs(targetTemperatureC - currentTemperatureC) <= 1) {
Printer::setAutoreportTemp(oldReport);
return;
}
}
}
fast8_t TemperatureController::errorState() {
if(isSensorDefect())
return 1;
if(isSensorDecoupled())
return 2;
#if EXTRUDER_JAM_CONTROL
if(isFilamentChange())
return 6;
#if JAM_METHOD == 1
if(isJammed())
return 5; // jammed or out of filament
if(isSlowedDown())
return 3; // slipping
#else // only a simple switch to pause on end of filament
if(isJammed())
return 6; // out of filament
#endif
#endif
return 0;
}
/* For pausing we negate target temperature, so negative value means paused extruder.
Since temp. is negative no heating will occur. */
void Extruder::pauseExtruders(bool bed) {
#if NUM_EXTRUDER > 0
disableAllExtruderMotors();
for(fast8_t i = 0; i < NUM_EXTRUDER; i++) {
if(extruder[i].tempControl.targetTemperatureC > 0) {
extruder[i].tempControl.targetTemperatureC = -fabs(extruder[i].tempControl.targetTemperatureC);
pwm_pos[extruder[i].tempControl.pwmIndex] = 0;
}
}
#endif
#if HAVE_HEATED_BED
if(bed) {
heatedBedController.targetTemperatureC = -fabs(heatedBedController.targetTemperatureC);
pwm_pos[heatedBedController.pwmIndex] = 0;
}
#endif
}
void Extruder::unpauseExtruders(bool wait) {
#if NUM_EXTRUDER > 0
// activate temperatures
for(fast8_t i = 0; i < NUM_EXTRUDER; i++) {
if(extruder[i].tempControl.targetTemperatureC < 0)
extruder[i].tempControl.targetTemperatureC = -extruder[i].tempControl.targetTemperatureC;
}
#endif
#if HAVE_HEATED_BED
bool waitBed = false;
if(heatedBedController.targetTemperatureC < 0) {
heatedBedController.targetTemperatureC = -heatedBedController.targetTemperatureC;
waitBed = true;
}
#endif
if(wait) {
#if NUM_EXTRUDER > 0
for(fast8_t i = 0; i < NUM_EXTRUDER; i++)
extruder[i].tempControl.waitForTargetTemperature();
#endif
#if HAVE_HEATED_BED
if(waitBed) {
heatedBedController.waitForTargetTemperature();
}
#endif
}
}
void TemperatureController::resetAllErrorStates() {
#if NUM_TEMPERATURE_LOOPS > 0
for(int i = 0; i < NUM_TEMPERATURE_LOOPS; i++) {
tempController[i]->removeErrorStates();
}
#endif
Printer::unsetAnyTempsensorDefect();
}
#if EXTRUDER_JAM_CONTROL
void TemperatureController::setJammed(bool on) {
if(on) {
flags |= TEMPERATURE_CONTROLLER_FLAG_JAM;
Printer::setInterruptEvent(PRINTER_INTERRUPT_EVENT_JAM_DETECTED, true);
} else flags &= ~(TEMPERATURE_CONTROLLER_FLAG_JAM);
}
void Extruder::markAllUnjammed() {
for(fast8_t i = 0; i < NUM_EXTRUDER; i++) {
extruder[i].tempControl.setJammed(false);
extruder[i].tempControl.setSlowedDown(false);
extruder[i].resetJamSteps();
if(Printer::feedrateMultiply == extruder[i].jamSlowdownTo)
Commands::changeFeedrateMultiply(100);
}
Printer::unsetAnyTempsensorDefect(); // stop alarm
Com::printInfoFLN(PSTR("Marked all extruders as unjammed."));
Printer::setUIErrorMessage(false);
}
void Extruder::resetJamSteps() {
jamStepsOnSignal = jamStepsSinceLastSignal;
jamStepsSinceLastSignal = 0;
if(tempControl.isFilamentChange()) {
tempControl.setFilamentChange(false);
} else {
Printer::setInterruptEvent(PRINTER_INTERRUPT_EVENT_JAM_SIGNAL0 + id, false);
}
}
#endif
void Extruder::initHeatedBed() {
#if HAVE_HEATED_BED
heatedBedController.updateTempControlVars();
#if defined(SUPPORT_MAX6675) || defined(SUPPORT_MAX31855)
if(heatedBedController.sensorType == 101 || heatedBedController.sensorType == 102) {
WRITE(SCK_PIN, 0);
SET_OUTPUT(SCK_PIN);
WRITE(MOSI_PIN, 1);
SET_OUTPUT(MOSI_PIN);
WRITE(MISO_PIN, 1);
SET_INPUT(MISO_PIN);
HAL::pinMode(SS, OUTPUT);
HAL::digitalWrite(SS, 1);
HAL::pinMode(heatedBedController.sensorPin, OUTPUT);
HAL::digitalWrite(heatedBedController.sensorPin, 1);
}
#endif
#endif
}
#if defined(USE_GENERIC_THERMISTORTABLE_1) || defined(USE_GENERIC_THERMISTORTABLE_2) || defined(USE_GENERIC_THERMISTORTABLE_3)
void createGenericTable(short table[GENERIC_THERM_NUM_ENTRIES][2], short minTemp, short maxTemp, float beta, float r0, float t0, float r1, float r2) {
t0 += 273.15f;
float rs, vs;
if(r1 == 0) {
rs = r2;
vs = GENERIC_THERM_VREF;
} else {
vs = static_cast<float>((GENERIC_THERM_VREF * r1) / (r1 + r2));
rs = (r2 * r1) / (r1 + r2);
}
float k = r0 * exp(-beta / t0);
float delta = (maxTemp - minTemp) / (GENERIC_THERM_NUM_ENTRIES - 1.0f);
for(uint8_t i = 0; i < GENERIC_THERM_NUM_ENTRIES; i++) {
#if FEATURE_WATCHDOG
HAL::pingWatchdog();
#endif // FEATURE_WATCHDOG
float t = maxTemp - i * delta;
float r = exp(beta / (t + 272.65)) * k;
float v = 4092 * r * vs / ((rs + r) * GENERIC_THERM_VREF);
int adc = static_cast<int>(v);
t *= 8;
if(adc > 4092) adc = 4092;
table[i][0] = (adc >> (ANALOG_REDUCE_BITS));
table[i][1] = static_cast<int>(t);
#ifdef DEBUG_GENERIC
Com::printF(Com::tGenTemp, table[i][0]);
Com::printFLN(Com::tComma, table[i][1]);
#endif
}
}
#endif
/** \brief Initializes all extruder.
Updates the pin configuration needed for the extruder and activates extruder 0.
Starts a interrupt based analog input reader, which is used by simple thermistors
for temperature reading.
*/
void Extruder::initExtruder() {
uint8_t i;
Extruder::current = &extruder[0];
#ifdef USE_GENERIC_THERMISTORTABLE_1
createGenericTable(temptable_generic1, GENERIC_THERM1_MIN_TEMP, GENERIC_THERM1_MAX_TEMP, GENERIC_THERM1_BETA, GENERIC_THERM1_R0, GENERIC_THERM1_T0, GENERIC_THERM1_R1, GENERIC_THERM1_R2);
#endif
#ifdef USE_GENERIC_THERMISTORTABLE_2
createGenericTable(temptable_generic2, GENERIC_THERM2_MIN_TEMP, GENERIC_THERM2_MAX_TEMP, GENERIC_THERM2_BETA, GENERIC_THERM2_R0, GENERIC_THERM2_T0, GENERIC_THERM2_R1, GENERIC_THERM2_R2);
#endif
#ifdef USE_GENERIC_THERMISTORTABLE_3
createGenericTable(temptable_generic3, GENERIC_THERM3_MIN_TEMP, GENERIC_THERM3_MAX_TEMP, GENERIC_THERM3_BETA, GENERIC_THERM3_R0, GENERIC_THERM3_T0, GENERIC_THERM3_R1, GENERIC_THERM3_R2);
#endif
#if defined(EXT0_STEP_PIN) && EXT0_STEP_PIN > -1 && NUM_EXTRUDER > 0
SET_OUTPUT(EXT0_DIR_PIN);
SET_OUTPUT(EXT0_STEP_PIN);
#if defined(EXT0_MIRROR_STEPPER) && EXT0_MIRROR_STEPPER
SET_OUTPUT(EXT0_DIR2_PIN);
SET_OUTPUT(EXT0_STEP2_PIN);
SET_OUTPUT(EXT0_ENABLE2_PIN);
WRITE(EXT0_ENABLE2_PIN, !EXT0_ENABLE_ON);
#endif
#endif
#if defined(EXT1_STEP_PIN) && EXT1_STEP_PIN > -1 && NUM_EXTRUDER > 1
SET_OUTPUT(EXT1_DIR_PIN);
SET_OUTPUT(EXT1_STEP_PIN);
#if defined(EXT1_MIRROR_STEPPER) && EXT1_MIRROR_STEPPER
SET_OUTPUT(EXT1_DIR2_PIN);
SET_OUTPUT(EXT1_STEP2_PIN);
SET_OUTPUT(EXT1_ENABLE2_PIN);
WRITE(EXT1_ENABLE2_PIN, !EXT1_ENABLE_ON);
#endif
#endif
#if defined(EXT2_STEP_PIN) && EXT2_STEP_PIN > -1 && NUM_EXTRUDER > 2
SET_OUTPUT(EXT2_DIR_PIN);
SET_OUTPUT(EXT2_STEP_PIN);
#if defined(EXT2_MIRROR_STEPPER) && EXT2_MIRROR_STEPPER
SET_OUTPUT(EXT2_DIR2_PIN);
SET_OUTPUT(EXT2_STEP2_PIN);
SET_OUTPUT(EXT2_ENABLE2_PIN);
WRITE(EXT2_ENABLE2_PIN, !EXT2_ENABLE_ON);
#endif
#endif
#if defined(EXT3_STEP_PIN) && EXT3_STEP_PIN > -1 && NUM_EXTRUDER > 3
SET_OUTPUT(EXT3_DIR_PIN);
SET_OUTPUT(EXT3_STEP_PIN);
#if defined(EXT3_MIRROR_STEPPER) && EXT3_MIRROR_STEPPER
SET_OUTPUT(EXT3_DIR2_PIN);
SET_OUTPUT(EXT3_STEP2_PIN);
SET_OUTPUT(EXT3_ENABLE2_PIN);
WRITE(EXT3_ENABLE2_PIN, !EXT3_ENABLE_ON);
#endif
#endif
#if defined(EXT4_STEP_PIN) && EXT4_STEP_PIN > -1 && NUM_EXTRUDER > 4
SET_OUTPUT(EXT4_DIR_PIN);
SET_OUTPUT(EXT4_STEP_PIN);
#if defined(EXT4_MIRROR_STEPPER) && EXT4_MIRROR_STEPPER
SET_OUTPUT(EXT4_DIR2_PIN);
SET_OUTPUT(EXT4_STEP2_PIN);
SET_OUTPUT(EXT4_ENABLE2_PIN);
WRITE(EXT4_ENABLE2_PIN, !EXT4_ENABLE_ON);
#endif
#endif
#if defined(EXT5_STEP_PIN) && EXT5_STEP_PIN > -1 && NUM_EXTRUDER > 5
SET_OUTPUT(EXT5_DIR_PIN);
SET_OUTPUT(EXT5_STEP_PIN);
#if defined(EXT5_MIRROR_STEPPER) && EXT5_MIRROR_STEPPER
SET_OUTPUT(EXT5_DIR2_PIN);
SET_OUTPUT(EXT5_STEP2_PIN);
SET_OUTPUT(EXT5_ENABLE2_PIN);
WRITE(EXT5_ENABLE2_PIN, !EXT5_ENABLE_ON);
#endif
#endif
for(i = 0; i < NUM_EXTRUDER; ++i) {
Extruder *act = &extruder[i];
if(act->enablePin > -1) {
HAL::pinMode(act->enablePin, OUTPUT);
HAL::digitalWrite(act->enablePin, !act->enableOn);
}
act->tempControl.lastTemperatureUpdate = HAL::timeInMilliseconds();
#if defined(SUPPORT_MAX6675) || defined(SUPPORT_MAX31855)
if(act->tempControl.sensorType == 101 || act->tempControl.sensorType == 102) {
WRITE(SCK_PIN, 0);
SET_OUTPUT(SCK_PIN);
WRITE(MOSI_PIN, 1);
SET_OUTPUT(MOSI_PIN);
WRITE(MISO_PIN, 1);
SET_INPUT(MISO_PIN);
//SET_OUTPUT(SS);
//WRITE(SS, HIGH);
HAL::pinMode(SS, OUTPUT);
HAL::digitalWrite(SS, 1);
HAL::pinMode(act->tempControl.sensorPin, OUTPUT);
HAL::digitalWrite(act->tempControl.sensorPin, 1);
}
#endif
}
#if HEATED_BED_HEATER_PIN > -1
SET_OUTPUT(HEATED_BED_HEATER_PIN);
WRITE(HEATED_BED_HEATER_PIN, HEATER_PINS_INVERTED);
Extruder::initHeatedBed();
#endif
#if ANALOG_INPUTS > 0
HAL::analogStart();
#endif
}
void TemperatureController::updateTempControlVars() {
if(heatManager == HTR_PID && pidIGain != 0) { // prevent division by zero
tempIStateLimitMax = (float)pidDriveMax * 10.0f / pidIGain;
tempIStateLimitMin = (float)pidDriveMin * 10.0f / pidIGain;
}
}
/** \brief Select extruder ext_num.
This function changes and initializes a new extruder. This is also called, after the eeprom values are changed.
*/
void Extruder::selectExtruderById(uint8_t extruderId) {
float cx, cy, cz;
Printer::realPosition(cx, cy, cz);
#if DUAL_X_AXIS && FEATURE_DITTO_PRINTING
if(dittoMode != 0) // In ditto mode only extruder 0 is usable and gets set by selecting ditto mode
return;
#endif
#if NUM_EXTRUDER > 0
Commands::waitUntilEndOfAllMoves();
#if MIXING_EXTRUDER
if(extruderId >= VIRTUAL_EXTRUDER)
extruderId = 0;
activeMixingExtruder = extruderId;
for(uint8_t i = 0; i < NUM_EXTRUDER; i++)
Extruder::setMixingWeight(i, extruder[i].virtualWeights[extruderId]);
Com::printFLN(PSTR("SelectExtruder:"), static_cast<int>(extruderId));
extruderId = 0;
#endif
if(extruderId >= NUM_EXTRUDER)
extruderId = 0;
Extruder *current = extruder->current;
Extruder *next = &extruder[extruderId];
bool executeSelect = extruderId != current->id;
#if RAISE_Z_ON_TOOLCHANGE > 0
float lastZ = Printer::lastCmdPos[Z_AXIS];
#endif
#if DUAL_X_AXIS
float lastX = Printer::lastCmdPos[X_AXIS];
float lastY = Printer::lastCmdPos[Y_AXIS];
// Park current extruder
int32_t dualXPosSteps = Printer::currentPositionSteps[X_AXIS] - Printer::xMinSteps; // here the extruder should be (steps from xmin pos)
#endif
#if !MIXING_EXTRUDER
Com::printFLN(PSTR("SelectExtruder:"), static_cast<int>(extruderId));
#endif
#if NUM_EXTRUDER > 1 && MIXING_EXTRUDER == 0
if(executeSelect) {
GCode::executeFString(Extruder::current->deselectCommands);
}
Commands::waitUntilEndOfAllMoves();
#endif
float oldfeedrate = Printer::feedrate;
current->extrudePosition = Printer::currentPositionSteps[E_AXIS];
#if RAISE_Z_ON_TOOLCHANGE > 0 && !LAZY_DUAL_X_AXIS
if (executeSelect && Printer::isZHomed())
PrintLine::moveRelativeDistanceInSteps(0, 0, static_cast<int32_t>(RAISE_Z_ON_TOOLCHANGE * Printer::axisStepsPerMM[Z_AXIS]), 0, Printer::homingFeedrate[Z_AXIS], true, false);
#endif
#if DUAL_X_AXIS
#if LAZY_DUAL_X_AXIS
if(Printer::sledParked) {
dualXPosSteps = Printer::lastCmdPos[X_AXIS] * Printer::axisStepsPerMM[X_AXIS] - Printer::xMinSteps; // correct to where we should be
}
#endif // LAZY_DUAL_X_AXIS
if(Printer::isXHomed() && executeSelect
#if LAZY_DUAL_X_AXIS
&& !Printer::sledParked
#endif
) { // park extruder that will become inactive
bool oldDestCheck = Printer::isNoDestinationCheck();
Printer::setNoDestinationCheck(true);
PrintLine::moveRelativeDistanceInSteps(current->xOffset - dualXPosSteps, 0, 0, 0, EXTRUDER_SWITCH_XY_SPEED, true, false);
Printer::setNoDestinationCheck(oldDestCheck);
#if LAZY_DUAL_X_AXIS
Printer::sledParked = true;
#endif
}
#endif
if(Printer::isHomedAll() && next->zOffset < current->zOffset) { // prevent extruder from hitting bed - move bed down a bit
Printer::offsetZ = -next->zOffset * Printer::invAxisStepsPerMM[Z_AXIS];
Printer::setNoDestinationCheck(true);
#if LAZY_DUAL_X_AXIS && DUAL_X_AXIS
Printer::moveToReal((Printer::xMinSteps + current->xOffset) * Printer::invAxisStepsPerMM[X_AXIS], IGNORE_COORDINATE, IGNORE_COORDINATE, IGNORE_COORDINATE, Printer::homingFeedrate[Z_AXIS]);
Printer::sledParked = true;
#else
Printer::moveToReal(IGNORE_COORDINATE, IGNORE_COORDINATE, IGNORE_COORDINATE, IGNORE_COORDINATE, Printer::homingFeedrate[Z_AXIS]);
#endif
Printer::setNoDestinationCheck(false);
Commands::waitUntilEndOfAllMoves();
Printer::updateCurrentPosition(true);
}
Extruder::current = next;
// --------------------- Now new extruder is active --------------------
#if DUAL_X_RESOLUTION
Printer::updateDerivedParameter(); // adjust to new resolution
dualXPosSteps = Printer::lastCmdPos[X_AXIS] * Printer::axisStepsPerMM[X_AXIS] - Printer::xMinSteps; // correct to where we should be in new coordinates
#endif
#ifdef SEPERATE_EXTRUDER_POSITIONS
// Use separate extruder positions only if being told. Slic3r e.g. creates a continuous extruder position increment
Printer::currentPositionSteps[E_AXIS] = Extruder::current->extrudePosition;
#endif
#if MIXING_EXTRUDER
recomputeMixingExtruderSteps();
#else
Printer::destinationSteps[E_AXIS] = Printer::currentPositionSteps[E_AXIS];
Printer::axisStepsPerMM[E_AXIS] = Extruder::current->stepsPerMM;
Printer::invAxisStepsPerMM[E_AXIS] = 1.0f / Printer::axisStepsPerMM[E_AXIS];
#endif
Printer::maxFeedrate[E_AXIS] = Extruder::current->maxFeedrate;
// max_start_speed_units_per_second[E_AXIS] = Extruder::current->maxStartFeedrate;
Printer::maxAccelerationMMPerSquareSecond[E_AXIS] = Printer::maxTravelAccelerationMMPerSquareSecond[E_AXIS] = next->maxAcceleration;
Printer::maxTravelAccelerationStepsPerSquareSecond[E_AXIS] =
Printer::maxPrintAccelerationStepsPerSquareSecond[E_AXIS] = Printer::maxAccelerationMMPerSquareSecond[E_AXIS] * Printer::axisStepsPerMM[E_AXIS];
#if USE_ADVANCE
Printer::maxExtruderSpeed = (ufast8_t)floor(HAL::maxExtruderTimerFrequency() / (Extruder::current->maxFeedrate * next->stepsPerMM));
#if CPU_ARCH == ARCH_ARM
if(Printer::maxExtruderSpeed > 40) Printer::maxExtruderSpeed = 40;
#else
if(Printer::maxExtruderSpeed > 15) Printer::maxExtruderSpeed = 15;
#endif
float fmax = ((float)HAL::maxExtruderTimerFrequency() / ((float)Printer::maxExtruderSpeed * Printer::axisStepsPerMM[E_AXIS])); // Limit feedrate to interrupt speed
if(fmax < Printer::maxFeedrate[E_AXIS]) Printer::maxFeedrate[E_AXIS] = fmax;
#endif // USE_ADVANCE
Extruder::current->tempControl.updateTempControlVars();
#if DUAL_X_AXIS
// Unpark new current extruder
if(executeSelect) {// Run only when changing
Commands::waitUntilEndOfAllMoves();
Printer::updateCurrentPosition(true); // does not update x in lazy mode!
GCode::executeFString(next->selectCommands);
}
#if LAZY_DUAL_X_AXIS == 0
if (executeSelect) {
Printer::currentPositionSteps[X_AXIS] = Extruder::current->xOffset - dualXPosSteps;
if(Printer::isXHomed()) {
PrintLine::moveRelativeDistanceInSteps(-next->xOffset + dualXPosSteps, 0, 0, 0, EXTRUDER_SWITCH_XY_SPEED, true, false);
Printer::currentPositionSteps[X_AXIS] = dualXPosSteps + Printer::xMinSteps;
}
}
#endif // LAZY_DUAL_X_AXIS == 0
Printer::offsetX = 0;
Printer::updateCurrentPosition(false);
#if LAZY_DUAL_X_AXIS
if(executeSelect) {
if(Printer::isHomedAll()) { // prevent extruder from hitting bed - move bed down a bit
Printer::offsetZ = -next->zOffset * Printer::invAxisStepsPerMM[Z_AXIS];
Printer::currentPositionSteps[X_AXIS] = Printer::xMinSteps + next->xOffset;
Printer::sledParked = false;
Printer::updateCurrentPosition(true);
Printer::moveToReal(IGNORE_COORDINATE, IGNORE_COORDINATE, cz, IGNORE_COORDINATE, Printer::homingFeedrate[Z_AXIS]);
Printer::sledParked = true;
Commands::waitUntilEndOfAllMoves();
Printer::updateCurrentPosition(true);
}
Printer::currentPosition[X_AXIS] = Printer::lastCmdPos[X_AXIS] = lastX;
Printer::lastCmdPos[Y_AXIS] = lastY;
Printer::currentPositionSteps[X_AXIS] = Printer::xMinSteps + next->xOffset;
}
#endif // LAZY_DUAL_X_AXIS
executeSelect = false;
Printer::lastCmdPos[X_AXIS] = lastX;
#else // DUAL_X_AXIS
Printer::offsetX = -next->xOffset * Printer::invAxisStepsPerMM[X_AXIS];
#endif
Printer::offsetY = -next->yOffset * Printer::invAxisStepsPerMM[Y_AXIS];
Printer::offsetZ = -next->zOffset * Printer::invAxisStepsPerMM[Z_AXIS];
Commands::changeFlowrateMultiply(Printer::extrudeMultiply); // needed to adjust extrusionFactor to possibly different diameter
#if DUAL_X_AXIS == 0 || LAZY_DUAL_X_AXIS == 0
#if RAISE_Z_ON_TOOLCHANGE > 0 && !LAZY_DUAL_X_AXIS
if (Printer::isZHomed()) {
Printer::moveToReal(IGNORE_COORDINATE, IGNORE_COORDINATE, cz, IGNORE_COORDINATE, Printer::homingFeedrate[Z_AXIS]);
Printer::lastCmdPos[Z_AXIS] = lastZ;
}
#endif
if(Printer::isHomedAll()) {
Printer::moveToReal(cx, cy, cz, IGNORE_COORDINATE, EXTRUDER_SWITCH_XY_SPEED);
}
#endif
Printer::feedrate = oldfeedrate;
Printer::updateCurrentPosition(true);
#if USE_ADVANCE
HAL::resetExtruderDirection();
#endif // USE_ADVANCE
#if NUM_EXTRUDER > 1 && MIXING_EXTRUDER == 0
if(executeSelect) {// Run only when changing
Commands::waitUntilEndOfAllMoves();
GCode::executeFString(next->selectCommands);
}
#endif
#endif
}
#if MIXING_EXTRUDER
void Extruder::recomputeMixingExtruderSteps() {
int32_t sum_w = 0;
float sum = 0;
for(fast8_t i = 0; i < NUM_EXTRUDER; i++) {
sum_w += extruder[i].mixingW;
sum += extruder[i].stepsPerMM * extruder[i].mixingW;
}
sum /= sum_w;
Printer::currentPositionSteps[E_AXIS] = Printer::currentPositionSteps[E_AXIS] * sum / Printer::axisStepsPerMM[E_AXIS]; // reposition according resolution change
Printer::destinationSteps[E_AXIS] = Printer::currentPositionSteps[E_AXIS];
Printer::axisStepsPerMM[E_AXIS] = sum;
Printer::invAxisStepsPerMM[E_AXIS] = 1.0f / Printer::axisStepsPerMM[E_AXIS];
}
#endif
void Extruder::setTemperatureForExtruder(float temperatureInCelsius, uint8_t extr, bool beep, bool wait) {
#if NUM_EXTRUDER > 0
#if MIXING_EXTRUDER || SHARED_EXTRUDER_HEATER
extr = 0; // map any virtual extruder number to 0
#endif // MIXING_EXTRUDER
bool alloffs = true;
for(uint8_t i = 0; i < NUM_EXTRUDER; i++)
if(tempController[i]->targetTemperatureC > 15) alloffs = false;
#ifdef MAXTEMP
if(temperatureInCelsius > MAXTEMP) temperatureInCelsius = MAXTEMP;
#endif
if(temperatureInCelsius < 0) temperatureInCelsius = 0;
#if SHARED_EXTRUDER_HEATER
for(fast8_t eid = 0; eid < NUM_EXTRUDER; eid++) {
TemperatureController *tc = tempController[eid];
#else
TemperatureController *tc = tempController[extr];
#endif
if(tc->sensorType == 0) temperatureInCelsius = 0;
//if(temperatureInCelsius==tc->targetTemperatureC) return;
if (temperatureInCelsius < MAX_ROOM_TEMPERATURE)
tc->resetPreheatTime();
else if (tc->targetTemperatureC == 0)
tc->startPreheatTime();
tc->setTargetTemperature(temperatureInCelsius);
tc->updateTempControlVars();
if(beep && temperatureInCelsius > MAX_ROOM_TEMPERATURE)
tc->setAlarm(true);
if(temperatureInCelsius >= EXTRUDER_FAN_COOL_TEMP) extruder[extr].coolerPWM = extruder[extr].coolerSpeed;
Com::printF(Com::tTargetExtr, extr, 0);
Com::printFLN(Com::tColon, temperatureInCelsius, 0);
#if SHARED_EXTRUDER_HEATER
}
TemperatureController *tc = tempController[extr];
#endif
#if FEATURE_DITTO_PRINTING
if(Extruder::dittoMode && extr == 0) {
TemperatureController *tc2 = tempController[1];
tc2->setTargetTemperature(temperatureInCelsius);
tc2->updateTempControlVars();
if(temperatureInCelsius >= EXTRUDER_FAN_COOL_TEMP) extruder[1].coolerPWM = extruder[1].coolerSpeed;
#if NUM_EXTRUDER > 2
if(Extruder::dittoMode > 1 && extr == 0) {
TemperatureController *tc2 = tempController[2];
tc2->setTargetTemperature(temperatureInCelsius);
tc2->updateTempControlVars();
if(temperatureInCelsius >= EXTRUDER_FAN_COOL_TEMP) extruder[2].coolerPWM = extruder[2].coolerSpeed;
}
#endif
#if NUM_EXTRUDER > 3
if(Extruder::dittoMode > 2 && extr == 0) {
TemperatureController *tc2 = tempController[3];
tc2->setTargetTemperature(temperatureInCelsius);
tc2->updateTempControlVars();
if(temperatureInCelsius >= EXTRUDER_FAN_COOL_TEMP) extruder[3].coolerPWM = extruder[3].coolerSpeed;
}
#endif
}
#endif // FEATURE_DITTO_PRINTING
if(wait && temperatureInCelsius > MAX_ROOM_TEMPERATURE
#if defined(SKIP_M109_IF_WITHIN) && SKIP_M109_IF_WITHIN > 0
&& !(abs(tc->currentTemperatureC - tc->targetTemperatureC) < (SKIP_M109_IF_WITHIN))// Already in range
#endif
) {
Extruder *actExtruder = &extruder[extr];
UI_STATUS_UPD_F(Com::translatedF(UI_TEXT_HEATING_EXTRUDER_ID));
EVENT_WAITING_HEATER(actExtruder->id);
bool dirRising = actExtruder->tempControl.targetTemperatureC > actExtruder->tempControl.currentTemperatureC;
//millis_t printedTime = HAL::timeInMilliseconds();
millis_t waituntil = 0;
#if RETRACT_DURING_HEATUP
uint8_t retracted = 0;
#endif
millis_t currentTime;
millis_t maxWaitUntil = 0;
bool oldAutoreport = Printer::isAutoreportTemp();
Printer::setAutoreportTemp(true);
do {
previousMillisCmd = currentTime = HAL::timeInMilliseconds();
/*if( (currentTime - printedTime) > 1000 ) //Print Temp Reading every 1 second while heating up.
{
Commands::printTemperatures();
printedTime = currentTime;
}*/
Commands::checkForPeriodicalActions(true);
GCode::keepAlive(WaitHeater);
//gcode_read_serial();
#if RETRACT_DURING_HEATUP
if (actExtruder == Extruder::current && actExtruder->waitRetractUnits > 0 && !retracted && dirRising && actExtruder->tempControl.currentTemperatureC > actExtruder->waitRetractTemperature) {
PrintLine::moveRelativeDistanceInSteps(0, 0, 0, -actExtruder->waitRetractUnits * Printer::axisStepsPerMM[E_AXIS], actExtruder->maxFeedrate / 4, false, false);
retracted = 1;
}
#endif
if(maxWaitUntil == 0) {
if(dirRising ? actExtruder->tempControl.currentTemperatureC >= actExtruder->tempControl.targetTemperatureC - 5 : actExtruder->tempControl.currentTemperatureC <= actExtruder->tempControl.targetTemperatureC + 5) {
maxWaitUntil = currentTime + 120000L;
}
} else if((millis_t)(maxWaitUntil - currentTime) < 2000000000UL) {
break;
}
if((waituntil == 0 &&
(dirRising ? actExtruder->tempControl.currentTemperatureC >= actExtruder->tempControl.targetTemperatureC - 1
: actExtruder->tempControl.currentTemperatureC <= actExtruder->tempControl.targetTemperatureC + 1))
#if defined(TEMP_HYSTERESIS) && TEMP_HYSTERESIS >= 1
|| (waituntil != 0 && (abs(actExtruder->tempControl.currentTemperatureC - actExtruder->tempControl.targetTemperatureC)) > TEMP_HYSTERESIS)
#endif
) {
waituntil = currentTime + 1000UL * (millis_t)actExtruder->watchPeriod; // now wait for temp. to stabilize
}
} while(waituntil == 0 || (waituntil != 0 && (millis_t)(waituntil - currentTime) < 2000000000UL));
Printer::setAutoreportTemp(oldAutoreport);
#if RETRACT_DURING_HEATUP
if (retracted && actExtruder == Extruder::current) {
PrintLine::moveRelativeDistanceInSteps(0, 0, 0, actExtruder->waitRetractUnits * Printer::axisStepsPerMM[E_AXIS], actExtruder->maxFeedrate / 4, false, false);
}
#endif
EVENT_HEATING_FINISHED(actExtruder->id);
}
UI_CLEAR_STATUS;
bool alloff = true;
for(uint8_t i = 0; i < NUM_EXTRUDER; i++)
if(tempController[i]->targetTemperatureC > 15) alloff = false;
#if EEPROM_MODE != 0
if(alloff && !alloffs) // All heaters are now switched off?
EEPROM::updatePrinterUsage();
#endif
if(alloffs && !alloff) { // heaters are turned on, start measuring printing time