Repository navigation
Expand file tree
/
Copy pathvrtx_tools_core.cfg
More file actions
1434 lines (1372 loc) · 78.6 KB
/
Copy pathvrtx_tools_core.cfg
File metadata and controls
1434 lines (1372 loc) · 78.6 KB
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
# vrtx_tools_core.cfg
#
# Tool commands T0 ... T9 for tool changers that use
# klipper-toolchanger, made for printers that swap only the hotend: one extruder,
# and every tool has its own heater, thermistor and fan.
#
# T0 ... T9 mount a tool (what the slicer's tool change line calls)
# VRTX_SET_TEMP set the temperature of a tool, with or without waiting
# VRTX_TOOL_STANDBY put a tool on its standby temperature
# VRTX_TOOLS_OFF switch all tool heaters off
# VRTX_TOOLS_STATUS one line per tool in the console
# VRTX_SET_FILAMENT the filament loaded in a tool: material, colour, name, temperature
# VRTX_TOOL_LOG the last tool changes with their time and result
# VRTX_RESUME after a failed tool change during a print: you fixed the tool by
# hand, this brings the changer back and resumes the print
# VRTX_RETRACT / VRTX_PRIME shared extruder: retract the filament of a tool before it is
# put away, push it back when it is mounted again
#
# The [toolchanger] hooks (section 4 of VRTX/vrtx.cfg):
# before_change_gcode: VRTX_BEFORE_CHANGE retract (shared extruder)
# after_change_gcode: VRTX_AFTER_CHANGE motor, pressure advance, prime, spool
# error_gcode: VRTX_TOOLCHANGE_ERROR pause the print, tell you
#
# Optional, in vrtx_tool_heaters.cfg: M104 / M109 with a T parameter, so that
# slicer lines such as "M104 T1 S220" reach the heater of tool 1.
#
# VRTX/vrtx.cfg loads it BEFORE the [tool Tn] sections (its section 5);
# otherwise klipper-toolchanger registers its own T0 ... T9 first and Klipper
# refuses the macros of the same name. VRTX_CHECK tells you if that happened.
# Change its settings in section 2 of VRTX/vrtx.cfg.
#
# Klipper strips everything after a hash sign on every line, so comments in
# this file are always whole lines.
[gcode_macro _VRTX_TOOLS_SETTINGS]
description: Settings of the VRTX tool commands. Not a command.
# What happens to the heater of a tool that is put away by a tool change.
# off: nothing. The slicer or your own macros look after the temperatures.
# standby: the old tool goes to standby_temp and returns to its previous
# temperature when it is mounted again.
variable_standby_mode: 'off'
# Standby temperature in C, 0 switches the heater off. Also used by
# VRTX_TOOL_STANDBY and the page.
variable_standby_temp: 0.0
# standby mode only: wait for the temperature after a tool is mounted again
variable_wait_on_select: True
# Temperature buttons on the page, "name:temperature" separated by commas.
# A tool can have its own list: add variable_presets_t3: 'TPU:225' (for T3)
# to your own [gcode_macro _VRTX_TOOLS_SETTINGS] section. '' = no buttons.
variable_presets: 'PLA:215, PETG:240, ABS:255'
# Copy the filament of every tool into Moonraker's lane_data, so OrcaSlicer's
# "Synchronize filaments" picks up the colour and material of every tool.
# Done by the VRTX page service (vrtx); False switches it off.
variable_slicer_sync: True
# Names of the tool switches: the [gcode_button ...] sections in your own
# board file or in section 6. VRTX only reads them, so they can have any name.
# head_switch: the carriage switch, pressed while a tool is attached
# dock_switches: one per tool in tool order T0, T1, T2 ... separated by
# commas. A missing or empty entry means vrtx_dock_t<n>.
# A switch that does not exist is skipped.
variable_head_switch: 'vrtx_head'
variable_dock_switches: ''
# Shared extruder: a hotend swapper with one motor and every hotend with its own
# filament loaded. Needs the hooks in section 4 of VRTX/vrtx.cfg.
# retract: mm of filament pulled back on the tool that is put away, 0 = off.
# It only happens during a print, and only when the nozzle is hot enough.
# The same length is pushed back when the tool is mounted again.
# Set the slicer's tool change retraction to 0 when you use this.
# prime_wait: wait for the nozzle to reach its temperature before the prime
# motor: the shared motor, the name of its [extruder_stepper] (or of the
# [extruder] whose motor it is). With it VRTX hands the motor to the
# mounted tool and applies the pressure advance (params_pressure_advance,
# params_pressure_advance_smooth_time) and params_rotation_distance
# of the [tool Tn] section to it. '' = nothing.
variable_retract: 0.0
variable_retract_speed: 40.0
variable_unretract_speed: 25.0
variable_prime_wait: True
# prime_macro: a macro of yours that does the prime where it belongs (over a brush, in a
# purge bucket). VRTX calls it <prime_macro> T=<tool> LENGTH=<mm> SPEED=<mm/s>
# after every change that has something to push back, after the tool is hot, and
# the macro must move there, push LENGTH mm of filament, wipe and come back
# (klipper-toolchanger also moves to the print position at the end of the change).
# '' = VRTX pushes the retract back where the nozzle is.
# first_prime: extra mm of filament pushed the first time a tool is used in a print, on top
# of the retract (a nozzle that has been standing empty). Only with prime_macro.
# first_prime_speed: mm/s of filament for the prime, 0 = unretract_speed
variable_prime_macro: ''
variable_first_prime: 0.0
variable_first_prime_speed: 0.0
variable_motor: ''
# Loading and unloading filament (VRTX_LOAD_FILAMENT, VRTX_UNLOAD_FILAMENT), for a hotend
# swapper where you push the filament into the hotend by hand and the motor does the rest:
# load_temp: nozzle temperature when the tool has no filament temperature (VRTX_SET_FILAMENT TEMP=)
# load_length: mm the motor pushes after the filament was pushed in by hand (to the nozzle)
# load_speed: mm/s of that push
# purge_length: mm pushed on to flush the old colour or fill the nozzle, purge_speed mm/s
# unload_push: mm pushed first when unloading, to soften the tip, at purge_speed
# unload_length: mm pulled back, so that the end of the filament is out of the hot zone and can
# be pulled out by hand; unload_speed mm/s. Moves are split in steps of at most
# 40 mm (Klipper's max_extrude_only_distance is 50).
variable_load_temp: 210.0
variable_load_length: 40.0
variable_load_speed: 5.0
variable_purge_length: 50.0
variable_purge_speed: 3.0
variable_unload_push: 10.0
variable_unload_length: 60.0
variable_unload_speed: 10.0
# changer: 'full' (every tool a full toolhead) or 'hotend' (a hotend swapper). The installer
# writes it into vrtx.cfg; VRTX_CHECK uses it to find a missing shared motor.
variable_changer: 'full'
# wait_below / wait_above: what M109 (vrtx_tool_heaters.cfg) waits for, in degrees around
# the target, instead of Klipper's slower "within 1 degree and flat" wait.
# wait_below 0 = Klipper's normal wait. wait_above 0 = no upper limit.
variable_wait_below: 0.0
variable_wait_above: 0.0
# A failed tool change (a tool missing, not seated, or not where the switches say)
# during a print pauses the print instead of stopping it. Needs the error_gcode
# hook in section 4. False = it stops the print.
variable_pause_on_error: True
# Name of a macro of yours that gets MSG=<text> when a print is paused by a failure,
# for example to send a message to your phone. '' = none.
variable_error_notify: ''
# Show a prompt with a Recover button (Mainsail and Fluidd show it)
variable_error_prompt: True
# At startup, find the tool on the carriage from the switches and tell klipper-toolchanger.
# Needs the carriage switch and a dock switch for every tool.
variable_auto_init: True
# A tool changed by hand: taken off the carriage, put on it, or swapped, outside a tool
# change. Needs the carriage switch and a dock switch for every tool (like auto_init). VRTX
# reads the switches every second; when they have not changed for manual_sync_delay seconds
# and say something else than the changer knows:
# silent: not printing, the changer follows the switches by itself (it is told the tool
# that is on the carriage, with its offsets, motor and spool).
# next: not printing, nothing happens until the next T command, which first follows the
# switches and then does the change it was asked for.
# off: nothing is watched, and the T commands do not check.
# In both silent and next a T command checks the switches first. During a print (and while it
# is paused) a hand change is never followed silently: the print is paused like after a
# failed tool change, and VRTX_RESUME (the Recover button) mounts the tool the print needs.
# Switches that do not add up (two empty docks, a tool on the carriage with every dock full)
# are reported, and a T command is refused until they do.
variable_manual_sync: 'silent'
variable_manual_sync_delay: 5.0
# Tell Spoolman which spool is in use when a tool is mounted, for tools with a
# spool id (VRTX_SET_FILAMENT SPOOL=). Needs SET_ACTIVE_SPOOL (Moonraker's Spoolman macros).
variable_spoolman: True
# Keep the spool_id variable of the T macros (Mainsail's Spoolman panel: one spool per
# tool, saved as t<n>__spool_id) in step with the filament of each tool: a spool set in
# Mainsail becomes the tool's spool here, VRTX_SET_FILAMENT and the page set it there.
# Checked every 2 s. False switches it off.
variable_mainsail_spools: True
gcode:
[gcode_macro _VRTX_TOOLS_STATE]
description: Internal state of the VRTX tool commands. Not a command.
# Temperature a tool was at before it went to standby. 0 = nothing to restore.
variable_t0_wanted: 0.0
variable_t1_wanted: 0.0
variable_t2_wanted: 0.0
variable_t3_wanted: 0.0
variable_t4_wanted: 0.0
variable_t5_wanted: 0.0
variable_t6_wanted: 0.0
variable_t7_wanted: 0.0
variable_t8_wanted: 0.0
variable_t9_wanted: 0.0
# Filament pulled back (mm) from a tool at its last tool change, to be pushed back by
# the next VRTX_PRIME. Also saved in [save_variables] (t<N>_retracted) and read back at startup.
variable_t0_retracted: 0.0
variable_t1_retracted: 0.0
variable_t2_retracted: 0.0
variable_t3_retracted: 0.0
variable_t4_retracted: 0.0
variable_t5_retracted: 0.0
variable_t6_retracted: 0.0
variable_t7_retracted: 0.0
variable_t8_retracted: 0.0
variable_t9_retracted: 0.0
# Tools that got their first prime in this print (see first_prime), and the print duration
# when that was last updated: a smaller print duration means a new print, the list starts again.
variable_primed: []
variable_primed_dur: 0.0
gcode:
# ---------------------------------------------------------------------------
# The tool commands
# ---------------------------------------------------------------------------
[gcode_macro _VRTX_T]
description: Mount tool T (internal, called by T0 ... T9)
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set tc = printer.toolchanger %}
{% set dock = printer["gcode_macro _VRTX_DOCK_STATE"] if printer["gcode_macro _VRTX_DOCK_STATE"] is defined else {} %}
# a tool changed by hand: the switches are read first (manual_sync), see _VRTX_SYNC_CHECK
{% if st.manual_sync|default('silent')|string|lower in ['silent', 'next'] and printer["gcode_macro _VRTX_DOCK_STATUS"] is defined and tc.status == 'ready' and dock.failed|default(0)|int == 0 and params.T|int in tc.tool_numbers %}
_VRTX_DOCK_STATUS
_VRTX_SYNC_CHECK T={params.T|int} SOURCE=t
_VRTX_T_SYNCED {rawparams}
{% else %}
_VRTX_T_RUN {rawparams}
{% endif %}
[gcode_macro _VRTX_T_SYNCED]
description: T command after the switch check (internal)
gcode:
{% if printer["gcode_macro _VRTX_SYNC_STATE"].go|int == 1 %}
_VRTX_T_RUN {rawparams}
{% endif %}
[gcode_macro _VRTX_T_RUN]
description: Mount tool T, the change itself (internal)
gcode:
{% set n = params.T|int %}
{% set tc = printer.toolchanger %}
{% if n not in tc.tool_numbers %}
_VRTX_ECHO TYPE=warning MSG="T{n} ignored, tool {n} is not configured."
{% elif printer["gcode_macro _VRTX_DOCK_STATE"] is defined and printer["gcode_macro _VRTX_DOCK_STATE"].failed|int == 1 %}
{ action_raise_error("VRTX: the last tool change failed and the changer was not repaired yet: fix the tool by hand and run VRTX_RESUME.") }
{% else %}
{% set cur = tc.tool_number|int %}
# klipper-toolchanger forgets the mounted tool after ANY G-code error, and SELECT_TOOL
# raises one when an axis it uses is not homed (uses_axis, unless on_axis_not_homed is
# 'home'). So that case is caught here and only warned about: the tool stays known.
{% set tcc = printer.configfile.settings['toolchanger'] if printer.configfile.settings['toolchanger'] is defined else {} %}
{% set autohome = tcc.on_axis_not_homed|default('abort')|string|lower == 'home' %}
{% set unhomed = tcc.uses_axis|default('xyz')|string|lower|list|reject('in', printer.toolhead.homed_axes)|list %}
{% if cur != n and unhomed and not autohome %}
_VRTX_ECHO TYPE=warning MSG="{'T%d ignored: the printer is not homed (%s). Home it first (G28), the tool on the carriage stays T%d.' % (n, unhomed|join|upper, cur) if cur >= 0 else 'T%d ignored: the printer is not homed (%s). Home it first (G28).' % (n, unhomed|join|upper)}"
{% elif cur != n %}
M400
_VRTX_T_BEGIN T={n}
_VRTX_LOG_BEGIN T={n} FROM={cur}
{% if cur >= 0 %}
_VRTX_TOOL_LEAVE T={cur}
{% endif %}
{% if params.RESTORE_AXIS is defined %}
SELECT_TOOL T={n} RESTORE_AXIS={params.RESTORE_AXIS}
{% else %}
SELECT_TOOL T={n}
{% endif %}
_VRTX_T_DONE T={n}
{% endif %}
{% endif %}
# A check that fails during a print does not raise an error (that would end the
# print), see _VRTX_DOCK_FAIL: the change finishes without docking anything
# more and comes back here, where the print is paused. What follows must only
# happen when the change really worked.
[gcode_macro _VRTX_T_BEGIN]
description: A T command starts (internal). T=
gcode:
{% if printer["gcode_macro _VRTX_DOCK_STATE"] is defined %}
SET_GCODE_VARIABLE MACRO=_VRTX_DOCK_STATE VARIABLE=in_t VALUE=1
SET_GCODE_VARIABLE MACRO=_VRTX_DOCK_STATE VARIABLE=change_to VALUE={params.T|int}
SET_GCODE_VARIABLE MACRO=_VRTX_DOCK_STATE VARIABLE=moved VALUE=0
SET_GCODE_VARIABLE MACRO=_VRTX_DOCK_STATE VARIABLE=restore_pending VALUE=0
{% endif %}
[gcode_macro _VRTX_T_DONE]
description: The rest of a tool change, only if it worked (internal)
gcode:
{% set n = params.T|int %}
{% set dock = printer["gcode_macro _VRTX_DOCK_STATE"] if printer["gcode_macro _VRTX_DOCK_STATE"] is defined else {} %}
{% if dock.in_t is defined %}
SET_GCODE_VARIABLE MACRO=_VRTX_DOCK_STATE VARIABLE=in_t VALUE=0
{% endif %}
{% if dock.failed|default(0)|int == 1 %}
VRTX_TOOLCHANGE_ERROR
{% elif printer.toolchanger.status == 'ready' and printer.toolchanger.tool_number|int == n %}
_VRTX_APPLY_GCODE_OFFSET
_VRTX_TOOL_ENTER T={n}
M400
_VRTX_LOG_END T={n}
VRTX_USER_AFTER_T T={n}
{% else %}
_VRTX_ECHO TYPE=warning MSG="{'The change to T%d did not finish (the changer is %s). Fix the tool, then run VRTX_RESUME.' % (n, printer.toolchanger.status)}"
{% endif %}
# Your own steps after a tool change that worked: redefine this macro in your config, after
# the VRTX include. It runs once the changer is ready again (so the G-code offsets are the ones
# of the new tool), at the end of a T command, never after a failed change. T= is the new tool.
# Not for the work of the change itself, that belongs to the [toolchanger] hooks.
[gcode_macro VRTX_USER_AFTER_T]
description: VRTX: your own steps after a tool change that worked (empty by default). T=
gcode:
{% set unused = params.T|default(-1)|int %}
[gcode_macro _VRTX_TOOL_LEAVE]
description: Handle the heater of the tool that is put away (internal)
gcode:
{% set n = params.T|int %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set hn = printer[printer.toolchanger.tool_names[printer.toolchanger.tool_numbers.index(n)]].heater %}
{% if st.standby_mode|string|lower == 'standby' and hn and printer[hn] is defined %}
{% set target = printer[hn].target|float %}
{% if target > st.standby_temp|float %}
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=t{n}_wanted VALUE={target}
SET_TOOL_TEMPERATURE T={n} TARGET={st.standby_temp|float}
{% endif %}
{% endif %}
[gcode_macro _VRTX_TOOL_FLAGS]
description: Set the active variable of the T macros (internal). T=-1 for no tool
gcode:
{% set n = params.T|int %}
{% for i in range(10) %}
{% if printer["gcode_macro T" ~ i] is defined and printer["gcode_macro T" ~ i].active is defined %}
SET_GCODE_VARIABLE MACRO=T{i} VARIABLE=active VALUE={'True' if i == n else 'False'}
{% endif %}
{% endfor %}
[gcode_macro _VRTX_SPOOL]
description: Tell Spoolman which spool the mounted tool has (internal). T=
gcode:
{% set n = params.T|int %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% if n >= 0 and st.spoolman|default(True) and printer.save_variables is defined and printer.gcode.commands['SET_ACTIVE_SPOOL'] is defined %}
{% set fil = printer.save_variables.variables['t%d_filament' % n]|default({}, true) %}
{% if fil.spool_id|default(0)|int > 0 %}
SET_ACTIVE_SPOOL ID={fil.spool_id|int}
{% endif %}
{% endif %}
# ---------------------------------------------------------------------------
# Spoolman per tool in Mainsail. Mainsail shows a spool per tool for every T macro
# that has a spool_id variable; it sets it with SET_GCODE_VARIABLE and saves it as
# t<n>__spool_id. VRTX keeps the spool in t<n>_filament. These keep the two in step:
# at startup (_VRTX_SPOOL_LOAD), when VRTX changes it (_VRTX_SPOOL_PUBLISH) and,
# every 2 s, when Mainsail changed it (_VRTX_SPOOL_WATCH). mainsail_spools: False = off.
# ---------------------------------------------------------------------------
[gcode_macro _VRTX_SPOOL_PUBLISH]
description: Show the spool of tool T on its T macro and save it for Mainsail (internal). T= ID= (0 = none)
gcode:
{% set n = params.T|int %}
{% set sid = params.ID|default(0)|int %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set tm = printer["gcode_macro T" ~ n] %}
{% if st.mainsail_spools|default(True) and tm is defined and tm.spool_id is defined %}
SET_GCODE_VARIABLE MACRO=T{n} VARIABLE=spool_id VALUE={sid if sid > 0 else 'None'}
{% if printer.save_variables is defined %}
SAVE_VARIABLE VARIABLE=t{n}__spool_id VALUE={sid if sid > 0 else 'None'}
{% endif %}
{% endif %}
[gcode_macro _VRTX_SPOOL_ADOPT]
description: Make spool ID the spool of tool T, keeping the rest of its filament (internal). T= ID= (0 = none)
gcode:
{% set n = params.T|int %}
{% set sid = params.ID|default(0)|int %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set old = printer.save_variables.variables['t%d_filament' % n]|default({}, true) %}
{% set fil = {'material': old.material|default(''), 'name': old.name|default(''), 'color': old.color|default(''), 'temp': old.temp|default(0)|float} %}
{% if sid > 0 %}
{% set fil = {'material': old.material|default(''), 'name': old.name|default(''), 'color': old.color|default(''), 'temp': old.temp|default(0)|float, 'spool_id': sid} %}
{% endif %}
SAVE_VARIABLE VARIABLE=t{n}_filament VALUE="{fil}"
_VRTX_ECHO MSG="{'T%d: Spoolman spool %s (set in the Spoolman panel).' % (n, sid if sid > 0 else 'none')}"
{% if printer.toolchanger.tool_number|int == n and st.spoolman|default(True) %}
{% if sid > 0 and printer.gcode.commands['SET_ACTIVE_SPOOL'] is defined %}
SET_ACTIVE_SPOOL ID={sid}
{% elif sid == 0 and printer.gcode.commands['CLEAR_ACTIVE_SPOOL'] is defined %}
CLEAR_ACTIVE_SPOOL
{% endif %}
{% endif %}
[gcode_macro _VRTX_SPOOL_LOAD]
description: At startup, put the spool of every tool on its T macro (internal)
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% if st.mainsail_spools|default(True) and printer.save_variables is defined %}
{% set svv = printer.save_variables.variables %}
{% for n in printer.toolchanger.tool_numbers %}
{% set tm = printer["gcode_macro T" ~ n] %}
{% if tm is defined and tm.spool_id is defined %}
{% set f = svv['t%d_filament' % n]|default({}, true) %}
{% set fs = f.spool_id|default(0)|int %}
{% set ms = svv['t%d__spool_id' % n]|default(0, true)|int %}
{% if fs == 0 and ms > 0 %}
# a spool that was only set in Mainsail (before VRTX, or with the sync off)
_VRTX_SPOOL_ADOPT T={n} ID={ms}
SET_GCODE_VARIABLE MACRO=T{n} VARIABLE=spool_id VALUE={ms}
{% elif fs != ms %}
_VRTX_SPOOL_PUBLISH T={n} ID={fs}
{% else %}
SET_GCODE_VARIABLE MACRO=T{n} VARIABLE=spool_id VALUE={fs if fs > 0 else 'None'}
{% endif %}
{% endif %}
{% endfor %}
UPDATE_DELAYED_GCODE ID=_VRTX_SPOOL_WATCH DURATION=2
{% endif %}
# A spool set in Mainsail's Spoolman panel: the T macro has a spool that the tool's
# filament has not. Only acts on a difference, so it costs nothing otherwise.
[delayed_gcode _VRTX_SPOOL_WATCH]
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% if st.mainsail_spools|default(True) and printer.save_variables is defined and printer.toolchanger is defined %}
{% set svv = printer.save_variables.variables %}
{% for n in printer.toolchanger.tool_numbers %}
{% set tm = printer["gcode_macro T" ~ n] %}
{% if tm is defined and tm.spool_id is defined %}
{% set ms = tm.spool_id|default(0, true)|int(0) %}
{% set fs = (svv['t%d_filament' % n]|default({}, true)).spool_id|default(0)|int %}
{% if ms != fs %}
_VRTX_SPOOL_ADOPT T={n} ID={ms}
{% endif %}
{% endif %}
{% endfor %}
UPDATE_DELAYED_GCODE ID=_VRTX_SPOOL_WATCH DURATION=2
{% endif %}
[gcode_macro _VRTX_TOOL_ENTER]
description: Bring the heater of the newly mounted tool back and update the state (internal)
gcode:
{% set n = params.T|int %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set tstate = printer["gcode_macro _VRTX_TOOLS_STATE"] %}
{% set hn = printer[printer.toolchanger.tool_names[printer.toolchanger.tool_numbers.index(n)]].heater %}
_VRTX_TOOL_FLAGS T={n}
_VRTX_SPOOL T={n}
{% if st.standby_mode|string|lower == 'standby' and hn and printer[hn] is defined %}
{% set wanted = tstate['t' ~ n ~ '_wanted']|default(0)|float %}
{% if wanted > 0 and printer[hn].target|float == st.standby_temp|float %}
SET_TOOL_TEMPERATURE T={n} TARGET={wanted} WAIT={1 if st.wait_on_select else 0}
{% endif %}
{% endif %}
# T0 ... T9 are in t_commands/T0.cfg ... T9.cfg, one file per command, so that a
# printer can load only the ones of its tools (Mainsail shows a tool button for
# every T command). core/vrtx_tools.cfg loads this file and all ten.
# ---------------------------------------------------------------------------
# Temperatures
# ---------------------------------------------------------------------------
[gcode_macro VRTX_SET_TEMP]
description: Set the temperature of a tool. T= (default: the mounted tool) TARGET= WAIT=0|1
gcode:
{% set tc = printer.toolchanger %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set n = params.T|default(tc.tool_number)|int %}
{% set target = params.TARGET|default(0)|float %}
{% if n not in tc.tool_numbers %}
_VRTX_ECHO TYPE=warning MSG="Temperature ignored, no tool {n}."
{% else %}
{% if target > st.standby_temp|float %}
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=t{n}_wanted VALUE={target}
{% else %}
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=t{n}_wanted VALUE=0.0
{% endif %}
SET_TOOL_TEMPERATURE T={n} TARGET={target} WAIT={params.WAIT|default(0)|int}
{% endif %}
[gcode_macro VRTX_TOOL_STANDBY]
description: Put a tool on the standby temperature. T= (default: the mounted tool)
gcode:
{% set tc = printer.toolchanger %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set n = params.T|default(tc.tool_number)|int %}
{% if n not in tc.tool_numbers %}
_VRTX_ECHO TYPE=warning MSG="Standby ignored, no tool {n}."
{% else %}
{% set hn = printer[tc.tool_names[tc.tool_numbers.index(n)]].heater %}
{% set target = printer[hn].target|float if hn and printer[hn] is defined else 0.0 %}
{% if target > st.standby_temp|float %}
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=t{n}_wanted VALUE={target}
{% endif %}
SET_TOOL_TEMPERATURE T={n} TARGET={st.standby_temp|float}
{% endif %}
[gcode_macro VRTX_TOOLS_OFF]
description: Switch the heaters of all tools off. RETRACT=1 first pulls the filament of the mounted tool back (shared extruder, needs retract), for the end of a print
gcode:
{% set tc = printer.toolchanger %}
{% if params.RETRACT|default(0)|int == 1 and tc.tool_number|int >= 0 %}
VRTX_RETRACT FORCE=1 AUTO=1
{% endif %}
# the print is over: the next one starts with every tool unprimed
VRTX_PRIMED_RESET
{% for n in tc.tool_numbers %}
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=t{n}_wanted VALUE=0.0
SET_TOOL_TEMPERATURE T={n} TARGET=0
{% endfor %}
[gcode_macro VRTX_TOOLS_STATUS]
description: List the tools with heater, temperature and fan
gcode:
{% set tc = printer.toolchanger %}
{% if tc.tool_numbers|length == 0 %}
_VRTX_ECHO TYPE=warning MSG="No tools configured."
{% endif %}
{% for n in tc.tool_numbers %}
{% set tool = printer[tc.tool_names[loop.index0]] %}
{% set hn = tool.heater %}
{% set temp = "%.1f / %.1f C" % (printer[hn].temperature|float, printer[hn].target|float) if hn and printer[hn] is defined else "no heater" %}
{% set mark = "mounted" if tc.tool_number|int == n else "docked" %}
{% set fil = (printer.save_variables.variables['t%d_filament' % n] if printer.save_variables is defined else {})|default({}, true) %}
{% set parts = [fil.material|default(''), fil.name|default(''), ('spool %d' % fil.spool_id|int) if fil.spool_id|default(0)|int > 0 else '']|select|list %}
{% set ftxt = (" " ~ parts|join(", ")) if parts else " no filament set" %}
_VRTX_ECHO MSG="T{n} {mark} {temp}{ftxt}"
{% endfor %}
# ---------------------------------------------------------------------------
# Filament per tool
# ---------------------------------------------------------------------------
# Saved in [save_variables] as t<N>_filament, for example
# t2_filament = {'material': 'PETG', 'name': 'Galaxy Blue', 'color': '1E5AFF', 'temp': 240.0, 'spool_id': 12}
# spool_id (the Spoolman id) is optional: with it the page and the OrcaSlicer sync
# take material, name and colour from Spoolman, and the spool is made the active
# one when the tool is mounted.
# The page shows it on the tool card and compares it with the colours a sliced
# file expects. Spoolman is optional.
[gcode_macro VRTX_SET_FILAMENT]
description: Set the filament of a tool. T= (default: mounted tool) MATERIAL=PLA COLOR=RRGGBB (no hash sign) NAME="Galaxy Blue" TEMP=215 SPOOL=<Spoolman id, 0 = none>, CLEAR=1 forgets it. Left-out values stay as they were.
gcode:
{% set tc = printer.toolchanger %}
{% set n = params.T|default(tc.tool_number)|int %}
{% if n not in tc.tool_numbers %}
_VRTX_ECHO TYPE=warning MSG="Filament ignored, no tool {n}."
{% elif printer.save_variables is not defined %}
_VRTX_ECHO TYPE=warning MSG="Filament ignored, it needs a [save_variables] section."
{% elif params.CLEAR|default(0)|int == 1 %}
SAVE_VARIABLE VARIABLE=t{n}_filament VALUE="{ {} }"
_VRTX_SPOOL_PUBLISH T={n} ID=0
_VRTX_ECHO MSG="T{n}: filament cleared."
{% else %}
{% set old = printer.save_variables.variables['t%d_filament' % n]|default({}, true) %}
{% set material = params.MATERIAL|default(old.material|default(''))|string|replace('"', '')|replace("'", '')|replace('\\', '')|trim|upper %}
{% set name = params.NAME|default(old.name|default(''))|string|replace('"', '')|replace("'", '')|replace('\\', '')|trim %}
{% set color = params.COLOR|default(old.color|default(''))|string|trim|upper %}
{% set temp = params.TEMP|default(old.temp|default(0))|float %}
{% set spool = params.SPOOL|default(old.spool_id|default(0))|int %}
{% set hex_ok = color == '' or (color|length == 6 and color|list|reject('in', '0123456789ABCDEF')|list|length == 0) %}
{% if not hex_ok %}
_VRTX_ECHO TYPE=warning MSG="T{n}: COLOR must be 6 hex digits like FF8800, got {color}. Nothing saved."
{% elif temp < 0 or temp > 500 %}
_VRTX_ECHO TYPE=warning MSG="T{n}: TEMP {temp} is out of range. Nothing saved."
{% elif params.SPOOL is defined and (params.SPOOL|string|trim == '' or params.SPOOL|string|trim|list|reject('in', '0123456789')|list|length > 0) %}
_VRTX_ECHO TYPE=warning MSG="T{n}: SPOOL must be the number of the spool in Spoolman (0 = none), got {params.SPOOL}. Nothing saved."
{% elif spool < 0 or spool > 999999 %}
_VRTX_ECHO TYPE=warning MSG="T{n}: SPOOL {spool} is not a Spoolman id. Nothing saved."
{% else %}
{% set fil = {'material': material[:20], 'name': name[:40], 'color': color, 'temp': temp} %}
{% if spool > 0 %}
{% set fil = {'material': material[:20], 'name': name[:40], 'color': color, 'temp': temp, 'spool_id': spool} %}
{% endif %}
SAVE_VARIABLE VARIABLE=t{n}_filament VALUE="{fil}"
_VRTX_SPOOL_PUBLISH T={n} ID={spool}
_VRTX_ECHO MSG="T{n}: filament {material} {name} {('colour ' ~ color) if color else ''}{(' Spoolman spool ' ~ spool) if spool > 0 else ''}"
{% if spool > 0 and tc.tool_number|int == n %}
_VRTX_SPOOL T={n}
{% endif %}
{% endif %}
{% endif %}
# ---------------------------------------------------------------------------
# Tool change log
# ---------------------------------------------------------------------------
# The T commands note the time of every change (with the heater wait in
# standby mode) and whether it worked: afterwards the tool must be the
# mounted one, and the carriage switch (head_switch), when there is one, must
# see a tool. A change that stopped with an error is logged as failed at the
# next change. Kept until Klipper restarts.
[gcode_macro _VRTX_LOG]
description: Tool change log of the VRTX T commands (internal). Not a command.
variable_entries: []
variable_pending: {}
variable_changes: 0
variable_failures: 0
variable_total_s: 0.0
gcode:
[gcode_macro _VRTX_LOG_BEGIN]
description: Start timing a tool change (internal). T= FROM=
gcode:
{% set lg = printer["gcode_macro _VRTX_LOG"] %}
{% set now = printer.toolhead.estimated_print_time|float %}
{% if lg.pending %}
{% set e = {'from': lg.pending['from'], 'to': lg.pending['to'], 's': -1.0, 'ok': False} %}
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=entries VALUE="{(lg.entries + [e])[-20:]}"
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=changes VALUE={lg.changes + 1}
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=failures VALUE={lg.failures + 1}
{% endif %}
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=pending VALUE="{ {'from': params.FROM|int, 'to': params.T|int, 'start': now} }"
[gcode_macro _VRTX_LOG_END]
description: Finish timing a tool change (internal). T=
gcode:
{% set lg = printer["gcode_macro _VRTX_LOG"] %}
{% set n = params.T|int %}
{% set p = lg.pending %}
{% if p and p['to'] == n %}
{% set dur = [printer.toolhead.estimated_print_time|float - p['start']|float, 0.0]|max %}
{% set ts = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] if printer["gcode_macro _VRTX_TOOLS_SETTINGS"] is defined else {} %}
{% set head = printer["gcode_button " ~ (ts.head_switch|default('vrtx_head')|string|trim)] %}
{% set head_ok = head is not defined or head.state == "PRESSED" %}
{% set ok = printer.toolchanger.tool_number|int == n and head_ok %}
{% set e = {'from': p['from'], 'to': n, 's': (dur * 10)|round / 10, 'ok': ok} %}
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=entries VALUE="{(lg.entries + [e])[-20:]}"
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=changes VALUE={lg.changes + 1}
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=pending VALUE="{ {} }"
{% if ok %}
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=total_s VALUE={lg.total_s + e.s}
{% else %}
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=failures VALUE={lg.failures + 1}
_VRTX_ECHO TYPE=warning MSG="T{n}: tool change finished but {'the carriage switch sees no tool' if not head_ok else 'T%d is not the mounted tool' % n}."
{% endif %}
{% endif %}
[gcode_macro VRTX_TOOL_LOG]
description: Print the last tool changes with time and result. RESET=1 clears the log.
gcode:
{% set lg = printer["gcode_macro _VRTX_LOG"] %}
{% if params.RESET|default(0)|int == 1 %}
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=entries VALUE="[]"
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=pending VALUE="{ {} }"
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=changes VALUE=0
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=failures VALUE=0
SET_GCODE_VARIABLE MACRO=_VRTX_LOG VARIABLE=total_s VALUE=0.0
_VRTX_ECHO MSG="Tool change log cleared."
{% else %}
{% set good = lg.changes - lg.failures %}
{% set avg = (lg.total_s / good) if good > 0 else 0 %}
_VRTX_ECHO MSG="{'%d tool changes, %d failed, %.1f s on average' % (lg.changes, lg.failures, avg)}"
{% for e in lg.entries %}
{% set src = ('T%d' % e['from']) if e['from'] >= 0 else 'none' %}
_VRTX_ECHO MSG="{'%s -> T%d %s %s' % (src, e['to'], ('%.1f s' % e.s) if e.s >= 0 else '-', 'ok' if e.ok else 'FAILED')}"
{% endfor %}
{% endif %}
# ---------------------------------------------------------------------------
# Shared extruder: retract and prime
# ---------------------------------------------------------------------------
# A hotend swapper with one motor lets go of one filament and grips the next,
# so the filament of the old tool is pulled back a little before the gears open
# and pushed forward again after the tool is mounted again. Both only happen
# during a print (a paused print is not printing), and only when the nozzle is
# hot enough. How much is in the settings (retract). The pull-back of every tool
# is remembered until it is pushed back, also across a pause.
[gcode_macro VRTX_RETRACT]
description: Retract the filament of the mounted tool (shared extruder). T= LENGTH= FORCE=1 also outside a print
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set tstate = printer["gcode_macro _VRTX_TOOLS_STATE"] %}
{% set n = params.T|default(printer.toolchanger.tool_number)|int %}
{% set length = params.LENGTH|default(st.retract|default(0))|float %}
{% set force = params.FORCE|default(0)|int == 1 %}
{% set auto = params.AUTO|default(0)|int == 1 %}
{% set printing = (printer.print_stats is defined and printer.print_stats.state|default('') in ['printing', 'paused']) or printer["gcode_macro _VRTX_DOCK_STATE"].force_printing|default(0)|int == 1 %}
{% if n < 0 or length <= 0 %}
{% if not auto %}
_VRTX_ECHO TYPE=warning MSG="Nothing to retract: no tool mounted, or retract is 0 in _VRTX_TOOLS_SETTINGS."
{% endif %}
{% elif not (printing or force) %}
{% if not auto %}
_VRTX_ECHO TYPE=warning MSG="Not printing, so nothing was retracted. Add FORCE=1 to do it anyway."
{% endif %}
{% elif tstate['t' ~ n ~ '_retracted']|default(0)|float > 0 %}
{% if not auto %}
_VRTX_ECHO MSG="{'T%d is already retracted by %.1f mm.' % (n, tstate['t' ~ n ~ '_retracted']|float)}"
{% endif %}
{% else %}
_VRTX_RETRACT_PUSH T={n} LENGTH={length}
{% endif %}
[gcode_macro _VRTX_RETRACT_PUSH]
description: Pull the filament back and remember it (internal). T= LENGTH=
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set n = params.T|int %}
{% set length = params.LENGTH|float %}
{% if not printer[printer.toolhead.extruder].can_extrude %}
_VRTX_ECHO TYPE=warning MSG="{'T%d is too cold to retract %.1f mm.' % (n, length)}"
{% else %}
SAVE_GCODE_STATE NAME=_vrtx_retract
M83
G1 E-{length} F{st.retract_speed|default(40)|float * 60}
RESTORE_GCODE_STATE NAME=_vrtx_retract MOVE=0
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=t{n}_retracted VALUE={length}
{% if printer.save_variables is defined %}
SAVE_VARIABLE VARIABLE=t{n}_retracted VALUE={length}
{% endif %}
{% endif %}
[gcode_macro VRTX_PRIME]
description: Push back the filament that VRTX_RETRACT pulled out of the mounted tool, and the first prime of a tool in this print. T= FORCE=1 also outside a print
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set tstate = printer["gcode_macro _VRTX_TOOLS_STATE"] %}
{% set tc = printer.toolchanger %}
{% set n = params.T|default(tc.tool_number)|int %}
{% set force = params.FORCE|default(0)|int == 1 %}
{% set auto = params.AUTO|default(0)|int == 1 %}
{% set printing = (printer.print_stats is defined and printer.print_stats.state|default('') in ['printing', 'paused']) or printer["gcode_macro _VRTX_DOCK_STATE"].force_printing|default(0)|int == 1 %}
{% set pending = tstate['t' ~ n ~ '_retracted']|default(0)|float if n >= 0 else 0 %}
# first use of the tool in this print. The list starts again when nothing is printing, and when
# the print duration is smaller than at the last prime (a new print); VRTX_PRIMED_RESET and
# VRTX_TOOLS_OFF clear it too
{% set dur = printer.print_stats.print_duration|default(0)|float if printer.print_stats is defined else 0.0 %}
{% set primed = tstate.primed|default([]) if (printing and dur >= tstate.primed_dur|default(0)|float) else [] %}
{% if not printing and tstate.primed|default([])|length > 0 %}
VRTX_PRIMED_RESET
{% endif %}
{% set extra = st.first_prime|default(0)|float if (printing and st.prime_macro|default('')|string|trim != '' and n >= 0 and n not in primed) else 0.0 %}
{% if pending + extra <= 0 or n not in tc.tool_numbers %}
{% if not auto %}
_VRTX_ECHO MSG="Nothing to push back."
{% endif %}
{% elif not (printing or force) %}
{% if not auto %}
_VRTX_ECHO TYPE=warning MSG="Not printing, so nothing was pushed back. Add FORCE=1 to do it anyway."
{% endif %}
{% else %}
{% set hn = printer[tc.tool_names[tc.tool_numbers.index(n)]].heater %}
{% set target = printer[hn].target|float if hn and printer[hn] is defined else 0.0 %}
{% if target <= 0 %}
_VRTX_ECHO TYPE=warning MSG="{'T%d heater is off, so its %.1f mm retract is still waiting to be pushed back.' % (n, pending)}"
{% else %}
{% if st.prime_wait|default(True) %}
TEMPERATURE_WAIT SENSOR="{hn}" MINIMUM={target - 3}
{% endif %}
_VRTX_PRIME_PUSH T={n} EXTRA={extra}
{% endif %}
{% endif %}
# Forget which tools got their first prime. Call it at the start of PRINT_START (it is also
# done by itself when a print with a shorter duration than the last prime starts).
[gcode_macro VRTX_PRIMED_RESET]
description: A new print starts: every tool gets its first prime again (first_prime)
gcode:
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=primed VALUE="{ [] }"
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=primed_dur VALUE=0.0
[gcode_macro _VRTX_PRIME_PUSH]
description: Push the filament back and forget the retract (internal). T= EXTRA=
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set tstate = printer["gcode_macro _VRTX_TOOLS_STATE"] %}
{% set n = params.T|int %}
{% set extra = params.EXTRA|default(0)|float %}
{% set pending = tstate['t' ~ n ~ '_retracted']|default(0)|float %}
{% set length = pending + extra %}
{% set macro = st.prime_macro|default('')|string|trim %}
{% set speed = st.first_prime_speed|default(0)|float if st.first_prime_speed|default(0)|float > 0 else st.unretract_speed|default(25)|float %}
{% set dur = printer.print_stats.print_duration|default(0)|float if printer.print_stats is defined else 0.0 %}
{% set printing = (printer.print_stats is defined and printer.print_stats.state|default('') in ['printing', 'paused']) or printer["gcode_macro _VRTX_DOCK_STATE"].force_printing|default(0)|int == 1 %}
{% set primed = tstate.primed|default([]) if (printing and dur >= tstate.primed_dur|default(0)|float) else [] %}
{% if not printer[printer.toolhead.extruder].can_extrude %}
_VRTX_ECHO TYPE=warning MSG="{'T%d is too cold to push back %.1f mm, it stays pending.' % (n, length)}"
{% elif length > 0 %}
{% if macro != '' %}
{macro} T={n} LENGTH={length} SPEED={speed}
{% else %}
SAVE_GCODE_STATE NAME=_vrtx_retract
M83
G1 E{length} F{speed * 60}
RESTORE_GCODE_STATE NAME=_vrtx_retract MOVE=0
{% endif %}
{% if extra > 0 %}
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=primed VALUE="{primed + [n]}"
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=primed_dur VALUE={dur}
{% endif %}
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=t{n}_retracted VALUE=0.0
{% if printer.save_variables is defined %}
SAVE_VARIABLE VARIABLE=t{n}_retracted VALUE=0.0
{% endif %}
{% endif %}
# ---------------------------------------------------------------------------
# The [toolchanger] hooks
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Loading and unloading filament
# ---------------------------------------------------------------------------
# For a hotend swapper where you push the filament into the hotend (in its dock) by hand:
# VRTX_LOAD_FILAMENT T=3 mount T3, heat it, push load_length and purge_length mm
# VRTX_UNLOAD_FILAMENT T=3 mount T3, heat it, push a little, pull unload_length mm back,
# put the tool back; then pull the filament out by hand
# Both are refused during a print. The heater is switched off at the end (HOT=1 keeps it).
# Whatever retract was waiting to be pushed back is forgotten: the filament is a new one.
[gcode_macro VRTX_LOAD_FILAMENT]
description: Load filament into a tool: mount it, heat it, feed and purge. T= TEMP= LENGTH= PURGE= HOT=1 keeps it hot, DROP=1 puts the tool back
gcode:
{% set tc = printer.toolchanger %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set n = params.T|default(tc.tool_number if tc is defined else -1)|int %}
{% set fil = printer.save_variables.variables['t%d_filament' % n]|default({}, true) if printer.save_variables is defined else {} %}
{% set ftemp = fil.temp|default(0)|float %}
{% set temp = params.TEMP|float if params.TEMP is defined else (ftemp if ftemp > 0 else st.load_temp|default(210)|float) %}
{% set length = params.LENGTH|default(st.load_length|default(40))|float %}
{% set purge = params.PURGE|default(st.purge_length|default(50))|float %}
{% set printing = printer.print_stats is defined and printer.print_stats.state|default('') in ['printing', 'paused'] %}
{% if tc is not defined %}
{ action_raise_error("VRTX_LOAD_FILAMENT: there is no [toolchanger] section.") }
{% elif n not in tc.tool_numbers %}
{ action_raise_error("VRTX_LOAD_FILAMENT: give T=<tool number>, no tool is mounted.") }
{% elif tc.status != 'ready' %}
{ action_raise_error("VRTX_LOAD_FILAMENT: the toolchanger is not ready (status " ~ tc.status ~ ").") }
{% elif printing %}
{ action_raise_error("VRTX_LOAD_FILAMENT: a print is running or paused.") }
{% elif temp < 150 or temp > 400 %}
{ action_raise_error("VRTX_LOAD_FILAMENT: TEMP %.0f is outside 150 to 400 C." % temp) }
{% elif length < 0 or purge < 0 %}
{ action_raise_error("VRTX_LOAD_FILAMENT: LENGTH and PURGE cannot be negative.") }
{% elif tc.tool_number|int != n and printer.toolhead.homed_axes != 'xyz' %}
{ action_raise_error("VRTX_LOAD_FILAMENT: home the printer first (G28), the tool has to be mounted.") }
{% else %}
_VRTX_FILAMENT_MOUNT T={n}
VRTX_SET_TEMP T={n} TARGET={temp} WAIT=1
_VRTX_FILAMENT_MOVE T={n} PUSH={length} SPEED={st.load_speed|default(5)|float} PUSH2={purge} SPEED2={st.purge_speed|default(3)|float}
_VRTX_FILAMENT_FINISH T={n} HOT={params.HOT|default(0)|int} DROP={params.DROP|default(0)|int} WHAT=load
{% endif %}
[gcode_macro VRTX_UNLOAD_FILAMENT]
description: Unload filament from a tool: mount it, heat it, push a little, pull back. T= TEMP= LENGTH= HOT=1 keeps it hot, DROP=0 leaves the tool mounted
gcode:
{% set tc = printer.toolchanger %}
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set n = params.T|default(tc.tool_number if tc is defined else -1)|int %}
{% set fil = printer.save_variables.variables['t%d_filament' % n]|default({}, true) if printer.save_variables is defined else {} %}
{% set ftemp = fil.temp|default(0)|float %}
{% set temp = params.TEMP|float if params.TEMP is defined else (ftemp if ftemp > 0 else st.load_temp|default(210)|float) %}
{% set length = params.LENGTH|default(st.unload_length|default(60))|float %}
{% set printing = printer.print_stats is defined and printer.print_stats.state|default('') in ['printing', 'paused'] %}
{% if tc is not defined %}
{ action_raise_error("VRTX_UNLOAD_FILAMENT: there is no [toolchanger] section.") }
{% elif n not in tc.tool_numbers %}
{ action_raise_error("VRTX_UNLOAD_FILAMENT: give T=<tool number>, no tool is mounted.") }
{% elif tc.status != 'ready' %}
{ action_raise_error("VRTX_UNLOAD_FILAMENT: the toolchanger is not ready (status " ~ tc.status ~ ").") }
{% elif printing %}
{ action_raise_error("VRTX_UNLOAD_FILAMENT: a print is running or paused.") }
{% elif temp < 150 or temp > 400 %}
{ action_raise_error("VRTX_UNLOAD_FILAMENT: TEMP %.0f is outside 150 to 400 C." % temp) }
{% elif length <= 0 %}
{ action_raise_error("VRTX_UNLOAD_FILAMENT: LENGTH must be above 0.") }
{% elif tc.tool_number|int != n and printer.toolhead.homed_axes != 'xyz' %}
{ action_raise_error("VRTX_UNLOAD_FILAMENT: home the printer first (G28), the tool has to be mounted.") }
{% else %}
_VRTX_FILAMENT_MOUNT T={n}
VRTX_SET_TEMP T={n} TARGET={temp} WAIT=1
_VRTX_FILAMENT_MOVE T={n} PUSH={st.unload_push|default(10)|float} SPEED={st.purge_speed|default(3)|float} PULL={length} SPEED2={st.unload_speed|default(10)|float}
_VRTX_FILAMENT_FINISH T={n} HOT={params.HOT|default(0)|int} DROP={params.DROP|default(1)|int} WHAT=unload
{% endif %}
[gcode_macro _VRTX_FILAMENT_MOUNT]
description: Mount tool T for loading or unloading, and stop if it did not work (internal). T=
gcode:
{% set n = params.T|int %}
{% if printer.toolchanger.tool_number|int != n %}
{% if printer["gcode_macro T" ~ n] is defined %}
T{n}
{% else %}
SELECT_TOOL T={n}
{% endif %}
{% endif %}
_VRTX_FILAMENT_MOUNT_CHECK T={n}
[gcode_macro _VRTX_FILAMENT_MOUNT_CHECK]
description: Stop if tool T is not mounted (internal). T=
gcode:
{% set n = params.T|int %}
{% if printer.toolchanger.tool_number|int != n or printer.toolchanger.status != 'ready' %}
{ action_raise_error("T%d is not mounted (the changer is %s). The filament was not moved." % (n, printer.toolchanger.status)) }
{% endif %}
# Moves of the shared motor in steps of at most 40 mm. PUSH/SPEED then PUSH2/SPEED2 push,
# PULL/SPEED2 pulls back. mm and mm/s.
[gcode_macro _VRTX_FILAMENT_MOVE]
description: Push and pull the filament of the mounted tool in steps (internal). T= PUSH= SPEED= PUSH2= PULL= SPEED2=
gcode:
{% set n = params.T|int %}
{% set a = params.PUSH|default(0)|float %}
{% set sa = params.SPEED|default(5)|float %}
{% set b = params.PUSH2|default(0)|float %}
{% set pull = params.PULL|default(0)|float %}
{% set sb = params.SPEED2|default(5)|float %}
{% if not printer[printer.toolhead.extruder].can_extrude %}
{ action_raise_error("T%d is too cold to move the filament. Nothing was moved." % n) }
{% else %}
SAVE_GCODE_STATE NAME=_vrtx_filament
M83
{% if a > 0 %}
{% set parts = (a / 40.0)|round(0, 'ceil')|int %}
{% for i in range(parts) %}
G1 E{"%.3f" % (a / parts)} F{(sa * 60)|round(0)|int}
{% endfor %}
{% endif %}
{% if b > 0 %}
{% set parts = (b / 40.0)|round(0, 'ceil')|int %}
{% for i in range(parts) %}
G1 E{"%.3f" % (b / parts)} F{(sb * 60)|round(0)|int}
{% endfor %}
{% endif %}
{% if pull > 0 %}
{% set parts = (pull / 40.0)|round(0, 'ceil')|int %}
{% for i in range(parts) %}
G1 E-{"%.3f" % (pull / parts)} F{(sb * 60)|round(0)|int}
{% endfor %}
{% endif %}
M400
RESTORE_GCODE_STATE NAME=_vrtx_filament MOVE=0
SET_GCODE_VARIABLE MACRO=_VRTX_TOOLS_STATE VARIABLE=t{n}_retracted VALUE=0.0
{% if printer.save_variables is defined %}
SAVE_VARIABLE VARIABLE=t{n}_retracted VALUE=0.0
{% endif %}
{% endif %}
[gcode_macro _VRTX_FILAMENT_FINISH]
description: Heater off, put the tool back, say what is left to do (internal). T= HOT= DROP= WHAT=load|unload
gcode:
{% set n = params.T|int %}
{% if params.HOT|default(0)|int != 1 %}
VRTX_SET_TEMP T={n} TARGET=0
{% endif %}
{% if params.DROP|default(0)|int == 1 %}
VRTX_DROP_TOOL
{% endif %}
{% if params.WHAT|default('load') == 'unload' %}
_VRTX_ECHO MSG="{'T%d: the filament is pulled back. Pull it out by hand%s.' % (n, '' if params.DROP|default(0)|int == 1 else ' (the tool is still mounted, the motor holds it: put the tool back first)')}"
{% else %}
_VRTX_ECHO MSG="{'T%d: filament loaded. Set what it is with VRTX_SET_FILAMENT T=%d MATERIAL= COLOR= TEMP= if you have not.' % (n, n)}"
{% endif %}
[gcode_macro VRTX_BEFORE_CHANGE]
description: VRTX: before_change_gcode of [toolchanger]. Retracts the tool that is put away (if retract is set)
gcode:
{% if printer.toolchanger.tool_number|int >= 0 %}
VRTX_RETRACT AUTO=1
{% endif %}
[gcode_macro VRTX_AFTER_CHANGE]
description: VRTX: after_change_gcode of [toolchanger]. T=<new tool>. Motor, pressure advance, prime
gcode:
{% set n = params.T|default(printer.toolchanger.tool_number)|int %}
{% set failed = printer["gcode_macro _VRTX_DOCK_STATE"].failed|default(0)|int == 1 if printer["gcode_macro _VRTX_DOCK_STATE"] is defined else false %}
{% if n >= 0 and not failed %}
_VRTX_MOTOR T={n}
VRTX_PRIME T={n} AUTO=1
{% if printer["gcode_macro VRTX_DOCK_RESTORE_PENDING"] is defined %}
# prime_before_restore: the way back to the print, if the prime_macro did not do it
VRTX_DOCK_RESTORE_PENDING
{% endif %}
{% endif %}
# The shared motor: give it to the extruder of the mounted tool and set the
# pressure advance and rotation distance that belong to the tool. They come
# from the [tool Tn] section:
# params_pressure_advance: 0.045
# params_pressure_advance_smooth_time: 0.04
# params_rotation_distance: 22.68
# A value that a tool does not have is left as it is.
[gcode_macro _VRTX_MOTOR]
description: Hand the shared motor to the tool and set its pressure advance (internal). T=
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set tc = printer.toolchanger %}
{% set n = params.T|int %}
{% set motor = st.motor|default('')|string|trim %}
{% if motor != '' and n in tc.tool_numbers %}
{% set tool = printer[tc.tool_names[tc.tool_numbers.index(n)]] %}
# the motor is an [extruder_stepper], or the stepper of an [extruder] that has step_pin (Layout C): hand it to the tool's queue
{% set mcfg = printer.configfile.settings[motor|lower] if printer.configfile.settings[motor|lower] is defined else {} %}
{% if (printer["extruder_stepper " ~ motor] is defined or mcfg.step_pin is defined) and tool.extruder %}
SYNC_EXTRUDER_MOTION EXTRUDER={motor} MOTION_QUEUE={tool.extruder}
{% endif %}
{% if tool.params_pressure_advance is defined %}
{% if tool.params_pressure_advance_smooth_time is defined %}
SET_PRESSURE_ADVANCE EXTRUDER={motor} ADVANCE={tool.params_pressure_advance|float} SMOOTH_TIME={tool.params_pressure_advance_smooth_time|float}
{% else %}
SET_PRESSURE_ADVANCE EXTRUDER={motor} ADVANCE={tool.params_pressure_advance|float}
{% endif %}
{% endif %}
{% if tool.params_rotation_distance is defined %}
SET_EXTRUDER_ROTATION_DISTANCE EXTRUDER={motor} DISTANCE={tool.params_rotation_distance|float}
{% endif %}
{% endif %}
# ---------------------------------------------------------------------------
# A failed tool change
# ---------------------------------------------------------------------------
# A check of the dock macros that fails during a print does not raise an error,
# that would end the print (see _VRTX_DOCK_FAIL in vrtx_dock.cfg): the change is
# finished without docking anything more and the T command ends up here. The
# print is paused and a prompt with a Recover button is shown. Fix the tool by
# hand, then run VRTX_RESUME (the button does it).
#
# This is also the error_gcode of [toolchanger], for the failures that
# klipper-toolchanger finds itself (VERIFY_TOOL_DETECTED, a tool that goes missing).
[gcode_macro VRTX_TOOLCHANGE_ERROR]
description: VRTX: pauses the print after a failed tool change. Also the error_gcode of [toolchanger]
gcode:
{% set st = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set dock = printer["gcode_macro _VRTX_DOCK_STATE"] if printer["gcode_macro _VRTX_DOCK_STATE"] is defined else {} %}
{% set printing = (printer.print_stats is defined and printer.print_stats.state|default('') in ['printing', 'paused']) or dock.force_printing|default(0)|int == 1 %}
{% set msg = dock.failed_msg|default('')|string or 'the tool change failed, see the message above' %}
RESPOND TYPE=error MSG="{'VRTX: tool change failed: %s' % msg}"
{% if not (printing and st.pause_on_error|default(True)) %}
_VRTX_ACCEL_RESTORE FORCE=1