Repository navigation
Expand file tree
/
Copy pathvrtx.cfg
More file actions
1020 lines (984 loc) · 62.6 KB
/
Copy pathvrtx.cfg
File metadata and controls
1020 lines (984 loc) · 62.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.cfg
#
# VRTX
#
# The calibration part of VRTX: automatic tool offset calibration for tool
# changers that use klipper-toolchanger (such as MadMax), on stock Klipper or
# Kalico. The tool commands are in vrtx_tools.cfg (vrtx_tools_core.cfg and t_commands/), the docking in vrtx_dock.cfg.
#
# XY: every nozzle touches a Nudge probe (a small switch that the nozzle
# pushes in X and Y) and the macros turn the touch points into X and Y
# tool offsets. The probing is done by klipper-toolchanger's
# [tools_calibrate] module.
# Z: from the same Nudge probe, or from an optional separate switch
# ([vrtx_z_probe]) that every nozzle presses from above. The switch is
# the more repeatable of the two in Z.
#
# One command does it all: VRTX_CALIBRATE
#
# That is the NUDGE method. The installer also offers the CAMERA method (method 2): X and
# Y are aligned by eye with a camera and Z is measured with the Z switch. Its macros are
# in vrtx_camera.cfg, it is switched on by calibration 'camera' (section 2 of your file),
# and VRTX_CALIBRATE_Z does its Z measurement.
#
# Loaded from VRTX/vrtx.cfg in your config folder (printer.cfg has the one
# line [include VRTX/vrtx.cfg] ). Change settings in section 2 of that
# file, never in this one.
#
# Klipper strips everything after a hash sign on every line, so comments in
# this file are always whole lines.
[gcode_macro _VRTX_SETTINGS]
description: Settings of the VRTX tool calibration. Not a command.
# Which tool is the reference, normally T0. Its offsets are the base for all others.
variable_reference_tool: 0
# How tool offsets reach Klipper.
# tool_parameter: for klipper-toolchanger from 2025.12.26 on. The offsets are the
# gcode_x_offset, gcode_y_offset and gcode_z_offset parameters of
# the tools. They are applied from the saved variables at startup.
# gcode_offset: for configs that apply offsets with SET_GCODE_OFFSET after every
# tool change, like the r2pdx MadMax configs. They read the
# t<N>_offset_x/y/z variables that this macro set also writes.
variable_offset_method: 'tool_parameter'
# How X and Y are measured.
# nudge: with the Nudge probe, VRTX_CALIBRATE does everything (the default)
# camera: with the camera, by eye (vrtx_camera.cfg: VRTX_START ... VRTX_END). Z is
# measured with the Z switch, by VRTX_CALIBRATE_Z.
variable_calibration: 'nudge'
# Nudge probe. Where the nozzle of the REFERENCE tool is when it hangs just above
# the middle of the probe pin, in gcode coordinates. The module finds the exact
# centre itself, 1 to 2 mm off is fine. nudge_z is the height of the nozzle tip:
# a few mm above the top of the pin, higher than the pin plus the lift of
# [tools_calibrate]. All three have to be set, 'unset' stops the calibration.
variable_nudge_x: 'unset'
variable_nudge_y: 'unset'
variable_nudge_z: 'unset'
# Where Z comes from.
# auto: the separate Z switch if [vrtx_z_probe] and its position exist, else the Nudge probe
# nudge: the Nudge probe
# switch: the separate Z switch
variable_z_source: 'auto'
# Z switch only. Where the nozzle of the reference tool is when it hangs above
# the middle of the switch, in gcode coordinates. 'unset' = no switch.
variable_z_switch_x: 'unset'
variable_z_switch_y: 'unset'
# Z for travel moves between the probes and the tool docks.
# It must be higher than everything on the bed. (Around the Nudge probe the toolhead
# goes up to nudge_clear_z above nudge_z instead, if that is higher.)
variable_safe_z: 30.0
# Speeds in mm/s
variable_travel_speed: 200.0
variable_z_speed: 10.0
# Z switch: coarse search speed from safe_z downwards, and the lowest Z that the
# search may reach. The switch has to be above this height.
variable_z_coarse_speed: 5.0
variable_z_search_min: 0.0
# Z switch: lift in mm between the coarse search and the measurement
variable_z_coarse_lift: 2.0
# Largest sample spread in mm of the switch that is accepted without a warning
variable_z_range_warning: 0.02
# Nozzle temperature for the measurement in C. 0 measures cold.
# Measuring hot includes the thermal expansion of each hotend, but hot nozzles
# need to be clean. 150 is enough to soften leftover filament.
variable_cal_temp: 0
# A measured offset larger than this (mm, any axis) is not saved. It means
# that something went wrong: the wrong spot was probed or a tool is not seated.
variable_max_offset: 10.0
# The same limit for one axis only, mm. 0 = use max_offset. Tools that are built
# and seated well differ by a fraction of a millimetre, so a tight limit such as
# 2 for X and Y and 3 for Z catches a badly seated tool.
variable_max_offset_x: 0.0
variable_max_offset_y: 0.0
variable_max_offset_z: 0.0
# VRTX_CALIBRATE RUNS=n: a tool whose runs differ by more than this (mm, largest
# minus smallest, any axis) gets a warning. It is usually seated badly or dirty.
variable_run_range_warning: 0.05
# Room in mm that the start check of the Nudge probe keeps free besides the spread
# of [tools_calibrate] (the centre is found 1 to 2 mm from where nudge_x/y says).
variable_nudge_margin: 1.0
# The toolhead goes up to this many mm above nudge_z (or to safe_z, if that is higher)
# before it moves over the Nudge pin and after every measurement, and never down.
variable_nudge_clear_z: 10.0
# 1 = VRTX_CALIBRATE ends by putting the tool that is mounted into its dock and moving
# the carriage back to where it started. 0 = it ends with the tool mounted that was
# mounted when it started.
variable_park_after_calibrate: 0
gcode:
[gcode_macro _VRTX_STATE]
description: Internal state of the VRTX macros. Not a command.
variable_z_ref: 0.0
variable_z_ref_offset: 0.0
variable_ref_ox: 0.0
variable_ref_oy: 0.0
variable_ref_oz: 0.0
# VRTX_CALIBRATE RUNS=n: the results of the runs of the tool that is measured
variable_runs_x: []
variable_runs_y: []
variable_runs_z: []
# The tool whose nozzle touch set Z (VRTX_Z_REFERENCE), -1 = the reference tool's frame
variable_z_frame: -1
# VRTX_CALIBRATE: 1 while a calibration with the Nudge runs (watched by _VRTX_CAL_WATCHDOG)
variable_cal_armed: 0
gcode:
[gcode_macro _VRTX_ECHO]
description: Console output. MSG=text, TYPE=info|warning
gcode:
{% set text = params.MSG|default("")|replace('"', "'") %}
{% if params.TYPE|default("info")|lower == "warning" %}
{ action_respond_info("VRTX WARNING: " ~ text) }
{% else %}
{ action_respond_info("VRTX: " ~ text) }
{% endif %}
# ---------------------------------------------------------------------------
# Small building blocks. They are separate macros because a macro reads the
# printer state when it starts, not when its lines run.
# ---------------------------------------------------------------------------
[gcode_macro _VRTX_SELECT]
description: Mount tool T. In gcode_offset mode also apply its saved offsets (internal)
gcode:
SELECT_TOOL T={params.T|int}
_VRTX_APPLY_GCODE_OFFSET
[gcode_macro _VRTX_APPLY_GCODE_OFFSET]
description: gcode_offset mode: set the G-code offset of the mounted tool from the saved variables (internal)
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set svv = printer.save_variables.variables %}
{% set n = printer.toolchanger.tool_number|int %}
{% if s.offset_method|string|lower == 'gcode_offset' and n >= 0 %}
SET_GCODE_OFFSET X={svv['t' ~ n ~ '_offset_x']|default(0)|float} Y={svv['t' ~ n ~ '_offset_y']|default(0)|float} Z={svv['t' ~ n ~ '_offset_z']|default(0)|float}
{% endif %}
[gcode_macro _VRTX_SAFE_Z]
description: Raise to safe_z unless already higher
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% if printer.toolhead.position.z|float < s.safe_z|float %}
G90
G1 Z{s.safe_z|float} F{s.z_speed|float * 60}
{% endif %}
# ---------------------------------------------------------------------------
# Offsets in the save file and in Klipper
# ---------------------------------------------------------------------------
[gcode_macro _VRTX_STORE_OFFSET]
description: Save the offset of tool T and hand it to Klipper. T= X= Y= Z=
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set t = params.T|int %}
{% set method = s.offset_method|string|lower %}
{% set vx = params.X|float|round(3) %}
{% set vy = params.Y|float|round(3) %}
{% set vz = params.Z|float|round(3) %}
SAVE_VARIABLE VARIABLE=t{t}_offset_x VALUE={vx}
SAVE_VARIABLE VARIABLE=t{t}_offset_y VALUE={vy}
SAVE_VARIABLE VARIABLE=t{t}_offset_z VALUE={vz}
{% if method == 'tool_parameter' %}
SET_TOOL_PARAMETER T={t} PARAMETER=gcode_x_offset VALUE={vx}
SET_TOOL_PARAMETER T={t} PARAMETER=gcode_y_offset VALUE={vy}
SET_TOOL_PARAMETER T={t} PARAMETER=gcode_z_offset VALUE={vz}
{% endif %}
[gcode_macro VRTX_APPLY_OFFSETS]
description: Load the saved offsets into the tools (offset_method tool_parameter). Runs at startup.
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set svv = printer.save_variables.variables %}
{% if s.offset_method|string|lower == 'tool_parameter' and printer.toolchanger is defined %}
{% for n in printer.toolchanger.tool_numbers %}
{% if svv['t' ~ n ~ '_offset_x'] is defined %}
SET_TOOL_PARAMETER T={n} PARAMETER=gcode_x_offset VALUE={svv['t' ~ n ~ '_offset_x']|float}
SET_TOOL_PARAMETER T={n} PARAMETER=gcode_y_offset VALUE={svv['t' ~ n ~ '_offset_y']|default(0)|float}
SET_TOOL_PARAMETER T={n} PARAMETER=gcode_z_offset VALUE={svv['t' ~ n ~ '_offset_z']|default(0)|float}
{% endif %}
{% endfor %}
_VRTX_ECHO MSG="Tool offsets loaded."
{% endif %}
[delayed_gcode _VRTX_STARTUP]
initial_duration: 2.0
gcode:
VRTX_APPLY_OFFSETS
[gcode_macro VRTX_ECHO_OFFSETS]
description: Show the tool offsets in use (offset_method tool_parameter: the ones of the tools; gcode_offset: the saved ones)
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set svv = printer.save_variables.variables if printer.save_variables is defined else {} %}
{% if printer.toolchanger is not defined %}
_VRTX_ECHO TYPE=warning MSG="klipper-toolchanger is not configured."
{% else %}
{% set tp = s.offset_method|string|lower == 'tool_parameter' %}
{% for n in printer.toolchanger.tool_numbers %}
{% set tool = printer[printer.toolchanger.tool_names[loop.index0]] %}
{% set ox = tool.gcode_x_offset|float if tp else svv['t' ~ n ~ '_offset_x']|default(0)|float %}
{% set oy = tool.gcode_y_offset|float if tp else svv['t' ~ n ~ '_offset_y']|default(0)|float %}
{% set oz = tool.gcode_z_offset|float if tp else svv['t' ~ n ~ '_offset_z']|default(0)|float %}
{% set note = " (not saved by VRTX yet)" if tp and svv['t' ~ n ~ '_offset_x'] is not defined else "" %}
{ action_respond_info("VRTX: T%d offset X %.3f Y %.3f Z %.3f%s" % (n, ox, oy, oz, note)) }
{% endfor %}
{% endif %}
[gcode_macro VRTX_RESET_OFFSETS]
description: Set the offsets of tool T to zero. Without T all tools except the reference tool. The tool must not be mounted.
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set ref = s.reference_tool|int %}
{% set only = params.T|default(-1)|int %}
{% set active = printer.toolchanger.tool_number|int %}
{% set ns = namespace(err="") %}
{% for n in printer.toolchanger.tool_numbers %}
{% if ((only == -1 and n != ref) or only == n) and n == active %}
{% set ns.err = "T" ~ n ~ " is mounted. Select another tool before resetting its offsets." %}
{% endif %}
{% endfor %}
{% if ns.err %}
_VRTX_ECHO TYPE=warning MSG="{ns.err} Nothing was reset."
{% else %}
{% for n in printer.toolchanger.tool_numbers %}
{% if (only == -1 and n != ref) or only == n %}
_VRTX_STORE_OFFSET T={n} X=0 Y=0 Z=0
{% endif %}
{% endfor %}
_VRTX_ECHO MSG="Offsets reset."
{% endif %}
# ---------------------------------------------------------------------------
# The calibration
# ---------------------------------------------------------------------------
[gcode_macro VRTX_CALIBRATE]
description: Measure the tool offsets (then park the tool and return the carriage with park_after_calibrate: 1). TOOL=<n> one tool only, XY=0 / Z=0 leave an axis out, TEMP=<nozzle C> overrides cal_temp, RUNS=<n> measure n times and use the mean, SAVE=0 only report.
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set ref = s.reference_tool|int %}
{% set runs = params.RUNS|default(1)|int %}
{% set save = params.SAVE|default(1)|int %}
{% set printing = (printer.print_stats is defined and printer.print_stats.state|default('') in ['printing', 'paused']) or (printer["gcode_macro _VRTX_DOCK_STATE"] is defined and printer["gcode_macro _VRTX_DOCK_STATE"].force_printing|default(0)|int == 1) %}
{% set method = s.offset_method|string|lower %}
{% set tmin = printer.toolhead.axis_minimum %}
{% set tmax = printer.toolhead.axis_maximum %}
{% set only = params.TOOL|default(-1)|int %}
{% set park = s.park_after_calibrate|default(0)|int %}
{% set start_x = printer.toolhead.position.x|float %}
{% set start_y = printer.toolhead.position.y|float %}
{% set start_z = printer.toolhead.position.z|float %}
{% set do_xy = params.XY|default(1)|int %}
{% set do_z = params.Z|default(1)|int %}
{% set temp = params.TEMP|default(s.cal_temp)|float %}
{% set start_tool = printer.toolchanger.tool_number|int if printer.toolchanger is defined else -1 %}
{% set svv = printer.save_variables.variables if printer.save_variables is defined else {} %}
{% set src = s.z_source|string|lower %}
{% set cam = s.calibration|string|lower == 'camera' %}
{% set has_switch = printer.vrtx_z_probe is defined and s.z_switch_x|string|lower != 'unset' and s.z_switch_y|string|lower != 'unset' %}
{% if src == 'auto' %}
{% set src = 'switch' if (has_switch or cam) else 'nudge' %}
{% endif %}
{% set nudge_set = s.nudge_x|string|lower != 'unset' and s.nudge_y|string|lower != 'unset' and s.nudge_z|string|lower != 'unset' %}
{% set need_nudge = do_xy == 1 or (do_z == 1 and src == 'nudge') %}
{% set spread = printer.configfile.settings.tools_calibrate.spread|default(5.0)|float if printer.configfile.settings.tools_calibrate is defined else 5.0 %}
{% set room = spread + s.nudge_margin|default(1.0)|float %}
{% set ns = namespace(err="", range="") %}
{% if need_nudge and nudge_set and printer.toolchanger is defined and svv is defined %}
{% for n in printer.toolchanger.tool_numbers %}
{% if n == ref or only == -1 or only == n %}
{% set tool = printer[printer.toolchanger.tool_names[loop.index0]] %}
{% set ox = tool.gcode_x_offset|float if method == 'tool_parameter' else svv['t' ~ n ~ '_offset_x']|default(0)|float %}
{% set oy = tool.gcode_y_offset|float if method == 'tool_parameter' else svv['t' ~ n ~ '_offset_y']|default(0)|float %}
{% set oz = tool.gcode_z_offset|float if method == 'tool_parameter' else svv['t' ~ n ~ '_offset_z']|default(0)|float %}
{% set sx = s.nudge_x|float + ox %}
{% set sy = s.nudge_y|float + oy %}
{% set sz = s.nudge_z|float + oz %}
{% if sx - room < tmin.x or sx + room > tmax.x or sy - room < tmin.y or sy + room > tmax.y or sz < tmin.z or sz > tmax.z %}
{% set ns.range = ns.range ~ ("T%d would probe around X %.1f Y %.1f Z %.1f (its offset is X %.2f Y %.2f Z %.2f) and needs %.1f mm free on each side, the printer allows X %.0f to %.0f, Y %.0f to %.0f, Z %.0f to %.0f. " % (n, sx, sy, sz, ox, oy, oz, room, tmin.x, tmax.x, tmin.y, tmax.y, tmin.z, tmax.z)) %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% if printer.toolchanger is not defined %}
{% set ns.err = "klipper-toolchanger is not configured." %}
{% elif printer.save_variables is not defined %}
{% set ns.err = "A [save_variables] section is required." %}
{% elif ref not in printer.toolchanger.tool_numbers %}
{% set ns.err = "The reference tool T" ~ ref ~ " does not exist." %}
{% elif only != -1 and only not in printer.toolchanger.tool_numbers %}
{% set ns.err = "Tool T" ~ only ~ " does not exist." %}
{% elif only == ref %}
{% set ns.err = "T" ~ ref ~ " is the reference tool, it has no offsets to measure." %}
{% elif s.calibration|string|lower not in ['nudge', 'camera'] %}
{% set ns.err = "calibration in _VRTX_SETTINGS must be nudge or camera." %}
{% elif printer["gcode_macro _VRTX_CAMERA_STATE"] is defined and printer["gcode_macro _VRTX_CAMERA_STATE"].active|string|lower == 'true' %}
{% set ns.err = "A camera alignment session is running. Finish it with VRTX_END first." %}
{% elif cam and do_xy == 1 %}
{% set ns.err = "The calibration method is camera: X and Y are aligned with the camera (VRTX_START, VRTX_SET_TOOL, VRTX_LOAD_TOOL, VRTX_END, or the Alignment tab of the VRTX page). VRTX_CALIBRATE_Z measures Z with the Z switch." %}
{% elif cam and src == 'nudge' %}
{% set ns.err = "The calibration method is camera: Z is measured with the Z switch, z_source nudge is not possible." %}
{% elif printing %}
{% set ns.err = "A print is running or paused. The calibration moves the toolhead and changes tools, so it is not done during a print." %}
{% elif runs < 1 or runs > 50 %}
{% set ns.err = "RUNS must be between 1 and 50." %}
{% elif printer.toolhead.homed_axes != "xyz" %}
{% set ns.err = "Home all axes first." %}
{% elif s.offset_method|string|lower not in ['tool_parameter', 'gcode_offset'] %}
{% set ns.err = "offset_method in _VRTX_SETTINGS must be tool_parameter or gcode_offset." %}
{% elif do_xy == 0 and do_z == 0 %}
{% set ns.err = "XY=0 and Z=0 leaves nothing to measure." %}
{% elif need_nudge and printer.tools_calibrate is not defined %}
{% set ns.err = "The [tools_calibrate] section (the Nudge probe) is missing. See README.md." %}
{% elif need_nudge and not nudge_set %}
{% set ns.err = "nudge_x, nudge_y and nudge_z in _VRTX_SETTINGS are not set." %}
{% elif need_nudge and (s.nudge_z|float + s.nudge_clear_z|default(10.0)|float) > tmax.z %}
{% set ns.err = "nudge_z plus nudge_clear_z is above the Z maximum of the printer, the toolhead could not rise clear of the Nudge. Lower nudge_clear_z." %}
{% elif do_z == 1 and src == 'switch' and not has_switch %}
{% set ns.err = "z_source is switch, but [vrtx_z_probe] or z_switch_x / z_switch_y are missing." %}
{% elif do_z == 1 and src == 'switch' and s.safe_z|float <= s.z_search_min|float %}
{% set ns.err = "safe_z must be higher than z_search_min in _VRTX_SETTINGS." %}
{% elif src not in ['nudge', 'switch'] %}
{% set ns.err = "z_source must be auto, nudge or switch." %}
{% elif ns.range != "" %}
{% set ns.err = "The Nudge probe is too close to the edge of the printer for a tool with that offset. " ~ ns.range ~ "Move nudge_x / nudge_y, or the tool offset is wrong." %}
{% endif %}
{% if ns.err %}
_VRTX_ECHO TYPE=warning MSG="{ns.err} Nothing was done."
{% else %}
_VRTX_ECHO MSG="{'Calibration of %s: XY %s, Z %s%s%s. ' % ('all tools' if only == -1 else 'T%d' % only, 'with the Nudge probe' if do_xy == 1 else 'skipped', ('from the ' ~ ('Z switch' if src == 'switch' else 'Nudge probe')) if do_z == 1 else 'skipped', (' at %d C' % temp) if temp > 0 else ' cold', ((', %d runs per tool' % runs) if runs > 1 and do_xy + do_z > 0 else '')) ~ ('' if save == 1 else 'Nothing is saved. ') ~ 'Do not touch the printer.'}"
SAVE_GCODE_STATE NAME=_VRTX
G90
{% if need_nudge %}
_VRTX_CAL_ARM
{% endif %}
{% if s.offset_method|string|lower == 'tool_parameter' %}
SET_GCODE_OFFSET X=0 Y=0 Z=0
{% endif %}
# the reference tool always goes first, the other tools are measured against it
_VRTX_MEASURE T={ref} REFERENCE=1 XY={do_xy} Z={do_z} SRC={src} TEMP={temp} RUNS=1 SAVE={save}
{% for n in printer.toolchanger.tool_numbers %}
{% if n != ref and (only == -1 or only == n) %}
# a tool whose dock is not set up cannot be picked up: leave it out instead of
# stopping the whole run (a tool asked for by TOOL= still stops with the dock error)
{% set dk = printer["gcode_macro _VRTX_DOCK_T" ~ n] %}
{% if only == -1 and dk is defined and dk.taught|default(1)|int != 1 %}
_VRTX_ECHO MSG="{'T%d skipped: its dock position is not set up yet.' % n}"
{% else %}
_VRTX_MEASURE T={n} REFERENCE=0 XY={do_xy} Z={do_z} SRC={src} TEMP={temp} RUNS={runs} SAVE={save}
{% endif %}
{% endif %}
{% endfor %}
# the last measurement lifted to the clear height: nothing watches the Nudge any more
{% if need_nudge %}
_VRTX_CAL_DISARM
{% endif %}
{% if park == 1 %}
# park_after_calibrate: the tool that is mounted goes back into its dock, from the
# clear height, and the carriage returns to where the calibration started
{% if temp > 0 %}
{% for i in range(printer.toolchanger.tool_numbers|length) %}
{% set hn = printer[printer.toolchanger.tool_names[i]].heater %}
{% if hn and printer[hn] is defined %}
SET_TOOL_TEMPERATURE T={printer.toolchanger.tool_numbers[i]} TARGET={printer[hn].target|float}
{% endif %}
{% endfor %}
{% endif %}
UNSELECT_TOOL
RESTORE_GCODE_STATE NAME=_VRTX MOVE=0
# no tool is mounted: no tool offset
SET_GCODE_OFFSET X=0 Y=0 Z=0
_VRTX_CAL_RETURN X={start_x} Y={start_y} Z={start_z}
VRTX_ECHO_OFFSETS
{% else %}
# back to the tool that was mounted (or the reference tool)
_VRTX_SAFE_Z
{% if start_tool == -1 %}
_VRTX_SELECT T={ref}
{% else %}
_VRTX_SELECT T={start_tool}
{% endif %}
# give every heater the target it had when this macro started
{% if temp > 0 %}
{% for i in range(printer.toolchanger.tool_numbers|length) %}
{% set hn = printer[printer.toolchanger.tool_names[i]].heater %}
{% if hn and printer[hn] is defined %}
SET_TOOL_TEMPERATURE T={printer.toolchanger.tool_numbers[i]} TARGET={printer[hn].target|float}
{% endif %}
{% endfor %}
{% endif %}
RESTORE_GCODE_STATE NAME=_VRTX MOVE=0
# gcode_offset mode: the saved state holds the offsets from before, so set the new ones
_VRTX_APPLY_GCODE_OFFSET
VRTX_ECHO_OFFSETS
{% endif %}
{% endif %}
[gcode_macro VRTX_CALIBRATE_XY]
description: Measure only the X and Y offsets with the Nudge probe. TOOL=<n> one tool only, TEMP= nozzle C.
gcode:
VRTX_CALIBRATE XY=1 Z=0 {rawparams}
[gcode_macro VRTX_CALIBRATE_Z]
description: Measure only the Z offsets. TOOL=<n> one tool only, TEMP= nozzle C. Can run on its own, for instance at print start.
gcode:
VRTX_CALIBRATE XY=0 Z=1 {rawparams}
[gcode_macro VRTX_AUTO_Z_OFFSET]
description: Print start helper. Measures the Z offsets of all tools. TEMP= overrides cal_temp. Call it after homing.
gcode:
VRTX_CALIBRATE XY=0 Z=1 {rawparams}
[gcode_macro _VRTX_MEASURE]
description: Mount tool T and measure it (internal). T= REFERENCE=0|1 XY=0|1 Z=0|1 SRC=nudge|switch TEMP= RUNS= SAVE=0|1
gcode:
{% set t = params.T|int %}
{% set ref = params.REFERENCE|int %}
{% set do_xy = params.XY|int %}
{% set do_z = params.Z|int %}
{% set src = params.SRC|string %}
{% set temp = params.TEMP|default(0)|float %}
{% set runs = params.RUNS|default(1)|int %}
{% set save = params.SAVE|default(1)|int %}
{% set ns = namespace(heater="") %}
{% for i in range(printer.toolchanger.tool_numbers|length) %}
{% if printer.toolchanger.tool_numbers[i]|int == t %}
{% set ns.heater = printer[printer.toolchanger.tool_names[i]].heater %}
{% endif %}
{% endfor %}
_VRTX_SAFE_Z
_VRTX_SELECT T={t}
{% if temp > 0 and ns.heater %}
SET_TOOL_TEMPERATURE T={t} TARGET={temp} WAIT=1
{% endif %}
{% if do_xy == 1 or (do_z == 1 and src == 'nudge') %}
{% if ref == 1 %}
_VRTX_NUDGE_GOTO
TOOL_LOCATE_SENSOR
{% else %}
_VRTX_RUNS_CLEAR
{% for r in range(runs) %}
_VRTX_NUDGE_GOTO
TOOL_CALIBRATE_TOOL_OFFSET
_VRTX_RUNS_ADD
{% endfor %}
{% endif %}
_VRTX_NUDGE_STORE T={t} REFERENCE={ref} XY={do_xy} Z={do_z} SRC={src} RUNS={runs} SAVE={save}
{% endif %}
{% if do_z == 1 and src == 'switch' %}
_VRTX_Z_GOTO_SWITCH
_VRTX_Z_COARSE
_VRTX_Z_FINE
_VRTX_Z_STORE T={t} REFERENCE={ref} SAVE={save}
{% endif %}
{% if ref == 1 and do_xy == 0 and not (do_z == 1 and src == 'nudge') %}
_VRTX_REMEMBER_REFERENCE
{% endif %}
[gcode_macro _VRTX_REMEMBER_REFERENCE]
description: Remember the offsets in use of the reference tool (internal)
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set svv = printer.save_variables.variables %}
{% set r = s.reference_tool|int %}
# offset_method tool_parameter: the offsets in use are those of the tool (they can come from
# its [tool] section with nothing saved yet); an axis that is not measured keeps that value
{% set tc_ = printer.toolchanger if printer.toolchanger is defined else {} %}
{% set tp = s.offset_method|string|lower == 'tool_parameter' and r in tc_.tool_numbers|default([]) %}
{% set tool = printer[tc_.tool_names[tc_.tool_numbers.index(r)]] if tp else {} %}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=ref_ox VALUE={tool.gcode_x_offset|float if tp else svv['t' ~ r ~ '_offset_x']|default(0)|float}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=ref_oy VALUE={tool.gcode_y_offset|float if tp else svv['t' ~ r ~ '_offset_y']|default(0)|float}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=ref_oz VALUE={tool.gcode_z_offset|float if tp else svv['t' ~ r ~ '_offset_z']|default(0)|float}
# ---------------------------------------------------------------------------
# Nudge probe
# ---------------------------------------------------------------------------
[gcode_macro _VRTX_NUDGE_CLEAR]
description: Raise to the Nudge clear height unless already higher (internal)
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
# The clear height is nudge_clear_z (10 mm by default) above the nozzle height at the pin,
# or safe_z when that is higher. It is in gcode Z of the mounted tool, like the Nudge moves.
# It never goes down: a position that is higher already is left alone.
{% set clear = [s.safe_z|float, s.nudge_z|float + s.nudge_clear_z|default(10.0)|float]|max %}
{% if printer.gcode_move.gcode_position.z|float < clear %}
G90
G1 Z{clear} F{s.z_speed|float * 60}
{% endif %}
# A macro that fails stops where it is. These three raise the toolhead if the calibration
# stops with the nozzle near the Nudge, so that a move by hand cannot drive into the pin.
[gcode_macro _VRTX_CAL_ARM]
description: Start the calibration watchdog (internal)
gcode:
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=cal_armed VALUE=1
UPDATE_DELAYED_GCODE ID=_VRTX_CAL_WATCHDOG DURATION=1
[gcode_macro _VRTX_CAL_DISARM]
description: Stop the calibration watchdog (internal)
gcode:
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=cal_armed VALUE=0
UPDATE_DELAYED_GCODE ID=_VRTX_CAL_WATCHDOG DURATION=0
# Klipper renders the text of a delayed gcode when its timer fires and only then waits for the
# running command to finish, so the state it reads is the state one second after the arming.
# This text therefore holds no condition: it calls the macro below, which is rendered when it
# runs, after the stopped calibration has released the queue. If the calibration ended normally
# it has disarmed already and the call does nothing.
[delayed_gcode _VRTX_CAL_WATCHDOG]
gcode:
_VRTX_CAL_WATCHDOG_RUN
[gcode_macro _VRTX_CAL_WATCHDOG_RUN]
description: Raise the toolhead clear of the Nudge if a calibration stopped near it (internal)
gcode:
{% if printer["gcode_macro _VRTX_STATE"].cal_armed|int == 1 %}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=cal_armed VALUE=0
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set nudge_set = s.nudge_x|string|lower != 'unset' and s.nudge_y|string|lower != 'unset' and s.nudge_z|string|lower != 'unset' %}
{% if nudge_set and printer.toolhead.homed_axes == "xyz" %}
{% set spread = printer.configfile.settings.tools_calibrate.spread|default(5.0)|float if printer.configfile.settings.tools_calibrate is defined else 5.0 %}
{% set room = spread + s.nudge_margin|default(1.0)|float + 2.0 %}
{% set gp = printer.gcode_move.gcode_position %}
{% if (gp.x|float - s.nudge_x|float)|abs <= room and (gp.y|float - s.nudge_y|float)|abs <= room %}
_VRTX_ECHO TYPE=warning MSG="The calibration stopped near the Nudge probe. Raising to the clear height, so that no move can run into the pin."
_VRTX_NUDGE_CLEAR
{% endif %}
{% endif %}
{% endif %}
[gcode_macro _VRTX_CAL_RETURN]
description: Go back to the position where VRTX_CALIBRATE started, from above (internal). X= Y= Z=
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set tx = params.X|float %}
{% set ty = params.Y|float %}
{% set tz = params.Z|float %}
{% set nudge_set = s.nudge_x|string|lower != 'unset' and s.nudge_y|string|lower != 'unset' and s.nudge_z|string|lower != 'unset' %}
{% set spread = printer.configfile.settings.tools_calibrate.spread|default(5.0)|float if printer.configfile.settings.tools_calibrate is defined else 5.0 %}
{% set room = spread + s.nudge_margin|default(1.0)|float %}
{% set clear = [s.safe_z|float, s.nudge_z|float + s.nudge_clear_z|default(10.0)|float]|max if nudge_set else s.safe_z|float %}
# not back to a spot over the Nudge pin that is lower than the clear height
{% if nudge_set and (tx - s.nudge_x|float)|abs <= room and (ty - s.nudge_y|float)|abs <= room and tz < clear %}
_VRTX_ECHO TYPE=warning MSG="The toolhead did not go back to where it started (X {'%.1f' % tx} Y {'%.1f' % ty} Z {'%.1f' % tz}): that is over the Nudge probe, below the clear height."
{% else %}
{% if nudge_set %}
_VRTX_NUDGE_CLEAR
{% else %}
_VRTX_SAFE_Z
{% endif %}
G90
G1 X{tx} Y{ty} F{s.travel_speed|float * 60}
G1 Z{tz} F{s.z_speed|float * 60}
{% endif %}
[gcode_macro _VRTX_NUDGE_GOTO]
description: Move the mounted nozzle above the Nudge probe, after checking that the probing fits (internal)
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set tc = printer.toolchanger %}
{% set n = tc.tool_number|int %}
{% set tmin = printer.toolhead.axis_minimum %}
{% set tmax = printer.toolhead.axis_maximum %}
{% set tool = printer[tc.tool_names[tc.tool_numbers.index(n)]] %}
{% set svv = printer.save_variables.variables %}
{% set tp = s.offset_method|string|lower == 'tool_parameter' %}
{% set ox = tool.gcode_x_offset|float if tp else svv['t' ~ n ~ '_offset_x']|default(0)|float %}
{% set oy = tool.gcode_y_offset|float if tp else svv['t' ~ n ~ '_offset_y']|default(0)|float %}
{% set oz = tool.gcode_z_offset|float if tp else svv['t' ~ n ~ '_offset_z']|default(0)|float %}
{% set spread = printer.configfile.settings.tools_calibrate.spread|default(5.0)|float if printer.configfile.settings.tools_calibrate is defined else 5.0 %}
{% set room = spread + s.nudge_margin|default(1.0)|float %}
{% set sx = s.nudge_x|float + ox %}
{% set sy = s.nudge_y|float + oy %}
{% set sz = s.nudge_z|float + oz %}
{% if sx - room < tmin.x or sx + room > tmax.x or sy - room < tmin.y or sy + room > tmax.y or sz < tmin.z or sz > tmax.z %}
{ action_raise_error("VRTX: T%d would start the Nudge probing around X %.1f Y %.1f Z %.1f (its offset is X %.2f Y %.2f Z %.2f). That needs %.1f mm free on each side but the printer only allows X %.0f to %.0f, Y %.0f to %.0f, Z %.0f to %.0f. Nothing moved. Move nudge_x / nudge_y away from the edge, or check the offset of T%d." % (n, sx, sy, sz, ox, oy, oz, room, tmin.x, tmax.x, tmin.y, tmax.y, tmin.z, tmax.z, n)) }
{% endif %}
_VRTX_NUDGE_CLEAR
G90
G1 X{s.nudge_x|float} Y{s.nudge_y|float} F{s.travel_speed|float * 60}
G1 Z{s.nudge_z|float} F{s.z_speed|float * 60}
# the pin must be open before the nozzle goes near it
TOOL_CALIBRATE_QUERY_PROBE
_VRTX_NUDGE_PIN_CHECK
[gcode_macro _VRTX_NUDGE_PIN_CHECK]
description: Stop if the Nudge probe already reads triggered (internal)
gcode:
{% if printer.tools_calibrate.calibration_probe_inactive|default(false) %}
{ action_raise_error("VRTX: the Nudge probe reads TRIGGERED before probing. Something presses the pin (a nozzle, filament, a stuck pin), or the pin of [tools_calibrate] has the wrong polarity (add or remove the !). Nothing was probed. VRTX_NUDGE_QUERY shows its state.") }
{% endif %}
# Results of the runs of one tool (RUNS=n)
[gcode_macro _VRTX_RUNS_CLEAR]
description: Forget the results of the previous runs (internal)
gcode:
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=runs_x VALUE="[]"
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=runs_y VALUE="[]"
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=runs_z VALUE="[]"
[gcode_macro _VRTX_RUNS_ADD]
description: Keep the result of the run that just finished (internal)
gcode:
{% set st = printer["gcode_macro _VRTX_STATE"] %}
{% set tc = printer.tools_calibrate %}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=runs_x VALUE="{st.runs_x + [tc.last_result[0]|float]}"
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=runs_y VALUE="{st.runs_y + [tc.last_result[1]|float]}"
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=runs_z VALUE="{st.runs_z + [tc.last_result[2]|float]}"
[gcode_macro _VRTX_NUDGE_STORE]
description: Turn the Nudge measurement into the offsets of tool T and lift off (internal). T= REFERENCE=0|1 XY=0|1 Z=0|1 SRC=nudge|switch RUNS= SAVE=0|1
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set st = printer["gcode_macro _VRTX_STATE"] %}
{% set svv = printer.save_variables.variables %}
{% set tc = printer.tools_calibrate %}
{% set t = params.T|int %}
{% set do_xy = params.XY|int %}
{% set use_z = params.Z|int == 1 and params.SRC|string == 'nudge' %}
{% set runs = params.RUNS|default(1)|int %}
{% set save = params.SAVE|default(1)|int %}
{% set lim = s.max_offset|float %}
{% set limx = s.max_offset_x|default(0)|float if s.max_offset_x|default(0)|float > 0 else lim %}
{% set limy = s.max_offset_y|default(0)|float if s.max_offset_y|default(0)|float > 0 else lim %}
{% set limz = s.max_offset_z|default(0)|float if s.max_offset_z|default(0)|float > 0 else lim %}
# offset_method tool_parameter: the offsets in use are those of the tool (they can come from
# its [tool] section with nothing saved yet); an axis that is not measured keeps that value
{% set tc_ = printer.toolchanger if printer.toolchanger is defined else {} %}
{% set tp = s.offset_method|string|lower == 'tool_parameter' and t in tc_.tool_numbers|default([]) %}
{% set tool = printer[tc_.tool_names[tc_.tool_numbers.index(t)]] if tp else {} %}
{% set ox = tool.gcode_x_offset|float if tp else svv['t' ~ t ~ '_offset_x']|default(0)|float %}
{% set oy = tool.gcode_y_offset|float if tp else svv['t' ~ t ~ '_offset_y']|default(0)|float %}
{% set oz = tool.gcode_z_offset|float if tp else svv['t' ~ t ~ '_offset_z']|default(0)|float %}
{% if params.REFERENCE|int == 1 %}
# the reference tool is the base: its own offsets are the zero point
_VRTX_REMEMBER_REFERENCE
_VRTX_ECHO MSG="{'T%d (reference) located the Nudge probe at X %.3f Y %.3f Z %.3f.' % (t, tc.last_result[0]|float, tc.last_result[1]|float, tc.last_result[2]|float)}"
{% else %}
{% set many = runs > 1 and st.runs_x|length > 1 %}
{% set rx = (st.runs_x|sum / st.runs_x|length) if many else tc.last_result[0]|float %}
{% set ry = (st.runs_y|sum / st.runs_y|length) if many else tc.last_result[1]|float %}
{% set rz = (st.runs_z|sum / st.runs_z|length) if many else tc.last_result[2]|float %}
{% set nx = (st.ref_ox|float + rx)|round(3) if do_xy == 1 else ox %}
{% set ny = (st.ref_oy|float + ry)|round(3) if do_xy == 1 else oy %}
{% set nz = (st.ref_oz|float + rz)|round(3) if use_z else oz %}
{% if many %}
{% set sprx = st.runs_x|max - st.runs_x|min %}
{% set spry = st.runs_y|max - st.runs_y|min %}
{% set sprz = st.runs_z|max - st.runs_z|min %}
_VRTX_ECHO MSG="{'T%d, %d runs: mean X %.3f Y %.3f Z %.3f, spread (largest minus smallest) X %.4f Y %.4f Z %.4f mm.' % (t, st.runs_x|length, rx, ry, rz, sprx, spry, sprz)}"
{% set w = s.run_range_warning|default(0.05)|float %}
{% if (do_xy == 1 and (sprx > w or spry > w)) or (use_z and sprz > w) %}
_VRTX_ECHO TYPE=warning MSG="{'T%d: the runs differ by more than %.3f mm. The tool may be seated badly, or the nozzle or the pin is dirty.' % (t, w)}"
{% endif %}
{% endif %}
{% if nx|abs > limx or ny|abs > limy or nz|abs > limz %}
_VRTX_ECHO TYPE=warning MSG="{'T%d measured X %.3f Y %.3f Z %.3f. That is more than max_offset (X %.1f, Y %.1f, Z %.1f mm) from the reference tool, so it was not saved. Is the tool seated and the nozzle clean?' % (t, nx, ny, nz, limx, limy, limz)}"
{% elif save != 1 %}
_VRTX_ECHO MSG="{'T%d measured X %.3f Y %.3f Z %.3f (in use: X %.3f Y %.3f Z %.3f). Nothing was saved (SAVE=0).' % (t, nx, ny, nz, ox, oy, oz)}"
{% else %}
_VRTX_STORE_OFFSET T={t} X={nx} Y={ny} Z={nz}
_VRTX_APPLY_GCODE_OFFSET
_VRTX_ECHO MSG="{'T%d offset saved: X %.3f Y %.3f Z %.3f (was X %.3f Y %.3f Z %.3f).' % (t, nx, ny, nz, ox, oy, oz)}"
{% endif %}
{% endif %}
# lift off with the offsets Klipper has now
_VRTX_NUDGE_LIFT
[gcode_macro _VRTX_NUDGE_LIFT]
description: Lift the nozzle from the Nudge probe to the Nudge clear height (internal)
gcode:
_VRTX_NUDGE_CLEAR
[gcode_macro VRTX_NUDGE_QUERY]
description: Is the Nudge probe triggered? Press it by hand and run this to test the wiring.
gcode:
TOOL_CALIBRATE_QUERY_PROBE
# ---------------------------------------------------------------------------
# Z switch (optional)
# ---------------------------------------------------------------------------
[gcode_macro _VRTX_Z_GOTO_SWITCH]
description: Move the mounted nozzle above the Z switch (internal)
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
_VRTX_SAFE_Z
G90
G1 X{s.z_switch_x|float} Y{s.z_switch_y|float} F{s.travel_speed|float * 60}
G1 Z{s.safe_z|float} F{s.z_speed|float * 60}
[gcode_macro _VRTX_Z_COARSE]
description: Fast single search for the switch from safe_z downwards (internal)
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
# how far the toolhead can go down between here and z_search_min (machine Z)
{% set travel = printer.toolhead.position.z|float - s.z_search_min|float %}
VRTX_Z_PROBE SAMPLES=1 PROBE_SPEED={s.z_coarse_speed|float} MAX_TRAVEL={travel}
[gcode_macro _VRTX_Z_FINE]
description: Lift a little and measure the switch with the configured samples (internal)
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
G91
G1 Z{s.z_coarse_lift|float} F{s.z_speed|float * 60}
G90
VRTX_Z_PROBE MAX_TRAVEL={s.z_coarse_lift|float * 2 + 1}
[gcode_macro _VRTX_Z_STORE]
description: Turn the measured trigger height into the Z offset of tool T (internal). T= REFERENCE=0|1 SAVE=0|1
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set st = printer["gcode_macro _VRTX_STATE"] %}
{% set svv = printer.save_variables.variables %}
{% set t = params.T|int %}
{% set z = printer.vrtx_z_probe.last_z_result|float %}
{% set rng = printer.vrtx_z_probe.last_range|float %}
# offset_method tool_parameter: the offsets in use are those of the tool (they can come from
# its [tool] section with nothing saved yet); an axis that is not measured keeps that value
{% set tc_ = printer.toolchanger if printer.toolchanger is defined else {} %}
{% set tp = s.offset_method|string|lower == 'tool_parameter' and t in tc_.tool_numbers|default([]) %}
{% set tool = printer[tc_.tool_names[tc_.tool_numbers.index(t)]] if tp else {} %}
{% set ox = tool.gcode_x_offset|float if tp else svv['t' ~ t ~ '_offset_x']|default(0)|float %}
{% set oy = tool.gcode_y_offset|float if tp else svv['t' ~ t ~ '_offset_y']|default(0)|float %}
{% set oz = tool.gcode_z_offset|float if tp else svv['t' ~ t ~ '_offset_z']|default(0)|float %}
{% set limit = s.max_offset_z|default(0)|float if s.max_offset_z|default(0)|float > 0 else s.max_offset|float %}
{% set save = params.SAVE|default(1)|int %}
# lift off the switch first, with the offsets Klipper still has
G91
G1 Z{s.z_coarse_lift|float} F{s.z_speed|float * 60}
G90
{% if rng > s.z_range_warning|float %}
_VRTX_ECHO TYPE=warning MSG="T{t}: the switch samples spread {'%.4f' % rng} mm, more than {'%.4f' % s.z_range_warning|float}. Clean the nozzle and the switch."
{% endif %}
{% if params.REFERENCE|int == 1 %}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=z_ref VALUE={z}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=z_ref_offset VALUE={oz}
_VRTX_REMEMBER_REFERENCE
_VRTX_ECHO MSG="T{t} (reference) triggers the switch at Z {'%.4f' % z}."
{% else %}
{% set new_z = (st.z_ref_offset|float + z - st.z_ref|float)|round(3) %}
{% if new_z|abs > limit %}
_VRTX_ECHO TYPE=warning MSG="{'T%d: Z offset %.3f is more than max_offset (%.1f mm) from the reference tool, so it was not saved. Is the tool seated and the nozzle clean?' % (t, new_z, limit)}"
{% elif save != 1 %}
_VRTX_ECHO MSG="T{t} triggers at Z {'%.4f' % z}, Z offset {'%.3f' % new_z} (saved: {'%.3f' % oz}). Nothing was saved (SAVE=0)."
{% else %}
_VRTX_STORE_OFFSET T={t} X={ox} Y={oy} Z={new_z}
_VRTX_APPLY_GCODE_OFFSET
_VRTX_ECHO MSG="T{t} triggers at Z {'%.4f' % z}, Z offset {'%.3f' % new_z} (was {'%.3f' % oz})."
{% endif %}
{% endif %}
# ---------------------------------------------------------------------------
# Z reference tool
# ---------------------------------------------------------------------------
# Every Z offset is relative to the reference tool (T0). That is right as long as
# Z comes from something that does not care which nozzle is mounted, such as a
# Beacon or Cartographer scan. A touch or tap with the NOZZLE, with any tool
# mounted, sets Z=0 at the nozzle of THAT tool, so this tool's Z offset would be
# counted twice. Run this once right after such a homing: Z is shifted by the
# Z offset of the mounted tool and then means the same as it does for the
# reference tool, whichever tool made the touch. After a scan homing, or after a
# touch with the reference tool, nothing is needed.
[gcode_macro VRTX_Z_REFERENCE]
description: Z was just homed by a touch with the nozzle of the mounted tool. Shifts Z to the frame of the reference tool. Run it once, right after that homing.
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set n = printer.toolchanger.tool_number|int if printer.toolchanger is defined else -1 %}
{% set svv = printer.save_variables.variables if printer.save_variables is defined else {} %}
{% if n < 0 %}
_VRTX_ECHO TYPE=warning MSG="No tool is mounted, nothing was shifted."
{% elif "z" not in printer.toolhead.homed_axes %}
_VRTX_ECHO TYPE=warning MSG="Z is not homed, nothing was shifted."
{% elif s.offset_method|string|lower != 'tool_parameter' and printer.gcode.commands['SET_KINEMATIC_POSITION'] is not defined %}
_VRTX_ECHO TYPE=warning MSG="With offset_method gcode_offset this needs SET_KINEMATIC_POSITION: add [force_move] enable_force_move: True to printer.cfg. Nothing was shifted."
{% elif n == s.reference_tool|int %}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=z_frame VALUE=-1
_VRTX_ECHO MSG="{'T%d is the reference tool, Z needs no shift.' % n}"
{% else %}
{% set tname = printer.toolchanger.tool_names[printer.toolchanger.tool_numbers.index(n)] %}
{% set oz = printer[tname].gcode_z_offset|float if s.offset_method|string|lower == 'tool_parameter' else svv['t' ~ n ~ '_offset_z']|default(0)|float %}
{% if s.offset_method|string|lower == 'tool_parameter' %}
ADJUST_Z_AFTER_TOOL_NOZZLE_HOME
{% else %}
SET_KINEMATIC_POSITION Z={printer.toolhead.position.z|float + oz}
{% endif %}
SET_GCODE_VARIABLE MACRO=_VRTX_STATE VARIABLE=z_frame VALUE={n}
_VRTX_ECHO MSG="{'Z was set with the nozzle of T%d, shifted by its Z offset (%.3f mm) to the frame of T%d.' % (n, oz, s.reference_tool|int)}"
{% endif %}
# ---------------------------------------------------------------------------
# Check
# ---------------------------------------------------------------------------
[gcode_macro VRTX_CHECK]
description: Check that everything the VRTX macros need is in place
gcode:
{% set s = printer["gcode_macro _VRTX_SETTINGS"] %}
{% set ns = namespace(problems=0) %}
{% if printer.toolchanger is not defined %}
{ action_respond_info("VRTX CHECK: FAIL [toolchanger] section (klipper-toolchanger) is missing.") }
{% set ns.problems = ns.problems + 1 %}
{% else %}
{ action_respond_info("VRTX CHECK: ok toolchanger with %d tools, reference tool T%d." % (printer.toolchanger.tool_numbers|length, s.reference_tool|int)) }
{% if s.reference_tool|int not in printer.toolchanger.tool_numbers %}
{ action_respond_info("VRTX CHECK: FAIL reference tool does not exist.") }
{% set ns.problems = ns.problems + 1 %}
{% endif %}
{% endif %}
{% if printer.toolchanger is defined %}
{% for n in printer.toolchanger.tool_numbers %}
{% set tool = printer[printer.toolchanger.tool_names[loop.index0]] %}
{% if not tool.heater %}
{ action_respond_info("VRTX CHECK: note T%d has no heater (no extruder: or heater: in its [tool] section)." % n) }
{% elif printer[tool.heater] is not defined %}
{ action_respond_info("VRTX CHECK: FAIL T%d heater '%s' does not exist." % (n, tool.heater)) }
{% set ns.problems = ns.problems + 1 %}
{% endif %}
{% set cs = printer.configfile.settings %}
{% set tcfg = cs[printer.toolchanger.tool_names[loop.index0]|lower] if cs[printer.toolchanger.tool_names[loop.index0]|lower] is defined else {} %}
{% set tcc = cs['toolchanger'] if cs['toolchanger'] is defined else {} %}
{% for opt in ['pickup_gcode', 'dropoff_gcode'] %}
{% set body = (tcfg[opt] if tcfg[opt] is defined else (tcc[opt] if tcc[opt] is defined else ''))|string|trim %}
{% if body == '' %}
{ action_respond_info("VRTX CHECK: WARN T%d has no %s. Tool changes will not move the toolhead to the dock, VRTX relies on it." % (n, opt)) }
{% endif %}
{% endfor %}
{% if printer["gcode_macro _VRTX_TOOLS_SETTINGS"] is defined and printer.gcode.commands['T' ~ n] is defined %}
{% if not (printer.gcode.commands['T' ~ n].help|string|lower).startswith('vrtx:') %}
{ action_respond_info("VRTX CHECK: FAIL T%d is not the VRTX tool macro. Include core/vrtx_tools.cfg (or core/vrtx_tools_core.cfg and core/t_commands/T<n>.cfg) before the [tool] sections." % n) }
{% set ns.problems = ns.problems + 1 %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% if printer.toolchanger is defined and printer["gcode_macro _VRTX_TOOLS_SETTINGS"] is defined %}
{% set nx = namespace(extra=[]) %}
{% for i in range(10) %}
{% if printer["gcode_macro T" ~ i] is defined and i not in printer.toolchanger.tool_numbers %}
{% set nx.extra = nx.extra + ["T%d" % i] %}
{% endif %}
{% endfor %}
{% if nx.extra|length > 0 %}
{ action_respond_info("VRTX CHECK: note T commands without a tool: %s. Mainsail shows a tool button for each of them. To load only yours, replace [include core/vrtx_tools.cfg] with [include core/vrtx_tools_core.cfg] and [include core/t_commands/T[0-%d].cfg] (section 1 of vrtx.cfg)." % (nx.extra|join(", "), printer.toolchanger.tool_numbers|max)) }
{% endif %}
{% endif %}
{% if printer.save_variables is not defined %}
{ action_respond_info("VRTX CHECK: FAIL [save_variables] section is missing.") }
{% set ns.problems = ns.problems + 1 %}
{% endif %}
{% if s.offset_method|string|lower not in ['tool_parameter', 'gcode_offset'] %}
{ action_respond_info("VRTX CHECK: FAIL offset_method must be tool_parameter or gcode_offset.") }
{% set ns.problems = ns.problems + 1 %}
{% else %}
{ action_respond_info("VRTX CHECK: ok offset_method %s." % s.offset_method) }
{% endif %}
{% if s.calibration|string|lower not in ['nudge', 'camera'] %}
{ action_respond_info("VRTX CHECK: FAIL calibration must be nudge or camera.") }
{% set ns.problems = ns.problems + 1 %}
{% elif s.calibration|string|lower == 'camera' %}
{ action_respond_info("VRTX CHECK: ok calibration method: camera (X and Y by eye, Z with the Z switch).") }
{% if printer["gcode_macro _VRTX_CAMERA_SETTINGS"] is not defined %}
{ action_respond_info("VRTX CHECK: FAIL the calibration method is camera, but core/vrtx_camera.cfg is not included (section 1 of vrtx.cfg).") }
{% set ns.problems = ns.problems + 1 %}
{% endif %}
{% if printer.vrtx_z_probe is defined and s.z_switch_x|string|lower != 'unset' and s.z_switch_y|string|lower != 'unset' %}
{ action_respond_info("VRTX CHECK: ok Z switch at X %.2f Y %.2f." % (s.z_switch_x|float, s.z_switch_y|float)) }
{% elif printer.vrtx_z_probe is defined %}
{ action_respond_info("VRTX CHECK: FAIL [vrtx_z_probe] exists, but z_switch_x / z_switch_y in _VRTX_SETTINGS are not set.") }
{% set ns.problems = ns.problems + 1 %}
{% else %}
{ action_respond_info("VRTX CHECK: note no Z switch ([vrtx_z_probe], section 3): only X and Y are aligned, the Z offsets are not measured.") }
{% endif %}
{% if printer["gcode_macro _VRTX_CAMERA_SETTINGS"] is defined %}
{% set cs = printer["gcode_macro _VRTX_CAMERA_SETTINGS"] %}
{ action_respond_info("VRTX CHECK: ok the camera alignment starts at X %.2f Y %.2f, focus Z %.2f." % (cs.camera_x|float, cs.camera_y|float, cs.focus_z|float)) }
{% set cv = printer.save_variables.variables if printer.save_variables is defined else {} %}
{% if cv.vrtx_ref_x is defined %}
{ action_respond_info("VRTX CHECK: ok camera reference saved (X %.3f Y %.3f focus Z %.3f)." % (cv.vrtx_ref_x|float, cv.vrtx_ref_y|float, cv.vrtx_ref_z|float)) }
{% else %}
{ action_respond_info("VRTX CHECK: todo no camera reference yet: VRTX_START, centre the reference tool, VRTX_SET_TOOL.") }
{% endif %}
{% if printer["neopixel vrtx_led"] is defined %}
{ action_respond_info("VRTX CHECK: ok LED ring found.") }
{% else %}
{ action_respond_info("VRTX CHECK: note no [neopixel vrtx_led], the LED is not controlled.") }
{% endif %}
{% endif %}
{% else %}
{ action_respond_info("VRTX CHECK: ok calibration method: Nudge.") }
{% if printer["gcode_macro _VRTX_CAMERA_SETTINGS"] is defined %}
{ action_respond_info("VRTX CHECK: note core/vrtx_camera.cfg is included, but the calibration method is nudge. Its macros do nothing useful; take the include out (section 1).") }
{% endif %}
{% if printer.tools_calibrate is not defined %}
{ action_respond_info("VRTX CHECK: FAIL [tools_calibrate] section (the Nudge probe) is missing, XY calibration is not possible.") }
{% set ns.problems = ns.problems + 1 %}
{% else %}
{ action_respond_info("VRTX CHECK: ok [tools_calibrate] found.") }
{% endif %}
{% if s.nudge_x|string|lower == 'unset' or s.nudge_y|string|lower == 'unset' or s.nudge_z|string|lower == 'unset' %}
{ action_respond_info("VRTX CHECK: FAIL nudge_x, nudge_y and nudge_z in _VRTX_SETTINGS are not set.") }
{% set ns.problems = ns.problems + 1 %}
{% else %}
{ action_respond_info("VRTX CHECK: ok Nudge probe position X %.2f Y %.2f Z %.2f." % (s.nudge_x|float, s.nudge_y|float, s.nudge_z|float)) }
{% endif %}
{% if printer.vrtx_z_probe is defined and s.z_switch_x|string|lower != 'unset' and s.z_switch_y|string|lower != 'unset' %}
{ action_respond_info("VRTX CHECK: ok Z switch at X %.2f Y %.2f, z_source %s." % (s.z_switch_x|float, s.z_switch_y|float, s.z_source)) }
{% elif s.z_source|string|lower == 'switch' %}
{ action_respond_info("VRTX CHECK: FAIL z_source is switch, but [vrtx_z_probe] or z_switch_x / z_switch_y are missing.") }
{% set ns.problems = ns.problems + 1 %}
{% else %}
{ action_respond_info("VRTX CHECK: note no separate Z switch, Z comes from the Nudge probe.") }
{% endif %}
{% endif %}
{% if printer.toolchanger is defined and printer["gcode_macro VRTX_DOCK_CHECK"] is defined %}
{ action_respond_info("VRTX CHECK: docking, see the VRTX DOCK CHECK lines below.") }
{% elif printer.toolchanger is defined %}
{ action_respond_info("VRTX CHECK: note vrtx_dock.cfg not included, the docking must come from your changer's own macros.") }
{% endif %}
{% if printer.toolchanger is defined and printer["gcode_macro _VRTX_TOOLS_SETTINGS"] is defined %}
{% set ts = printer["gcode_macro _VRTX_TOOLS_SETTINGS"] %}
{% set cs = printer.configfile.settings %}
{% set tcc = cs['toolchanger'] if cs['toolchanger'] is defined else {} %}
{% set errbody = tcc.error_gcode|default('')|string|trim %}
{% set befbody = tcc.before_change_gcode|default('')|string|lower %}
{% set aftbody = tcc.after_change_gcode|default('')|string|lower %}
{% if ts.pause_on_error|default(True) %}
{% if printer.pause_resume is not defined %}
{ action_respond_info("VRTX CHECK: WARN pause_on_error is on, but there is no [pause_resume] section, so a print cannot be paused. Add [pause_resume] to printer.cfg.") }
{% else %}
{ action_respond_info("VRTX CHECK: ok a failed tool change during a print pauses it (VRTX_RESUME carries on).") }
{% set rcfg = printer.configfile.settings['gcode_macro resume'] if printer.configfile.settings['gcode_macro resume'] is defined else none %}
{% set ccfg = printer.configfile.settings['gcode_macro _client_variable'] if printer.configfile.settings['gcode_macro _client_variable'] is defined else none %}
# user_resume_macro is VRTX_RESUME_GUARD itself, or a hook macro of your own that calls it
{% set umac = ccfg.variable_user_resume_macro|default('')|string|replace('"', '')|replace("'", '')|trim|lower if ccfg is not none else '' %}
{% set ucfg = printer.configfile.settings['gcode_macro ' ~ umac] if umac != '' and printer.configfile.settings['gcode_macro ' ~ umac] is defined else none %}
{% set via_client = ccfg is not none and ('vrtx_resume_guard' in umac or (ucfg is not none and 'vrtx_resume_guard' in ucfg.gcode|default('')|string|lower)) %}
{% set rbody = rcfg.gcode|default('')|string|lower if rcfg is not none else '' %}
{% set rename = rcfg.rename_existing|default('')|string|upper if rcfg is not none else '' %}
{% if via_client %}
{ action_respond_info("VRTX CHECK: ok the Resume button is guarded (user_resume_macro of the Mainsail / Fluidd client macros).") }
{% elif ccfg is not none and rename == 'VRTX_BASE_RESUME' %}
{ action_respond_info("VRTX CHECK: WARN core/vrtx_resume_guard.cfg replaces the RESUME of your Mainsail / Fluidd client macros (same section name, the last one wins), so their resume steps are lost. Remove that include and set variable_user_resume_macro: \"VRTX_RESUME_GUARD\" in [gcode_macro _CLIENT_VARIABLE] instead (docs/docking.md).") }
{% elif rcfg is none %}
{ action_respond_info("VRTX CHECK: note the plain RESUME (the Resume button) is not guarded: after a failed tool change it would continue with the wrong tool. With Mainsail or Fluidd set variable_user_resume_macro: \"VRTX_RESUME_GUARD\" in [gcode_macro _CLIENT_VARIABLE]; otherwise include core/vrtx_resume_guard.cfg (docs/docking.md).") }
{% elif 'vrtx_resume_guard' not in rbody %}
{ action_respond_info("VRTX CHECK: WARN your RESUME macro does not call VRTX_RESUME_GUARD, so the Resume button continues with the wrong tool after a failed tool change. Put VRTX_RESUME_GUARD on its first line, or with Mainsail / Fluidd use user_resume_macro (docs/docking.md).") }
{% else %}
{ action_respond_info("VRTX CHECK: ok RESUME is guarded while a failed tool change is not repaired.") }
{% endif %}
{% endif %}
{% if errbody == '' %}
{ action_respond_info("VRTX CHECK: note [toolchanger] has no error_gcode. Failures found by VRTX's own docking pause the print anyway. Errors raised by the changer itself (a tool that does not detect, a failed lock) can only be reported by error_gcode and may still stop the print. Add error_gcode: VRTX_TOOLCHANGE_ERROR so they are reported and the print is paused where Klipper allows it (section 4).") }
{% endif %}
{% endif %}
{% if ts.prime_macro|default('')|string|trim != '' %}
{% if printer.gcode.commands[ts.prime_macro|string|trim|upper] is not defined %}
{ action_respond_info("VRTX CHECK: WARN prime_macro " ~ ts.prime_macro ~ " is not a macro of this printer.") }
{% else %}
{ action_respond_info("VRTX CHECK: ok prime_macro " ~ ts.prime_macro ~ " does the prime (T= LENGTH= SPEED=).") }
{% endif %}
{% elif ts.first_prime|default(0)|float > 0 %}
{ action_respond_info("VRTX CHECK: WARN first_prime is set, but it only works together with prime_macro (a purge over a brush). It is ignored.") }
{% endif %}
{% if ts.prime_macro|default('')|string|trim != '' and ts.first_prime|default(0)|float > 0 %}
# PRINT_START and START_PRINT both looked at: one is often only an alias of the other
{% set cs = printer.configfile.settings %}
{% set psc = [cs['gcode_macro print_start'] if cs['gcode_macro print_start'] is defined else none, cs['gcode_macro start_print'] if cs['gcode_macro start_print'] is defined else none]|select|list %}
{% set psbody = psc|map(attribute='gcode')|map('string')|join(' ')|lower %}
{% if psc|length > 0 and 'vrtx_primed_reset' not in psbody %}
{ action_respond_info("VRTX CHECK: note first_prime is set and your PRINT_START does not call VRTX_PRIMED_RESET. The first-prime list is also cleared at the end of a print (VRTX_TOOLS_OFF) and when a print starts with a shorter duration, but calling it at the start is the sure way.") }
{% endif %}
{% endif %}
{% if ts.retract|default(0)|float > 0 %}
{% if 'vrtx_before_change' not in befbody or 'vrtx_after_change' not in aftbody %}
{ action_respond_info("VRTX CHECK: WARN retract is set, but before_change_gcode / after_change_gcode of [toolchanger] do not call VRTX_BEFORE_CHANGE / VRTX_AFTER_CHANGE. Nothing is retracted or primed (section 4).") }
{% else %}
{ action_respond_info("VRTX CHECK: ok shared extruder retract %.1f mm. Set the slicer's tool change retraction to 0." % ts.retract|float) }
{% endif %}
{% endif %}
{% set motor = ts.motor|default('')|string|trim %}
{% if motor != '' %}
{% if printer['extruder_stepper ' ~ motor] is not defined and printer[motor] is not defined %}
{ action_respond_info("VRTX CHECK: FAIL motor '%s' (_VRTX_TOOLS_SETTINGS) is neither an [extruder_stepper] nor an [extruder]." % motor) }
{% set ns.problems = ns.problems + 1 %}
{% elif 'vrtx_after_change' not in aftbody %}