My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

motion.cpp 56KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  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 remember_feedrate_and_scaling() {
  370. saved_feedrate_mm_s = feedrate_mm_s;
  371. saved_feedrate_percentage = feedrate_percentage;
  372. }
  373. void remember_feedrate_scaling_off() {
  374. remember_feedrate_and_scaling();
  375. feedrate_percentage = 100;
  376. }
  377. void restore_feedrate_and_scaling() {
  378. feedrate_mm_s = saved_feedrate_mm_s;
  379. feedrate_percentage = saved_feedrate_percentage;
  380. }
  381. #if HAS_SOFTWARE_ENDSTOPS
  382. bool soft_endstops_enabled = true;
  383. // Software Endstops are based on the configured limits.
  384. axis_limits_t soft_endstop[XYZ] = { { X_MIN_BED, X_MAX_BED }, { Y_MIN_BED, Y_MAX_BED }, { Z_MIN_POS, Z_MAX_POS } };
  385. /**
  386. * Software endstops can be used to monitor the open end of
  387. * an axis that has a hardware endstop on the other end. Or
  388. * they can prevent axes from moving past endstops and grinding.
  389. *
  390. * To keep doing their job as the coordinate system changes,
  391. * the software endstop positions must be refreshed to remain
  392. * at the same positions relative to the machine.
  393. */
  394. void update_software_endstops(const AxisEnum axis
  395. #if HAS_HOTEND_OFFSET
  396. , const uint8_t old_tool_index/*=0*/, const uint8_t new_tool_index/*=0*/
  397. #endif
  398. ) {
  399. #if ENABLED(DUAL_X_CARRIAGE)
  400. if (axis == X_AXIS) {
  401. // In Dual X mode hotend_offset[X] is T1's home position
  402. const float dual_max_x = _MAX(hotend_offset[X_AXIS][1], X2_MAX_POS);
  403. if (new_tool_index != 0) {
  404. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  405. soft_endstop[X_AXIS].min = X2_MIN_POS;
  406. soft_endstop[X_AXIS].max = dual_max_x;
  407. }
  408. else if (dxc_is_duplicating()) {
  409. // In Duplication Mode, T0 can move as far left as X1_MIN_POS
  410. // but not so far to the right that T1 would move past the end
  411. soft_endstop[X_AXIS].min = X1_MIN_POS;
  412. soft_endstop[X_AXIS].max = _MIN(X1_MAX_POS, dual_max_x - duplicate_extruder_x_offset);
  413. }
  414. else {
  415. // In other modes, T0 can move from X1_MIN_POS to X1_MAX_POS
  416. soft_endstop[X_AXIS].min = X1_MIN_POS;
  417. soft_endstop[X_AXIS].max = X1_MAX_POS;
  418. }
  419. }
  420. #elif ENABLED(DELTA)
  421. soft_endstop[axis].min = base_min_pos(axis);
  422. soft_endstop[axis].max = (axis == Z_AXIS ? delta_height
  423. #if HAS_BED_PROBE
  424. - zprobe_offset[Z_AXIS]
  425. #endif
  426. : base_max_pos(axis));
  427. switch (axis) {
  428. case X_AXIS:
  429. case Y_AXIS:
  430. // Get a minimum radius for clamping
  431. 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);
  432. delta_max_radius_2 = sq(delta_max_radius);
  433. break;
  434. case Z_AXIS:
  435. delta_clip_start_height = soft_endstop[axis].max - delta_safe_distance_from_top();
  436. default: break;
  437. }
  438. #elif HAS_HOTEND_OFFSET
  439. // Software endstops are relative to the tool 0 workspace, so
  440. // the movement limits must be shifted by the tool offset to
  441. // retain the same physical limit when other tools are selected.
  442. if (old_tool_index != new_tool_index) {
  443. const float offs = hotend_offset[axis][new_tool_index] - hotend_offset[axis][old_tool_index];
  444. soft_endstop[axis].min += offs;
  445. soft_endstop[axis].max += offs;
  446. }
  447. else {
  448. const float offs = hotend_offset[axis][active_extruder];
  449. soft_endstop[axis].min = base_min_pos(axis) + offs;
  450. soft_endstop[axis].max = base_max_pos(axis) + offs;
  451. }
  452. #else
  453. soft_endstop[axis].min = base_min_pos(axis);
  454. soft_endstop[axis].max = base_max_pos(axis);
  455. #endif
  456. if (DEBUGGING(LEVELING))
  457. SERIAL_ECHOLNPAIR("Axis ", axis_codes[axis], " min:", soft_endstop[axis].min, " max:", soft_endstop[axis].max);
  458. }
  459. /**
  460. * Constrain the given coordinates to the software endstops.
  461. *
  462. * For DELTA/SCARA the XY constraint is based on the smallest
  463. * radius within the set software endstops.
  464. */
  465. void apply_motion_limits(float target[XYZ]) {
  466. if (!soft_endstops_enabled) return;
  467. #if IS_KINEMATIC
  468. #if HAS_HOTEND_OFFSET && ENABLED(DELTA)
  469. // The effector center position will be the target minus the hotend offset.
  470. const float offx = hotend_offset[X_AXIS][active_extruder], offy = hotend_offset[Y_AXIS][active_extruder];
  471. #else
  472. // SCARA needs to consider the angle of the arm through the entire move, so for now use no tool offset.
  473. constexpr float offx = 0, offy = 0;
  474. #endif
  475. const float dist_2 = HYPOT2(target[X_AXIS] - offx, target[Y_AXIS] - offy);
  476. if (dist_2 > delta_max_radius_2) {
  477. const float ratio = (delta_max_radius) / SQRT(dist_2); // 200 / 300 = 0.66
  478. target[X_AXIS] *= ratio;
  479. target[Y_AXIS] *= ratio;
  480. }
  481. #else
  482. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  483. NOLESS(target[X_AXIS], soft_endstop[X_AXIS].min);
  484. #endif
  485. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  486. NOMORE(target[X_AXIS], soft_endstop[X_AXIS].max);
  487. #endif
  488. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  489. NOLESS(target[Y_AXIS], soft_endstop[Y_AXIS].min);
  490. #endif
  491. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  492. NOMORE(target[Y_AXIS], soft_endstop[Y_AXIS].max);
  493. #endif
  494. #endif
  495. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  496. NOLESS(target[Z_AXIS], soft_endstop[Z_AXIS].min);
  497. #endif
  498. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  499. NOMORE(target[Z_AXIS], soft_endstop[Z_AXIS].max);
  500. #endif
  501. }
  502. #endif // HAS_SOFTWARE_ENDSTOPS
  503. #if !UBL_SEGMENTED
  504. #if IS_KINEMATIC
  505. #if IS_SCARA
  506. /**
  507. * Before raising this value, use M665 S[seg_per_sec] to decrease
  508. * the number of segments-per-second. Default is 200. Some deltas
  509. * do better with 160 or lower. It would be good to know how many
  510. * segments-per-second are actually possible for SCARA on AVR.
  511. *
  512. * Longer segments result in less kinematic overhead
  513. * but may produce jagged lines. Try 0.5mm, 1.0mm, and 2.0mm
  514. * and compare the difference.
  515. */
  516. #define SCARA_MIN_SEGMENT_LENGTH 0.5f
  517. #endif
  518. /**
  519. * Prepare a linear move in a DELTA or SCARA setup.
  520. *
  521. * Called from prepare_move_to_destination as the
  522. * default Delta/SCARA segmenter.
  523. *
  524. * This calls planner.buffer_line several times, adding
  525. * small incremental moves for DELTA or SCARA.
  526. *
  527. * For Unified Bed Leveling (Delta or Segmented Cartesian)
  528. * the ubl.prepare_segmented_line_to method replaces this.
  529. *
  530. * For Auto Bed Leveling (Bilinear) with SEGMENT_LEVELED_MOVES
  531. * this is replaced by segmented_line_to_destination below.
  532. */
  533. inline bool prepare_kinematic_move_to(const float (&rtarget)[XYZE]) {
  534. // Get the top feedrate of the move in the XY plane
  535. const float _feedrate_mm_s = MMS_SCALED(feedrate_mm_s);
  536. const float xdiff = rtarget[X_AXIS] - current_position[X_AXIS],
  537. ydiff = rtarget[Y_AXIS] - current_position[Y_AXIS];
  538. // If the move is only in Z/E don't split up the move
  539. if (!xdiff && !ydiff) {
  540. planner.buffer_line(rtarget, _feedrate_mm_s, active_extruder);
  541. return false; // caller will update current_position
  542. }
  543. // Fail if attempting move outside printable radius
  544. if (!position_is_reachable(rtarget[X_AXIS], rtarget[Y_AXIS])) return true;
  545. // Remaining cartesian distances
  546. const float zdiff = rtarget[Z_AXIS] - current_position[Z_AXIS],
  547. ediff = rtarget[E_AXIS] - current_position[E_AXIS];
  548. // Get the linear distance in XYZ
  549. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  550. // If the move is very short, check the E move distance
  551. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(ediff);
  552. // No E move either? Game over.
  553. if (UNEAR_ZERO(cartesian_mm)) return true;
  554. // Minimum number of seconds to move the given distance
  555. const float seconds = cartesian_mm / _feedrate_mm_s;
  556. // The number of segments-per-second times the duration
  557. // gives the number of segments
  558. uint16_t segments = delta_segments_per_second * seconds;
  559. // For SCARA enforce a minimum segment size
  560. #if IS_SCARA
  561. NOMORE(segments, cartesian_mm * RECIPROCAL(SCARA_MIN_SEGMENT_LENGTH));
  562. #endif
  563. // At least one segment is required
  564. NOLESS(segments, 1U);
  565. // The approximate length of each segment
  566. const float inv_segments = 1.0f / float(segments),
  567. segment_distance[XYZE] = {
  568. xdiff * inv_segments,
  569. ydiff * inv_segments,
  570. zdiff * inv_segments,
  571. ediff * inv_segments
  572. },
  573. cartesian_segment_mm = cartesian_mm * inv_segments;
  574. #if ENABLED(SCARA_FEEDRATE_SCALING)
  575. const float inv_duration = _feedrate_mm_s / cartesian_segment_mm;
  576. #endif
  577. /*
  578. SERIAL_ECHOPAIR("mm=", cartesian_mm);
  579. SERIAL_ECHOPAIR(" seconds=", seconds);
  580. SERIAL_ECHOPAIR(" segments=", segments);
  581. SERIAL_ECHOPAIR(" segment_mm=", cartesian_segment_mm);
  582. SERIAL_EOL();
  583. //*/
  584. // Get the current position as starting point
  585. float raw[XYZE];
  586. COPY(raw, current_position);
  587. // Calculate and execute the segments
  588. while (--segments) {
  589. static millis_t next_idle_ms = millis() + 200UL;
  590. thermalManager.manage_heater(); // This returns immediately if not really needed.
  591. if (ELAPSED(millis(), next_idle_ms)) {
  592. next_idle_ms = millis() + 200UL;
  593. idle();
  594. }
  595. LOOP_XYZE(i) raw[i] += segment_distance[i];
  596. if (!planner.buffer_line(raw, _feedrate_mm_s, active_extruder, cartesian_segment_mm
  597. #if ENABLED(SCARA_FEEDRATE_SCALING)
  598. , inv_duration
  599. #endif
  600. ))
  601. break;
  602. }
  603. // Ensure last segment arrives at target location.
  604. planner.buffer_line(rtarget, _feedrate_mm_s, active_extruder, cartesian_segment_mm
  605. #if ENABLED(SCARA_FEEDRATE_SCALING)
  606. , inv_duration
  607. #endif
  608. );
  609. return false; // caller will update current_position
  610. }
  611. #else // !IS_KINEMATIC
  612. #if ENABLED(SEGMENT_LEVELED_MOVES)
  613. /**
  614. * Prepare a segmented move on a CARTESIAN setup.
  615. *
  616. * This calls planner.buffer_line several times, adding
  617. * small incremental moves. This allows the planner to
  618. * apply more detailed bed leveling to the full move.
  619. */
  620. inline void segmented_line_to_destination(const float &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
  621. const float xdiff = destination[X_AXIS] - current_position[X_AXIS],
  622. ydiff = destination[Y_AXIS] - current_position[Y_AXIS];
  623. // If the move is only in Z/E don't split up the move
  624. if (!xdiff && !ydiff) {
  625. planner.buffer_line(destination, fr_mm_s, active_extruder);
  626. return;
  627. }
  628. // Remaining cartesian distances
  629. const float zdiff = destination[Z_AXIS] - current_position[Z_AXIS],
  630. ediff = destination[E_AXIS] - current_position[E_AXIS];
  631. // Get the linear distance in XYZ
  632. // If the move is very short, check the E move distance
  633. // No E move either? Game over.
  634. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  635. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(ediff);
  636. if (UNEAR_ZERO(cartesian_mm)) return;
  637. // The length divided by the segment size
  638. // At least one segment is required
  639. uint16_t segments = cartesian_mm / segment_size;
  640. NOLESS(segments, 1U);
  641. // The approximate length of each segment
  642. const float inv_segments = 1.0f / float(segments),
  643. cartesian_segment_mm = cartesian_mm * inv_segments,
  644. segment_distance[XYZE] = {
  645. xdiff * inv_segments,
  646. ydiff * inv_segments,
  647. zdiff * inv_segments,
  648. ediff * inv_segments
  649. };
  650. #if ENABLED(SCARA_FEEDRATE_SCALING)
  651. const float inv_duration = _feedrate_mm_s / cartesian_segment_mm;
  652. #endif
  653. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  654. // SERIAL_ECHOLNPAIR(" segments=", segments);
  655. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  656. // Get the raw current position as starting point
  657. float raw[XYZE];
  658. COPY(raw, current_position);
  659. // Calculate and execute the segments
  660. while (--segments) {
  661. static millis_t next_idle_ms = millis() + 200UL;
  662. thermalManager.manage_heater(); // This returns immediately if not really needed.
  663. if (ELAPSED(millis(), next_idle_ms)) {
  664. next_idle_ms = millis() + 200UL;
  665. idle();
  666. }
  667. LOOP_XYZE(i) raw[i] += segment_distance[i];
  668. if (!planner.buffer_line(raw, fr_mm_s, active_extruder, cartesian_segment_mm
  669. #if ENABLED(SCARA_FEEDRATE_SCALING)
  670. , inv_duration
  671. #endif
  672. ))
  673. break;
  674. }
  675. // Since segment_distance is only approximate,
  676. // the final move must be to the exact destination.
  677. planner.buffer_line(destination, fr_mm_s, active_extruder, cartesian_segment_mm
  678. #if ENABLED(SCARA_FEEDRATE_SCALING)
  679. , inv_duration
  680. #endif
  681. );
  682. }
  683. #endif // SEGMENT_LEVELED_MOVES
  684. /**
  685. * Prepare a linear move in a Cartesian setup.
  686. *
  687. * When a mesh-based leveling system is active, moves are segmented
  688. * according to the configuration of the leveling system.
  689. *
  690. * Returns true if current_position[] was set to destination[]
  691. */
  692. inline bool prepare_move_to_destination_cartesian() {
  693. #if HAS_MESH
  694. if (planner.leveling_active && planner.leveling_active_at_z(destination[Z_AXIS])) {
  695. #if ENABLED(AUTO_BED_LEVELING_UBL)
  696. ubl.line_to_destination_cartesian(MMS_SCALED(feedrate_mm_s), active_extruder); // UBL's motion routine needs to know about
  697. return true; // all moves, including Z-only moves.
  698. #elif ENABLED(SEGMENT_LEVELED_MOVES)
  699. segmented_line_to_destination(MMS_SCALED(feedrate_mm_s));
  700. return false; // caller will update current_position
  701. #else
  702. /**
  703. * For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
  704. * Otherwise fall through to do a direct single move.
  705. */
  706. if (current_position[X_AXIS] != destination[X_AXIS] || current_position[Y_AXIS] != destination[Y_AXIS]) {
  707. #if ENABLED(MESH_BED_LEVELING)
  708. mbl.line_to_destination(MMS_SCALED(feedrate_mm_s));
  709. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  710. bilinear_line_to_destination(MMS_SCALED(feedrate_mm_s));
  711. #endif
  712. return true;
  713. }
  714. #endif
  715. }
  716. #endif // HAS_MESH
  717. buffer_line_to_destination(MMS_SCALED(feedrate_mm_s));
  718. return false; // caller will update current_position
  719. }
  720. #endif // !IS_KINEMATIC
  721. #endif // !UBL_SEGMENTED
  722. #if HAS_DUPLICATION_MODE
  723. bool extruder_duplication_enabled,
  724. mirrored_duplication_mode;
  725. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  726. uint8_t duplication_e_mask; // = 0
  727. #endif
  728. #endif
  729. #if ENABLED(DUAL_X_CARRIAGE)
  730. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  731. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  732. raised_parked_position[XYZE], // used in mode 1
  733. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  734. bool active_extruder_parked = false; // used in mode 1 & 2
  735. millis_t delayed_move_time = 0; // used in mode 1
  736. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  737. float x_home_pos(const int extruder) {
  738. if (extruder == 0)
  739. return base_home_pos(X_AXIS);
  740. else
  741. /**
  742. * In dual carriage mode the extruder offset provides an override of the
  743. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  744. * This allows soft recalibration of the second extruder home position
  745. * without firmware reflash (through the M218 command).
  746. */
  747. return hotend_offset[X_AXIS][1] > 0 ? hotend_offset[X_AXIS][1] : X2_HOME_POS;
  748. }
  749. /**
  750. * Prepare a linear move in a dual X axis setup
  751. *
  752. * Return true if current_position[] was set to destination[]
  753. */
  754. inline bool dual_x_carriage_unpark() {
  755. if (active_extruder_parked) {
  756. switch (dual_x_carriage_mode) {
  757. case DXC_FULL_CONTROL_MODE:
  758. break;
  759. case DXC_AUTO_PARK_MODE:
  760. if (current_position[E_AXIS] == destination[E_AXIS]) {
  761. // This is a travel move (with no extrusion)
  762. // Skip it, but keep track of the current position
  763. // (so it can be used as the start of the next non-travel move)
  764. if (delayed_move_time != 0xFFFFFFFFUL) {
  765. set_current_from_destination();
  766. NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
  767. delayed_move_time = millis();
  768. return true;
  769. }
  770. }
  771. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  772. #define CUR_X current_position[X_AXIS]
  773. #define CUR_Y current_position[Y_AXIS]
  774. #define CUR_Z current_position[Z_AXIS]
  775. #define CUR_E current_position[E_AXIS]
  776. #define RAISED_X raised_parked_position[X_AXIS]
  777. #define RAISED_Y raised_parked_position[Y_AXIS]
  778. #define RAISED_Z raised_parked_position[Z_AXIS]
  779. if ( planner.buffer_line(RAISED_X, RAISED_Y, RAISED_Z, CUR_E, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder))
  780. if (planner.buffer_line( CUR_X, CUR_Y, RAISED_Z, CUR_E, PLANNER_XY_FEEDRATE(), active_extruder))
  781. planner.buffer_line( CUR_X, CUR_Y, CUR_Z, CUR_E, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder);
  782. delayed_move_time = 0;
  783. active_extruder_parked = false;
  784. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Clear active_extruder_parked");
  785. break;
  786. case DXC_MIRRORED_MODE:
  787. case DXC_DUPLICATION_MODE:
  788. if (active_extruder == 0) {
  789. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Set planner X", inactive_extruder_x_pos, " ... Line to X", current_position[X_AXIS] + duplicate_extruder_x_offset);
  790. // move duplicate extruder into correct duplication position.
  791. planner.set_position_mm(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  792. if (!planner.buffer_line(
  793. dual_x_carriage_mode == DXC_DUPLICATION_MODE ? duplicate_extruder_x_offset + current_position[X_AXIS] : inactive_extruder_x_pos,
  794. current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
  795. planner.settings.max_feedrate_mm_s[X_AXIS], 1
  796. )
  797. ) break;
  798. planner.synchronize();
  799. sync_plan_position();
  800. extruder_duplication_enabled = true;
  801. active_extruder_parked = false;
  802. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  803. }
  804. else if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Active extruder not 0");
  805. break;
  806. }
  807. }
  808. stepper.set_directions();
  809. return false;
  810. }
  811. #endif // DUAL_X_CARRIAGE
  812. /**
  813. * Prepare a single move and get ready for the next one
  814. *
  815. * This may result in several calls to planner.buffer_line to
  816. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  817. *
  818. * Make sure current_position[E] and destination[E] are good
  819. * before calling or cold/lengthy extrusion may get missed.
  820. */
  821. void prepare_move_to_destination() {
  822. apply_motion_limits(destination);
  823. #if EITHER(PREVENT_COLD_EXTRUSION, PREVENT_LENGTHY_EXTRUDE)
  824. if (!DEBUGGING(DRYRUN)) {
  825. if (destination[E_AXIS] != current_position[E_AXIS]) {
  826. #if ENABLED(PREVENT_COLD_EXTRUSION)
  827. if (thermalManager.tooColdToExtrude(active_extruder)) {
  828. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  829. SERIAL_ECHO_MSG(MSG_ERR_COLD_EXTRUDE_STOP);
  830. }
  831. #endif // PREVENT_COLD_EXTRUSION
  832. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  833. const float e_delta = ABS(destination[E_AXIS] - current_position[E_AXIS]) * planner.e_factor[active_extruder];
  834. if (e_delta > (EXTRUDE_MAXLENGTH)) {
  835. #if ENABLED(MIXING_EXTRUDER)
  836. bool ignore_e = false;
  837. float collector[MIXING_STEPPERS];
  838. mixer.refresh_collector(1.0, mixer.get_current_vtool(), collector);
  839. MIXER_STEPPER_LOOP(e)
  840. if (e_delta * collector[e] > (EXTRUDE_MAXLENGTH)) { ignore_e = true; break; }
  841. #else
  842. constexpr bool ignore_e = true;
  843. #endif
  844. if (ignore_e) {
  845. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  846. SERIAL_ECHO_MSG(MSG_ERR_LONG_EXTRUDE_STOP);
  847. }
  848. }
  849. #endif // PREVENT_LENGTHY_EXTRUDE
  850. }
  851. }
  852. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  853. #if ENABLED(DUAL_X_CARRIAGE)
  854. if (dual_x_carriage_unpark()) return;
  855. #endif
  856. if (
  857. #if UBL_SEGMENTED
  858. //ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s)) // This doesn't seem to work correctly on UBL.
  859. #if IS_KINEMATIC // Use Kinematic / Cartesian cases as a workaround for now.
  860. ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s))
  861. #else
  862. prepare_move_to_destination_cartesian()
  863. #endif
  864. #elif IS_KINEMATIC
  865. prepare_kinematic_move_to(destination)
  866. #else
  867. prepare_move_to_destination_cartesian()
  868. #endif
  869. ) return;
  870. set_current_from_destination();
  871. }
  872. bool axis_unhomed_error(const bool x/*=true*/, const bool y/*=true*/, const bool z/*=true*/) {
  873. #if ENABLED(HOME_AFTER_DEACTIVATE)
  874. const bool xx = x && !TEST(axis_known_position, X_AXIS),
  875. yy = y && !TEST(axis_known_position, Y_AXIS),
  876. zz = z && !TEST(axis_known_position, Z_AXIS);
  877. #else
  878. const bool xx = x && !TEST(axis_homed, X_AXIS),
  879. yy = y && !TEST(axis_homed, Y_AXIS),
  880. zz = z && !TEST(axis_homed, Z_AXIS);
  881. #endif
  882. if (xx || yy || zz) {
  883. SERIAL_ECHO_START();
  884. SERIAL_ECHOPGM(MSG_HOME " ");
  885. if (xx) SERIAL_CHAR('X');
  886. if (yy) SERIAL_CHAR('Y');
  887. if (zz) SERIAL_CHAR('Z');
  888. SERIAL_ECHOLNPGM(" " MSG_FIRST);
  889. #if HAS_DISPLAY
  890. ui.status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
  891. #endif
  892. return true;
  893. }
  894. return false;
  895. }
  896. /**
  897. * Homing bump feedrate (mm/s)
  898. */
  899. float get_homing_bump_feedrate(const AxisEnum axis) {
  900. #if HOMING_Z_WITH_PROBE
  901. if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW);
  902. #endif
  903. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  904. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  905. if (hbd < 1) {
  906. hbd = 10;
  907. SERIAL_ECHO_MSG("Warning: Homing Bump Divisor < 1");
  908. }
  909. return homing_feedrate(axis) / hbd;
  910. }
  911. #if ENABLED(SENSORLESS_HOMING)
  912. /**
  913. * Set sensorless homing if the axis has it, accounting for Core Kinematics.
  914. */
  915. sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis) {
  916. sensorless_t stealth_states { false };
  917. switch (axis) {
  918. default: break;
  919. #if X_SENSORLESS
  920. case X_AXIS:
  921. stealth_states.x = tmc_enable_stallguard(stepperX);
  922. #if AXIS_HAS_STALLGUARD(X2)
  923. stealth_states.x2 = tmc_enable_stallguard(stepperX2);
  924. #endif
  925. #if CORE_IS_XY && Y_SENSORLESS
  926. stealth_states.y = tmc_enable_stallguard(stepperY);
  927. #elif CORE_IS_XZ && Z_SENSORLESS
  928. stealth_states.z = tmc_enable_stallguard(stepperZ);
  929. #endif
  930. break;
  931. #endif
  932. #if Y_SENSORLESS
  933. case Y_AXIS:
  934. stealth_states.y = tmc_enable_stallguard(stepperY);
  935. #if AXIS_HAS_STALLGUARD(Y2)
  936. stealth_states.y2 = tmc_enable_stallguard(stepperY2);
  937. #endif
  938. #if CORE_IS_XY && X_SENSORLESS
  939. stealth_states.x = tmc_enable_stallguard(stepperX);
  940. #elif CORE_IS_YZ && Z_SENSORLESS
  941. stealth_states.z = tmc_enable_stallguard(stepperZ);
  942. #endif
  943. break;
  944. #endif
  945. #if Z_SENSORLESS
  946. case Z_AXIS:
  947. stealth_states.z = tmc_enable_stallguard(stepperZ);
  948. #if AXIS_HAS_STALLGUARD(Z2)
  949. stealth_states.z2 = tmc_enable_stallguard(stepperZ2);
  950. #endif
  951. #if AXIS_HAS_STALLGUARD(Z3)
  952. stealth_states.z3 = tmc_enable_stallguard(stepperZ3);
  953. #endif
  954. #if CORE_IS_XZ && X_SENSORLESS
  955. stealth_states.x = tmc_enable_stallguard(stepperX);
  956. #elif CORE_IS_YZ && Y_SENSORLESS
  957. stealth_states.y = tmc_enable_stallguard(stepperY);
  958. #endif
  959. break;
  960. #endif
  961. }
  962. #if ENABLED(SPI_ENDSTOPS)
  963. switch (axis) {
  964. #if X_SPI_SENSORLESS
  965. case X_AXIS: endstops.tmc_spi_homing.x = true; break;
  966. #endif
  967. #if Y_SPI_SENSORLESS
  968. case Y_AXIS: endstops.tmc_spi_homing.y = true; break;
  969. #endif
  970. #if Z_SPI_SENSORLESS
  971. case Z_AXIS: endstops.tmc_spi_homing.z = true; break;
  972. #endif
  973. default: break;
  974. }
  975. #endif
  976. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  977. sg_guard_period = millis() + default_sg_guard_duration;
  978. #endif
  979. return stealth_states;
  980. }
  981. void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) {
  982. switch (axis) {
  983. default: break;
  984. #if X_SENSORLESS
  985. case X_AXIS:
  986. tmc_disable_stallguard(stepperX, enable_stealth.x);
  987. #if AXIS_HAS_STALLGUARD(X2)
  988. tmc_disable_stallguard(stepperX2, enable_stealth.x2);
  989. #endif
  990. #if CORE_IS_XY && Y_SENSORLESS
  991. tmc_disable_stallguard(stepperY, enable_stealth.y);
  992. #elif CORE_IS_XZ && Z_SENSORLESS
  993. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  994. #endif
  995. break;
  996. #endif
  997. #if Y_SENSORLESS
  998. case Y_AXIS:
  999. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1000. #if AXIS_HAS_STALLGUARD(Y2)
  1001. tmc_disable_stallguard(stepperY2, enable_stealth.y2);
  1002. #endif
  1003. #if CORE_IS_XY && X_SENSORLESS
  1004. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1005. #elif CORE_IS_YZ && Z_SENSORLESS
  1006. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1007. #endif
  1008. break;
  1009. #endif
  1010. #if Z_SENSORLESS
  1011. case Z_AXIS:
  1012. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1013. #if AXIS_HAS_STALLGUARD(Z2)
  1014. tmc_disable_stallguard(stepperZ2, enable_stealth.z2);
  1015. #endif
  1016. #if AXIS_HAS_STALLGUARD(Z3)
  1017. tmc_disable_stallguard(stepperZ3, enable_stealth.z3);
  1018. #endif
  1019. #if CORE_IS_XZ && X_SENSORLESS
  1020. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1021. #elif CORE_IS_YZ && Y_SENSORLESS
  1022. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1023. #endif
  1024. break;
  1025. #endif
  1026. }
  1027. #if ENABLED(SPI_ENDSTOPS)
  1028. switch (axis) {
  1029. #if X_SPI_SENSORLESS
  1030. case X_AXIS: endstops.tmc_spi_homing.x = false; break;
  1031. #endif
  1032. #if Y_SPI_SENSORLESS
  1033. case Y_AXIS: endstops.tmc_spi_homing.y = false; break;
  1034. #endif
  1035. #if Z_SPI_SENSORLESS
  1036. case Z_AXIS: endstops.tmc_spi_homing.z = false; break;
  1037. #endif
  1038. default: break;
  1039. }
  1040. #endif
  1041. }
  1042. #endif // SENSORLESS_HOMING
  1043. /**
  1044. * Home an individual linear axis
  1045. */
  1046. void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) {
  1047. if (DEBUGGING(LEVELING)) {
  1048. DEBUG_ECHOPAIR(">>> do_homing_move(", axis_codes[axis], ", ", distance, ", ");
  1049. if (fr_mm_s)
  1050. DEBUG_ECHO(fr_mm_s);
  1051. else
  1052. DEBUG_ECHOPAIR("[", homing_feedrate(axis), "]");
  1053. DEBUG_ECHOLNPGM(")");
  1054. }
  1055. #if HOMING_Z_WITH_PROBE && HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  1056. // Wait for bed to heat back up between probing points
  1057. if (axis == Z_AXIS && distance < 0 && thermalManager.isHeatingBed()) {
  1058. serialprintPGM(msg_wait_for_bed_heating);
  1059. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1060. thermalManager.wait_for_bed();
  1061. ui.reset_status();
  1062. }
  1063. #endif
  1064. // Only do some things when moving towards an endstop
  1065. const int8_t axis_home_dir =
  1066. #if ENABLED(DUAL_X_CARRIAGE)
  1067. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  1068. #endif
  1069. home_dir(axis);
  1070. const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
  1071. #if ENABLED(SENSORLESS_HOMING)
  1072. sensorless_t stealth_states;
  1073. #endif
  1074. if (is_home_dir) {
  1075. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1076. if (axis == Z_AXIS) probing_pause(true);
  1077. #endif
  1078. // Disable stealthChop if used. Enable diag1 pin on driver.
  1079. #if ENABLED(SENSORLESS_HOMING)
  1080. stealth_states = start_sensorless_homing_per_axis(axis);
  1081. #endif
  1082. }
  1083. #if IS_SCARA
  1084. // Tell the planner the axis is at 0
  1085. current_position[axis] = 0;
  1086. sync_plan_position();
  1087. current_position[axis] = distance;
  1088. planner.buffer_line(current_position, fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
  1089. #else
  1090. 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) };
  1091. target[axis] = 0;
  1092. planner.set_machine_position_mm(target);
  1093. target[axis] = distance;
  1094. #if IS_KINEMATIC && ENABLED(JUNCTION_DEVIATION)
  1095. const float delta_mm_cart[XYZE] = {0, 0, 0, 0};
  1096. #endif
  1097. // Set delta/cartesian axes directly
  1098. planner.buffer_segment(target
  1099. #if IS_KINEMATIC && ENABLED(JUNCTION_DEVIATION)
  1100. , delta_mm_cart
  1101. #endif
  1102. , fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder
  1103. );
  1104. #endif
  1105. planner.synchronize();
  1106. if (is_home_dir) {
  1107. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1108. if (axis == Z_AXIS) probing_pause(false);
  1109. #endif
  1110. endstops.validate_homing_move();
  1111. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  1112. #if ENABLED(SENSORLESS_HOMING)
  1113. end_sensorless_homing_per_axis(axis, stealth_states);
  1114. #endif
  1115. }
  1116. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< do_homing_move(", axis_codes[axis], ")");
  1117. }
  1118. /**
  1119. * Set an axis' current position to its home position (after homing).
  1120. *
  1121. * For Core and Cartesian robots this applies one-to-one when an
  1122. * individual axis has been homed.
  1123. *
  1124. * DELTA should wait until all homing is done before setting the XYZ
  1125. * current_position to home, because homing is a single operation.
  1126. * In the case where the axis positions are already known and previously
  1127. * homed, DELTA could home to X or Y individually by moving either one
  1128. * to the center. However, homing Z always homes XY and Z.
  1129. *
  1130. * SCARA should wait until all XY homing is done before setting the XY
  1131. * current_position to home, because neither X nor Y is at home until
  1132. * both are at home. Z can however be homed individually.
  1133. *
  1134. * Callers must sync the planner position after calling this!
  1135. */
  1136. void set_axis_is_at_home(const AxisEnum axis) {
  1137. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_at_home(", axis_codes[axis], ")");
  1138. SBI(axis_known_position, axis);
  1139. SBI(axis_homed, axis);
  1140. #if ENABLED(DUAL_X_CARRIAGE)
  1141. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  1142. current_position[X_AXIS] = x_home_pos(active_extruder);
  1143. return;
  1144. }
  1145. #endif
  1146. #if ENABLED(MORGAN_SCARA)
  1147. scara_set_axis_is_at_home(axis);
  1148. #elif ENABLED(DELTA)
  1149. current_position[axis] = (axis == Z_AXIS ? delta_height
  1150. #if HAS_BED_PROBE
  1151. - zprobe_offset[Z_AXIS]
  1152. #endif
  1153. : base_home_pos(axis));
  1154. #else
  1155. current_position[axis] = base_home_pos(axis);
  1156. #endif
  1157. /**
  1158. * Z Probe Z Homing? Account for the probe's Z offset.
  1159. */
  1160. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  1161. if (axis == Z_AXIS) {
  1162. #if HOMING_Z_WITH_PROBE
  1163. current_position[Z_AXIS] -= zprobe_offset[Z_AXIS];
  1164. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***\n> zprobe_offset[Z_AXIS] = ", zprobe_offset[Z_AXIS]);
  1165. #else
  1166. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("*** Z HOMED TO ENDSTOP ***");
  1167. #endif
  1168. }
  1169. #endif
  1170. #if ENABLED(I2C_POSITION_ENCODERS)
  1171. I2CPEM.homed(axis);
  1172. #endif
  1173. #if ENABLED(BABYSTEP_DISPLAY_TOTAL)
  1174. babystep.reset_total(axis);
  1175. #endif
  1176. if (DEBUGGING(LEVELING)) {
  1177. #if HAS_HOME_OFFSET
  1178. DEBUG_ECHOLNPAIR("> home_offset[", axis_codes[axis], "] = ", home_offset[axis]);
  1179. #endif
  1180. DEBUG_POS("", current_position);
  1181. DEBUG_ECHOLNPAIR("<<< set_axis_is_at_home(", axis_codes[axis], ")");
  1182. }
  1183. }
  1184. /**
  1185. * Set an axis' to be unhomed.
  1186. */
  1187. void set_axis_is_not_at_home(const AxisEnum axis) {
  1188. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_not_at_home(", axis_codes[axis], ")");
  1189. CBI(axis_known_position, axis);
  1190. CBI(axis_homed, axis);
  1191. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< set_axis_is_not_at_home(", axis_codes[axis], ")");
  1192. #if ENABLED(I2C_POSITION_ENCODERS)
  1193. I2CPEM.unhomed(axis);
  1194. #endif
  1195. }
  1196. /**
  1197. * Home an individual "raw axis" to its endstop.
  1198. * This applies to XYZ on Cartesian and Core robots, and
  1199. * to the individual ABC steppers on DELTA and SCARA.
  1200. *
  1201. * At the end of the procedure the axis is marked as
  1202. * homed and the current position of that axis is updated.
  1203. * Kinematic robots should wait till all axes are homed
  1204. * before updating the current position.
  1205. */
  1206. void homeaxis(const AxisEnum axis) {
  1207. #if IS_SCARA
  1208. // Only Z homing (with probe) is permitted
  1209. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  1210. #else
  1211. #define _CAN_HOME(A) \
  1212. (axis == _AXIS(A) && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  1213. #if X_SPI_SENSORLESS
  1214. #define CAN_HOME_X true
  1215. #else
  1216. #define CAN_HOME_X _CAN_HOME(X)
  1217. #endif
  1218. #if Y_SPI_SENSORLESS
  1219. #define CAN_HOME_Y true
  1220. #else
  1221. #define CAN_HOME_Y _CAN_HOME(Y)
  1222. #endif
  1223. #if Z_SPI_SENSORLESS
  1224. #define CAN_HOME_Z true
  1225. #else
  1226. #define CAN_HOME_Z _CAN_HOME(Z)
  1227. #endif
  1228. if (!CAN_HOME_X && !CAN_HOME_Y && !CAN_HOME_Z) return;
  1229. #endif
  1230. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")");
  1231. const int axis_home_dir = (
  1232. #if ENABLED(DUAL_X_CARRIAGE)
  1233. axis == X_AXIS ? x_home_dir(active_extruder) :
  1234. #endif
  1235. home_dir(axis)
  1236. );
  1237. // Homing Z towards the bed? Deploy the Z probe or endstop.
  1238. #if HOMING_Z_WITH_PROBE
  1239. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  1240. #endif
  1241. // Set flags for X, Y, Z motor locking
  1242. #if HAS_EXTRA_ENDSTOPS
  1243. switch (axis) {
  1244. #if ENABLED(X_DUAL_ENDSTOPS)
  1245. case X_AXIS:
  1246. #endif
  1247. #if ENABLED(Y_DUAL_ENDSTOPS)
  1248. case Y_AXIS:
  1249. #endif
  1250. #if Z_MULTI_ENDSTOPS
  1251. case Z_AXIS:
  1252. #endif
  1253. stepper.set_separate_multi_axis(true);
  1254. default: break;
  1255. }
  1256. #endif
  1257. // Fast move towards endstop until triggered
  1258. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 1 Fast:");
  1259. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1260. if (axis == Z_AXIS && bltouch.deploy()) return; // The initial DEPLOY
  1261. #endif
  1262. do_homing_move(axis, 1.5f * max_length(
  1263. #if ENABLED(DELTA)
  1264. Z_AXIS
  1265. #else
  1266. axis
  1267. #endif
  1268. ) * axis_home_dir
  1269. );
  1270. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1271. if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE)
  1272. #endif
  1273. // When homing Z with probe respect probe clearance
  1274. const float bump = axis_home_dir * (
  1275. #if HOMING_Z_WITH_PROBE
  1276. (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? _MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) :
  1277. #endif
  1278. home_bump_mm(axis)
  1279. );
  1280. // If a second homing move is configured...
  1281. if (bump) {
  1282. // Move away from the endstop by the axis HOME_BUMP_MM
  1283. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Move Away:");
  1284. do_homing_move(axis, -bump
  1285. #if HOMING_Z_WITH_PROBE
  1286. , axis == Z_AXIS ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) : 0.0
  1287. #endif
  1288. );
  1289. // Slow move towards endstop until triggered
  1290. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 2 Slow:");
  1291. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1292. if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE)
  1293. #endif
  1294. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1295. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1296. if (axis == Z_AXIS) bltouch.stow(); // The final STOW
  1297. #endif
  1298. }
  1299. #if HAS_EXTRA_ENDSTOPS
  1300. const bool pos_dir = axis_home_dir > 0;
  1301. #if ENABLED(X_DUAL_ENDSTOPS)
  1302. if (axis == X_AXIS) {
  1303. const float adj = ABS(endstops.x2_endstop_adj);
  1304. if (adj) {
  1305. if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1306. do_homing_move(axis, pos_dir ? -adj : adj);
  1307. stepper.set_x_lock(false);
  1308. stepper.set_x2_lock(false);
  1309. }
  1310. }
  1311. #endif
  1312. #if ENABLED(Y_DUAL_ENDSTOPS)
  1313. if (axis == Y_AXIS) {
  1314. const float adj = ABS(endstops.y2_endstop_adj);
  1315. if (adj) {
  1316. if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1317. do_homing_move(axis, pos_dir ? -adj : adj);
  1318. stepper.set_y_lock(false);
  1319. stepper.set_y2_lock(false);
  1320. }
  1321. }
  1322. #endif
  1323. #if ENABLED(Z_DUAL_ENDSTOPS)
  1324. if (axis == Z_AXIS) {
  1325. const float adj = ABS(endstops.z2_endstop_adj);
  1326. if (adj) {
  1327. if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  1328. do_homing_move(axis, pos_dir ? -adj : adj);
  1329. stepper.set_z_lock(false);
  1330. stepper.set_z2_lock(false);
  1331. }
  1332. }
  1333. #endif
  1334. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  1335. if (axis == Z_AXIS) {
  1336. // we push the function pointers for the stepper lock function into an array
  1337. void (*lock[3]) (bool)= {&stepper.set_z_lock, &stepper.set_z2_lock, &stepper.set_z3_lock};
  1338. float adj[3] = {0, endstops.z2_endstop_adj, endstops.z3_endstop_adj};
  1339. void (*tempLock) (bool);
  1340. float tempAdj;
  1341. // manual bubble sort by adjust value
  1342. if (adj[1] < adj[0]) {
  1343. tempLock = lock[0], tempAdj = adj[0];
  1344. lock[0] = lock[1], adj[0] = adj[1];
  1345. lock[1] = tempLock, adj[1] = tempAdj;
  1346. }
  1347. if (adj[2] < adj[1]) {
  1348. tempLock = lock[1], tempAdj = adj[1];
  1349. lock[1] = lock[2], adj[1] = adj[2];
  1350. lock[2] = tempLock, adj[2] = tempAdj;
  1351. }
  1352. if (adj[1] < adj[0]) {
  1353. tempLock = lock[0], tempAdj = adj[0];
  1354. lock[0] = lock[1], adj[0] = adj[1];
  1355. lock[1] = tempLock, adj[1] = tempAdj;
  1356. }
  1357. if (pos_dir) {
  1358. // normalize adj to smallest value and do the first move
  1359. (*lock[0])(true);
  1360. do_homing_move(axis, adj[1] - adj[0]);
  1361. // lock the second stepper for the final correction
  1362. (*lock[1])(true);
  1363. do_homing_move(axis, adj[2] - adj[1]);
  1364. }
  1365. else {
  1366. (*lock[2])(true);
  1367. do_homing_move(axis, adj[1] - adj[2]);
  1368. (*lock[1])(true);
  1369. do_homing_move(axis, adj[0] - adj[1]);
  1370. }
  1371. stepper.set_z_lock(false);
  1372. stepper.set_z2_lock(false);
  1373. stepper.set_z3_lock(false);
  1374. }
  1375. #endif
  1376. // Reset flags for X, Y, Z motor locking
  1377. switch (axis) {
  1378. #if ENABLED(X_DUAL_ENDSTOPS)
  1379. case X_AXIS:
  1380. #endif
  1381. #if ENABLED(Y_DUAL_ENDSTOPS)
  1382. case Y_AXIS:
  1383. #endif
  1384. #if Z_MULTI_ENDSTOPS
  1385. case Z_AXIS:
  1386. #endif
  1387. stepper.set_separate_multi_axis(false);
  1388. default: break;
  1389. }
  1390. #endif
  1391. #if IS_SCARA
  1392. set_axis_is_at_home(axis);
  1393. sync_plan_position();
  1394. #elif ENABLED(DELTA)
  1395. // Delta has already moved all three towers up in G28
  1396. // so here it re-homes each tower in turn.
  1397. // Delta homing treats the axes as normal linear axes.
  1398. // retrace by the amount specified in delta_endstop_adj + additional dist in order to have minimum steps
  1399. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  1400. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("delta_endstop_adj:");
  1401. do_homing_move(axis, delta_endstop_adj[axis] - (MIN_STEPS_PER_SEGMENT + 1) * planner.steps_to_mm[axis] * Z_HOME_DIR);
  1402. }
  1403. #else // CARTESIAN / CORE
  1404. set_axis_is_at_home(axis);
  1405. sync_plan_position();
  1406. destination[axis] = current_position[axis];
  1407. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1408. #endif
  1409. // Put away the Z probe
  1410. #if HOMING_Z_WITH_PROBE
  1411. if (axis == Z_AXIS && STOW_PROBE()) return;
  1412. #endif
  1413. #ifdef HOMING_BACKOFF_MM
  1414. constexpr float endstop_backoff[XYZ] = HOMING_BACKOFF_MM;
  1415. const float backoff_mm = endstop_backoff[
  1416. #if ENABLED(DELTA)
  1417. Z_AXIS
  1418. #else
  1419. axis
  1420. #endif
  1421. ];
  1422. if (backoff_mm) {
  1423. current_position[axis] -= ABS(backoff_mm) * axis_home_dir;
  1424. line_to_current_position(
  1425. #if HOMING_Z_WITH_PROBE
  1426. (axis == Z_AXIS) ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) :
  1427. #endif
  1428. homing_feedrate(axis)
  1429. );
  1430. }
  1431. #endif
  1432. // Clear retracted status if homing the Z axis
  1433. #if ENABLED(FWRETRACT)
  1434. if (axis == Z_AXIS) fwretract.current_hop = 0.0;
  1435. #endif
  1436. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< homeaxis(", axis_codes[axis], ")");
  1437. } // homeaxis()
  1438. #if HAS_WORKSPACE_OFFSET
  1439. void update_workspace_offset(const AxisEnum axis) {
  1440. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1441. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Axis ", axis_codes[axis], " home_offset = ", home_offset[axis], " position_shift = ", position_shift[axis]);
  1442. }
  1443. #endif
  1444. #if HAS_M206_COMMAND
  1445. /**
  1446. * Change the home offset for an axis.
  1447. * Also refreshes the workspace offset.
  1448. */
  1449. void set_home_offset(const AxisEnum axis, const float v) {
  1450. home_offset[axis] = v;
  1451. update_workspace_offset(axis);
  1452. }
  1453. #endif // HAS_M206_COMMAND