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

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