My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

motion.cpp 56KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * motion.cpp
  24. */
  25. #include "motion.h"
  26. #include "endstops.h"
  27. #include "stepper.h"
  28. #include "planner.h"
  29. #include "temperature.h"
  30. #include "../gcode/gcode.h"
  31. #include "../inc/MarlinConfig.h"
  32. #if IS_SCARA
  33. #include "../libs/buzzer.h"
  34. #include "../lcd/ultralcd.h"
  35. #endif
  36. #if HAS_BED_PROBE
  37. #include "probe.h"
  38. #endif
  39. #if HAS_LEVELING
  40. #include "../feature/bedlevel/bedlevel.h"
  41. #endif
  42. #if ENABLED(BLTOUCH)
  43. #include "../feature/bltouch.h"
  44. #endif
  45. #if HAS_DISPLAY
  46. #include "../lcd/ultralcd.h"
  47. #endif
  48. #if ENABLED(SENSORLESS_HOMING)
  49. #include "../feature/tmc_util.h"
  50. #endif
  51. #if ENABLED(FWRETRACT)
  52. #include "../feature/fwretract.h"
  53. #endif
  54. #if ENABLED(BABYSTEP_DISPLAY_TOTAL)
  55. #include "../feature/babystep.h"
  56. #endif
  57. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  58. #include "../core/debug_out.h"
  59. #define XYZ_CONSTS(type, array, CONFIG) const PROGMEM type array##_P[XYZ] = { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }
  60. XYZ_CONSTS(float, base_min_pos, MIN_POS);
  61. XYZ_CONSTS(float, base_max_pos, MAX_POS);
  62. XYZ_CONSTS(float, base_home_pos, HOME_POS);
  63. XYZ_CONSTS(float, max_length, MAX_LENGTH);
  64. XYZ_CONSTS(float, home_bump_mm, HOME_BUMP_MM);
  65. XYZ_CONSTS(signed char, home_dir, HOME_DIR);
  66. /**
  67. * axis_homed
  68. * Flags that each linear axis was homed.
  69. * XYZ on cartesian, ABC on delta, ABZ on SCARA.
  70. *
  71. * axis_known_position
  72. * Flags that the position is known in each linear axis. Set when homed.
  73. * Cleared whenever a stepper powers off, potentially losing its position.
  74. */
  75. uint8_t axis_homed, axis_known_position; // = 0
  76. // Relative Mode. Enable with G91, disable with G90.
  77. bool relative_mode; // = false;
  78. /**
  79. * Cartesian Current Position
  80. * Used to track the native machine position as moves are queued.
  81. * Used by 'line_to_current_position' to do a move after changing it.
  82. * Used by 'sync_plan_position' to update 'planner.position'.
  83. */
  84. float current_position[XYZE] = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
  85. /**
  86. * Cartesian Destination
  87. * The destination for a move, filled in by G-code movement commands,
  88. * and expected by functions like 'prepare_move_to_destination'.
  89. * G-codes can set destination using 'get_destination_from_command'
  90. */
  91. float destination[XYZE]; // = { 0 }
  92. // The active extruder (tool). Set with T<extruder> command.
  93. #if EXTRUDERS > 1
  94. uint8_t active_extruder; // = 0
  95. #endif
  96. // Extruder offsets
  97. #if HAS_HOTEND_OFFSET
  98. float hotend_offset[XYZ][HOTENDS]; // Initialized by settings.load()
  99. void reset_hotend_offsets() {
  100. constexpr float tmp[XYZ][HOTENDS] = { HOTEND_OFFSET_X, HOTEND_OFFSET_Y, HOTEND_OFFSET_Z };
  101. static_assert(
  102. tmp[X_AXIS][0] == 0 && tmp[Y_AXIS][0] == 0 && tmp[Z_AXIS][0] == 0,
  103. "Offsets for the first hotend must be 0.0."
  104. );
  105. LOOP_XYZ(i) HOTEND_LOOP() hotend_offset[i][e] = tmp[i][e];
  106. #if ENABLED(DUAL_X_CARRIAGE)
  107. hotend_offset[X_AXIS][1] = _MAX(X2_HOME_POS, X2_MAX_POS);
  108. #endif
  109. }
  110. #endif
  111. // The feedrate for the current move, often used as the default if
  112. // no other feedrate is specified. Overridden for special moves.
  113. // Set by the last G0 through G5 command's "F" parameter.
  114. // Functions that override this for custom moves *must always* restore it!
  115. float feedrate_mm_s = MMM_TO_MMS(1500.0f);
  116. int16_t feedrate_percentage = 100;
  117. // Homing feedrate is const progmem - compare to constexpr in the header
  118. const float homing_feedrate_mm_s[XYZ] PROGMEM = {
  119. #if ENABLED(DELTA)
  120. MMM_TO_MMS(HOMING_FEEDRATE_Z), MMM_TO_MMS(HOMING_FEEDRATE_Z),
  121. #else
  122. MMM_TO_MMS(HOMING_FEEDRATE_XY), MMM_TO_MMS(HOMING_FEEDRATE_XY),
  123. #endif
  124. MMM_TO_MMS(HOMING_FEEDRATE_Z)
  125. };
  126. // Cartesian conversion result goes here:
  127. float cartes[XYZ];
  128. #if IS_KINEMATIC
  129. float delta[ABC];
  130. #if HAS_SCARA_OFFSET
  131. float scara_home_offset[ABC];
  132. #endif
  133. #if HAS_SOFTWARE_ENDSTOPS
  134. float delta_max_radius, delta_max_radius_2;
  135. #elif IS_SCARA
  136. constexpr float delta_max_radius = SCARA_PRINTABLE_RADIUS,
  137. delta_max_radius_2 = sq(SCARA_PRINTABLE_RADIUS);
  138. #else // DELTA
  139. constexpr float delta_max_radius = DELTA_PRINTABLE_RADIUS,
  140. delta_max_radius_2 = sq(DELTA_PRINTABLE_RADIUS);
  141. #endif
  142. #endif
  143. /**
  144. * The workspace can be offset by some commands, or
  145. * these offsets may be omitted to save on computation.
  146. */
  147. #if HAS_POSITION_SHIFT
  148. // The distance that XYZ has been offset by G92. Reset by G28.
  149. float position_shift[XYZ] = { 0 };
  150. #endif
  151. #if HAS_HOME_OFFSET
  152. // This offset is added to the configured home position.
  153. // Set by M206, M428, or menu item. Saved to EEPROM.
  154. float home_offset[XYZ] = { 0 };
  155. #endif
  156. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  157. // The above two are combined to save on computes
  158. float workspace_offset[XYZ] = { 0 };
  159. #endif
  160. #if HAS_ABL_NOT_UBL
  161. float xy_probe_feedrate_mm_s = MMM_TO_MMS(XY_PROBE_SPEED);
  162. #endif
  163. /**
  164. * Output the current position to serial
  165. */
  166. void report_current_position() {
  167. SERIAL_ECHOPAIR("X:", LOGICAL_X_POSITION(current_position[X_AXIS]));
  168. SERIAL_ECHOPAIR(" Y:", LOGICAL_Y_POSITION(current_position[Y_AXIS]));
  169. SERIAL_ECHOPAIR(" Z:", LOGICAL_Z_POSITION(current_position[Z_AXIS]));
  170. SERIAL_ECHOPAIR(" E:", current_position[E_AXIS]);
  171. stepper.report_positions();
  172. #if IS_SCARA
  173. scara_report_positions();
  174. #endif
  175. }
  176. /**
  177. * sync_plan_position
  178. *
  179. * Set the planner/stepper positions directly from current_position with
  180. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  181. */
  182. void sync_plan_position() {
  183. if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position", current_position);
  184. planner.set_position_mm(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  185. }
  186. void sync_plan_position_e() { planner.set_e_position_mm(current_position[E_AXIS]); }
  187. /**
  188. * Get the stepper positions in the cartes[] array.
  189. * Forward kinematics are applied for DELTA and SCARA.
  190. *
  191. * The result is in the current coordinate space with
  192. * leveling applied. The coordinates need to be run through
  193. * unapply_leveling to obtain the "ideal" coordinates
  194. * suitable for current_position, etc.
  195. */
  196. void get_cartesian_from_steppers() {
  197. #if ENABLED(DELTA)
  198. forward_kinematics_DELTA(
  199. planner.get_axis_position_mm(A_AXIS),
  200. planner.get_axis_position_mm(B_AXIS),
  201. planner.get_axis_position_mm(C_AXIS)
  202. );
  203. #else
  204. #if IS_SCARA
  205. forward_kinematics_SCARA(
  206. planner.get_axis_position_degrees(A_AXIS),
  207. planner.get_axis_position_degrees(B_AXIS)
  208. );
  209. #else
  210. cartes[X_AXIS] = planner.get_axis_position_mm(X_AXIS);
  211. cartes[Y_AXIS] = planner.get_axis_position_mm(Y_AXIS);
  212. #endif
  213. cartes[Z_AXIS] = planner.get_axis_position_mm(Z_AXIS);
  214. #endif
  215. }
  216. /**
  217. * Set the current_position for an axis based on
  218. * the stepper positions, removing any leveling that
  219. * may have been applied.
  220. *
  221. * To prevent small shifts in axis position always call
  222. * sync_plan_position after updating axes with this.
  223. *
  224. * To keep hosts in sync, always call report_current_position
  225. * after updating the current_position.
  226. */
  227. void set_current_from_steppers_for_axis(const AxisEnum axis) {
  228. get_cartesian_from_steppers();
  229. #if HAS_POSITION_MODIFIERS
  230. float pos[XYZE] = { cartes[X_AXIS], cartes[Y_AXIS], cartes[Z_AXIS], current_position[E_AXIS] };
  231. planner.unapply_modifiers(pos
  232. #if HAS_LEVELING
  233. , true
  234. #endif
  235. );
  236. const float (&cartes)[XYZE] = pos;
  237. #endif
  238. if (axis == ALL_AXES)
  239. COPY(current_position, cartes);
  240. else
  241. current_position[axis] = cartes[axis];
  242. }
  243. /**
  244. * Move the planner to the current position from wherever it last moved
  245. * (or from wherever it has been told it is located).
  246. */
  247. void line_to_current_position(const float &fr_mm_s/*=feedrate_mm_s*/) {
  248. planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr_mm_s, active_extruder);
  249. }
  250. /**
  251. * Move the planner to the position stored in the destination array, which is
  252. * used by G0/G1/G2/G3/G5 and many other functions to set a destination.
  253. */
  254. void buffer_line_to_destination(const float fr_mm_s) {
  255. planner.buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], fr_mm_s, active_extruder);
  256. }
  257. #if IS_KINEMATIC
  258. /**
  259. * Calculate delta, start a line, and set current_position to destination
  260. */
  261. void prepare_uninterpolated_move_to_destination(const float &fr_mm_s/*=0.0*/) {
  262. if (DEBUGGING(LEVELING)) DEBUG_POS("prepare_uninterpolated_move_to_destination", destination);
  263. #if UBL_SEGMENTED
  264. // ubl segmented line will do z-only moves in single segment
  265. ubl.prepare_segmented_line_to(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s));
  266. #else
  267. if ( current_position[X_AXIS] == destination[X_AXIS]
  268. && current_position[Y_AXIS] == destination[Y_AXIS]
  269. && current_position[Z_AXIS] == destination[Z_AXIS]
  270. && current_position[E_AXIS] == destination[E_AXIS]
  271. ) return;
  272. planner.buffer_line(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s), active_extruder);
  273. #endif
  274. set_current_from_destination();
  275. }
  276. #endif // IS_KINEMATIC
  277. /**
  278. * Plan a move to (X, Y, Z) and set the current_position
  279. */
  280. void do_blocking_move_to(const float rx, const float ry, const float rz, const float &fr_mm_s/*=0.0*/) {
  281. if (DEBUGGING(LEVELING)) DEBUG_XYZ(">>> do_blocking_move_to", rx, ry, rz);
  282. const float z_feedrate = fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS),
  283. xy_feedrate = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  284. #if ENABLED(DELTA)
  285. if (!position_is_reachable(rx, ry)) return;
  286. REMEMBER(fr, feedrate_mm_s, xy_feedrate);
  287. set_destination_from_current(); // sync destination at the start
  288. if (DEBUGGING(LEVELING)) DEBUG_POS("set_destination_from_current", destination);
  289. // when in the danger zone
  290. if (current_position[Z_AXIS] > delta_clip_start_height) {
  291. if (rz > delta_clip_start_height) { // staying in the danger zone
  292. destination[X_AXIS] = rx; // move directly (uninterpolated)
  293. destination[Y_AXIS] = ry;
  294. destination[Z_AXIS] = rz;
  295. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  296. if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position);
  297. return;
  298. }
  299. destination[Z_AXIS] = delta_clip_start_height;
  300. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  301. if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position);
  302. }
  303. if (rz > current_position[Z_AXIS]) { // raising?
  304. destination[Z_AXIS] = rz;
  305. prepare_uninterpolated_move_to_destination(z_feedrate); // set_current_from_destination()
  306. if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position);
  307. }
  308. destination[X_AXIS] = rx;
  309. destination[Y_AXIS] = ry;
  310. prepare_move_to_destination(); // set_current_from_destination()
  311. if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position);
  312. if (rz < current_position[Z_AXIS]) { // lowering?
  313. destination[Z_AXIS] = rz;
  314. prepare_uninterpolated_move_to_destination(z_feedrate); // set_current_from_destination()
  315. if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position);
  316. }
  317. #elif IS_SCARA
  318. if (!position_is_reachable(rx, ry)) return;
  319. set_destination_from_current();
  320. // If Z needs to raise, do it before moving XY
  321. if (destination[Z_AXIS] < rz) {
  322. destination[Z_AXIS] = rz;
  323. prepare_uninterpolated_move_to_destination(z_feedrate);
  324. }
  325. destination[X_AXIS] = rx;
  326. destination[Y_AXIS] = ry;
  327. prepare_uninterpolated_move_to_destination(xy_feedrate);
  328. // If Z needs to lower, do it after moving XY
  329. if (destination[Z_AXIS] > rz) {
  330. destination[Z_AXIS] = rz;
  331. prepare_uninterpolated_move_to_destination(z_feedrate);
  332. }
  333. #else
  334. // If Z needs to raise, do it before moving XY
  335. if (current_position[Z_AXIS] < rz) {
  336. current_position[Z_AXIS] = rz;
  337. line_to_current_position(z_feedrate);
  338. }
  339. current_position[X_AXIS] = rx;
  340. current_position[Y_AXIS] = ry;
  341. line_to_current_position(xy_feedrate);
  342. // If Z needs to lower, do it after moving XY
  343. if (current_position[Z_AXIS] > rz) {
  344. current_position[Z_AXIS] = rz;
  345. line_to_current_position(z_feedrate);
  346. }
  347. #endif
  348. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< do_blocking_move_to");
  349. planner.synchronize();
  350. }
  351. void do_blocking_move_to_x(const float &rx, const float &fr_mm_s/*=0.0*/) {
  352. do_blocking_move_to(rx, current_position[Y_AXIS], current_position[Z_AXIS], fr_mm_s);
  353. }
  354. void do_blocking_move_to_y(const float &ry, const float &fr_mm_s/*=0.0*/) {
  355. do_blocking_move_to(current_position[X_AXIS], ry, current_position[Z_AXIS], fr_mm_s);
  356. }
  357. void do_blocking_move_to_z(const float &rz, const float &fr_mm_s/*=0.0*/) {
  358. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], rz, fr_mm_s);
  359. }
  360. void do_blocking_move_to_xy(const float &rx, const float &ry, const float &fr_mm_s/*=0.0*/) {
  361. do_blocking_move_to(rx, ry, current_position[Z_AXIS], fr_mm_s);
  362. }
  363. //
  364. // Prepare to do endstop or probe moves with custom feedrates.
  365. // - Save / restore current feedrate and multiplier
  366. //
  367. static float saved_feedrate_mm_s;
  368. static int16_t saved_feedrate_percentage;
  369. void setup_for_endstop_or_probe_move() {
  370. saved_feedrate_mm_s = feedrate_mm_s;
  371. saved_feedrate_percentage = feedrate_percentage;
  372. feedrate_percentage = 100;
  373. }
  374. void clean_up_after_endstop_or_probe_move() {
  375. feedrate_mm_s = saved_feedrate_mm_s;
  376. feedrate_percentage = saved_feedrate_percentage;
  377. }
  378. #if HAS_SOFTWARE_ENDSTOPS
  379. bool soft_endstops_enabled = true;
  380. // Software Endstops are based on the configured limits.
  381. axis_limits_t soft_endstop[XYZ] = { { X_MIN_BED, X_MAX_BED }, { Y_MIN_BED, Y_MAX_BED }, { Z_MIN_POS, Z_MAX_POS } };
  382. /**
  383. * Software endstops can be used to monitor the open end of
  384. * an axis that has a hardware endstop on the other end. Or
  385. * they can prevent axes from moving past endstops and grinding.
  386. *
  387. * To keep doing their job as the coordinate system changes,
  388. * the software endstop positions must be refreshed to remain
  389. * at the same positions relative to the machine.
  390. */
  391. void update_software_endstops(const AxisEnum axis
  392. #if HAS_HOTEND_OFFSET
  393. , const uint8_t old_tool_index/*=0*/, const uint8_t new_tool_index/*=0*/
  394. #endif
  395. ) {
  396. #if ENABLED(DUAL_X_CARRIAGE)
  397. if (axis == X_AXIS) {
  398. // In Dual X mode hotend_offset[X] is T1's home position
  399. const float dual_max_x = _MAX(hotend_offset[X_AXIS][1], X2_MAX_POS);
  400. if (new_tool_index != 0) {
  401. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  402. soft_endstop[X_AXIS].min = X2_MIN_POS;
  403. soft_endstop[X_AXIS].max = dual_max_x;
  404. }
  405. else if (dxc_is_duplicating()) {
  406. // In Duplication Mode, T0 can move as far left as X1_MIN_POS
  407. // but not so far to the right that T1 would move past the end
  408. soft_endstop[X_AXIS].min = X1_MIN_POS;
  409. soft_endstop[X_AXIS].max = _MIN(X1_MAX_POS, dual_max_x - duplicate_extruder_x_offset);
  410. }
  411. else {
  412. // In other modes, T0 can move from X1_MIN_POS to X1_MAX_POS
  413. soft_endstop[X_AXIS].min = X1_MIN_POS;
  414. soft_endstop[X_AXIS].max = X1_MAX_POS;
  415. }
  416. }
  417. #elif ENABLED(DELTA)
  418. soft_endstop[axis].min = base_min_pos(axis);
  419. soft_endstop[axis].max = (axis == Z_AXIS ? delta_height
  420. #if HAS_BED_PROBE
  421. - zprobe_zoffset
  422. #endif
  423. : base_max_pos(axis));
  424. switch (axis) {
  425. case X_AXIS:
  426. case Y_AXIS:
  427. // Get a minimum radius for clamping
  428. delta_max_radius = _MIN(ABS(_MAX(soft_endstop[X_AXIS].min, soft_endstop[Y_AXIS].min)), soft_endstop[X_AXIS].max, soft_endstop[Y_AXIS].max);
  429. delta_max_radius_2 = sq(delta_max_radius);
  430. break;
  431. case Z_AXIS:
  432. delta_clip_start_height = soft_endstop[axis].max - delta_safe_distance_from_top();
  433. default: break;
  434. }
  435. #elif HAS_HOTEND_OFFSET
  436. // Software endstops are relative to the tool 0 workspace, so
  437. // the movement limits must be shifted by the tool offset to
  438. // retain the same physical limit when other tools are selected.
  439. if (old_tool_index != new_tool_index) {
  440. const float offs = hotend_offset[axis][new_tool_index] - hotend_offset[axis][old_tool_index];
  441. soft_endstop[axis].min += offs;
  442. soft_endstop[axis].max += offs;
  443. }
  444. else {
  445. const float offs = hotend_offset[axis][active_extruder];
  446. soft_endstop[axis].min = base_min_pos(axis) + offs;
  447. soft_endstop[axis].max = base_max_pos(axis) + offs;
  448. }
  449. #else
  450. soft_endstop[axis].min = base_min_pos(axis);
  451. soft_endstop[axis].max = base_max_pos(axis);
  452. #endif
  453. if (DEBUGGING(LEVELING))
  454. SERIAL_ECHOLNPAIR("Axis ", axis_codes[axis], " min:", soft_endstop[axis].min, " max:", soft_endstop[axis].max);
  455. }
  456. /**
  457. * Constrain the given coordinates to the software endstops.
  458. *
  459. * For DELTA/SCARA the XY constraint is based on the smallest
  460. * radius within the set software endstops.
  461. */
  462. void apply_motion_limits(float target[XYZ]) {
  463. if (!soft_endstops_enabled) return;
  464. #if IS_KINEMATIC
  465. #if HAS_HOTEND_OFFSET && ENABLED(DELTA)
  466. // The effector center position will be the target minus the hotend offset.
  467. const float offx = hotend_offset[X_AXIS][active_extruder], offy = hotend_offset[Y_AXIS][active_extruder];
  468. #else
  469. // SCARA needs to consider the angle of the arm through the entire move, so for now use no tool offset.
  470. constexpr float offx = 0, offy = 0;
  471. #endif
  472. const float dist_2 = HYPOT2(target[X_AXIS] - offx, target[Y_AXIS] - offy);
  473. if (dist_2 > delta_max_radius_2) {
  474. const float ratio = (delta_max_radius) / SQRT(dist_2); // 200 / 300 = 0.66
  475. target[X_AXIS] *= ratio;
  476. target[Y_AXIS] *= ratio;
  477. }
  478. #else
  479. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  480. NOLESS(target[X_AXIS], soft_endstop[X_AXIS].min);
  481. #endif
  482. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  483. NOMORE(target[X_AXIS], soft_endstop[X_AXIS].max);
  484. #endif
  485. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  486. NOLESS(target[Y_AXIS], soft_endstop[Y_AXIS].min);
  487. #endif
  488. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  489. NOMORE(target[Y_AXIS], soft_endstop[Y_AXIS].max);
  490. #endif
  491. #endif
  492. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  493. NOLESS(target[Z_AXIS], soft_endstop[Z_AXIS].min);
  494. #endif
  495. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  496. NOMORE(target[Z_AXIS], soft_endstop[Z_AXIS].max);
  497. #endif
  498. }
  499. #endif // HAS_SOFTWARE_ENDSTOPS
  500. #if !UBL_SEGMENTED
  501. #if IS_KINEMATIC
  502. #if IS_SCARA
  503. /**
  504. * Before raising this value, use M665 S[seg_per_sec] to decrease
  505. * the number of segments-per-second. Default is 200. Some deltas
  506. * do better with 160 or lower. It would be good to know how many
  507. * segments-per-second are actually possible for SCARA on AVR.
  508. *
  509. * Longer segments result in less kinematic overhead
  510. * but may produce jagged lines. Try 0.5mm, 1.0mm, and 2.0mm
  511. * and compare the difference.
  512. */
  513. #define SCARA_MIN_SEGMENT_LENGTH 0.5f
  514. #endif
  515. /**
  516. * Prepare a linear move in a DELTA or SCARA setup.
  517. *
  518. * Called from prepare_move_to_destination as the
  519. * default Delta/SCARA segmenter.
  520. *
  521. * This calls planner.buffer_line several times, adding
  522. * small incremental moves for DELTA or SCARA.
  523. *
  524. * For Unified Bed Leveling (Delta or Segmented Cartesian)
  525. * the ubl.prepare_segmented_line_to method replaces this.
  526. *
  527. * For Auto Bed Leveling (Bilinear) with SEGMENT_LEVELED_MOVES
  528. * this is replaced by segmented_line_to_destination below.
  529. */
  530. inline bool prepare_kinematic_move_to(const float (&rtarget)[XYZE]) {
  531. // Get the top feedrate of the move in the XY plane
  532. const float _feedrate_mm_s = MMS_SCALED(feedrate_mm_s);
  533. const float xdiff = rtarget[X_AXIS] - current_position[X_AXIS],
  534. ydiff = rtarget[Y_AXIS] - current_position[Y_AXIS];
  535. // If the move is only in Z/E don't split up the move
  536. if (!xdiff && !ydiff) {
  537. planner.buffer_line(rtarget, _feedrate_mm_s, active_extruder);
  538. return false; // caller will update current_position
  539. }
  540. // Fail if attempting move outside printable radius
  541. if (!position_is_reachable(rtarget[X_AXIS], rtarget[Y_AXIS])) return true;
  542. // Remaining cartesian distances
  543. const float zdiff = rtarget[Z_AXIS] - current_position[Z_AXIS],
  544. ediff = rtarget[E_AXIS] - current_position[E_AXIS];
  545. // Get the linear distance in XYZ
  546. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  547. // If the move is very short, check the E move distance
  548. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(ediff);
  549. // No E move either? Game over.
  550. if (UNEAR_ZERO(cartesian_mm)) return true;
  551. // Minimum number of seconds to move the given distance
  552. const float seconds = cartesian_mm / _feedrate_mm_s;
  553. // The number of segments-per-second times the duration
  554. // gives the number of segments
  555. uint16_t segments = delta_segments_per_second * seconds;
  556. // For SCARA enforce a minimum segment size
  557. #if IS_SCARA
  558. NOMORE(segments, cartesian_mm * RECIPROCAL(SCARA_MIN_SEGMENT_LENGTH));
  559. #endif
  560. // At least one segment is required
  561. NOLESS(segments, 1U);
  562. // The approximate length of each segment
  563. const float inv_segments = 1.0f / float(segments),
  564. segment_distance[XYZE] = {
  565. xdiff * inv_segments,
  566. ydiff * inv_segments,
  567. zdiff * inv_segments,
  568. ediff * inv_segments
  569. },
  570. cartesian_segment_mm = cartesian_mm * inv_segments;
  571. #if ENABLED(SCARA_FEEDRATE_SCALING)
  572. const float inv_duration = _feedrate_mm_s / cartesian_segment_mm;
  573. #endif
  574. /*
  575. SERIAL_ECHOPAIR("mm=", cartesian_mm);
  576. SERIAL_ECHOPAIR(" seconds=", seconds);
  577. SERIAL_ECHOPAIR(" segments=", segments);
  578. SERIAL_ECHOPAIR(" segment_mm=", cartesian_segment_mm);
  579. SERIAL_EOL();
  580. //*/
  581. // Get the current position as starting point
  582. float raw[XYZE];
  583. COPY(raw, current_position);
  584. // Calculate and execute the segments
  585. while (--segments) {
  586. static millis_t next_idle_ms = millis() + 200UL;
  587. thermalManager.manage_heater(); // This returns immediately if not really needed.
  588. if (ELAPSED(millis(), next_idle_ms)) {
  589. next_idle_ms = millis() + 200UL;
  590. idle();
  591. }
  592. LOOP_XYZE(i) raw[i] += segment_distance[i];
  593. if (!planner.buffer_line(raw, _feedrate_mm_s, active_extruder, cartesian_segment_mm
  594. #if ENABLED(SCARA_FEEDRATE_SCALING)
  595. , inv_duration
  596. #endif
  597. ))
  598. break;
  599. }
  600. // Ensure last segment arrives at target location.
  601. planner.buffer_line(rtarget, _feedrate_mm_s, active_extruder, cartesian_segment_mm
  602. #if ENABLED(SCARA_FEEDRATE_SCALING)
  603. , inv_duration
  604. #endif
  605. );
  606. return false; // caller will update current_position
  607. }
  608. #else // !IS_KINEMATIC
  609. #if ENABLED(SEGMENT_LEVELED_MOVES)
  610. /**
  611. * Prepare a segmented move on a CARTESIAN setup.
  612. *
  613. * This calls planner.buffer_line several times, adding
  614. * small incremental moves. This allows the planner to
  615. * apply more detailed bed leveling to the full move.
  616. */
  617. inline void segmented_line_to_destination(const float &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
  618. const float xdiff = destination[X_AXIS] - current_position[X_AXIS],
  619. ydiff = destination[Y_AXIS] - current_position[Y_AXIS];
  620. // If the move is only in Z/E don't split up the move
  621. if (!xdiff && !ydiff) {
  622. planner.buffer_line(destination, fr_mm_s, active_extruder);
  623. return;
  624. }
  625. // Remaining cartesian distances
  626. const float zdiff = destination[Z_AXIS] - current_position[Z_AXIS],
  627. ediff = destination[E_AXIS] - current_position[E_AXIS];
  628. // Get the linear distance in XYZ
  629. // If the move is very short, check the E move distance
  630. // No E move either? Game over.
  631. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  632. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(ediff);
  633. if (UNEAR_ZERO(cartesian_mm)) return;
  634. // The length divided by the segment size
  635. // At least one segment is required
  636. uint16_t segments = cartesian_mm / segment_size;
  637. NOLESS(segments, 1U);
  638. // The approximate length of each segment
  639. const float inv_segments = 1.0f / float(segments),
  640. cartesian_segment_mm = cartesian_mm * inv_segments,
  641. segment_distance[XYZE] = {
  642. xdiff * inv_segments,
  643. ydiff * inv_segments,
  644. zdiff * inv_segments,
  645. ediff * inv_segments
  646. };
  647. #if ENABLED(SCARA_FEEDRATE_SCALING)
  648. const float inv_duration = _feedrate_mm_s / cartesian_segment_mm;
  649. #endif
  650. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  651. // SERIAL_ECHOLNPAIR(" segments=", segments);
  652. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  653. // Get the raw current position as starting point
  654. float raw[XYZE];
  655. COPY(raw, current_position);
  656. // Calculate and execute the segments
  657. while (--segments) {
  658. static millis_t next_idle_ms = millis() + 200UL;
  659. thermalManager.manage_heater(); // This returns immediately if not really needed.
  660. if (ELAPSED(millis(), next_idle_ms)) {
  661. next_idle_ms = millis() + 200UL;
  662. idle();
  663. }
  664. LOOP_XYZE(i) raw[i] += segment_distance[i];
  665. if (!planner.buffer_line(raw, fr_mm_s, active_extruder, cartesian_segment_mm
  666. #if ENABLED(SCARA_FEEDRATE_SCALING)
  667. , inv_duration
  668. #endif
  669. ))
  670. break;
  671. }
  672. // Since segment_distance is only approximate,
  673. // the final move must be to the exact destination.
  674. planner.buffer_line(destination, fr_mm_s, active_extruder, cartesian_segment_mm
  675. #if ENABLED(SCARA_FEEDRATE_SCALING)
  676. , inv_duration
  677. #endif
  678. );
  679. }
  680. #endif // SEGMENT_LEVELED_MOVES
  681. /**
  682. * Prepare a linear move in a Cartesian setup.
  683. *
  684. * When a mesh-based leveling system is active, moves are segmented
  685. * according to the configuration of the leveling system.
  686. *
  687. * Returns true if current_position[] was set to destination[]
  688. */
  689. inline bool prepare_move_to_destination_cartesian() {
  690. #if HAS_MESH
  691. if (planner.leveling_active && planner.leveling_active_at_z(destination[Z_AXIS])) {
  692. #if ENABLED(AUTO_BED_LEVELING_UBL)
  693. ubl.line_to_destination_cartesian(MMS_SCALED(feedrate_mm_s), active_extruder); // UBL's motion routine needs to know about
  694. return true; // all moves, including Z-only moves.
  695. #elif ENABLED(SEGMENT_LEVELED_MOVES)
  696. segmented_line_to_destination(MMS_SCALED(feedrate_mm_s));
  697. return false; // caller will update current_position
  698. #else
  699. /**
  700. * For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
  701. * Otherwise fall through to do a direct single move.
  702. */
  703. if (current_position[X_AXIS] != destination[X_AXIS] || current_position[Y_AXIS] != destination[Y_AXIS]) {
  704. #if ENABLED(MESH_BED_LEVELING)
  705. mbl.line_to_destination(MMS_SCALED(feedrate_mm_s));
  706. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  707. bilinear_line_to_destination(MMS_SCALED(feedrate_mm_s));
  708. #endif
  709. return true;
  710. }
  711. #endif
  712. }
  713. #endif // HAS_MESH
  714. buffer_line_to_destination(MMS_SCALED(feedrate_mm_s));
  715. return false; // caller will update current_position
  716. }
  717. #endif // !IS_KINEMATIC
  718. #endif // !UBL_SEGMENTED
  719. #if HAS_DUPLICATION_MODE
  720. bool extruder_duplication_enabled,
  721. mirrored_duplication_mode;
  722. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  723. uint8_t duplication_e_mask; // = 0
  724. #endif
  725. #endif
  726. #if ENABLED(DUAL_X_CARRIAGE)
  727. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  728. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  729. raised_parked_position[XYZE], // used in mode 1
  730. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  731. bool active_extruder_parked = false; // used in mode 1 & 2
  732. millis_t delayed_move_time = 0; // used in mode 1
  733. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  734. float x_home_pos(const int extruder) {
  735. if (extruder == 0)
  736. return base_home_pos(X_AXIS);
  737. else
  738. /**
  739. * In dual carriage mode the extruder offset provides an override of the
  740. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  741. * This allows soft recalibration of the second extruder home position
  742. * without firmware reflash (through the M218 command).
  743. */
  744. return hotend_offset[X_AXIS][1] > 0 ? hotend_offset[X_AXIS][1] : X2_HOME_POS;
  745. }
  746. /**
  747. * Prepare a linear move in a dual X axis setup
  748. *
  749. * Return true if current_position[] was set to destination[]
  750. */
  751. inline bool dual_x_carriage_unpark() {
  752. if (active_extruder_parked) {
  753. switch (dual_x_carriage_mode) {
  754. case DXC_FULL_CONTROL_MODE:
  755. break;
  756. case DXC_AUTO_PARK_MODE:
  757. if (current_position[E_AXIS] == destination[E_AXIS]) {
  758. // This is a travel move (with no extrusion)
  759. // Skip it, but keep track of the current position
  760. // (so it can be used as the start of the next non-travel move)
  761. if (delayed_move_time != 0xFFFFFFFFUL) {
  762. set_current_from_destination();
  763. NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
  764. delayed_move_time = millis();
  765. return true;
  766. }
  767. }
  768. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  769. #define CUR_X current_position[X_AXIS]
  770. #define CUR_Y current_position[Y_AXIS]
  771. #define CUR_Z current_position[Z_AXIS]
  772. #define CUR_E current_position[E_AXIS]
  773. #define RAISED_X raised_parked_position[X_AXIS]
  774. #define RAISED_Y raised_parked_position[Y_AXIS]
  775. #define RAISED_Z raised_parked_position[Z_AXIS]
  776. if ( planner.buffer_line(RAISED_X, RAISED_Y, RAISED_Z, CUR_E, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder))
  777. if (planner.buffer_line( CUR_X, CUR_Y, RAISED_Z, CUR_E, PLANNER_XY_FEEDRATE(), active_extruder))
  778. planner.buffer_line( CUR_X, CUR_Y, CUR_Z, CUR_E, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder);
  779. delayed_move_time = 0;
  780. active_extruder_parked = false;
  781. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Clear active_extruder_parked");
  782. break;
  783. case DXC_MIRRORED_MODE:
  784. case DXC_DUPLICATION_MODE:
  785. if (active_extruder == 0) {
  786. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Set planner X", inactive_extruder_x_pos, " ... Line to X", current_position[X_AXIS] + duplicate_extruder_x_offset);
  787. // move duplicate extruder into correct duplication position.
  788. planner.set_position_mm(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  789. if (!planner.buffer_line(
  790. dual_x_carriage_mode == DXC_DUPLICATION_MODE ? duplicate_extruder_x_offset + current_position[X_AXIS] : inactive_extruder_x_pos,
  791. current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
  792. planner.settings.max_feedrate_mm_s[X_AXIS], 1
  793. )
  794. ) break;
  795. planner.synchronize();
  796. sync_plan_position();
  797. extruder_duplication_enabled = true;
  798. active_extruder_parked = false;
  799. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  800. }
  801. else if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Active extruder not 0");
  802. break;
  803. }
  804. }
  805. stepper.set_directions();
  806. return false;
  807. }
  808. #endif // DUAL_X_CARRIAGE
  809. /**
  810. * Prepare a single move and get ready for the next one
  811. *
  812. * This may result in several calls to planner.buffer_line to
  813. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  814. *
  815. * Make sure current_position[E] and destination[E] are good
  816. * before calling or cold/lengthy extrusion may get missed.
  817. */
  818. void prepare_move_to_destination() {
  819. apply_motion_limits(destination);
  820. #if EITHER(PREVENT_COLD_EXTRUSION, PREVENT_LENGTHY_EXTRUDE)
  821. if (!DEBUGGING(DRYRUN)) {
  822. if (destination[E_AXIS] != current_position[E_AXIS]) {
  823. #if ENABLED(PREVENT_COLD_EXTRUSION)
  824. if (thermalManager.tooColdToExtrude(active_extruder)) {
  825. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  826. SERIAL_ECHO_MSG(MSG_ERR_COLD_EXTRUDE_STOP);
  827. }
  828. #endif // PREVENT_COLD_EXTRUSION
  829. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  830. const float e_delta = ABS(destination[E_AXIS] - current_position[E_AXIS]) * planner.e_factor[active_extruder];
  831. if (e_delta > (EXTRUDE_MAXLENGTH)) {
  832. #if ENABLED(MIXING_EXTRUDER)
  833. bool ignore_e = false;
  834. float collector[MIXING_STEPPERS];
  835. mixer.refresh_collector(1.0, mixer.get_current_vtool(), collector);
  836. MIXER_STEPPER_LOOP(e)
  837. if (e_delta * collector[e] > (EXTRUDE_MAXLENGTH)) { ignore_e = true; break; }
  838. #else
  839. constexpr bool ignore_e = true;
  840. #endif
  841. if (ignore_e) {
  842. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  843. SERIAL_ECHO_MSG(MSG_ERR_LONG_EXTRUDE_STOP);
  844. }
  845. }
  846. #endif // PREVENT_LENGTHY_EXTRUDE
  847. }
  848. }
  849. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  850. #if ENABLED(DUAL_X_CARRIAGE)
  851. if (dual_x_carriage_unpark()) return;
  852. #endif
  853. if (
  854. #if UBL_SEGMENTED
  855. //ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s)) // This doesn't seem to work correctly on UBL.
  856. #if IS_KINEMATIC // Use Kinematic / Cartesian cases as a workaround for now.
  857. ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s))
  858. #else
  859. prepare_move_to_destination_cartesian()
  860. #endif
  861. #elif IS_KINEMATIC
  862. prepare_kinematic_move_to(destination)
  863. #else
  864. prepare_move_to_destination_cartesian()
  865. #endif
  866. ) return;
  867. set_current_from_destination();
  868. }
  869. bool axis_unhomed_error(const bool x/*=true*/, const bool y/*=true*/, const bool z/*=true*/) {
  870. #if ENABLED(HOME_AFTER_DEACTIVATE)
  871. const bool xx = x && !TEST(axis_known_position, X_AXIS),
  872. yy = y && !TEST(axis_known_position, Y_AXIS),
  873. zz = z && !TEST(axis_known_position, Z_AXIS);
  874. #else
  875. const bool xx = x && !TEST(axis_homed, X_AXIS),
  876. yy = y && !TEST(axis_homed, Y_AXIS),
  877. zz = z && !TEST(axis_homed, Z_AXIS);
  878. #endif
  879. if (xx || yy || zz) {
  880. SERIAL_ECHO_START();
  881. SERIAL_ECHOPGM(MSG_HOME " ");
  882. if (xx) SERIAL_CHAR('X');
  883. if (yy) SERIAL_CHAR('Y');
  884. if (zz) SERIAL_CHAR('Z');
  885. SERIAL_ECHOLNPGM(" " MSG_FIRST);
  886. #if HAS_DISPLAY
  887. ui.status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
  888. #endif
  889. return true;
  890. }
  891. return false;
  892. }
  893. /**
  894. * Homing bump feedrate (mm/s)
  895. */
  896. float get_homing_bump_feedrate(const AxisEnum axis) {
  897. #if HOMING_Z_WITH_PROBE
  898. if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW);
  899. #endif
  900. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  901. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  902. if (hbd < 1) {
  903. hbd = 10;
  904. SERIAL_ECHO_MSG("Warning: Homing Bump Divisor < 1");
  905. }
  906. return homing_feedrate(axis) / hbd;
  907. }
  908. #if ENABLED(SENSORLESS_HOMING)
  909. /**
  910. * Set sensorless homing if the axis has it, accounting for Core Kinematics.
  911. */
  912. sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis) {
  913. sensorless_t stealth_states { false };
  914. switch (axis) {
  915. default: break;
  916. #if X_SENSORLESS
  917. case X_AXIS:
  918. stealth_states.x = tmc_enable_stallguard(stepperX);
  919. #if AXIS_HAS_STALLGUARD(X2)
  920. stealth_states.x2 = tmc_enable_stallguard(stepperX2);
  921. #endif
  922. #if CORE_IS_XY && Y_SENSORLESS
  923. stealth_states.y = tmc_enable_stallguard(stepperY);
  924. #elif CORE_IS_XZ && Z_SENSORLESS
  925. stealth_states.z = tmc_enable_stallguard(stepperZ);
  926. #endif
  927. break;
  928. #endif
  929. #if Y_SENSORLESS
  930. case Y_AXIS:
  931. stealth_states.y = tmc_enable_stallguard(stepperY);
  932. #if AXIS_HAS_STALLGUARD(Y2)
  933. stealth_states.y2 = tmc_enable_stallguard(stepperY2);
  934. #endif
  935. #if CORE_IS_XY && X_SENSORLESS
  936. stealth_states.x = tmc_enable_stallguard(stepperX);
  937. #elif CORE_IS_YZ && Z_SENSORLESS
  938. stealth_states.z = tmc_enable_stallguard(stepperZ);
  939. #endif
  940. break;
  941. #endif
  942. #if Z_SENSORLESS
  943. case Z_AXIS:
  944. stealth_states.z = tmc_enable_stallguard(stepperZ);
  945. #if AXIS_HAS_STALLGUARD(Z2)
  946. stealth_states.z2 = tmc_enable_stallguard(stepperZ2);
  947. #endif
  948. #if AXIS_HAS_STALLGUARD(Z3)
  949. stealth_states.z3 = tmc_enable_stallguard(stepperZ3);
  950. #endif
  951. #if CORE_IS_XZ && X_SENSORLESS
  952. stealth_states.x = tmc_enable_stallguard(stepperX);
  953. #elif CORE_IS_YZ && Y_SENSORLESS
  954. stealth_states.y = tmc_enable_stallguard(stepperY);
  955. #endif
  956. break;
  957. #endif
  958. }
  959. #if ENABLED(SPI_ENDSTOPS)
  960. switch (axis) {
  961. #if X_SPI_SENSORLESS
  962. case X_AXIS: endstops.tmc_spi_homing.x = true; break;
  963. #endif
  964. #if Y_SPI_SENSORLESS
  965. case Y_AXIS: endstops.tmc_spi_homing.y = true; break;
  966. #endif
  967. #if Z_SPI_SENSORLESS
  968. case Z_AXIS: endstops.tmc_spi_homing.z = true; break;
  969. #endif
  970. default: break;
  971. }
  972. #endif
  973. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  974. sg_guard_period = millis() + default_sg_guard_duration;
  975. #endif
  976. return stealth_states;
  977. }
  978. void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) {
  979. switch (axis) {
  980. default: break;
  981. #if X_SENSORLESS
  982. case X_AXIS:
  983. tmc_disable_stallguard(stepperX, enable_stealth.x);
  984. #if AXIS_HAS_STALLGUARD(X2)
  985. tmc_disable_stallguard(stepperX2, enable_stealth.x2);
  986. #endif
  987. #if CORE_IS_XY && Y_SENSORLESS
  988. tmc_disable_stallguard(stepperY, enable_stealth.y);
  989. #elif CORE_IS_XZ && Z_SENSORLESS
  990. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  991. #endif
  992. break;
  993. #endif
  994. #if Y_SENSORLESS
  995. case Y_AXIS:
  996. tmc_disable_stallguard(stepperY, enable_stealth.y);
  997. #if AXIS_HAS_STALLGUARD(Y2)
  998. tmc_disable_stallguard(stepperY2, enable_stealth.y2);
  999. #endif
  1000. #if CORE_IS_XY && X_SENSORLESS
  1001. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1002. #elif CORE_IS_YZ && Z_SENSORLESS
  1003. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1004. #endif
  1005. break;
  1006. #endif
  1007. #if Z_SENSORLESS
  1008. case Z_AXIS:
  1009. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1010. #if AXIS_HAS_STALLGUARD(Z2)
  1011. tmc_disable_stallguard(stepperZ2, enable_stealth.z2);
  1012. #endif
  1013. #if AXIS_HAS_STALLGUARD(Z3)
  1014. tmc_disable_stallguard(stepperZ3, enable_stealth.z3);
  1015. #endif
  1016. #if CORE_IS_XZ && X_SENSORLESS
  1017. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1018. #elif CORE_IS_YZ && Y_SENSORLESS
  1019. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1020. #endif
  1021. break;
  1022. #endif
  1023. }
  1024. #if ENABLED(SPI_ENDSTOPS)
  1025. switch (axis) {
  1026. #if X_SPI_SENSORLESS
  1027. case X_AXIS: endstops.tmc_spi_homing.x = false; break;
  1028. #endif
  1029. #if Y_SPI_SENSORLESS
  1030. case Y_AXIS: endstops.tmc_spi_homing.y = false; break;
  1031. #endif
  1032. #if Z_SPI_SENSORLESS
  1033. case Z_AXIS: endstops.tmc_spi_homing.z = false; break;
  1034. #endif
  1035. default: break;
  1036. }
  1037. #endif
  1038. }
  1039. #endif // SENSORLESS_HOMING
  1040. /**
  1041. * Home an individual linear axis
  1042. */
  1043. void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) {
  1044. if (DEBUGGING(LEVELING)) {
  1045. DEBUG_ECHOPAIR(">>> do_homing_move(", axis_codes[axis], ", ", distance, ", ");
  1046. if (fr_mm_s)
  1047. DEBUG_ECHO(fr_mm_s);
  1048. else
  1049. DEBUG_ECHOPAIR("[", homing_feedrate(axis), "]");
  1050. DEBUG_ECHOLNPGM(")");
  1051. }
  1052. #if HOMING_Z_WITH_PROBE && HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  1053. // Wait for bed to heat back up between probing points
  1054. if (axis == Z_AXIS && distance < 0 && thermalManager.isHeatingBed()) {
  1055. serialprintPGM(msg_wait_for_bed_heating);
  1056. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1057. thermalManager.wait_for_bed();
  1058. ui.reset_status();
  1059. }
  1060. #endif
  1061. // Only do some things when moving towards an endstop
  1062. const int8_t axis_home_dir =
  1063. #if ENABLED(DUAL_X_CARRIAGE)
  1064. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  1065. #endif
  1066. home_dir(axis);
  1067. const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
  1068. #if ENABLED(SENSORLESS_HOMING)
  1069. sensorless_t stealth_states;
  1070. #endif
  1071. if (is_home_dir) {
  1072. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1073. if (axis == Z_AXIS) probing_pause(true);
  1074. #endif
  1075. // Disable stealthChop if used. Enable diag1 pin on driver.
  1076. #if ENABLED(SENSORLESS_HOMING)
  1077. stealth_states = start_sensorless_homing_per_axis(axis);
  1078. #endif
  1079. }
  1080. #if IS_SCARA
  1081. // Tell the planner the axis is at 0
  1082. current_position[axis] = 0;
  1083. sync_plan_position();
  1084. current_position[axis] = distance;
  1085. planner.buffer_line(current_position, fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
  1086. #else
  1087. float target[ABCE] = { planner.get_axis_position_mm(A_AXIS), planner.get_axis_position_mm(B_AXIS), planner.get_axis_position_mm(C_AXIS), planner.get_axis_position_mm(E_AXIS) };
  1088. target[axis] = 0;
  1089. planner.set_machine_position_mm(target);
  1090. target[axis] = distance;
  1091. #if IS_KINEMATIC && ENABLED(JUNCTION_DEVIATION)
  1092. const float delta_mm_cart[XYZE] = {0, 0, 0, 0};
  1093. #endif
  1094. // Set delta/cartesian axes directly
  1095. planner.buffer_segment(target
  1096. #if IS_KINEMATIC && ENABLED(JUNCTION_DEVIATION)
  1097. , delta_mm_cart
  1098. #endif
  1099. , fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder
  1100. );
  1101. #endif
  1102. planner.synchronize();
  1103. if (is_home_dir) {
  1104. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1105. if (axis == Z_AXIS) probing_pause(false);
  1106. #endif
  1107. endstops.validate_homing_move();
  1108. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  1109. #if ENABLED(SENSORLESS_HOMING)
  1110. end_sensorless_homing_per_axis(axis, stealth_states);
  1111. #endif
  1112. }
  1113. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< do_homing_move(", axis_codes[axis], ")");
  1114. }
  1115. /**
  1116. * Set an axis' current position to its home position (after homing).
  1117. *
  1118. * For Core and Cartesian robots this applies one-to-one when an
  1119. * individual axis has been homed.
  1120. *
  1121. * DELTA should wait until all homing is done before setting the XYZ
  1122. * current_position to home, because homing is a single operation.
  1123. * In the case where the axis positions are already known and previously
  1124. * homed, DELTA could home to X or Y individually by moving either one
  1125. * to the center. However, homing Z always homes XY and Z.
  1126. *
  1127. * SCARA should wait until all XY homing is done before setting the XY
  1128. * current_position to home, because neither X nor Y is at home until
  1129. * both are at home. Z can however be homed individually.
  1130. *
  1131. * Callers must sync the planner position after calling this!
  1132. */
  1133. void set_axis_is_at_home(const AxisEnum axis) {
  1134. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_at_home(", axis_codes[axis], ")");
  1135. SBI(axis_known_position, axis);
  1136. SBI(axis_homed, axis);
  1137. #if ENABLED(DUAL_X_CARRIAGE)
  1138. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  1139. current_position[X_AXIS] = x_home_pos(active_extruder);
  1140. return;
  1141. }
  1142. #endif
  1143. #if ENABLED(MORGAN_SCARA)
  1144. scara_set_axis_is_at_home(axis);
  1145. #elif ENABLED(DELTA)
  1146. current_position[axis] = (axis == Z_AXIS ? delta_height
  1147. #if HAS_BED_PROBE
  1148. - zprobe_zoffset
  1149. #endif
  1150. : base_home_pos(axis));
  1151. #else
  1152. current_position[axis] = base_home_pos(axis);
  1153. #endif
  1154. /**
  1155. * Z Probe Z Homing? Account for the probe's Z offset.
  1156. */
  1157. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  1158. if (axis == Z_AXIS) {
  1159. #if HOMING_Z_WITH_PROBE
  1160. current_position[Z_AXIS] -= zprobe_zoffset;
  1161. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***\n> zprobe_zoffset = ", zprobe_zoffset);
  1162. #else
  1163. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("*** Z HOMED TO ENDSTOP ***");
  1164. #endif
  1165. }
  1166. #endif
  1167. #if ENABLED(I2C_POSITION_ENCODERS)
  1168. I2CPEM.homed(axis);
  1169. #endif
  1170. #if ENABLED(BABYSTEP_DISPLAY_TOTAL)
  1171. babystep.reset_total(axis);
  1172. #endif
  1173. if (DEBUGGING(LEVELING)) {
  1174. #if HAS_HOME_OFFSET
  1175. DEBUG_ECHOLNPAIR("> home_offset[", axis_codes[axis], "] = ", home_offset[axis]);
  1176. #endif
  1177. DEBUG_POS("", current_position);
  1178. DEBUG_ECHOLNPAIR("<<< set_axis_is_at_home(", axis_codes[axis], ")");
  1179. }
  1180. }
  1181. /**
  1182. * Set an axis' to be unhomed.
  1183. */
  1184. void set_axis_is_not_at_home(const AxisEnum axis) {
  1185. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_not_at_home(", axis_codes[axis], ")");
  1186. CBI(axis_known_position, axis);
  1187. CBI(axis_homed, axis);
  1188. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< set_axis_is_not_at_home(", axis_codes[axis], ")");
  1189. #if ENABLED(I2C_POSITION_ENCODERS)
  1190. I2CPEM.unhomed(axis);
  1191. #endif
  1192. }
  1193. /**
  1194. * Home an individual "raw axis" to its endstop.
  1195. * This applies to XYZ on Cartesian and Core robots, and
  1196. * to the individual ABC steppers on DELTA and SCARA.
  1197. *
  1198. * At the end of the procedure the axis is marked as
  1199. * homed and the current position of that axis is updated.
  1200. * Kinematic robots should wait till all axes are homed
  1201. * before updating the current position.
  1202. */
  1203. void homeaxis(const AxisEnum axis) {
  1204. #if IS_SCARA
  1205. // Only Z homing (with probe) is permitted
  1206. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  1207. #else
  1208. #define _CAN_HOME(A) \
  1209. (axis == _AXIS(A) && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  1210. #if X_SPI_SENSORLESS
  1211. #define CAN_HOME_X true
  1212. #else
  1213. #define CAN_HOME_X _CAN_HOME(X)
  1214. #endif
  1215. #if Y_SPI_SENSORLESS
  1216. #define CAN_HOME_Y true
  1217. #else
  1218. #define CAN_HOME_Y _CAN_HOME(Y)
  1219. #endif
  1220. #if Z_SPI_SENSORLESS
  1221. #define CAN_HOME_Z true
  1222. #else
  1223. #define CAN_HOME_Z _CAN_HOME(Z)
  1224. #endif
  1225. if (!CAN_HOME_X && !CAN_HOME_Y && !CAN_HOME_Z) return;
  1226. #endif
  1227. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")");
  1228. const int axis_home_dir = (
  1229. #if ENABLED(DUAL_X_CARRIAGE)
  1230. axis == X_AXIS ? x_home_dir(active_extruder) :
  1231. #endif
  1232. home_dir(axis)
  1233. );
  1234. // Homing Z towards the bed? Deploy the Z probe or endstop.
  1235. #if HOMING_Z_WITH_PROBE
  1236. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  1237. #endif
  1238. // Set flags for X, Y, Z motor locking
  1239. #if HAS_EXTRA_ENDSTOPS
  1240. switch (axis) {
  1241. #if ENABLED(X_DUAL_ENDSTOPS)
  1242. case X_AXIS:
  1243. #endif
  1244. #if ENABLED(Y_DUAL_ENDSTOPS)
  1245. case Y_AXIS:
  1246. #endif
  1247. #if Z_MULTI_ENDSTOPS
  1248. case Z_AXIS:
  1249. #endif
  1250. stepper.set_separate_multi_axis(true);
  1251. default: break;
  1252. }
  1253. #endif
  1254. // Fast move towards endstop until triggered
  1255. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 1 Fast:");
  1256. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1257. if (axis == Z_AXIS && bltouch.deploy()) return; // The initial DEPLOY
  1258. #endif
  1259. do_homing_move(axis, 1.5f * max_length(
  1260. #if ENABLED(DELTA)
  1261. Z_AXIS
  1262. #else
  1263. axis
  1264. #endif
  1265. ) * axis_home_dir
  1266. );
  1267. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1268. if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE)
  1269. #endif
  1270. // When homing Z with probe respect probe clearance
  1271. const float bump = axis_home_dir * (
  1272. #if HOMING_Z_WITH_PROBE
  1273. (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? _MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) :
  1274. #endif
  1275. home_bump_mm(axis)
  1276. );
  1277. // If a second homing move is configured...
  1278. if (bump) {
  1279. // Move away from the endstop by the axis HOME_BUMP_MM
  1280. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Move Away:");
  1281. do_homing_move(axis, -bump
  1282. #if HOMING_Z_WITH_PROBE
  1283. , axis == Z_AXIS ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) : 0.0
  1284. #endif
  1285. );
  1286. // Slow move towards endstop until triggered
  1287. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 2 Slow:");
  1288. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1289. if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE)
  1290. #endif
  1291. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1292. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1293. if (axis == Z_AXIS) bltouch.stow(); // The final STOW
  1294. #endif
  1295. }
  1296. #if HAS_EXTRA_ENDSTOPS
  1297. const bool pos_dir = axis_home_dir > 0;
  1298. #if ENABLED(X_DUAL_ENDSTOPS)
  1299. if (axis == X_AXIS) {
  1300. const float adj = ABS(endstops.x2_endstop_adj);
  1301. if (adj) {
  1302. if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1303. do_homing_move(axis, pos_dir ? -adj : adj);
  1304. stepper.set_x_lock(false);
  1305. stepper.set_x2_lock(false);
  1306. }
  1307. }
  1308. #endif
  1309. #if ENABLED(Y_DUAL_ENDSTOPS)
  1310. if (axis == Y_AXIS) {
  1311. const float adj = ABS(endstops.y2_endstop_adj);
  1312. if (adj) {
  1313. if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1314. do_homing_move(axis, pos_dir ? -adj : adj);
  1315. stepper.set_y_lock(false);
  1316. stepper.set_y2_lock(false);
  1317. }
  1318. }
  1319. #endif
  1320. #if ENABLED(Z_DUAL_ENDSTOPS)
  1321. if (axis == Z_AXIS) {
  1322. const float adj = ABS(endstops.z2_endstop_adj);
  1323. if (adj) {
  1324. if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  1325. do_homing_move(axis, pos_dir ? -adj : adj);
  1326. stepper.set_z_lock(false);
  1327. stepper.set_z2_lock(false);
  1328. }
  1329. }
  1330. #endif
  1331. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  1332. if (axis == Z_AXIS) {
  1333. // we push the function pointers for the stepper lock function into an array
  1334. void (*lock[3]) (bool)= {&stepper.set_z_lock, &stepper.set_z2_lock, &stepper.set_z3_lock};
  1335. float adj[3] = {0, endstops.z2_endstop_adj, endstops.z3_endstop_adj};
  1336. void (*tempLock) (bool);
  1337. float tempAdj;
  1338. // manual bubble sort by adjust value
  1339. if (adj[1] < adj[0]) {
  1340. tempLock = lock[0], tempAdj = adj[0];
  1341. lock[0] = lock[1], adj[0] = adj[1];
  1342. lock[1] = tempLock, adj[1] = tempAdj;
  1343. }
  1344. if (adj[2] < adj[1]) {
  1345. tempLock = lock[1], tempAdj = adj[1];
  1346. lock[1] = lock[2], adj[1] = adj[2];
  1347. lock[2] = tempLock, adj[2] = tempAdj;
  1348. }
  1349. if (adj[1] < adj[0]) {
  1350. tempLock = lock[0], tempAdj = adj[0];
  1351. lock[0] = lock[1], adj[0] = adj[1];
  1352. lock[1] = tempLock, adj[1] = tempAdj;
  1353. }
  1354. if (pos_dir) {
  1355. // normalize adj to smallest value and do the first move
  1356. (*lock[0])(true);
  1357. do_homing_move(axis, adj[1] - adj[0]);
  1358. // lock the second stepper for the final correction
  1359. (*lock[1])(true);
  1360. do_homing_move(axis, adj[2] - adj[1]);
  1361. }
  1362. else {
  1363. (*lock[2])(true);
  1364. do_homing_move(axis, adj[1] - adj[2]);
  1365. (*lock[1])(true);
  1366. do_homing_move(axis, adj[0] - adj[1]);
  1367. }
  1368. stepper.set_z_lock(false);
  1369. stepper.set_z2_lock(false);
  1370. stepper.set_z3_lock(false);
  1371. }
  1372. #endif
  1373. // Reset flags for X, Y, Z motor locking
  1374. switch (axis) {
  1375. #if ENABLED(X_DUAL_ENDSTOPS)
  1376. case X_AXIS:
  1377. #endif
  1378. #if ENABLED(Y_DUAL_ENDSTOPS)
  1379. case Y_AXIS:
  1380. #endif
  1381. #if Z_MULTI_ENDSTOPS
  1382. case Z_AXIS:
  1383. #endif
  1384. stepper.set_separate_multi_axis(false);
  1385. default: break;
  1386. }
  1387. #endif
  1388. #if IS_SCARA
  1389. set_axis_is_at_home(axis);
  1390. sync_plan_position();
  1391. #elif ENABLED(DELTA)
  1392. // Delta has already moved all three towers up in G28
  1393. // so here it re-homes each tower in turn.
  1394. // Delta homing treats the axes as normal linear axes.
  1395. // retrace by the amount specified in delta_endstop_adj + additional dist in order to have minimum steps
  1396. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  1397. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("delta_endstop_adj:");
  1398. do_homing_move(axis, delta_endstop_adj[axis] - (MIN_STEPS_PER_SEGMENT + 1) * planner.steps_to_mm[axis] * Z_HOME_DIR);
  1399. }
  1400. #else // CARTESIAN / CORE
  1401. set_axis_is_at_home(axis);
  1402. sync_plan_position();
  1403. destination[axis] = current_position[axis];
  1404. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1405. #endif
  1406. // Put away the Z probe
  1407. #if HOMING_Z_WITH_PROBE
  1408. if (axis == Z_AXIS && STOW_PROBE()) return;
  1409. #endif
  1410. #ifdef HOMING_BACKOFF_MM
  1411. constexpr float endstop_backoff[XYZ] = HOMING_BACKOFF_MM;
  1412. const float backoff_mm = endstop_backoff[
  1413. #if ENABLED(DELTA)
  1414. Z_AXIS
  1415. #else
  1416. axis
  1417. #endif
  1418. ];
  1419. if (backoff_mm) {
  1420. current_position[axis] -= ABS(backoff_mm) * axis_home_dir;
  1421. line_to_current_position(
  1422. #if HOMING_Z_WITH_PROBE
  1423. (axis == Z_AXIS) ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) :
  1424. #endif
  1425. homing_feedrate(axis)
  1426. );
  1427. }
  1428. #endif
  1429. // Clear retracted status if homing the Z axis
  1430. #if ENABLED(FWRETRACT)
  1431. if (axis == Z_AXIS) fwretract.current_hop = 0.0;
  1432. #endif
  1433. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< homeaxis(", axis_codes[axis], ")");
  1434. } // homeaxis()
  1435. #if HAS_WORKSPACE_OFFSET
  1436. void update_workspace_offset(const AxisEnum axis) {
  1437. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1438. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Axis ", axis_codes[axis], " home_offset = ", home_offset[axis], " position_shift = ", position_shift[axis]);
  1439. }
  1440. #endif
  1441. #if HAS_M206_COMMAND
  1442. /**
  1443. * Change the home offset for an axis.
  1444. * Also refreshes the workspace offset.
  1445. */
  1446. void set_home_offset(const AxisEnum axis, const float v) {
  1447. home_offset[axis] = v;
  1448. update_workspace_offset(axis);
  1449. }
  1450. #endif // HAS_M206_COMMAND