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

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