forked from prusa3d/Prusa-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmu.cpp
executable file
·1606 lines (1464 loc) · 44.1 KB
/
mmu.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
//! @file
#include "mmu.h"
#include "planner.h"
#include "language.h"
#include "lcd.h"
#include "uart2.h"
#include "temperature.h"
#include "Configuration_prusa.h"
#include "fsensor.h"
#include "cardreader.h"
#include "ultralcd.h"
#include "sound.h"
#include "printers.h"
#include <avr/pgmspace.h>
#include "io_atmega2560.h"
#include "AutoDeplete.h"
//-//
#include "util.h"
#ifdef TMC2130
#include "tmc2130.h"
#endif //TMC2130
#define MMU_TODELAY 100
#define MMU_TIMEOUT 10
#define MMU_CMD_TIMEOUT 45000ul //45s timeout for mmu commands (except P0)
#define MMU_P0_TIMEOUT 3000ul //timeout for P0 command: 3seconds
#define MMU_MAX_RESEND_ATTEMPTS 2
#ifdef MMU_HWRESET
#define MMU_RST_PIN 76
#endif //MMU_HWRESET
namespace
{
enum class S : uint_least8_t
{
WaitStealthMode,
GetFindaInit,
GetBuildNr,
GetVersion,
Init,
Disabled,
Idle,
GetFinda,
WaitCmd, //!< wait for command response
Pause,
GetDrvError, //!< get power failures count
SwitchMode //switch mmu between stealth and normal mode
};
}
bool mmu_enabled = false;
bool mmu_ready = false;
bool mmu_fil_loaded = false; //if true: blocks execution of duplicit T-codes
static S mmu_state = S::Disabled;
MmuCmd mmu_cmd = MmuCmd::None;
//idler ir sensor
static uint8_t mmu_idl_sens = 0;
bool ir_sensor_detected = false;
static bool mmu_loading_flag = false; //when set to true, we assume that mmu2 unload was finished and loading phase is now performed; printer can send 'A' to mmu2 to abort loading process
uint8_t mmu_extruder = MMU_FILAMENT_UNKNOWN;
//! This variable probably has no meaning and is planed to be removed
uint8_t tmp_extruder = MMU_FILAMENT_UNKNOWN;
int8_t mmu_finda = -1;
uint32_t mmu_last_finda_response = 0;
int16_t mmu_version = -1;
int16_t mmu_buildnr = -1;
uint32_t mmu_last_request = 0;
uint32_t mmu_last_response = 0;
MmuCmd mmu_last_cmd = MmuCmd::None;
uint16_t mmu_power_failures = 0;
#ifdef MMU_DEBUG
static const auto DEBUG_PUTCHAR = putchar;
static const auto DEBUG_PUTS_P = puts_P;
static const auto DEBUG_PRINTF_P = printf_P;
#else //MMU_DEBUG
#define DEBUG_PUTCHAR(c)
#define DEBUG_PUTS_P(str)
#define DEBUG_PRINTF_P( __fmt, ... )
#endif //MMU_DEBUG
#if defined(MMU_FINDA_DEBUG) && defined(MMU_DEBUG)
static const auto FDEBUG_PUTS_P = puts_P;
static const auto FDEBUG_PRINTF_P = printf_P;
#else
#define FDEBUG_PUTS_P(str)
#define FDEBUG_PRINTF_P( __fmt, ... )
#endif //defined(MMU_FINDA_DEBUG) && defined(MMU_DEBUG)
//clear rx buffer
void mmu_clr_rx_buf(void)
{
while (fgetc(uart2io) >= 0);
}
//send command - puts
int mmu_puts_P(const char* str)
{
mmu_clr_rx_buf(); //clear rx buffer
int r = fputs_P(str, uart2io); //send command
mmu_last_request = _millis();
return r;
}
//send command - printf
int mmu_printf_P(const char* format, ...)
{
va_list args;
va_start(args, format);
mmu_clr_rx_buf(); //clear rx buffer
int r = vfprintf_P(uart2io, format, args); //send command
va_end(args);
mmu_last_request = _millis();
return r;
}
//check 'ok' response
int8_t mmu_rx_ok(void)
{
int8_t res = uart2_rx_str_P(PSTR("ok\n"));
if (res == 1) mmu_last_response = _millis();
return res;
}
//check 'start' response
int8_t mmu_rx_start(void)
{
int8_t res = uart2_rx_str_P(PSTR("start\n"));
if (res == 1) mmu_last_response = _millis();
return res;
}
//initialize mmu2 unit - first part - should be done at begining of startup process
void mmu_init(void)
{
#ifdef MMU_HWRESET
digitalWrite(MMU_RST_PIN, HIGH);
pinMode(MMU_RST_PIN, OUTPUT); //setup reset pin
#endif //MMU_HWRESET
uart2_init(); //init uart2
_delay_ms(10); //wait 10ms for sure
mmu_reset(); //reset mmu (HW or SW), do not wait for response
mmu_state = S::Init;
PIN_INP(IR_SENSOR_PIN); //input mode
PIN_SET(IR_SENSOR_PIN); //pullup
}
//if IR_SENSOR defined, always returns true
//otherwise check for ir sensor and returns true if idler IR sensor was detected, otherwise returns false
bool check_for_ir_sensor()
{
#ifdef IR_SENSOR
return true;
#else //IR_SENSOR
bool detected = false;
//if IR_SENSOR_PIN input is low and pat9125sensor is not present we detected idler sensor
if ((PIN_GET(IR_SENSOR_PIN) == 0)
#ifdef PAT9125
&& fsensor_not_responding
#endif //PAT9125
)
{
detected = true;
//printf_P(PSTR("Idler IR sensor detected\n"));
}
else
{
//printf_P(PSTR("Idler IR sensor not detected\n"));
}
return detected;
#endif //IR_SENSOR
}
static bool activate_stealth_mode()
{
#ifdef MMU_FORCE_STEALTH_MODE
return true;
#else
return (eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH) == 1);
#endif
}
//mmu main loop - state machine processing
void mmu_loop(void)
{
static uint8_t mmu_attempt_nr = 0;
// printf_P(PSTR("MMU loop, state=%d\n"), mmu_state);
switch (mmu_state)
{
case S::Disabled:
return;
case S::Init:
if (mmu_rx_start() > 0)
{
DEBUG_PUTS_P(PSTR("MMU => 'start'"));
DEBUG_PUTS_P(PSTR("MMU <= 'S1'"));
mmu_puts_P(PSTR("S1\n")); //send 'read version' request
mmu_state = S::GetVersion;
}
else if (_millis() > 30000) //30sec after reset disable mmu
{
puts_P(PSTR("MMU not responding - DISABLED"));
mmu_state = S::Disabled;
}
return;
case S::GetVersion:
if (mmu_rx_ok() > 0)
{
fscanf_P(uart2io, PSTR("%u"), &mmu_version); //scan version from buffer
DEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_version);
DEBUG_PUTS_P(PSTR("MMU <= 'S2'"));
mmu_puts_P(PSTR("S2\n")); //send 'read buildnr' request
mmu_state = S::GetBuildNr;
}
return;
case S::GetBuildNr:
if (mmu_rx_ok() > 0)
{
fscanf_P(uart2io, PSTR("%u"), &mmu_buildnr); //scan buildnr from buffer
DEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_buildnr);
bool version_valid = mmu_check_version();
if (!version_valid) mmu_show_warning();
else puts_P(PSTR("MMU version valid"));
if (!activate_stealth_mode())
{
FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
mmu_state = S::GetFindaInit;
}
else
{
DEBUG_PUTS_P(PSTR("MMU <= 'M1'"));
mmu_puts_P(PSTR("M1\n")); //set mmu mode to stealth
mmu_state = S::WaitStealthMode;
}
}
return;
case S::WaitStealthMode:
if (mmu_rx_ok() > 0)
{
FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
mmu_state = S::GetFindaInit;
}
return;
case S::GetFindaInit:
if (mmu_rx_ok() > 0)
{
fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
mmu_last_finda_response = _millis();
FDEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_finda);
puts_P(PSTR("MMU - ENABLED"));
mmu_enabled = true;
//-//
// ... PrinterType/Name
fSetMmuMode(true);
mmu_state = S::Idle;
}
return;
case S::Idle:
if (mmu_cmd != MmuCmd::None) //command request ?
{
if ((mmu_cmd >= MmuCmd::T0) && (mmu_cmd <= MmuCmd::T4))
{
const uint8_t filament = mmu_cmd - MmuCmd::T0;
DEBUG_PRINTF_P(PSTR("MMU <= 'T%d'\n"), filament);
mmu_printf_P(PSTR("T%d\n"), filament);
mmu_state = S::WaitCmd; // wait for response
mmu_fil_loaded = true;
mmu_idl_sens = 1;
}
else if ((mmu_cmd >= MmuCmd::L0) && (mmu_cmd <= MmuCmd::L4))
{
const uint8_t filament = mmu_cmd - MmuCmd::L0;
DEBUG_PRINTF_P(PSTR("MMU <= 'L%d'\n"), filament);
mmu_printf_P(PSTR("L%d\n"), filament);
mmu_state = S::WaitCmd; // wait for response
}
else if (mmu_cmd == MmuCmd::C0)
{
DEBUG_PRINTF_P(PSTR("MMU <= 'C0'\n"));
mmu_puts_P(PSTR("C0\n")); //send 'continue loading'
mmu_state = S::WaitCmd;
mmu_idl_sens = 1;
}
else if (mmu_cmd == MmuCmd::U0)
{
DEBUG_PRINTF_P(PSTR("MMU <= 'U0'\n"));
mmu_puts_P(PSTR("U0\n")); //send 'unload current filament'
mmu_fil_loaded = false;
mmu_state = S::WaitCmd;
}
else if ((mmu_cmd >= MmuCmd::E0) && (mmu_cmd <= MmuCmd::E4))
{
const uint8_t filament = mmu_cmd - MmuCmd::E0;
DEBUG_PRINTF_P(PSTR("MMU <= 'E%d'\n"), filament);
mmu_printf_P(PSTR("E%d\n"), filament); //send eject filament
mmu_fil_loaded = false;
mmu_state = S::WaitCmd;
}
else if ((mmu_cmd >= MmuCmd::K0) && (mmu_cmd <= MmuCmd::K4))
{
const uint8_t filament = mmu_cmd - MmuCmd::K0;
DEBUG_PRINTF_P(PSTR("MMU <= 'K%d'\n"), filament);
mmu_printf_P(PSTR("K%d\n"), filament); //send eject filament
mmu_fil_loaded = false;
mmu_state = S::WaitCmd;
}
else if (mmu_cmd == MmuCmd::R0)
{
DEBUG_PRINTF_P(PSTR("MMU <= 'R0'\n"));
mmu_puts_P(PSTR("R0\n")); //send recover after eject
mmu_state = S::WaitCmd;
}
else if (mmu_cmd == MmuCmd::S3)
{
DEBUG_PRINTF_P(PSTR("MMU <= 'S3'\n"));
mmu_puts_P(PSTR("S3\n")); //send power failures request
mmu_state = S::GetDrvError;
}
else if (mmu_cmd == MmuCmd::W0)
{
DEBUG_PRINTF_P(PSTR("MMU <= 'W0'\n"));
mmu_puts_P(PSTR("W0\n"));
mmu_state = S::Pause;
}
mmu_last_cmd = mmu_cmd;
mmu_cmd = MmuCmd::None;
}
else if ((eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH) != SilentModeMenu_MMU) && mmu_ready) {
DEBUG_PRINTF_P(PSTR("MMU <= 'M%d'\n"), SilentModeMenu_MMU);
mmu_printf_P(PSTR("M%d\n"), SilentModeMenu_MMU);
mmu_state = S::SwitchMode;
}
else if ((mmu_last_response + 300) < _millis()) //request every 300ms
{
#ifndef IR_SENSOR
if(check_for_ir_sensor()) ir_sensor_detected = true;
#endif //IR_SENSOR not defined
FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
mmu_state = S::GetFinda;
}
return;
case S::GetFinda: //response to command P0
if (mmu_idl_sens)
{
if (PIN_GET(IR_SENSOR_PIN) == 0 && mmu_loading_flag)
{
#ifdef MMU_DEBUG
printf_P(PSTR("MMU <= 'A'\n"));
#endif //MMU_DEBUG
mmu_puts_P(PSTR("A\n")); //send 'abort' request
mmu_idl_sens = 0;
//printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
}
//else
//printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
}
if (mmu_rx_ok() > 0)
{
fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
mmu_last_finda_response = _millis();
FDEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_finda);
//printf_P(PSTR("Eact: %d\n"), int(e_active()));
if (!mmu_finda && CHECK_FSENSOR && fsensor_enabled) {
fsensor_checkpoint_print();
ad_markDepleted(mmu_extruder);
if (lcd_autoDepleteEnabled() && !ad_allDepleted())
{
enquecommand_front_P(PSTR("M600 AUTO")); //save print and run M600 command
}
else
{
enquecommand_front_P(PSTR("M600")); //save print and run M600 command
}
}
mmu_state = S::Idle;
if (mmu_cmd == MmuCmd::None)
mmu_ready = true;
}
else if ((mmu_last_request + MMU_P0_TIMEOUT) < _millis())
{ //resend request after timeout (30s)
mmu_state = S::Idle;
}
return;
case S::WaitCmd: //response to mmu commands
if (mmu_idl_sens)
{
if (PIN_GET(IR_SENSOR_PIN) == 0 && mmu_loading_flag)
{
DEBUG_PRINTF_P(PSTR("MMU <= 'A'\n"));
mmu_puts_P(PSTR("A\n")); //send 'abort' request
mmu_idl_sens = 0;
//printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
}
//else
//printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
}
if (mmu_rx_ok() > 0)
{
DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
mmu_attempt_nr = 0;
mmu_last_cmd = MmuCmd::None;
mmu_ready = true;
mmu_state = S::Idle;
}
else if ((mmu_last_request + MMU_CMD_TIMEOUT) < _millis())
{ //resend request after timeout (5 min)
if (mmu_last_cmd != MmuCmd::None)
{
if (mmu_attempt_nr++ < MMU_MAX_RESEND_ATTEMPTS &&
mmu_last_cmd >= MmuCmd::T0 && mmu_last_cmd <= MmuCmd::T4)
{
DEBUG_PRINTF_P(PSTR("MMU retry attempt nr. %d\n"), mmu_attempt_nr - 1);
mmu_cmd = mmu_last_cmd;
}
else {
mmu_cmd = MmuCmd::None;
mmu_last_cmd = MmuCmd::None; //check
mmu_attempt_nr = 0;
}
}
mmu_state = S::Idle;
}
return;
case S::Pause:
if (mmu_rx_ok() > 0)
{
DEBUG_PRINTF_P(PSTR("MMU => 'ok', resume print\n"));
mmu_attempt_nr = 0;
mmu_last_cmd = MmuCmd::None;
mmu_ready = true;
mmu_state = S::Idle;
lcd_resume_print();
}
if (mmu_cmd != MmuCmd::None)
{
mmu_state = S::Idle;
}
return;
case S::GetDrvError:
if (mmu_rx_ok() > 0)
{
fscanf_P(uart2io, PSTR("%d"), &mmu_power_failures); //scan power failures
DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
mmu_last_cmd = MmuCmd::None;
mmu_ready = true;
mmu_state = S::Idle;
}
else if ((mmu_last_request + MMU_CMD_TIMEOUT) < _millis())
{ //timeout 45 s
mmu_state = S::Idle;
}
return;
case S::SwitchMode:
if (mmu_rx_ok() > 0)
{
DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
eeprom_update_byte((uint8_t*)EEPROM_MMU_STEALTH, SilentModeMenu_MMU);
mmu_state = S::Idle;
}
else if ((mmu_last_request + MMU_CMD_TIMEOUT) < _millis())
{ //timeout 45 s
mmu_state = S::Idle;
}
return;
}
}
void mmu_reset(void)
{
#ifdef MMU_HWRESET //HW - pulse reset pin
digitalWrite(MMU_RST_PIN, LOW);
_delay_us(100);
digitalWrite(MMU_RST_PIN, HIGH);
#else //SW - send X0 command
mmu_puts_P(PSTR("X0\n"));
#endif
}
int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament)
{
printf_P(PSTR("MMU <= 'F%d %d'\n"), extruder, filament);
mmu_printf_P(PSTR("F%d %d\n"), extruder, filament);
unsigned char timeout = MMU_TIMEOUT; //10x100ms
while ((mmu_rx_ok() <= 0) && (--timeout))
delay_keep_alive(MMU_TODELAY);
return timeout?1:0;
}
//! @brief Enqueue MMUv2 command
//!
//! Call manage_response() after enqueuing to process command.
//! If T command is enqueued, it disables current for extruder motor if TMC2130 driver present.
//! If T or L command is enqueued, it marks filament loaded in AutoDeplete module.
void mmu_command(MmuCmd cmd)
{
if ((cmd >= MmuCmd::T0) && (cmd <= MmuCmd::T4))
{
//disable extruder motor
#ifdef TMC2130
tmc2130_set_pwr(E_AXIS, 0);
#endif //TMC2130
//printf_P(PSTR("E-axis disabled\n"));
ad_markLoaded(cmd - MmuCmd::T0);
}
if ((cmd >= MmuCmd::L0) && (cmd <= MmuCmd::L4))
{
ad_markLoaded(cmd - MmuCmd::L0);
}
mmu_cmd = cmd;
mmu_ready = false;
}
//! @brief Rotate extruder idler to catch filament
//! @par synchronize
//! * true blocking call
//! * false non-blocking call
void mmu_load_step(bool synchronize)
{
current_position[E_AXIS] = current_position[E_AXIS] + MMU_LOAD_FEEDRATE * 0.1;
plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE, active_extruder);
if (synchronize) st_synchronize();
}
//! @brief Is nozzle hot enough to move extruder wheels and do we have idler sensor?
//!
//! Do load steps only if temperature is higher then min. temp for safe extrusion and
//! idler sensor present.
//! Otherwise "cold extrusion prevented" would be send to serial line periodically
//! and watchdog reset will be triggered by lack of keep_alive processing.
//!
//! @retval true temperature is high enough to move extruder
//! @retval false temperature is not high enough to move extruder, turned
//! off E-stepper to prevent over-heating and allow filament pull-out if necessary
bool can_extrude()
{
if ((degHotend(active_extruder) < EXTRUDE_MINTEMP) || !ir_sensor_detected)
{
disable_e0();
delay_keep_alive(100);
return false;
}
return true;
}
static void get_response_print_info(uint8_t move) {
printf_P(PSTR("mmu_get_response - begin move: "), move);
switch (move) {
case MMU_LOAD_MOVE: printf_P(PSTR("load\n")); break;
case MMU_UNLOAD_MOVE: printf_P(PSTR("unload\n")); break;
case MMU_TCODE_MOVE: printf_P(PSTR("T-code\n")); break;
case MMU_NO_MOVE: printf_P(PSTR("no move\n")); break;
default: printf_P(PSTR("error: unknown move\n")); break;
}
}
bool mmu_get_response(uint8_t move)
{
get_response_print_info(move);
KEEPALIVE_STATE(IN_PROCESS);
while (mmu_cmd != MmuCmd::None)
{
delay_keep_alive(100);
}
while (!mmu_ready)
{
if ((mmu_state != S::WaitCmd) && (mmu_last_cmd == MmuCmd::None))
break;
switch (move) {
case MMU_LOAD_MOVE:
mmu_loading_flag = true;
if (can_extrude()) mmu_load_step();
//don't rely on "ok" signal from mmu unit; if filament detected by idler sensor during loading stop loading movements to prevent infinite loading
if (PIN_GET(IR_SENSOR_PIN) == 0) move = MMU_NO_MOVE;
break;
case MMU_UNLOAD_MOVE:
if (PIN_GET(IR_SENSOR_PIN) == 0) //filament is still detected by idler sensor, printer helps with unlading
{
if (can_extrude())
{
printf_P(PSTR("Unload 1\n"));
current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE, active_extruder);
st_synchronize();
}
}
else //filament was unloaded from idler, no additional movements needed
{
printf_P(PSTR("Unloading finished 1\n"));
disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
move = MMU_NO_MOVE;
}
break;
case MMU_TCODE_MOVE: //first do unload and then continue with infinite loading movements
if (PIN_GET(IR_SENSOR_PIN) == 0) //filament detected by idler sensor, we must unload first
{
if (can_extrude())
{
printf_P(PSTR("Unload 2\n"));
current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE, active_extruder);
st_synchronize();
}
}
else //delay to allow mmu unit to pull out filament from bondtech gears and then start with infinite loading
{
printf_P(PSTR("Unloading finished 2\n"));
disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
delay_keep_alive(MMU_LOAD_TIME_MS);
move = MMU_LOAD_MOVE;
get_response_print_info(move);
}
break;
case MMU_NO_MOVE:
default:
delay_keep_alive(100);
break;
}
}
printf_P(PSTR("mmu_get_response() returning: %d\n"), mmu_ready);
bool ret = mmu_ready;
mmu_ready = false;
// printf_P(PSTR("mmu_get_response - end %d\n"), ret?1:0);
return ret;
}
//! @brief Wait for active extruder to reach temperature set
//!
//! This function is blocking and showing lcd_wait_for_heater() screen
//! which is constantly updated with nozzle temperature.
void mmu_wait_for_heater_blocking()
{
while ((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)
{
delay_keep_alive(1000);
lcd_wait_for_heater();
}
}
void manage_response(bool move_axes, bool turn_off_nozzle, uint8_t move)
{
bool response = false;
mmu_print_saved = false;
bool lcd_update_was_enabled = false;
float hotend_temp_bckp = degTargetHotend(active_extruder);
float z_position_bckp = current_position[Z_AXIS];
float x_position_bckp = current_position[X_AXIS];
float y_position_bckp = current_position[Y_AXIS];
uint8_t screen = 0; //used for showing multiscreen messages
mmu_loading_flag = false;
while(!response)
{
response = mmu_get_response(move); //wait for "ok" from mmu
if (!response) { //no "ok" was received in reserved time frame, user will fix the issue on mmu unit
if (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater
uint8_t mmu_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL);
uint16_t mmu_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT);
if(mmu_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_FAIL, mmu_fail + 1);
if(mmu_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_FAIL_TOT, mmu_fail_tot + 1);
if (lcd_update_enabled) {
lcd_update_was_enabled = true;
lcd_update_enable(false);
}
st_synchronize();
mmu_print_saved = true;
printf_P(PSTR("MMU not responding\n"));
KEEPALIVE_STATE(PAUSED_FOR_USER);
hotend_temp_bckp = degTargetHotend(active_extruder);
if (move_axes) {
z_position_bckp = current_position[Z_AXIS];
x_position_bckp = current_position[X_AXIS];
y_position_bckp = current_position[Y_AXIS];
//lift z
current_position[Z_AXIS] += Z_PAUSE_LIFT;
if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
plan_buffer_line_curposXYZE(15, active_extruder);
st_synchronize();
//Move XY to side
current_position[X_AXIS] = X_PAUSE_POS;
current_position[Y_AXIS] = Y_PAUSE_POS;
plan_buffer_line_curposXYZE(50, active_extruder);
st_synchronize();
}
if (turn_off_nozzle) {
//set nozzle target temperature to 0
setAllTargetHotends(0);
}
disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
}
//first three lines are used for printing multiscreen message; last line contains measured and target nozzle temperature
if (screen == 0) { //screen 0
lcd_display_message_fullscreen_P(_i("MMU needs user attention."));
screen++;
}
else { //screen 1
if((degTargetHotend(active_extruder) == 0) && turn_off_nozzle) lcd_display_message_fullscreen_P(_i("Press the knob to resume nozzle temperature."));
else lcd_display_message_fullscreen_P(_i("Fix the issue and then press button on MMU unit."));
screen=0;
}
lcd_set_degree();
//5 seconds delay
for (uint8_t i = 0; i < 5; i++) {
if (lcd_clicked()) {
setTargetHotend(hotend_temp_bckp, active_extruder);
/// mmu_cmd = mmu_last_cmd;
break;
}
//Print the hotend temperature (9 chars total) and fill rest of the line with space
lcd_set_cursor(0, 4); //line 4
int chars = lcd_printf_P(_N("%c%3d/%d%c"), LCD_STR_THERMOMETER[0],(int)(degHotend(active_extruder) + 0.5), (int)(degTargetHotend(active_extruder) + 0.5), LCD_STR_DEGREE[0]);
lcd_space(9 - chars);
delay_keep_alive(1000);
}
}
else if (mmu_print_saved) {
printf_P(PSTR("MMU starts responding\n"));
KEEPALIVE_STATE(IN_HANDLER);
mmu_loading_flag = false;
if (turn_off_nozzle)
{
lcd_clear();
setTargetHotend(hotend_temp_bckp, active_extruder);
if (((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)) {
lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature..."));
delay_keep_alive(3000);
}
mmu_wait_for_heater_blocking();
}
if (move_axes) {
lcd_clear();
lcd_display_message_fullscreen_P(_i("MMU OK. Resuming position..."));
current_position[X_AXIS] = x_position_bckp;
current_position[Y_AXIS] = y_position_bckp;
plan_buffer_line_curposXYZE(50, active_extruder);
st_synchronize();
current_position[Z_AXIS] = z_position_bckp;
plan_buffer_line_curposXYZE(15, active_extruder);
st_synchronize();
}
else {
lcd_clear();
lcd_display_message_fullscreen_P(_i("MMU OK. Resuming..."));
delay_keep_alive(1000); //delay just for showing MMU OK message for a while in case that there are no xyz movements
}
}
}
if (lcd_update_was_enabled) lcd_update_enable(true);
#ifdef TMC2130
//enable extruder motor (disabled in mmu_command, start of T-code processing)
tmc2130_set_pwr(E_AXIS, 1);
//printf_P(PSTR("E-axis enabled\n"));
#endif //TMC2130
}
//! @brief load filament to nozzle of multimaterial printer
//!
//! This function is used only only after T? (user select filament) and M600 (change filament).
//! It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
//! filament to nozzle.
//!
void mmu_load_to_nozzle()
{
st_synchronize();
bool saved_e_relative_mode = axis_relative_modes[E_AXIS];
if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = true;
if (ir_sensor_detected)
{
current_position[E_AXIS] += 3.0f;
}
else
{
current_position[E_AXIS] += 7.2f;
}
float feedrate = 562;
plan_buffer_line_curposXYZE(feedrate / 60, active_extruder);
st_synchronize();
current_position[E_AXIS] += 14.4f;
feedrate = 871;
plan_buffer_line_curposXYZE(feedrate / 60, active_extruder);
st_synchronize();
current_position[E_AXIS] += 36.0f;
feedrate = 1393;
plan_buffer_line_curposXYZE(feedrate / 60, active_extruder);
st_synchronize();
current_position[E_AXIS] += 14.4f;
feedrate = 871;
plan_buffer_line_curposXYZE(feedrate / 60, active_extruder);
st_synchronize();
if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = false;
}
void mmu_M600_wait_and_beep() {
//Beep and wait for user to remove old filament and prepare new filament for load
KEEPALIVE_STATE(PAUSED_FOR_USER);
int counterBeep = 0;
lcd_display_message_fullscreen_P(_i("Remove old filament and press the knob to start loading new filament."));
bool bFirst=true;
while (!lcd_clicked()){
manage_heater();
manage_inactivity(true);
#if BEEPER > 0
if (counterBeep == 500) {
counterBeep = 0;
}
SET_OUTPUT(BEEPER);
if (counterBeep == 0) {
if((eSoundMode==e_SOUND_MODE_BLIND)|| (eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst))
{
bFirst=false;
WRITE(BEEPER, HIGH);
}
}
if (counterBeep == 20) {
WRITE(BEEPER, LOW);
}
counterBeep++;
#endif //BEEPER > 0
delay_keep_alive(4);
}
WRITE(BEEPER, LOW);
}
//! @brief load filament for mmu v2
//! @par nozzle_temp nozzle temperature to load filament
void mmu_M600_load_filament(bool automatic, float nozzle_temp)
{
tmp_extruder = mmu_extruder;
if (!automatic)
{
#ifdef MMU_M600_SWITCH_EXTRUDER
bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
if(yes) tmp_extruder = choose_extruder_menu();
#endif //MMU_M600_SWITCH_EXTRUDER
}
else
{
tmp_extruder = ad_getAlternative(tmp_extruder);
}
lcd_update_enable(false);
lcd_clear();
lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
lcd_print(" ");
lcd_print(tmp_extruder + 1);
snmm_filaments_used |= (1 << tmp_extruder); //for stop print
//printf_P(PSTR("T code: %d \n"), tmp_extruder);
//mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
setTargetHotend(nozzle_temp,active_extruder);
mmu_wait_for_heater_blocking();
mmu_command(MmuCmd::T0 + tmp_extruder);
manage_response(false, true, MMU_LOAD_MOVE);
mmu_continue_loading(is_usb_printing || (lcd_commands_type == LcdCommands::Layer1Cal));
mmu_extruder = tmp_extruder; //filament change is finished
mmu_load_to_nozzle();
load_filament_final_feed();
st_synchronize();
}
#ifdef SNMM
void extr_mov(float shift, float feed_rate)
{ //move extruder no matter what the current heater temperature is
set_extrude_min_temp(.0);
current_position[E_AXIS] += shift;
plan_buffer_line_curposXYZE(feed_rate, active_extruder);
set_extrude_min_temp(EXTRUDE_MINTEMP);
}
#endif //SNMM
void change_extr(int
#ifdef SNMM
extr
#endif //SNMM
) { //switches multiplexer for extruders
#ifdef SNMM
st_synchronize();
_delay(100);
disable_e0();
disable_e1();
disable_e2();
mmu_extruder = extr;
pinMode(E_MUX0_PIN, OUTPUT);
pinMode(E_MUX1_PIN, OUTPUT);
switch (extr) {
case 1:
WRITE(E_MUX0_PIN, HIGH);
WRITE(E_MUX1_PIN, LOW);
break;
case 2:
WRITE(E_MUX0_PIN, LOW);
WRITE(E_MUX1_PIN, HIGH);
break;
case 3:
WRITE(E_MUX0_PIN, HIGH);
WRITE(E_MUX1_PIN, HIGH);
break;
default:
WRITE(E_MUX0_PIN, LOW);
WRITE(E_MUX1_PIN, LOW);
break;
}
_delay(100);
#endif
}
int get_ext_nr()
{ //reads multiplexer input pins and return current extruder number (counted from 0)
#ifndef SNMM
return(mmu_extruder); //update needed
#else
return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
#endif
}
void display_loading()
{
switch (mmu_extruder)
{
case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
}
}
void extr_adj(uint8_t extruder) //loading filament for SNMM
{
#ifndef SNMM
MmuCmd cmd = MmuCmd::L0 + extruder;
if (extruder > (MmuCmd::L4 - MmuCmd::L0))
{
printf_P(PSTR("Filament out of range %d \n"),extruder);
return;
}
mmu_command(cmd);
//show which filament is currently loaded
lcd_update_enable(false);
lcd_clear();
lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
//if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
//else lcd.print(" ");
lcd_print(" ");
lcd_print(extruder + 1);
// get response