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 61KB

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