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

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