My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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