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

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