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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
|
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* motion.cpp
*/
#include "motion.h"
#include "endstops.h"
#include "stepper.h"
#include "planner.h"
#include "temperature.h"
#include "../gcode/gcode.h"
#include "../inc/MarlinConfig.h"
#if IS_SCARA
#include "../libs/buzzer.h"
#include "../lcd/marlinui.h"
#endif
#if HAS_BED_PROBE
#include "probe.h"
#endif
#if HAS_LEVELING
#include "../feature/bedlevel/bedlevel.h"
#endif
#if ENABLED(BLTOUCH)
#include "../feature/bltouch.h"
#endif
#if HAS_DISPLAY
#include "../lcd/marlinui.h"
#endif
#if HAS_FILAMENT_SENSOR
#include "../feature/runout.h"
#endif
#if ENABLED(SENSORLESS_HOMING)
#include "../feature/tmc_util.h"
#endif
#if ENABLED(FWRETRACT)
#include "../feature/fwretract.h"
#endif
#if ENABLED(BABYSTEP_DISPLAY_TOTAL)
#include "../feature/babystep.h"
#endif
#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
#include "../core/debug_out.h"
/**
* axis_homed
* Flags that each linear axis was homed.
* XYZ on cartesian, ABC on delta, ABZ on SCARA.
*
* axis_trusted
* Flags that the position is trusted in each linear axis. Set when homed.
* Cleared whenever a stepper powers off, potentially losing its position.
*/
uint8_t axis_homed, axis_trusted; // = 0
// Relative Mode. Enable with G91, disable with G90.
bool relative_mode; // = false;
/**
* Cartesian Current Position
* Used to track the native machine position as moves are queued.
* Used by 'line_to_current_position' to do a move after changing it.
* Used by 'sync_plan_position' to update 'planner.position'.
*/
xyze_pos_t current_position = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
/**
* Cartesian Destination
* The destination for a move, filled in by G-code movement commands,
* and expected by functions like 'prepare_line_to_destination'.
* G-codes can set destination using 'get_destination_from_command'
*/
xyze_pos_t destination; // {0}
// G60/G61 Position Save and Return
#if SAVED_POSITIONS
uint8_t saved_slots[(SAVED_POSITIONS + 7) >> 3];
xyz_pos_t stored_position[SAVED_POSITIONS];
#endif
// The active extruder (tool). Set with T<extruder> command.
#if HAS_MULTI_EXTRUDER
uint8_t active_extruder = 0; // = 0
#endif
#if ENABLED(LCD_SHOW_E_TOTAL)
float e_move_accumulator; // = 0
#endif
// Extruder offsets
#if HAS_HOTEND_OFFSET
xyz_pos_t hotend_offset[HOTENDS]; // Initialized by settings.load()
void reset_hotend_offsets() {
constexpr float tmp[XYZ][HOTENDS] = { HOTEND_OFFSET_X, HOTEND_OFFSET_Y, HOTEND_OFFSET_Z };
static_assert(
!tmp[X_AXIS][0] && !tmp[Y_AXIS][0] && !tmp[Z_AXIS][0],
"Offsets for the first hotend must be 0.0."
);
// Transpose from [XYZ][HOTENDS] to [HOTENDS][XYZ]
HOTEND_LOOP() LOOP_XYZ(a) hotend_offset[e][a] = tmp[a][e];
#if ENABLED(DUAL_X_CARRIAGE)
hotend_offset[1].x = _MAX(X2_HOME_POS, X2_MAX_POS);
#endif
}
#endif
// The feedrate for the current move, often used as the default if
// no other feedrate is specified. Overridden for special moves.
// Set by the last G0 through G5 command's "F" parameter.
// Functions that override this for custom moves *must always* restore it!
feedRate_t feedrate_mm_s = MMM_TO_MMS(1500);
int16_t feedrate_percentage = 100;
// Cartesian conversion result goes here:
xyz_pos_t cartes;
#if IS_KINEMATIC
abc_pos_t delta;
#if HAS_SCARA_OFFSET
abc_pos_t scara_home_offset;
#endif
#if HAS_SOFTWARE_ENDSTOPS
float delta_max_radius, delta_max_radius_2;
#elif IS_SCARA
constexpr float delta_max_radius = SCARA_PRINTABLE_RADIUS,
delta_max_radius_2 = sq(SCARA_PRINTABLE_RADIUS);
#else // DELTA
constexpr float delta_max_radius = DELTA_PRINTABLE_RADIUS,
delta_max_radius_2 = sq(DELTA_PRINTABLE_RADIUS);
#endif
#endif
/**
* The workspace can be offset by some commands, or
* these offsets may be omitted to save on computation.
*/
#if HAS_POSITION_SHIFT
// The distance that XYZ has been offset by G92. Reset by G28.
xyz_pos_t position_shift{0};
#endif
#if HAS_HOME_OFFSET
// This offset is added to the configured home position.
// Set by M206, M428, or menu item. Saved to EEPROM.
xyz_pos_t home_offset{0};
#endif
#if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
// The above two are combined to save on computes
xyz_pos_t workspace_offset{0};
#endif
#if HAS_ABL_NOT_UBL
feedRate_t xy_probe_feedrate_mm_s = MMM_TO_MMS(XY_PROBE_SPEED);
#endif
/**
* Output the current position to serial
*/
inline void report_more_positions() {
stepper.report_positions();
TERN_(IS_SCARA, scara_report_positions());
}
// Report the logical position for a given machine position
inline void report_logical_position(const xyze_pos_t &rpos) {
const xyze_pos_t lpos = rpos.asLogical();
SERIAL_ECHOPAIR_P(X_LBL, lpos.x, SP_Y_LBL, lpos.y, SP_Z_LBL, lpos.z, SP_E_LBL, lpos.e);
}
// Report the real current position according to the steppers.
// Forward kinematics and un-leveling are applied.
void report_real_position() {
get_cartesian_from_steppers();
xyze_pos_t npos = cartes;
npos.e = planner.get_axis_position_mm(E_AXIS);
#if HAS_POSITION_MODIFIERS
planner.unapply_modifiers(npos, true);
#endif
report_logical_position(npos);
report_more_positions();
}
// Report the logical current position according to the most recent G-code command
void report_current_position() {
report_logical_position(current_position);
report_more_positions();
}
/**
* Report the logical current position according to the most recent G-code command.
* The planner.position always corresponds to the last G-code too. This makes M114
* suitable for debugging kinematics and leveling while avoiding planner sync that
* definitively interrupts the printing flow.
*/
void report_current_position_projected() {
report_logical_position(current_position);
stepper.report_a_position(planner.position);
}
/**
* Run out the planner buffer and re-sync the current
* position from the last-updated stepper positions.
*/
void quickstop_stepper() {
planner.quick_stop();
planner.synchronize();
set_current_from_steppers_for_axis(ALL_AXES);
sync_plan_position();
}
/**
* Set the planner/stepper positions directly from current_position with
* no kinematic translation. Used for homing axes and cartesian/core syncing.
*/
void sync_plan_position() {
if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position", current_position);
planner.set_position_mm(current_position);
}
void sync_plan_position_e() { planner.set_e_position_mm(current_position.e); }
/**
* Get the stepper positions in the cartes[] array.
* Forward kinematics are applied for DELTA and SCARA.
*
* The result is in the current coordinate space with
* leveling applied. The coordinates need to be run through
* unapply_leveling to obtain the "ideal" coordinates
* suitable for current_position, etc.
*/
void get_cartesian_from_steppers() {
#if ENABLED(DELTA)
forward_kinematics_DELTA(planner.get_axis_positions_mm());
#else
#if IS_SCARA
forward_kinematics_SCARA(
planner.get_axis_position_degrees(A_AXIS),
planner.get_axis_position_degrees(B_AXIS)
);
#else
cartes.set(planner.get_axis_position_mm(X_AXIS), planner.get_axis_position_mm(Y_AXIS));
#endif
cartes.z = planner.get_axis_position_mm(Z_AXIS);
#endif
}
/**
* Set the current_position for an axis based on
* the stepper positions, removing any leveling that
* may have been applied.
*
* To prevent small shifts in axis position always call
* sync_plan_position after updating axes with this.
*
* To keep hosts in sync, always call report_current_position
* after updating the current_position.
*/
void set_current_from_steppers_for_axis(const AxisEnum axis) {
get_cartesian_from_steppers();
xyze_pos_t pos = cartes;
pos.e = planner.get_axis_position_mm(E_AXIS);
#if HAS_POSITION_MODIFIERS
planner.unapply_modifiers(pos, true);
#endif
if (axis == ALL_AXES)
current_position = pos;
else
current_position[axis] = pos[axis];
}
/**
* Move the planner to the current position from wherever it last moved
* (or from wherever it has been told it is located).
*/
void line_to_current_position(const feedRate_t &fr_mm_s/*=feedrate_mm_s*/) {
planner.buffer_line(current_position, fr_mm_s, active_extruder);
}
#if EXTRUDERS
void unscaled_e_move(const float &length, const feedRate_t &fr_mm_s) {
TERN_(HAS_FILAMENT_SENSOR, runout.reset());
current_position.e += length / planner.e_factor[active_extruder];
line_to_current_position(fr_mm_s);
planner.synchronize();
}
#endif
#if IS_KINEMATIC
/**
* Buffer a fast move without interpolation. Set current_position to destination
*/
void prepare_fast_move_to_destination(const feedRate_t &scaled_fr_mm_s/*=MMS_SCALED(feedrate_mm_s)*/) {
if (DEBUGGING(LEVELING)) DEBUG_POS("prepare_fast_move_to_destination", destination);
#if UBL_SEGMENTED
// UBL segmented line will do Z-only moves in single segment
ubl.line_to_destination_segmented(scaled_fr_mm_s);
#else
if (current_position == destination) return;
planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
#endif
current_position = destination;
}
#endif // IS_KINEMATIC
/**
* Do a fast or normal move to 'destination' with an optional FR.
* - Move at normal speed regardless of feedrate percentage.
* - Extrude the specified length regardless of flow percentage.
*/
void _internal_move_to_destination(const feedRate_t &fr_mm_s/*=0.0f*/
#if IS_KINEMATIC
, const bool is_fast/*=false*/
#endif
) {
const feedRate_t old_feedrate = feedrate_mm_s;
if (fr_mm_s) feedrate_mm_s = fr_mm_s;
const uint16_t old_pct = feedrate_percentage;
feedrate_percentage = 100;
#if EXTRUDERS
const float old_fac = planner.e_factor[active_extruder];
planner.e_factor[active_extruder] = 1.0f;
#endif
#if IS_KINEMATIC
if (is_fast)
prepare_fast_move_to_destination();
else
#endif
prepare_line_to_destination();
feedrate_mm_s = old_feedrate;
feedrate_percentage = old_pct;
#if EXTRUDERS
planner.e_factor[active_extruder] = old_fac;
#endif
}
/**
* Plan a move to (X, Y, Z) and set the current_position
*/
void do_blocking_move_to(const float rx, const float ry, const float rz, const feedRate_t &fr_mm_s/*=0.0*/) {
DEBUG_SECTION(log_move, "do_blocking_move_to", DEBUGGING(LEVELING));
if (DEBUGGING(LEVELING)) DEBUG_XYZ("> ", rx, ry, rz);
const feedRate_t z_feedrate = fr_mm_s ?: homing_feedrate(Z_AXIS),
xy_feedrate = fr_mm_s ?: feedRate_t(XY_PROBE_FEEDRATE_MM_S);
#if ENABLED(DELTA)
if (!position_is_reachable(rx, ry)) return;
REMEMBER(fr, feedrate_mm_s, xy_feedrate);
destination = current_position; // sync destination at the start
if (DEBUGGING(LEVELING)) DEBUG_POS("destination = current_position", destination);
// when in the danger zone
if (current_position.z > delta_clip_start_height) {
if (rz > delta_clip_start_height) { // staying in the danger zone
destination.set(rx, ry, rz); // move directly (uninterpolated)
prepare_internal_fast_move_to_destination(); // set current_position from destination
if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position);
return;
}
destination.z = delta_clip_start_height;
prepare_internal_fast_move_to_destination(); // set current_position from destination
if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position);
}
if (rz > current_position.z) { // raising?
destination.z = rz;
prepare_internal_fast_move_to_destination(z_feedrate); // set current_position from destination
if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position);
}
destination.set(rx, ry);
prepare_internal_move_to_destination(); // set current_position from destination
if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position);
if (rz < current_position.z) { // lowering?
destination.z = rz;
prepare_internal_fast_move_to_destination(z_feedrate); // set current_position from destination
if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position);
}
#elif IS_SCARA
if (!position_is_reachable(rx, ry)) return;
destination = current_position;
// If Z needs to raise, do it before moving XY
if (destination.z < rz) {
destination.z = rz;
prepare_internal_fast_move_to_destination(z_feedrate);
}
destination.set(rx, ry);
prepare_internal_fast_move_to_destination(xy_feedrate);
// If Z needs to lower, do it after moving XY
if (destination.z > rz) {
destination.z = rz;
prepare_internal_fast_move_to_destination(z_feedrate);
}
#else
// If Z needs to raise, do it before moving XY
if (current_position.z < rz) {
current_position.z = rz;
line_to_current_position(z_feedrate);
}
current_position.set(rx, ry);
line_to_current_position(xy_feedrate);
// If Z needs to lower, do it after moving XY
if (current_position.z > rz) {
current_position.z = rz;
line_to_current_position(z_feedrate);
}
#endif
planner.synchronize();
}
void do_blocking_move_to(const xy_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
do_blocking_move_to(raw.x, raw.y, current_position.z, fr_mm_s);
}
void do_blocking_move_to(const xyz_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
do_blocking_move_to(raw.x, raw.y, raw.z, fr_mm_s);
}
void do_blocking_move_to(const xyze_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
do_blocking_move_to(raw.x, raw.y, raw.z, fr_mm_s);
}
void do_blocking_move_to_x(const float &rx, const feedRate_t &fr_mm_s/*=0.0*/) {
do_blocking_move_to(rx, current_position.y, current_position.z, fr_mm_s);
}
void do_blocking_move_to_y(const float &ry, const feedRate_t &fr_mm_s/*=0.0*/) {
do_blocking_move_to(current_position.x, ry, current_position.z, fr_mm_s);
}
void do_blocking_move_to_z(const float &rz, const feedRate_t &fr_mm_s/*=0.0*/) {
do_blocking_move_to_xy_z(current_position, rz, fr_mm_s);
}
void do_blocking_move_to_xy(const float &rx, const float &ry, const feedRate_t &fr_mm_s/*=0.0*/) {
do_blocking_move_to(rx, ry, current_position.z, fr_mm_s);
}
void do_blocking_move_to_xy(const xy_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
do_blocking_move_to_xy(raw.x, raw.y, fr_mm_s);
}
void do_blocking_move_to_xy_z(const xy_pos_t &raw, const float &z, const feedRate_t &fr_mm_s/*=0.0f*/) {
do_blocking_move_to(raw.x, raw.y, z, fr_mm_s);
}
void do_z_clearance(const float &zclear, const bool z_trusted/*=true*/, const bool raise_on_untrusted/*=true*/, const bool lower_allowed/*=false*/) {
const bool rel = raise_on_untrusted && !z_trusted;
float zdest = zclear + (rel ? current_position.z : 0.0f);
if (!lower_allowed) NOLESS(zdest, current_position.z);
do_blocking_move_to_z(_MIN(zdest, Z_MAX_POS), TERN(HAS_BED_PROBE, z_probe_fast_mm_s, homing_feedrate(Z_AXIS)));
}
//
// Prepare to do endstop or probe moves with custom feedrates.
// - Save / restore current feedrate and multiplier
//
static float saved_feedrate_mm_s;
static int16_t saved_feedrate_percentage;
void remember_feedrate_and_scaling() {
saved_feedrate_mm_s = feedrate_mm_s;
saved_feedrate_percentage = feedrate_percentage;
}
void remember_feedrate_scaling_off() {
remember_feedrate_and_scaling();
feedrate_percentage = 100;
}
void restore_feedrate_and_scaling() {
feedrate_mm_s = saved_feedrate_mm_s;
feedrate_percentage = saved_feedrate_percentage;
}
#if HAS_SOFTWARE_ENDSTOPS
// Software Endstops are based on the configured limits.
soft_endstops_t soft_endstop = {
true, false,
{ X_MIN_POS, Y_MIN_POS, Z_MIN_POS },
{ X_MAX_POS, Y_MAX_POS, Z_MAX_POS }
};
/**
* Software endstops can be used to monitor the open end of
* an axis that has a hardware endstop on the other end. Or
* they can prevent axes from moving past endstops and grinding.
*
* To keep doing their job as the coordinate system changes,
* the software endstop positions must be refreshed to remain
* at the same positions relative to the machine.
*/
void update_software_endstops(const AxisEnum axis
#if HAS_HOTEND_OFFSET
, const uint8_t old_tool_index/*=0*/
, const uint8_t new_tool_index/*=0*/
#endif
) {
#if ENABLED(DUAL_X_CARRIAGE)
if (axis == X_AXIS) {
// In Dual X mode hotend_offset[X] is T1's home position
const float dual_max_x = _MAX(hotend_offset[1].x, X2_MAX_POS);
if (new_tool_index != 0) {
// T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
soft_endstop.min.x = X2_MIN_POS;
soft_endstop.max.x = dual_max_x;
}
else if (idex_is_duplicating()) {
// In Duplication Mode, T0 can move as far left as X1_MIN_POS
// but not so far to the right that T1 would move past the end
soft_endstop.min.x = X1_MIN_POS;
soft_endstop.max.x = _MIN(X1_MAX_POS, dual_max_x - duplicate_extruder_x_offset);
}
else {
// In other modes, T0 can move from X1_MIN_POS to X1_MAX_POS
soft_endstop.min.x = X1_MIN_POS;
soft_endstop.max.x = X1_MAX_POS;
}
}
#elif ENABLED(DELTA)
soft_endstop.min[axis] = base_min_pos(axis);
soft_endstop.max[axis] = (axis == Z_AXIS) ? delta_height - TERN0(HAS_BED_PROBE, probe.offset.z) : base_max_pos(axis);
switch (axis) {
case X_AXIS:
case Y_AXIS:
// Get a minimum radius for clamping
delta_max_radius = _MIN(ABS(_MAX(soft_endstop.min.x, soft_endstop.min.y)), soft_endstop.max.x, soft_endstop.max.y);
delta_max_radius_2 = sq(delta_max_radius);
break;
case Z_AXIS:
delta_clip_start_height = soft_endstop.max[axis] - delta_safe_distance_from_top();
default: break;
}
#elif HAS_HOTEND_OFFSET
// Software endstops are relative to the tool 0 workspace, so
// the movement limits must be shifted by the tool offset to
// retain the same physical limit when other tools are selected.
if (new_tool_index == old_tool_index || axis == Z_AXIS) { // The Z axis is "special" and shouldn't be modified
const float offs = (axis == Z_AXIS) ? 0 : hotend_offset[active_extruder][axis];
soft_endstop.min[axis] = base_min_pos(axis) + offs;
soft_endstop.max[axis] = base_max_pos(axis) + offs;
}
else {
const float diff = hotend_offset[new_tool_index][axis] - hotend_offset[old_tool_index][axis];
soft_endstop.min[axis] += diff;
soft_endstop.max[axis] += diff;
}
#else
soft_endstop.min[axis] = base_min_pos(axis);
soft_endstop.max[axis] = base_max_pos(axis);
#endif
if (DEBUGGING(LEVELING))
SERIAL_ECHOLNPAIR("Axis ", XYZ_CHAR(axis), " min:", soft_endstop.min[axis], " max:", soft_endstop.max[axis]);
}
/**
* Constrain the given coordinates to the software endstops.
*
* For DELTA/SCARA the XY constraint is based on the smallest
* radius within the set software endstops.
*/
void apply_motion_limits(xyz_pos_t &target) {
if (!soft_endstop._enabled) return;
#if IS_KINEMATIC
if (TERN0(DELTA, !all_axes_homed())) return;
#if BOTH(HAS_HOTEND_OFFSET, DELTA)
// The effector center position will be the target minus the hotend offset.
const xy_pos_t offs = hotend_offset[active_extruder];
#else
// SCARA needs to consider the angle of the arm through the entire move, so for now use no tool offset.
constexpr xy_pos_t offs{0};
#endif
if (TERN1(IS_SCARA, axis_was_homed(X_AXIS) && axis_was_homed(Y_AXIS))) {
const float dist_2 = HYPOT2(target.x - offs.x, target.y - offs.y);
if (dist_2 > delta_max_radius_2)
target *= float(delta_max_radius / SQRT(dist_2)); // 200 / 300 = 0.66
}
#else
if (axis_was_homed(X_AXIS)) {
#if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_X)
NOLESS(target.x, soft_endstop.min.x);
#endif
#if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_X)
NOMORE(target.x, soft_endstop.max.x);
#endif
}
if (axis_was_homed(Y_AXIS)) {
#if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
NOLESS(target.y, soft_endstop.min.y);
#endif
#if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
NOMORE(target.y, soft_endstop.max.y);
#endif
}
#endif
if (axis_was_homed(Z_AXIS)) {
#if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
NOLESS(target.z, soft_endstop.min.z);
#endif
#if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
NOMORE(target.z, soft_endstop.max.z);
#endif
}
}
#else // !HAS_SOFTWARE_ENDSTOPS
soft_endstops_t soft_endstop;
#endif // !HAS_SOFTWARE_ENDSTOPS
#if !UBL_SEGMENTED
FORCE_INLINE void segment_idle(millis_t &next_idle_ms) {
const millis_t ms = millis();
if (ELAPSED(ms, next_idle_ms)) {
next_idle_ms = ms + 200UL;
return idle();
}
thermalManager.manage_heater(); // Returns immediately on most calls
}
#if IS_KINEMATIC
#if IS_SCARA
/**
* Before raising this value, use M665 S[seg_per_sec] to decrease
* the number of segments-per-second. Default is 200. Some deltas
* do better with 160 or lower. It would be good to know how many
* segments-per-second are actually possible for SCARA on AVR.
*
* Longer segments result in less kinematic overhead
* but may produce jagged lines. Try 0.5mm, 1.0mm, and 2.0mm
* and compare the difference.
*/
#define SCARA_MIN_SEGMENT_LENGTH 0.5f
#endif
/**
* Prepare a linear move in a DELTA or SCARA setup.
*
* Called from prepare_line_to_destination as the
* default Delta/SCARA segmenter.
*
* This calls planner.buffer_line several times, adding
* small incremental moves for DELTA or SCARA.
*
* For Unified Bed Leveling (Delta or Segmented Cartesian)
* the ubl.line_to_destination_segmented method replaces this.
*
* For Auto Bed Leveling (Bilinear) with SEGMENT_LEVELED_MOVES
* this is replaced by segmented_line_to_destination below.
*/
inline bool line_to_destination_kinematic() {
// Get the top feedrate of the move in the XY plane
const float scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
const xyze_float_t diff = destination - current_position;
// If the move is only in Z/E don't split up the move
if (!diff.x && !diff.y) {
planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
return false; // caller will update current_position
}
// Fail if attempting move outside printable radius
if (!position_is_reachable(destination)) return true;
// Get the linear distance in XYZ
float cartesian_mm = diff.magnitude();
// If the move is very short, check the E move distance
if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(diff.e);
// No E move either? Game over.
if (UNEAR_ZERO(cartesian_mm)) return true;
// Minimum number of seconds to move the given distance
const float seconds = cartesian_mm / scaled_fr_mm_s;
// The number of segments-per-second times the duration
// gives the number of segments
uint16_t segments = delta_segments_per_second * seconds;
// For SCARA enforce a minimum segment size
#if IS_SCARA
NOMORE(segments, cartesian_mm * RECIPROCAL(SCARA_MIN_SEGMENT_LENGTH));
#endif
// At least one segment is required
NOLESS(segments, 1U);
// The approximate length of each segment
const float inv_segments = 1.0f / float(segments),
cartesian_segment_mm = cartesian_mm * inv_segments;
const xyze_float_t segment_distance = diff * inv_segments;
#if ENABLED(SCARA_FEEDRATE_SCALING)
const float inv_duration = scaled_fr_mm_s / cartesian_segment_mm;
#endif
/*
SERIAL_ECHOPAIR("mm=", cartesian_mm);
SERIAL_ECHOPAIR(" seconds=", seconds);
SERIAL_ECHOPAIR(" segments=", segments);
SERIAL_ECHOPAIR(" segment_mm=", cartesian_segment_mm);
SERIAL_EOL();
//*/
// Get the current position as starting point
xyze_pos_t raw = current_position;
// Calculate and execute the segments
millis_t next_idle_ms = millis() + 200UL;
while (--segments) {
segment_idle(next_idle_ms);
raw += segment_distance;
if (!planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, cartesian_segment_mm
#if ENABLED(SCARA_FEEDRATE_SCALING)
, inv_duration
#endif
)) break;
}
// Ensure last segment arrives at target location.
planner.buffer_line(destination, scaled_fr_mm_s, active_extruder, cartesian_segment_mm
#if ENABLED(SCARA_FEEDRATE_SCALING)
, inv_duration
#endif
);
return false; // caller will update current_position
}
#else // !IS_KINEMATIC
#if ENABLED(SEGMENT_LEVELED_MOVES)
/**
* Prepare a segmented move on a CARTESIAN setup.
*
* This calls planner.buffer_line several times, adding
* small incremental moves. This allows the planner to
* apply more detailed bed leveling to the full move.
*/
inline void segmented_line_to_destination(const feedRate_t &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
const xyze_float_t diff = destination - current_position;
// If the move is only in Z/E don't split up the move
if (!diff.x && !diff.y) {
planner.buffer_line(destination, fr_mm_s, active_extruder);
return;
}
// Get the linear distance in XYZ
// If the move is very short, check the E move distance
// No E move either? Game over.
float cartesian_mm = diff.magnitude();
if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(diff.e);
if (UNEAR_ZERO(cartesian_mm)) return;
// The length divided by the segment size
// At least one segment is required
uint16_t segments = cartesian_mm / segment_size;
NOLESS(segments, 1U);
// The approximate length of each segment
const float inv_segments = 1.0f / float(segments),
cartesian_segment_mm = cartesian_mm * inv_segments;
const xyze_float_t segment_distance = diff * inv_segments;
#if ENABLED(SCARA_FEEDRATE_SCALING)
const float inv_duration = scaled_fr_mm_s / cartesian_segment_mm;
#endif
// SERIAL_ECHOPAIR("mm=", cartesian_mm);
// SERIAL_ECHOLNPAIR(" segments=", segments);
// SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
// Get the raw current position as starting point
xyze_pos_t raw = current_position;
// Calculate and execute the segments
millis_t next_idle_ms = millis() + 200UL;
while (--segments) {
segment_idle(next_idle_ms);
raw += segment_distance;
if (!planner.buffer_line(raw, fr_mm_s, active_extruder, cartesian_segment_mm
#if ENABLED(SCARA_FEEDRATE_SCALING)
, inv_duration
#endif
)) break;
}
// Since segment_distance is only approximate,
// the final move must be to the exact destination.
planner.buffer_line(destination, fr_mm_s, active_extruder, cartesian_segment_mm
#if ENABLED(SCARA_FEEDRATE_SCALING)
, inv_duration
#endif
);
}
#endif // SEGMENT_LEVELED_MOVES
/**
* Prepare a linear move in a Cartesian setup.
*
* When a mesh-based leveling system is active, moves are segmented
* according to the configuration of the leveling system.
*
* Return true if 'current_position' was set to 'destination'
*/
inline bool line_to_destination_cartesian() {
const float scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
#if HAS_MESH
if (planner.leveling_active && planner.leveling_active_at_z(destination.z)) {
#if ENABLED(AUTO_BED_LEVELING_UBL)
ubl.line_to_destination_cartesian(scaled_fr_mm_s, active_extruder); // UBL's motion routine needs to know about
return true; // all moves, including Z-only moves.
#elif ENABLED(SEGMENT_LEVELED_MOVES)
segmented_line_to_destination(scaled_fr_mm_s);
return false; // caller will update current_position
#else
/**
* For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
* Otherwise fall through to do a direct single move.
*/
if (xy_pos_t(current_position) != xy_pos_t(destination)) {
#if ENABLED(MESH_BED_LEVELING)
mbl.line_to_destination(scaled_fr_mm_s);
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
bilinear_line_to_destination(scaled_fr_mm_s);
#endif
return true;
}
#endif
}
#endif // HAS_MESH
planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
return false; // caller will update current_position
}
#endif // !IS_KINEMATIC
#endif // !UBL_SEGMENTED
#if HAS_DUPLICATION_MODE
bool extruder_duplication_enabled;
#if ENABLED(MULTI_NOZZLE_DUPLICATION)
uint8_t duplication_e_mask; // = 0
#endif
#endif
#if ENABLED(DUAL_X_CARRIAGE)
DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
float inactive_extruder_x = X2_MAX_POS, // Used in mode 0 & 1
duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // Used in mode 2
xyz_pos_t raised_parked_position; // Used in mode 1
bool active_extruder_parked = false; // Used in mode 1 & 2
millis_t delayed_move_time = 0; // Used in mode 1
int16_t duplicate_extruder_temp_offset = 0; // Used in mode 2
bool idex_mirrored_mode = false; // Used in mode 3
float x_home_pos(const uint8_t extruder) {
if (extruder == 0)
return base_home_pos(X_AXIS);
else
/**
* In dual carriage mode the extruder offset provides an override of the
* second X-carriage position when homed - otherwise X2_HOME_POS is used.
* This allows soft recalibration of the second extruder home position
* without firmware reflash (through the M218 command).
*/
return hotend_offset[1].x > 0 ? hotend_offset[1].x : X2_HOME_POS;
}
void idex_set_mirrored_mode(const bool mirr) {
idex_mirrored_mode = mirr;
stepper.set_directions();
}
void set_duplication_enabled(const bool dupe, const int8_t tool_index/*=-1*/) {
extruder_duplication_enabled = dupe;
if (tool_index >= 0) active_extruder = tool_index;
stepper.set_directions();
}
void idex_set_parked(const bool park/*=true*/) {
delayed_move_time = 0;
active_extruder_parked = park;
if (park) raised_parked_position = current_position; // Remember current raised toolhead position for use by unpark
}
/**
* Prepare a linear move in a dual X axis setup
*
* Return true if current_position[] was set to destination[]
*/
inline bool dual_x_carriage_unpark() {
if (active_extruder_parked) {
switch (dual_x_carriage_mode) {
case DXC_FULL_CONTROL_MODE: break;
case DXC_AUTO_PARK_MODE: {
if (current_position.e == destination.e) {
// This is a travel move (with no extrusion)
// Skip it, but keep track of the current position
// (so it can be used as the start of the next non-travel move)
if (delayed_move_time != 0xFFFFFFFFUL) {
current_position = destination;
NOLESS(raised_parked_position.z, destination.z);
delayed_move_time = millis() + 1000UL;
return true;
}
}
//
// Un-park the active extruder
//
const feedRate_t fr_zfast = planner.settings.max_feedrate_mm_s[Z_AXIS];
#define CURPOS current_position
#define RAISED raised_parked_position
// 1. Move to the raised parked XYZ. Presumably the tool is already at XY.
if (planner.buffer_line(RAISED.x, RAISED.y, RAISED.z, CURPOS.e, fr_zfast, active_extruder)) {
// 2. Move to the current native XY and raised Z. Presumably this is a null move.
if (planner.buffer_line(CURPOS.x, CURPOS.y, RAISED.z, CURPOS.e, PLANNER_XY_FEEDRATE(), active_extruder)) {
// 3. Lower Z back down
line_to_current_position(fr_zfast);
}
}
stepper.set_directions();
idex_set_parked(false);
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("idex_set_parked(false)");
} break;
case DXC_MIRRORED_MODE:
case DXC_DUPLICATION_MODE:
if (active_extruder == 0) {
xyze_pos_t new_pos = current_position;
if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
new_pos.x += duplicate_extruder_x_offset;
else
new_pos.x = inactive_extruder_x;
// Move duplicate extruder into correct duplication position.
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Set planner X", inactive_extruder_x, " ... Line to X", new_pos.x);
planner.set_position_mm(inactive_extruder_x, current_position.y, current_position.z, current_position.e);
if (!planner.buffer_line(new_pos, planner.settings.max_feedrate_mm_s[X_AXIS], 1)) break;
planner.synchronize();
sync_plan_position();
set_duplication_enabled(true);
idex_set_parked(false);
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("set_duplication_enabled(true)\nidex_set_parked(false)");
}
else if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Active extruder not 0");
break;
}
}
return false;
}
#endif // DUAL_X_CARRIAGE
/**
* Prepare a single move and get ready for the next one
*
* This may result in several calls to planner.buffer_line to
* do smaller moves for DELTA, SCARA, mesh moves, etc.
*
* Make sure current_position.e and destination.e are good
* before calling or cold/lengthy extrusion may get missed.
*
* Before exit, current_position is set to destination.
*/
void prepare_line_to_destination() {
apply_motion_limits(destination);
#if EITHER(PREVENT_COLD_EXTRUSION, PREVENT_LENGTHY_EXTRUDE)
if (!DEBUGGING(DRYRUN) && destination.e != current_position.e) {
bool ignore_e = false;
#if ENABLED(PREVENT_COLD_EXTRUSION)
ignore_e = thermalManager.tooColdToExtrude(active_extruder);
if (ignore_e) SERIAL_ECHO_MSG(STR_ERR_COLD_EXTRUDE_STOP);
#endif
#if ENABLED(PREVENT_LENGTHY_EXTRUDE)
const float e_delta = ABS(destination.e - current_position.e) * planner.e_factor[active_extruder];
if (e_delta > (EXTRUDE_MAXLENGTH)) {
#if ENABLED(MIXING_EXTRUDER)
float collector[MIXING_STEPPERS];
mixer.refresh_collector(1.0, mixer.get_current_vtool(), collector);
MIXER_STEPPER_LOOP(e) {
if (e_delta * collector[e] > (EXTRUDE_MAXLENGTH)) {
ignore_e = true;
SERIAL_ECHO_MSG(STR_ERR_LONG_EXTRUDE_STOP);
break;
}
}
#else
ignore_e = true;
SERIAL_ECHO_MSG(STR_ERR_LONG_EXTRUDE_STOP);
#endif
}
#endif
if (ignore_e) {
current_position.e = destination.e; // Behave as if the E move really took place
planner.set_e_position_mm(destination.e); // Prevent the planner from complaining too
}
}
#endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
if (TERN0(DUAL_X_CARRIAGE, dual_x_carriage_unpark())) return;
if (
#if UBL_SEGMENTED
#if IS_KINEMATIC // UBL using Kinematic / Cartesian cases as a workaround for now.
ubl.line_to_destination_segmented(MMS_SCALED(feedrate_mm_s))
#else
line_to_destination_cartesian()
#endif
#elif IS_KINEMATIC
line_to_destination_kinematic()
#else
line_to_destination_cartesian()
#endif
) return;
current_position = destination;
}
uint8_t axes_should_home(uint8_t axis_bits/*=0x07*/) {
#define SHOULD_HOME(A) TERN(HOME_AFTER_DEACTIVATE, axis_is_trusted, axis_was_homed)(A)
// Clear test bits that are trusted
if (TEST(axis_bits, X_AXIS) && SHOULD_HOME(X_AXIS)) CBI(axis_bits, X_AXIS);
if (TEST(axis_bits, Y_AXIS) && SHOULD_HOME(Y_AXIS)) CBI(axis_bits, Y_AXIS);
if (TEST(axis_bits, Z_AXIS) && SHOULD_HOME(Z_AXIS)) CBI(axis_bits, Z_AXIS);
return axis_bits;
}
bool homing_needed_error(uint8_t axis_bits/*=0x07*/) {
if ((axis_bits = axes_should_home(axis_bits))) {
PGM_P home_first = GET_TEXT(MSG_HOME_FIRST);
char msg[strlen_P(home_first)+1];
sprintf_P(msg, home_first,
TEST(axis_bits, X_AXIS) ? "X" : "",
TEST(axis_bits, Y_AXIS) ? "Y" : "",
TEST(axis_bits, Z_AXIS) ? "Z" : ""
);
SERIAL_ECHO_START();
SERIAL_ECHOLN(msg);
TERN_(HAS_DISPLAY, ui.set_status(msg));
return true;
}
return false;
}
/**
* Homing bump feedrate (mm/s)
*/
feedRate_t get_homing_bump_feedrate(const AxisEnum axis) {
#if HOMING_Z_WITH_PROBE
if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW);
#endif
static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
if (hbd < 1) {
hbd = 10;
SERIAL_ECHO_MSG("Warning: Homing Bump Divisor < 1");
}
return homing_feedrate(axis) / float(hbd);
}
#if ENABLED(SENSORLESS_HOMING)
/**
* Set sensorless homing if the axis has it, accounting for Core Kinematics.
*/
sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis) {
sensorless_t stealth_states { false };
switch (axis) {
default: break;
#if X_SENSORLESS
case X_AXIS:
stealth_states.x = tmc_enable_stallguard(stepperX);
#if AXIS_HAS_STALLGUARD(X2)
stealth_states.x2 = tmc_enable_stallguard(stepperX2);
#endif
#if EITHER(CORE_IS_XY, MARKFORGED_XY) && Y_SENSORLESS
stealth_states.y = tmc_enable_stallguard(stepperY);
#elif CORE_IS_XZ && Z_SENSORLESS
stealth_states.z = tmc_enable_stallguard(stepperZ);
#endif
break;
#endif
#if Y_SENSORLESS
case Y_AXIS:
stealth_states.y = tmc_enable_stallguard(stepperY);
#if AXIS_HAS_STALLGUARD(Y2)
stealth_states.y2 = tmc_enable_stallguard(stepperY2);
#endif
#if EITHER(CORE_IS_XY, MARKFORGED_XY) && X_SENSORLESS
stealth_states.x = tmc_enable_stallguard(stepperX);
#elif CORE_IS_YZ && Z_SENSORLESS
stealth_states.z = tmc_enable_stallguard(stepperZ);
#endif
break;
#endif
#if Z_SENSORLESS
case Z_AXIS:
stealth_states.z = tmc_enable_stallguard(stepperZ);
#if AXIS_HAS_STALLGUARD(Z2)
stealth_states.z2 = tmc_enable_stallguard(stepperZ2);
#endif
#if AXIS_HAS_STALLGUARD(Z3)
stealth_states.z3 = tmc_enable_stallguard(stepperZ3);
#endif
#if AXIS_HAS_STALLGUARD(Z4)
stealth_states.z4 = tmc_enable_stallguard(stepperZ4);
#endif
#if CORE_IS_XZ && X_SENSORLESS
stealth_states.x = tmc_enable_stallguard(stepperX);
#elif CORE_IS_YZ && Y_SENSORLESS
stealth_states.y = tmc_enable_stallguard(stepperY);
#endif
break;
#endif
}
#if ENABLED(SPI_ENDSTOPS)
switch (axis) {
case X_AXIS: if (ENABLED(X_SPI_SENSORLESS)) endstops.tmc_spi_homing.x = true; break;
case Y_AXIS: if (ENABLED(Y_SPI_SENSORLESS)) endstops.tmc_spi_homing.y = true; break;
case Z_AXIS: if (ENABLED(Z_SPI_SENSORLESS)) endstops.tmc_spi_homing.z = true; break;
default: break;
}
#endif
TERN_(IMPROVE_HOMING_RELIABILITY, sg_guard_period = millis() + default_sg_guard_duration);
return stealth_states;
}
void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) {
switch (axis) {
default: break;
#if X_SENSORLESS
case X_AXIS:
tmc_disable_stallguard(stepperX, enable_stealth.x);
#if AXIS_HAS_STALLGUARD(X2)
tmc_disable_stallguard(stepperX2, enable_stealth.x2);
#endif
#if EITHER(CORE_IS_XY, MARKFORGED_XY) && Y_SENSORLESS
tmc_disable_stallguard(stepperY, enable_stealth.y);
#elif CORE_IS_XZ && Z_SENSORLESS
tmc_disable_stallguard(stepperZ, enable_stealth.z);
#endif
break;
#endif
#if Y_SENSORLESS
case Y_AXIS:
tmc_disable_stallguard(stepperY, enable_stealth.y);
#if AXIS_HAS_STALLGUARD(Y2)
tmc_disable_stallguard(stepperY2, enable_stealth.y2);
#endif
#if EITHER(CORE_IS_XY, MARKFORGED_XY) && X_SENSORLESS
tmc_disable_stallguard(stepperX, enable_stealth.x);
#elif CORE_IS_YZ && Z_SENSORLESS
tmc_disable_stallguard(stepperZ, enable_stealth.z);
#endif
break;
#endif
#if Z_SENSORLESS
case Z_AXIS:
tmc_disable_stallguard(stepperZ, enable_stealth.z);
#if AXIS_HAS_STALLGUARD(Z2)
tmc_disable_stallguard(stepperZ2, enable_stealth.z2);
#endif
#if AXIS_HAS_STALLGUARD(Z3)
tmc_disable_stallguard(stepperZ3, enable_stealth.z3);
#endif
#if AXIS_HAS_STALLGUARD(Z4)
tmc_disable_stallguard(stepperZ4, enable_stealth.z4);
#endif
#if CORE_IS_XZ && X_SENSORLESS
tmc_disable_stallguard(stepperX, enable_stealth.x);
#elif CORE_IS_YZ && Y_SENSORLESS
tmc_disable_stallguard(stepperY, enable_stealth.y);
#endif
break;
#endif
}
#if ENABLED(SPI_ENDSTOPS)
switch (axis) {
case X_AXIS: if (ENABLED(X_SPI_SENSORLESS)) endstops.tmc_spi_homing.x = false; break;
case Y_AXIS: if (ENABLED(Y_SPI_SENSORLESS)) endstops.tmc_spi_homing.y = false; break;
case Z_AXIS: if (ENABLED(Z_SPI_SENSORLESS)) endstops.tmc_spi_homing.z = false; break;
default: break;
}
#endif
}
#endif // SENSORLESS_HOMING
/**
* Home an individual linear axis
*/
void do_homing_move(const AxisEnum axis, const float distance, const feedRate_t fr_mm_s=0.0, const bool final_approach=true) {
DEBUG_SECTION(log_move, "do_homing_move", DEBUGGING(LEVELING));
const feedRate_t home_fr_mm_s = fr_mm_s ?: homing_feedrate(axis);
if (DEBUGGING(LEVELING)) {
DEBUG_ECHOPAIR("...(", axis_codes[axis], ", ", distance, ", ");
if (fr_mm_s)
DEBUG_ECHO(fr_mm_s);
else
DEBUG_ECHOPAIR("[", home_fr_mm_s, "]");
DEBUG_ECHOLNPGM(")");
}
// Only do some things when moving towards an endstop
const int8_t axis_home_dir = TERN0(DUAL_X_CARRIAGE, axis == X_AXIS)
? x_home_dir(active_extruder) : home_dir(axis);
const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
#if ENABLED(SENSORLESS_HOMING)
sensorless_t stealth_states;
#endif
if (is_home_dir) {
if (TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS)) {
#if ALL(HAS_HEATED_BED, WAIT_FOR_BED_HEATER)
// Wait for bed to heat back up between probing points
thermalManager.wait_for_bed_heating();
#endif
TERN_(HAS_QUIET_PROBING, if (final_approach) probe.set_probing_paused(true));
}
// Disable stealthChop if used. Enable diag1 pin on driver.
TERN_(SENSORLESS_HOMING, stealth_states = start_sensorless_homing_per_axis(axis));
}
#if IS_SCARA
// Tell the planner the axis is at 0
current_position[axis] = 0;
sync_plan_position();
current_position[axis] = distance;
line_to_current_position(home_fr_mm_s);
#else
// Get the ABC or XYZ positions in mm
abce_pos_t target = planner.get_axis_positions_mm();
target[axis] = 0; // Set the single homing axis to 0
planner.set_machine_position_mm(target); // Update the machine position
#if HAS_DIST_MM_ARG
const xyze_float_t cart_dist_mm{0};
#endif
// Set delta/cartesian axes directly
target[axis] = distance; // The move will be towards the endstop
planner.buffer_segment(target
#if HAS_DIST_MM_ARG
, cart_dist_mm
#endif
, home_fr_mm_s, active_extruder
);
#endif
planner.synchronize();
if (is_home_dir) {
#if HOMING_Z_WITH_PROBE && HAS_QUIET_PROBING
if (axis == Z_AXIS && final_approach) probe.set_probing_paused(false);
#endif
endstops.validate_homing_move();
// Re-enable stealthChop if used. Disable diag1 pin on driver.
TERN_(SENSORLESS_HOMING, end_sensorless_homing_per_axis(axis, stealth_states));
}
}
/**
* Set an axis' current position to its home position (after homing).
*
* For Core and Cartesian robots this applies one-to-one when an
* individual axis has been homed.
*
* DELTA should wait until all homing is done before setting the XYZ
* current_position to home, because homing is a single operation.
* In the case where the axis positions are trusted and previously
* homed, DELTA could home to X or Y individually by moving either one
* to the center. However, homing Z always homes XY and Z.
*
* SCARA should wait until all XY homing is done before setting the XY
* current_position to home, because neither X nor Y is at home until
* both are at home. Z can however be homed individually.
*
* Callers must sync the planner position after calling this!
*/
void set_axis_is_at_home(const AxisEnum axis) {
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_at_home(", axis_codes[axis], ")");
set_axis_trusted(axis);
set_axis_homed(axis);
#if ENABLED(DUAL_X_CARRIAGE)
if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
current_position.x = x_home_pos(active_extruder);
return;
}
#endif
#if ENABLED(MORGAN_SCARA)
scara_set_axis_is_at_home(axis);
#elif ENABLED(DELTA)
current_position[axis] = (axis == Z_AXIS) ? delta_height - TERN0(HAS_BED_PROBE, probe.offset.z) : base_home_pos(axis);
#else
current_position[axis] = base_home_pos(axis);
#endif
/**
* Z Probe Z Homing? Account for the probe's Z offset.
*/
#if HAS_BED_PROBE && Z_HOME_DIR < 0
if (axis == Z_AXIS) {
#if HOMING_Z_WITH_PROBE
current_position.z -= probe.offset.z;
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***\n> probe.offset.z = ", probe.offset.z);
#else
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("*** Z HOMED TO ENDSTOP ***");
#endif
}
#endif
TERN_(I2C_POSITION_ENCODERS, I2CPEM.homed(axis));
TERN_(BABYSTEP_DISPLAY_TOTAL, babystep.reset_total(axis));
#if HAS_POSITION_SHIFT
position_shift[axis] = 0;
update_workspace_offset(axis);
#endif
if (DEBUGGING(LEVELING)) {
#if HAS_HOME_OFFSET
DEBUG_ECHOLNPAIR("> home_offset[", axis_codes[axis], "] = ", home_offset[axis]);
#endif
DEBUG_POS("", current_position);
DEBUG_ECHOLNPAIR("<<< set_axis_is_at_home(", axis_codes[axis], ")");
}
}
/**
* Set an axis to be unhomed.
*/
void set_axis_never_homed(const AxisEnum axis) {
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_never_homed(", axis_codes[axis], ")");
set_axis_untrusted(axis);
set_axis_unhomed(axis);
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< set_axis_never_homed(", axis_codes[axis], ")");
TERN_(I2C_POSITION_ENCODERS, I2CPEM.unhomed(axis));
}
#ifdef TMC_HOME_PHASE
/**
* Move the axis back to its home_phase if set and driver is capable (TMC)
*
* Improves homing repeatability by homing to stepper coil's nearest absolute
* phase position. Trinamic drivers use a stepper phase table with 1024 values
* spanning 4 full steps with 256 positions each (ergo, 1024 positions).
*/
void backout_to_tmc_homing_phase(const AxisEnum axis) {
const xyz_long_t home_phase = TMC_HOME_PHASE;
// check if home phase is disabled for this axis.
if (home_phase[axis] < 0) return;
int16_t phasePerUStep, // TMC µsteps(phase) per Marlin µsteps
phaseCurrent, // The TMC µsteps(phase) count of the current position
effectorBackoutDir, // Direction in which the effector mm coordinates move away from endstop.
stepperBackoutDir; // Direction in which the TMC µstep count(phase) move away from endstop.
#define PHASE_PER_MICROSTEP(N) (256 / _MAX(1, N##_MICROSTEPS))
switch (axis) {
#ifdef X_MICROSTEPS
case X_AXIS:
phasePerUStep = PHASE_PER_MICROSTEP(X);
phaseCurrent = stepperX.get_microstep_counter();
effectorBackoutDir = -X_HOME_DIR;
stepperBackoutDir = INVERT_X_DIR ? effectorBackoutDir : -effectorBackoutDir;
break;
#endif
#ifdef Y_MICROSTEPS
case Y_AXIS:
phasePerUStep = PHASE_PER_MICROSTEP(Y);
phaseCurrent = stepperY.get_microstep_counter();
effectorBackoutDir = -Y_HOME_DIR;
stepperBackoutDir = INVERT_Y_DIR ? effectorBackoutDir : -effectorBackoutDir;
break;
#endif
#ifdef Z_MICROSTEPS
case Z_AXIS:
phasePerUStep = PHASE_PER_MICROSTEP(Z);
phaseCurrent = stepperZ.get_microstep_counter();
effectorBackoutDir = -Z_HOME_DIR;
stepperBackoutDir = INVERT_Z_DIR ? effectorBackoutDir : -effectorBackoutDir;
break;
#endif
default: return;
}
// Phase distance to nearest home phase position when moving in the backout direction from endstop(may be negative).
int16_t phaseDelta = (home_phase[axis] - phaseCurrent) * stepperBackoutDir;
// Check if home distance within endstop assumed repeatability noise of .05mm and warn.
if (ABS(phaseDelta) * planner.steps_to_mm[axis] / phasePerUStep < 0.05f)
SERIAL_ECHOLNPAIR("Selected home phase ", home_phase[axis],
" too close to endstop trigger phase ", phaseCurrent,
". Pick a different phase for ", axis_codes[axis]);
// Skip to next if target position is behind current. So it only moves away from endstop.
if (phaseDelta < 0) phaseDelta += 1024;
// Convert TMC µsteps(phase) to whole Marlin µsteps to effector backout direction to mm
const float mmDelta = int16_t(phaseDelta / phasePerUStep) * effectorBackoutDir * planner.steps_to_mm[axis];
// Optional debug messages
if (DEBUGGING(LEVELING)) {
DEBUG_ECHOLNPAIR(
"Endstop ", axis_codes[axis], " hit at Phase:", phaseCurrent,
" Delta:", phaseDelta, " Distance:", mmDelta
);
}
if (mmDelta != 0) {
// Retrace by the amount computed in mmDelta.
do_homing_move(axis, mmDelta, get_homing_bump_feedrate(axis));
}
}
#endif
/**
* Home an individual "raw axis" to its endstop.
* This applies to XYZ on Cartesian and Core robots, and
* to the individual ABC steppers on DELTA and SCARA.
*
* At the end of the procedure the axis is marked as
* homed and the current position of that axis is updated.
* Kinematic robots should wait till all axes are homed
* before updating the current position.
*/
void homeaxis(const AxisEnum axis) {
#if IS_SCARA
// Only Z homing (with probe) is permitted
if (axis != Z_AXIS) { BUZZ(100, 880); return; }
#else
#define _CAN_HOME(A) (axis == _AXIS(A) && ( \
ENABLED(A##_SPI_SENSORLESS) \
|| (_AXIS(A) == Z_AXIS && ENABLED(HOMING_Z_WITH_PROBE)) \
|| (A##_MIN_PIN > -1 && A##_HOME_DIR < 0) \
|| (A##_MAX_PIN > -1 && A##_HOME_DIR > 0) \
))
if (!_CAN_HOME(X) && !_CAN_HOME(Y) && !_CAN_HOME(Z)) return;
#endif
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")");
const int axis_home_dir = TERN0(DUAL_X_CARRIAGE, axis == X_AXIS)
? x_home_dir(active_extruder) : home_dir(axis);
//
// Homing Z with a probe? Raise Z (maybe) and deploy the Z probe.
//
if (TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS && probe.deploy()))
return;
// Set flags for X, Y, Z motor locking
#if HAS_EXTRA_ENDSTOPS
switch (axis) {
TERN_(X_DUAL_ENDSTOPS, case X_AXIS:)
TERN_(Y_DUAL_ENDSTOPS, case Y_AXIS:)
TERN_(Z_MULTI_ENDSTOPS, case Z_AXIS:)
stepper.set_separate_multi_axis(true);
default: break;
}
#endif
//
// Deploy BLTouch or tare the probe just before probing
//
#if HOMING_Z_WITH_PROBE
if (axis == Z_AXIS) {
if (TERN0(BLTOUCH, bltouch.deploy())) return; // BLTouch was deployed above, but get the alarm state.
if (TERN0(PROBE_TARE, probe.tare())) return;
}
#endif
//
// Back away to prevent an early X/Y sensorless trigger
//
#if DISABLED(DELTA) && defined(SENSORLESS_BACKOFF_MM)
const xy_float_t backoff = SENSORLESS_BACKOFF_MM;
if ((TERN0(X_SENSORLESS, axis == X_AXIS) || TERN0(Y_SENSORLESS, axis == Y_AXIS)) && backoff[axis]) {
const float backoff_length = -ABS(backoff[axis]) * axis_home_dir;
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Sensorless backoff: ", backoff_length, "mm");
do_homing_move(axis, backoff_length, homing_feedrate(axis));
}
#endif
// Determine if a homing bump will be done and the bumps distance
// When homing Z with probe respect probe clearance
const bool use_probe_bump = TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS && home_bump_mm(Z_AXIS));
const float bump = axis_home_dir * (
use_probe_bump ? _MAX(TERN0(HOMING_Z_WITH_PROBE, Z_CLEARANCE_BETWEEN_PROBES), home_bump_mm(Z_AXIS)) : home_bump_mm(axis)
);
//
// Fast move towards endstop until triggered
//
const float move_length = 1.5f * max_length(TERN(DELTA, Z_AXIS, axis)) * axis_home_dir;
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Home Fast: ", move_length, "mm");
do_homing_move(axis, move_length, 0.0, !use_probe_bump);
#if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH_SLOW_MODE)
if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE)
#endif
// If a second homing move is configured...
if (bump) {
// Move away from the endstop by the axis HOMING_BUMP_MM
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Move Away: ", -bump, "mm");
do_homing_move(axis, -bump, TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS) ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) : 0, false);
#if ENABLED(DETECT_BROKEN_ENDSTOP)
// Check for a broken endstop
EndstopEnum es;
switch (axis) {
default:
case X_AXIS: es = X_ENDSTOP; break;
case Y_AXIS: es = Y_ENDSTOP; break;
case Z_AXIS: es = Z_ENDSTOP; break;
}
if (TEST(endstops.state(), es)) {
SERIAL_ECHO_MSG("Bad ", axis_codes[axis], " Endstop?");
kill(GET_TEXT(MSG_KILL_HOMING_FAILED));
}
#endif
#if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH_SLOW_MODE)
if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE)
#endif
// Slow move towards endstop until triggered
const float rebump = bump * 2;
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Re-bump: ", rebump, "mm");
do_homing_move(axis, rebump, get_homing_bump_feedrate(axis), true);
#if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH)
if (axis == Z_AXIS) bltouch.stow(); // The final STOW
#endif
}
#if HAS_EXTRA_ENDSTOPS
const bool pos_dir = axis_home_dir > 0;
#if ENABLED(X_DUAL_ENDSTOPS)
if (axis == X_AXIS) {
const float adj = ABS(endstops.x2_endstop_adj);
if (adj) {
if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
do_homing_move(axis, pos_dir ? -adj : adj);
stepper.set_x_lock(false);
stepper.set_x2_lock(false);
}
}
#endif
#if ENABLED(Y_DUAL_ENDSTOPS)
if (axis == Y_AXIS) {
const float adj = ABS(endstops.y2_endstop_adj);
if (adj) {
if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
do_homing_move(axis, pos_dir ? -adj : adj);
stepper.set_y_lock(false);
stepper.set_y2_lock(false);
}
}
#endif
#if ENABLED(Z_MULTI_ENDSTOPS)
if (axis == Z_AXIS) {
#if NUM_Z_STEPPER_DRIVERS == 2
const float adj = ABS(endstops.z2_endstop_adj);
if (adj) {
if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z1_lock(true); else stepper.set_z2_lock(true);
do_homing_move(axis, pos_dir ? -adj : adj);
stepper.set_z1_lock(false);
stepper.set_z2_lock(false);
}
#else
// Handy arrays of stepper lock function pointers
typedef void (*adjustFunc_t)(const bool);
adjustFunc_t lock[] = {
stepper.set_z1_lock, stepper.set_z2_lock, stepper.set_z3_lock
#if NUM_Z_STEPPER_DRIVERS >= 4
, stepper.set_z4_lock
#endif
};
float adj[] = {
0, endstops.z2_endstop_adj, endstops.z3_endstop_adj
#if NUM_Z_STEPPER_DRIVERS >= 4
, endstops.z4_endstop_adj
#endif
};
adjustFunc_t tempLock;
float tempAdj;
// Manual bubble sort by adjust value
if (adj[1] < adj[0]) {
tempLock = lock[0], tempAdj = adj[0];
lock[0] = lock[1], adj[0] = adj[1];
lock[1] = tempLock, adj[1] = tempAdj;
}
if (adj[2] < adj[1]) {
tempLock = lock[1], tempAdj = adj[1];
lock[1] = lock[2], adj[1] = adj[2];
lock[2] = tempLock, adj[2] = tempAdj;
}
#if NUM_Z_STEPPER_DRIVERS >= 4
if (adj[3] < adj[2]) {
tempLock = lock[2], tempAdj = adj[2];
lock[2] = lock[3], adj[2] = adj[3];
lock[3] = tempLock, adj[3] = tempAdj;
}
if (adj[2] < adj[1]) {
tempLock = lock[1], tempAdj = adj[1];
lock[1] = lock[2], adj[1] = adj[2];
lock[2] = tempLock, adj[2] = tempAdj;
}
#endif
if (adj[1] < adj[0]) {
tempLock = lock[0], tempAdj = adj[0];
lock[0] = lock[1], adj[0] = adj[1];
lock[1] = tempLock, adj[1] = tempAdj;
}
if (pos_dir) {
// normalize adj to smallest value and do the first move
(*lock[0])(true);
do_homing_move(axis, adj[1] - adj[0]);
// lock the second stepper for the final correction
(*lock[1])(true);
do_homing_move(axis, adj[2] - adj[1]);
#if NUM_Z_STEPPER_DRIVERS >= 4
// lock the third stepper for the final correction
(*lock[2])(true);
do_homing_move(axis, adj[3] - adj[2]);
#endif
}
else {
#if NUM_Z_STEPPER_DRIVERS >= 4
(*lock[3])(true);
do_homing_move(axis, adj[2] - adj[3]);
#endif
(*lock[2])(true);
do_homing_move(axis, adj[1] - adj[2]);
(*lock[1])(true);
do_homing_move(axis, adj[0] - adj[1]);
}
stepper.set_z1_lock(false);
stepper.set_z2_lock(false);
stepper.set_z3_lock(false);
#if NUM_Z_STEPPER_DRIVERS >= 4
stepper.set_z4_lock(false);
#endif
#endif
}
#endif
// Reset flags for X, Y, Z motor locking
switch (axis) {
default: break;
TERN_(X_DUAL_ENDSTOPS, case X_AXIS:)
TERN_(Y_DUAL_ENDSTOPS, case Y_AXIS:)
TERN_(Z_MULTI_ENDSTOPS, case Z_AXIS:)
stepper.set_separate_multi_axis(false);
}
#endif
#ifdef TMC_HOME_PHASE
// move back to homing phase if configured and capable
backout_to_tmc_homing_phase(axis);
#endif
#if IS_SCARA
set_axis_is_at_home(axis);
sync_plan_position();
#elif ENABLED(DELTA)
// Delta has already moved all three towers up in G28
// so here it re-homes each tower in turn.
// Delta homing treats the axes as normal linear axes.
const float adjDistance = delta_endstop_adj[axis],
minDistance = (MIN_STEPS_PER_SEGMENT) * planner.steps_to_mm[axis];
// Retrace by the amount specified in delta_endstop_adj if more than min steps.
if (adjDistance * (Z_HOME_DIR) < 0 && ABS(adjDistance) > minDistance) { // away from endstop, more than min distance
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("adjDistance:", adjDistance);
do_homing_move(axis, adjDistance, get_homing_bump_feedrate(axis));
}
#else // CARTESIAN / CORE / MARKFORGED_XY
set_axis_is_at_home(axis);
sync_plan_position();
destination[axis] = current_position[axis];
if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
#endif
// Put away the Z probe
#if HOMING_Z_WITH_PROBE
if (axis == Z_AXIS && probe.stow()) return;
#endif
#if DISABLED(DELTA) && defined(HOMING_BACKOFF_POST_MM)
const xyz_float_t endstop_backoff = HOMING_BACKOFF_POST_MM;
if (endstop_backoff[axis]) {
current_position[axis] -= ABS(endstop_backoff[axis]) * axis_home_dir;
line_to_current_position(
#if HOMING_Z_WITH_PROBE
(axis == Z_AXIS) ? z_probe_fast_mm_s :
#endif
homing_feedrate(axis)
);
#if ENABLED(SENSORLESS_HOMING)
planner.synchronize();
if (false
#if EITHER(IS_CORE, MARKFORGED_XY)
|| axis != NORMAL_AXIS
#endif
) safe_delay(200); // Short delay to allow belts to spring back
#endif
}
#endif
// Clear retracted status if homing the Z axis
#if ENABLED(FWRETRACT)
if (axis == Z_AXIS) fwretract.current_hop = 0.0;
#endif
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< homeaxis(", axis_codes[axis], ")");
} // homeaxis()
#if HAS_WORKSPACE_OFFSET
void update_workspace_offset(const AxisEnum axis) {
workspace_offset[axis] = home_offset[axis] + position_shift[axis];
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Axis ", XYZ_CHAR(axis), " home_offset = ", home_offset[axis], " position_shift = ", position_shift[axis]);
}
#endif
#if HAS_M206_COMMAND
/**
* Change the home offset for an axis.
* Also refreshes the workspace offset.
*/
void set_home_offset(const AxisEnum axis, const float v) {
home_offset[axis] = v;
update_workspace_offset(axis);
}
#endif // HAS_M206_COMMAND
|