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

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