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.

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