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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  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 <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 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. /**
  63. * axis_homed
  64. * Flags that each linear axis was homed.
  65. * XYZ on cartesian, ABC on delta, ABZ on SCARA.
  66. *
  67. * axis_known_position
  68. * Flags that the position is known in each linear axis. Set when homed.
  69. * Cleared whenever a stepper powers off, potentially losing its position.
  70. */
  71. uint8_t axis_homed, axis_known_position; // = 0
  72. // Relative Mode. Enable with G91, disable with G90.
  73. bool relative_mode; // = false;
  74. /**
  75. * Cartesian Current Position
  76. * Used to track the native machine position as moves are queued.
  77. * Used by 'line_to_current_position' to do a move after changing it.
  78. * Used by 'sync_plan_position' to update 'planner.position'.
  79. */
  80. xyze_pos_t current_position = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
  81. /**
  82. * Cartesian Destination
  83. * The destination for a move, filled in by G-code movement commands,
  84. * and expected by functions like 'prepare_line_to_destination'.
  85. * G-codes can set destination using 'get_destination_from_command'
  86. */
  87. xyze_pos_t destination; // {0}
  88. // G60/G61 Position Save and Return
  89. #if SAVED_POSITIONS
  90. uint8_t saved_slots[(SAVED_POSITIONS + 7) >> 3];
  91. xyz_pos_t stored_position[SAVED_POSITIONS];
  92. #endif
  93. // The active extruder (tool). Set with T<extruder> command.
  94. #if EXTRUDERS > 1
  95. uint8_t active_extruder = 0; // = 0
  96. #endif
  97. #if ENABLED(LCD_SHOW_E_TOTAL)
  98. float e_move_accumulator; // = 0
  99. #endif
  100. // Extruder offsets
  101. #if HAS_HOTEND_OFFSET
  102. xyz_pos_t hotend_offset[HOTENDS]; // Initialized by settings.load()
  103. void reset_hotend_offsets() {
  104. constexpr float tmp[XYZ][HOTENDS] = { HOTEND_OFFSET_X, HOTEND_OFFSET_Y, HOTEND_OFFSET_Z };
  105. static_assert(
  106. !tmp[X_AXIS][0] && !tmp[Y_AXIS][0] && !tmp[Z_AXIS][0],
  107. "Offsets for the first hotend must be 0.0."
  108. );
  109. // Transpose from [XYZ][HOTENDS] to [HOTENDS][XYZ]
  110. HOTEND_LOOP() LOOP_XYZ(a) hotend_offset[e][a] = tmp[a][e];
  111. #if ENABLED(DUAL_X_CARRIAGE)
  112. hotend_offset[1].x = _MAX(X2_HOME_POS, X2_MAX_POS);
  113. #endif
  114. }
  115. #endif
  116. // The feedrate for the current move, often used as the default if
  117. // no other feedrate is specified. Overridden for special moves.
  118. // Set by the last G0 through G5 command's "F" parameter.
  119. // Functions that override this for custom moves *must always* restore it!
  120. feedRate_t feedrate_mm_s = MMM_TO_MMS(1500);
  121. int16_t feedrate_percentage = 100;
  122. // Homing feedrate is const progmem - compare to constexpr in the header
  123. const feedRate_t homing_feedrate_mm_s[XYZ] PROGMEM = {
  124. #if ENABLED(DELTA)
  125. MMM_TO_MMS(HOMING_FEEDRATE_Z), MMM_TO_MMS(HOMING_FEEDRATE_Z),
  126. #else
  127. MMM_TO_MMS(HOMING_FEEDRATE_XY), MMM_TO_MMS(HOMING_FEEDRATE_XY),
  128. #endif
  129. MMM_TO_MMS(HOMING_FEEDRATE_Z)
  130. };
  131. // Cartesian conversion result goes here:
  132. xyz_pos_t cartes;
  133. #if IS_KINEMATIC
  134. abc_pos_t delta;
  135. #if HAS_SCARA_OFFSET
  136. abc_pos_t scara_home_offset;
  137. #endif
  138. #if HAS_SOFTWARE_ENDSTOPS
  139. float delta_max_radius, delta_max_radius_2;
  140. #elif IS_SCARA
  141. constexpr float delta_max_radius = SCARA_PRINTABLE_RADIUS,
  142. delta_max_radius_2 = sq(SCARA_PRINTABLE_RADIUS);
  143. #else // DELTA
  144. constexpr float delta_max_radius = DELTA_PRINTABLE_RADIUS,
  145. delta_max_radius_2 = sq(DELTA_PRINTABLE_RADIUS);
  146. #endif
  147. #endif
  148. /**
  149. * The workspace can be offset by some commands, or
  150. * these offsets may be omitted to save on computation.
  151. */
  152. #if HAS_POSITION_SHIFT
  153. // The distance that XYZ has been offset by G92. Reset by G28.
  154. xyz_pos_t position_shift{0};
  155. #endif
  156. #if HAS_HOME_OFFSET
  157. // This offset is added to the configured home position.
  158. // Set by M206, M428, or menu item. Saved to EEPROM.
  159. xyz_pos_t home_offset{0};
  160. #endif
  161. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  162. // The above two are combined to save on computes
  163. xyz_pos_t workspace_offset{0};
  164. #endif
  165. #if HAS_ABL_NOT_UBL
  166. float xy_probe_feedrate_mm_s = MMM_TO_MMS(XY_PROBE_SPEED);
  167. #endif
  168. /**
  169. * Output the current position to serial
  170. */
  171. inline void report_more_positions() {
  172. stepper.report_positions();
  173. TERN_(IS_SCARA, scara_report_positions());
  174. }
  175. // Report the logical position for a given machine position
  176. inline void report_logical_position(const xyze_pos_t &rpos) {
  177. const xyze_pos_t lpos = rpos.asLogical();
  178. SERIAL_ECHOPAIR_P(X_LBL, lpos.x, SP_Y_LBL, lpos.y, SP_Z_LBL, lpos.z, SP_E_LBL, lpos.e);
  179. }
  180. // Report the real current position according to the steppers.
  181. // Forward kinematics and un-leveling are applied.
  182. void report_real_position() {
  183. get_cartesian_from_steppers();
  184. xyze_pos_t npos = cartes;
  185. npos.e = planner.get_axis_position_mm(E_AXIS);
  186. #if HAS_POSITION_MODIFIERS
  187. planner.unapply_modifiers(npos, true);
  188. #endif
  189. report_logical_position(npos);
  190. report_more_positions();
  191. }
  192. // Report the logical current position according to the most recent G-code command
  193. void report_current_position() {
  194. report_logical_position(current_position);
  195. report_more_positions();
  196. }
  197. /**
  198. * Report the logical current position according to the most recent G-code command.
  199. * The planner.position always corresponds to the last G-code too. This makes M114
  200. * suitable for debugging kinematics and leveling while avoiding planner sync that
  201. * definitively interrupts the printing flow.
  202. */
  203. void report_current_position_projected() {
  204. report_logical_position(current_position);
  205. stepper.report_a_position(planner.position);
  206. }
  207. /**
  208. * sync_plan_position
  209. *
  210. * Set the planner/stepper positions directly from current_position with
  211. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  212. */
  213. void sync_plan_position() {
  214. if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position", current_position);
  215. planner.set_position_mm(current_position);
  216. }
  217. void sync_plan_position_e() { planner.set_e_position_mm(current_position.e); }
  218. /**
  219. * Get the stepper positions in the cartes[] array.
  220. * Forward kinematics are applied for DELTA and SCARA.
  221. *
  222. * The result is in the current coordinate space with
  223. * leveling applied. The coordinates need to be run through
  224. * unapply_leveling to obtain the "ideal" coordinates
  225. * suitable for current_position, etc.
  226. */
  227. void get_cartesian_from_steppers() {
  228. #if ENABLED(DELTA)
  229. forward_kinematics_DELTA(planner.get_axis_positions_mm());
  230. #else
  231. #if IS_SCARA
  232. forward_kinematics_SCARA(
  233. planner.get_axis_position_degrees(A_AXIS),
  234. planner.get_axis_position_degrees(B_AXIS)
  235. );
  236. #else
  237. cartes.set(planner.get_axis_position_mm(X_AXIS), planner.get_axis_position_mm(Y_AXIS));
  238. #endif
  239. cartes.z = planner.get_axis_position_mm(Z_AXIS);
  240. #endif
  241. }
  242. /**
  243. * Set the current_position for an axis based on
  244. * the stepper positions, removing any leveling that
  245. * may have been applied.
  246. *
  247. * To prevent small shifts in axis position always call
  248. * sync_plan_position after updating axes with this.
  249. *
  250. * To keep hosts in sync, always call report_current_position
  251. * after updating the current_position.
  252. */
  253. void set_current_from_steppers_for_axis(const AxisEnum axis) {
  254. get_cartesian_from_steppers();
  255. xyze_pos_t pos = cartes;
  256. pos.e = planner.get_axis_position_mm(E_AXIS);
  257. #if HAS_POSITION_MODIFIERS
  258. planner.unapply_modifiers(pos, true);
  259. #endif
  260. if (axis == ALL_AXES)
  261. current_position = pos;
  262. else
  263. current_position[axis] = pos[axis];
  264. }
  265. /**
  266. * Move the planner to the current position from wherever it last moved
  267. * (or from wherever it has been told it is located).
  268. */
  269. void line_to_current_position(const feedRate_t &fr_mm_s/*=feedrate_mm_s*/) {
  270. planner.buffer_line(current_position, fr_mm_s, active_extruder);
  271. }
  272. #if EXTRUDERS
  273. void unscaled_e_move(const float &length, const feedRate_t &fr_mm_s) {
  274. TERN_(HAS_FILAMENT_SENSOR, runout.reset());
  275. current_position.e += length / planner.e_factor[active_extruder];
  276. line_to_current_position(fr_mm_s);
  277. planner.synchronize();
  278. }
  279. #endif
  280. #if IS_KINEMATIC
  281. /**
  282. * Buffer a fast move without interpolation. Set current_position to destination
  283. */
  284. void prepare_fast_move_to_destination(const feedRate_t &scaled_fr_mm_s/*=MMS_SCALED(feedrate_mm_s)*/) {
  285. if (DEBUGGING(LEVELING)) DEBUG_POS("prepare_fast_move_to_destination", destination);
  286. #if UBL_SEGMENTED
  287. // UBL segmented line will do Z-only moves in single segment
  288. ubl.line_to_destination_segmented(scaled_fr_mm_s);
  289. #else
  290. if (current_position == destination) return;
  291. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  292. #endif
  293. current_position = destination;
  294. }
  295. #endif // IS_KINEMATIC
  296. /**
  297. * Do a fast or normal move to 'destination' with an optional FR.
  298. * - Move at normal speed regardless of feedrate percentage.
  299. * - Extrude the specified length regardless of flow percentage.
  300. */
  301. void _internal_move_to_destination(const feedRate_t &fr_mm_s/*=0.0f*/
  302. #if IS_KINEMATIC
  303. , const bool is_fast/*=false*/
  304. #endif
  305. ) {
  306. const feedRate_t old_feedrate = feedrate_mm_s;
  307. if (fr_mm_s) feedrate_mm_s = fr_mm_s;
  308. const uint16_t old_pct = feedrate_percentage;
  309. feedrate_percentage = 100;
  310. #if EXTRUDERS
  311. const float old_fac = planner.e_factor[active_extruder];
  312. planner.e_factor[active_extruder] = 1.0f;
  313. #endif
  314. #if IS_KINEMATIC
  315. if (is_fast)
  316. prepare_fast_move_to_destination();
  317. else
  318. #endif
  319. prepare_line_to_destination();
  320. feedrate_mm_s = old_feedrate;
  321. feedrate_percentage = old_pct;
  322. #if EXTRUDERS
  323. planner.e_factor[active_extruder] = old_fac;
  324. #endif
  325. }
  326. /**
  327. * Plan a move to (X, Y, Z) and set the current_position
  328. */
  329. void do_blocking_move_to(const float rx, const float ry, const float rz, const feedRate_t &fr_mm_s/*=0.0*/) {
  330. if (DEBUGGING(LEVELING)) DEBUG_XYZ(">>> do_blocking_move_to", rx, ry, rz);
  331. const feedRate_t z_feedrate = fr_mm_s ?: homing_feedrate(Z_AXIS),
  332. xy_feedrate = fr_mm_s ?: feedRate_t(XY_PROBE_FEEDRATE_MM_S);
  333. #if ENABLED(DELTA)
  334. if (!position_is_reachable(rx, ry)) return;
  335. REMEMBER(fr, feedrate_mm_s, xy_feedrate);
  336. destination = current_position; // sync destination at the start
  337. if (DEBUGGING(LEVELING)) DEBUG_POS("destination = current_position", destination);
  338. // when in the danger zone
  339. if (current_position.z > delta_clip_start_height) {
  340. if (rz > delta_clip_start_height) { // staying in the danger zone
  341. destination.set(rx, ry, rz); // move directly (uninterpolated)
  342. prepare_internal_fast_move_to_destination(); // set current_position from destination
  343. if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position);
  344. return;
  345. }
  346. destination.z = delta_clip_start_height;
  347. prepare_internal_fast_move_to_destination(); // set current_position from destination
  348. if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position);
  349. }
  350. if (rz > current_position.z) { // raising?
  351. destination.z = rz;
  352. prepare_internal_fast_move_to_destination(z_feedrate); // set current_position from destination
  353. if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position);
  354. }
  355. destination.set(rx, ry);
  356. prepare_internal_move_to_destination(); // set current_position from destination
  357. if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position);
  358. if (rz < current_position.z) { // lowering?
  359. destination.z = rz;
  360. prepare_internal_fast_move_to_destination(z_feedrate); // set current_position from destination
  361. if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position);
  362. }
  363. #elif IS_SCARA
  364. if (!position_is_reachable(rx, ry)) return;
  365. destination = current_position;
  366. // If Z needs to raise, do it before moving XY
  367. if (destination.z < rz) {
  368. destination.z = rz;
  369. prepare_internal_fast_move_to_destination(z_feedrate);
  370. }
  371. destination.set(rx, ry);
  372. prepare_internal_fast_move_to_destination(xy_feedrate);
  373. // If Z needs to lower, do it after moving XY
  374. if (destination.z > rz) {
  375. destination.z = rz;
  376. prepare_internal_fast_move_to_destination(z_feedrate);
  377. }
  378. #else
  379. // If Z needs to raise, do it before moving XY
  380. if (current_position.z < rz) {
  381. current_position.z = rz;
  382. line_to_current_position(z_feedrate);
  383. }
  384. current_position.set(rx, ry);
  385. line_to_current_position(xy_feedrate);
  386. // If Z needs to lower, do it after moving XY
  387. if (current_position.z > rz) {
  388. current_position.z = rz;
  389. line_to_current_position(z_feedrate);
  390. }
  391. #endif
  392. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< do_blocking_move_to");
  393. planner.synchronize();
  394. }
  395. void do_blocking_move_to(const xy_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
  396. do_blocking_move_to(raw.x, raw.y, current_position.z, fr_mm_s);
  397. }
  398. void do_blocking_move_to(const xyz_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
  399. do_blocking_move_to(raw.x, raw.y, raw.z, fr_mm_s);
  400. }
  401. void do_blocking_move_to(const xyze_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
  402. do_blocking_move_to(raw.x, raw.y, raw.z, fr_mm_s);
  403. }
  404. void do_blocking_move_to_x(const float &rx, const feedRate_t &fr_mm_s/*=0.0*/) {
  405. do_blocking_move_to(rx, current_position.y, current_position.z, fr_mm_s);
  406. }
  407. void do_blocking_move_to_y(const float &ry, const feedRate_t &fr_mm_s/*=0.0*/) {
  408. do_blocking_move_to(current_position.x, ry, current_position.z, fr_mm_s);
  409. }
  410. void do_blocking_move_to_z(const float &rz, const feedRate_t &fr_mm_s/*=0.0*/) {
  411. do_blocking_move_to_xy_z(current_position, rz, fr_mm_s);
  412. }
  413. void do_blocking_move_to_xy(const float &rx, const float &ry, const feedRate_t &fr_mm_s/*=0.0*/) {
  414. do_blocking_move_to(rx, ry, current_position.z, fr_mm_s);
  415. }
  416. void do_blocking_move_to_xy(const xy_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
  417. do_blocking_move_to_xy(raw.x, raw.y, fr_mm_s);
  418. }
  419. void do_blocking_move_to_xy_z(const xy_pos_t &raw, const float &z, const feedRate_t &fr_mm_s/*=0.0f*/) {
  420. do_blocking_move_to(raw.x, raw.y, z, fr_mm_s);
  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. bool soft_endstops_enabled = true;
  442. // Software Endstops are based on the configured limits.
  443. axis_limits_t soft_endstop = {
  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 (dxc_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 (old_tool_index != new_tool_index) {
  502. const float offs = hotend_offset[new_tool_index][axis] - hotend_offset[old_tool_index][axis];
  503. soft_endstop.min[axis] += offs;
  504. soft_endstop.max[axis] += offs;
  505. }
  506. else {
  507. const float offs = hotend_offset[active_extruder][axis];
  508. soft_endstop.min[axis] = base_min_pos(axis) + offs;
  509. soft_endstop.max[axis] = base_max_pos(axis) + offs;
  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_endstops_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, TEST(axis_homed, X_AXIS) && TEST(axis_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 (TEST(axis_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 (TEST(axis_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 (TEST(axis_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. #endif // HAS_SOFTWARE_ENDSTOPS
  568. #if !UBL_SEGMENTED
  569. FORCE_INLINE void segment_idle(millis_t &next_idle_ms) {
  570. const millis_t ms = millis();
  571. if (ELAPSED(ms, next_idle_ms)) {
  572. next_idle_ms = ms + 200UL;
  573. return idle();
  574. }
  575. thermalManager.manage_heater(); // Returns immediately on most calls
  576. }
  577. #if IS_KINEMATIC
  578. #if IS_SCARA
  579. /**
  580. * Before raising this value, use M665 S[seg_per_sec] to decrease
  581. * the number of segments-per-second. Default is 200. Some deltas
  582. * do better with 160 or lower. It would be good to know how many
  583. * segments-per-second are actually possible for SCARA on AVR.
  584. *
  585. * Longer segments result in less kinematic overhead
  586. * but may produce jagged lines. Try 0.5mm, 1.0mm, and 2.0mm
  587. * and compare the difference.
  588. */
  589. #define SCARA_MIN_SEGMENT_LENGTH 0.5f
  590. #endif
  591. /**
  592. * Prepare a linear move in a DELTA or SCARA setup.
  593. *
  594. * Called from prepare_line_to_destination as the
  595. * default Delta/SCARA segmenter.
  596. *
  597. * This calls planner.buffer_line several times, adding
  598. * small incremental moves for DELTA or SCARA.
  599. *
  600. * For Unified Bed Leveling (Delta or Segmented Cartesian)
  601. * the ubl.line_to_destination_segmented method replaces this.
  602. *
  603. * For Auto Bed Leveling (Bilinear) with SEGMENT_LEVELED_MOVES
  604. * this is replaced by segmented_line_to_destination below.
  605. */
  606. inline bool line_to_destination_kinematic() {
  607. // Get the top feedrate of the move in the XY plane
  608. const float scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
  609. const xyze_float_t diff = destination - current_position;
  610. // If the move is only in Z/E don't split up the move
  611. if (!diff.x && !diff.y) {
  612. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  613. return false; // caller will update current_position
  614. }
  615. // Fail if attempting move outside printable radius
  616. if (!position_is_reachable(destination)) return true;
  617. // Get the linear distance in XYZ
  618. float cartesian_mm = diff.magnitude();
  619. // If the move is very short, check the E move distance
  620. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(diff.e);
  621. // No E move either? Game over.
  622. if (UNEAR_ZERO(cartesian_mm)) return true;
  623. // Minimum number of seconds to move the given distance
  624. const float seconds = cartesian_mm / scaled_fr_mm_s;
  625. // The number of segments-per-second times the duration
  626. // gives the number of segments
  627. uint16_t segments = delta_segments_per_second * seconds;
  628. // For SCARA enforce a minimum segment size
  629. #if IS_SCARA
  630. NOMORE(segments, cartesian_mm * RECIPROCAL(SCARA_MIN_SEGMENT_LENGTH));
  631. #endif
  632. // At least one segment is required
  633. NOLESS(segments, 1U);
  634. // The approximate length of each segment
  635. const float inv_segments = 1.0f / float(segments),
  636. cartesian_segment_mm = cartesian_mm * inv_segments;
  637. const xyze_float_t segment_distance = diff * inv_segments;
  638. #if ENABLED(SCARA_FEEDRATE_SCALING)
  639. const float inv_duration = scaled_fr_mm_s / cartesian_segment_mm;
  640. #endif
  641. /*
  642. SERIAL_ECHOPAIR("mm=", cartesian_mm);
  643. SERIAL_ECHOPAIR(" seconds=", seconds);
  644. SERIAL_ECHOPAIR(" segments=", segments);
  645. SERIAL_ECHOPAIR(" segment_mm=", cartesian_segment_mm);
  646. SERIAL_EOL();
  647. //*/
  648. // Get the current position as starting point
  649. xyze_pos_t raw = current_position;
  650. // Calculate and execute the segments
  651. millis_t next_idle_ms = millis() + 200UL;
  652. while (--segments) {
  653. segment_idle(next_idle_ms);
  654. raw += segment_distance;
  655. if (!planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, cartesian_segment_mm
  656. #if ENABLED(SCARA_FEEDRATE_SCALING)
  657. , inv_duration
  658. #endif
  659. )) break;
  660. }
  661. // Ensure last segment arrives at target location.
  662. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder, cartesian_segment_mm
  663. #if ENABLED(SCARA_FEEDRATE_SCALING)
  664. , inv_duration
  665. #endif
  666. );
  667. return false; // caller will update current_position
  668. }
  669. #else // !IS_KINEMATIC
  670. #if ENABLED(SEGMENT_LEVELED_MOVES)
  671. /**
  672. * Prepare a segmented move on a CARTESIAN setup.
  673. *
  674. * This calls planner.buffer_line several times, adding
  675. * small incremental moves. This allows the planner to
  676. * apply more detailed bed leveling to the full move.
  677. */
  678. inline void segmented_line_to_destination(const feedRate_t &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
  679. const xyze_float_t diff = destination - current_position;
  680. // If the move is only in Z/E don't split up the move
  681. if (!diff.x && !diff.y) {
  682. planner.buffer_line(destination, fr_mm_s, active_extruder);
  683. return;
  684. }
  685. // Get the linear distance in XYZ
  686. // If the move is very short, check the E move distance
  687. // No E move either? Game over.
  688. float cartesian_mm = diff.magnitude();
  689. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(diff.e);
  690. if (UNEAR_ZERO(cartesian_mm)) return;
  691. // The length divided by the segment size
  692. // At least one segment is required
  693. uint16_t segments = cartesian_mm / segment_size;
  694. NOLESS(segments, 1U);
  695. // The approximate length of each segment
  696. const float inv_segments = 1.0f / float(segments),
  697. cartesian_segment_mm = cartesian_mm * inv_segments;
  698. const xyze_float_t segment_distance = diff * inv_segments;
  699. #if ENABLED(SCARA_FEEDRATE_SCALING)
  700. const float inv_duration = scaled_fr_mm_s / cartesian_segment_mm;
  701. #endif
  702. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  703. // SERIAL_ECHOLNPAIR(" segments=", segments);
  704. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  705. // Get the raw current position as starting point
  706. xyze_pos_t raw = current_position;
  707. // Calculate and execute the segments
  708. millis_t next_idle_ms = millis() + 200UL;
  709. while (--segments) {
  710. segment_idle(next_idle_ms);
  711. raw += segment_distance;
  712. if (!planner.buffer_line(raw, fr_mm_s, active_extruder, cartesian_segment_mm
  713. #if ENABLED(SCARA_FEEDRATE_SCALING)
  714. , inv_duration
  715. #endif
  716. )) break;
  717. }
  718. // Since segment_distance is only approximate,
  719. // the final move must be to the exact destination.
  720. planner.buffer_line(destination, fr_mm_s, active_extruder, cartesian_segment_mm
  721. #if ENABLED(SCARA_FEEDRATE_SCALING)
  722. , inv_duration
  723. #endif
  724. );
  725. }
  726. #endif // SEGMENT_LEVELED_MOVES
  727. /**
  728. * Prepare a linear move in a Cartesian setup.
  729. *
  730. * When a mesh-based leveling system is active, moves are segmented
  731. * according to the configuration of the leveling system.
  732. *
  733. * Return true if 'current_position' was set to 'destination'
  734. */
  735. inline bool line_to_destination_cartesian() {
  736. const float scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
  737. #if HAS_MESH
  738. if (planner.leveling_active && planner.leveling_active_at_z(destination.z)) {
  739. #if ENABLED(AUTO_BED_LEVELING_UBL)
  740. ubl.line_to_destination_cartesian(scaled_fr_mm_s, active_extruder); // UBL's motion routine needs to know about
  741. return true; // all moves, including Z-only moves.
  742. #elif ENABLED(SEGMENT_LEVELED_MOVES)
  743. segmented_line_to_destination(scaled_fr_mm_s);
  744. return false; // caller will update current_position
  745. #else
  746. /**
  747. * For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
  748. * Otherwise fall through to do a direct single move.
  749. */
  750. if (xy_pos_t(current_position) != xy_pos_t(destination)) {
  751. #if ENABLED(MESH_BED_LEVELING)
  752. mbl.line_to_destination(scaled_fr_mm_s);
  753. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  754. bilinear_line_to_destination(scaled_fr_mm_s);
  755. #endif
  756. return true;
  757. }
  758. #endif
  759. }
  760. #endif // HAS_MESH
  761. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  762. return false; // caller will update current_position
  763. }
  764. #endif // !IS_KINEMATIC
  765. #endif // !UBL_SEGMENTED
  766. #if HAS_DUPLICATION_MODE
  767. bool extruder_duplication_enabled,
  768. mirrored_duplication_mode;
  769. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  770. uint8_t duplication_e_mask; // = 0
  771. #endif
  772. #endif
  773. #if ENABLED(DUAL_X_CARRIAGE)
  774. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  775. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  776. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  777. xyz_pos_t raised_parked_position; // used in mode 1
  778. bool active_extruder_parked = false; // used in mode 1 & 2
  779. millis_t delayed_move_time = 0; // used in mode 1
  780. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  781. float x_home_pos(const int extruder) {
  782. if (extruder == 0)
  783. return base_home_pos(X_AXIS);
  784. else
  785. /**
  786. * In dual carriage mode the extruder offset provides an override of the
  787. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  788. * This allows soft recalibration of the second extruder home position
  789. * without firmware reflash (through the M218 command).
  790. */
  791. return hotend_offset[1].x > 0 ? hotend_offset[1].x : X2_HOME_POS;
  792. }
  793. /**
  794. * Prepare a linear move in a dual X axis setup
  795. *
  796. * Return true if current_position[] was set to destination[]
  797. */
  798. inline bool dual_x_carriage_unpark() {
  799. if (active_extruder_parked) {
  800. switch (dual_x_carriage_mode) {
  801. case DXC_FULL_CONTROL_MODE:
  802. break;
  803. case DXC_AUTO_PARK_MODE:
  804. if (current_position.e == destination.e) {
  805. // This is a travel move (with no extrusion)
  806. // Skip it, but keep track of the current position
  807. // (so it can be used as the start of the next non-travel move)
  808. if (delayed_move_time != 0xFFFFFFFFUL) {
  809. current_position = destination;
  810. NOLESS(raised_parked_position.z, destination.z);
  811. delayed_move_time = millis();
  812. return true;
  813. }
  814. }
  815. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  816. #define CUR_X current_position.x
  817. #define CUR_Y current_position.y
  818. #define CUR_Z current_position.z
  819. #define CUR_E current_position.e
  820. #define RAISED_X raised_parked_position.x
  821. #define RAISED_Y raised_parked_position.y
  822. #define RAISED_Z raised_parked_position.z
  823. if ( planner.buffer_line(RAISED_X, RAISED_Y, RAISED_Z, CUR_E, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder))
  824. if (planner.buffer_line( CUR_X, CUR_Y, RAISED_Z, CUR_E, PLANNER_XY_FEEDRATE(), active_extruder))
  825. line_to_current_position(planner.settings.max_feedrate_mm_s[Z_AXIS]);
  826. delayed_move_time = 0;
  827. active_extruder_parked = false;
  828. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Clear active_extruder_parked");
  829. break;
  830. case DXC_MIRRORED_MODE:
  831. case DXC_DUPLICATION_MODE:
  832. if (active_extruder == 0) {
  833. xyze_pos_t new_pos = current_position;
  834. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  835. new_pos.x += duplicate_extruder_x_offset;
  836. else
  837. new_pos.x = inactive_extruder_x_pos;
  838. // move duplicate extruder into correct duplication position.
  839. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Set planner X", inactive_extruder_x_pos, " ... Line to X", new_pos.x);
  840. planner.set_position_mm(inactive_extruder_x_pos, current_position.y, current_position.z, current_position.e);
  841. if (!planner.buffer_line(new_pos, planner.settings.max_feedrate_mm_s[X_AXIS], 1)) break;
  842. planner.synchronize();
  843. sync_plan_position();
  844. extruder_duplication_enabled = true;
  845. active_extruder_parked = false;
  846. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  847. }
  848. else if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Active extruder not 0");
  849. break;
  850. }
  851. }
  852. stepper.set_directions();
  853. return false;
  854. }
  855. #endif // DUAL_X_CARRIAGE
  856. /**
  857. * Prepare a single move and get ready for the next one
  858. *
  859. * This may result in several calls to planner.buffer_line to
  860. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  861. *
  862. * Make sure current_position.e and destination.e are good
  863. * before calling or cold/lengthy extrusion may get missed.
  864. *
  865. * Before exit, current_position is set to destination.
  866. */
  867. void prepare_line_to_destination() {
  868. apply_motion_limits(destination);
  869. #if EITHER(PREVENT_COLD_EXTRUSION, PREVENT_LENGTHY_EXTRUDE)
  870. if (!DEBUGGING(DRYRUN) && destination.e != current_position.e) {
  871. bool ignore_e = false;
  872. #if ENABLED(PREVENT_COLD_EXTRUSION)
  873. ignore_e = thermalManager.tooColdToExtrude(active_extruder);
  874. if (ignore_e) SERIAL_ECHO_MSG(STR_ERR_COLD_EXTRUDE_STOP);
  875. #endif
  876. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  877. const float e_delta = ABS(destination.e - current_position.e) * planner.e_factor[active_extruder];
  878. if (e_delta > (EXTRUDE_MAXLENGTH)) {
  879. #if ENABLED(MIXING_EXTRUDER)
  880. float collector[MIXING_STEPPERS];
  881. mixer.refresh_collector(1.0, mixer.get_current_vtool(), collector);
  882. MIXER_STEPPER_LOOP(e) {
  883. if (e_delta * collector[e] > (EXTRUDE_MAXLENGTH)) {
  884. ignore_e = true;
  885. SERIAL_ECHO_MSG(STR_ERR_LONG_EXTRUDE_STOP);
  886. break;
  887. }
  888. }
  889. #else
  890. ignore_e = true;
  891. SERIAL_ECHO_MSG(STR_ERR_LONG_EXTRUDE_STOP);
  892. #endif
  893. }
  894. #endif
  895. if (ignore_e) {
  896. current_position.e = destination.e; // Behave as if the E move really took place
  897. planner.set_e_position_mm(destination.e); // Prevent the planner from complaining too
  898. }
  899. }
  900. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  901. if (TERN0(DUAL_X_CARRIAGE, dual_x_carriage_unpark())) return;
  902. if (
  903. #if UBL_SEGMENTED
  904. #if IS_KINEMATIC // UBL using Kinematic / Cartesian cases as a workaround for now.
  905. ubl.line_to_destination_segmented(MMS_SCALED(feedrate_mm_s))
  906. #else
  907. line_to_destination_cartesian()
  908. #endif
  909. #elif IS_KINEMATIC
  910. line_to_destination_kinematic()
  911. #else
  912. line_to_destination_cartesian()
  913. #endif
  914. ) return;
  915. current_position = destination;
  916. }
  917. uint8_t axes_need_homing(uint8_t axis_bits/*=0x07*/) {
  918. #if ENABLED(HOME_AFTER_DEACTIVATE)
  919. #define HOMED_FLAGS axis_known_position
  920. #else
  921. #define HOMED_FLAGS axis_homed
  922. #endif
  923. // Clear test bits that are homed
  924. if (TEST(axis_bits, X_AXIS) && TEST(HOMED_FLAGS, X_AXIS)) CBI(axis_bits, X_AXIS);
  925. if (TEST(axis_bits, Y_AXIS) && TEST(HOMED_FLAGS, Y_AXIS)) CBI(axis_bits, Y_AXIS);
  926. if (TEST(axis_bits, Z_AXIS) && TEST(HOMED_FLAGS, Z_AXIS)) CBI(axis_bits, Z_AXIS);
  927. return axis_bits;
  928. }
  929. bool axis_unhomed_error(uint8_t axis_bits/*=0x07*/) {
  930. if ((axis_bits = axes_need_homing(axis_bits))) {
  931. PGM_P home_first = GET_TEXT(MSG_HOME_FIRST);
  932. char msg[strlen_P(home_first)+1];
  933. sprintf_P(msg, home_first,
  934. TEST(axis_bits, X_AXIS) ? "X" : "",
  935. TEST(axis_bits, Y_AXIS) ? "Y" : "",
  936. TEST(axis_bits, Z_AXIS) ? "Z" : ""
  937. );
  938. SERIAL_ECHO_START();
  939. SERIAL_ECHOLN(msg);
  940. TERN_(HAS_DISPLAY, ui.set_status(msg));
  941. return true;
  942. }
  943. return false;
  944. }
  945. /**
  946. * Homing bump feedrate (mm/s)
  947. */
  948. feedRate_t get_homing_bump_feedrate(const AxisEnum axis) {
  949. if (TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS))
  950. return MMM_TO_MMS(Z_PROBE_SPEED_SLOW);
  951. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  952. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  953. if (hbd < 1) {
  954. hbd = 10;
  955. SERIAL_ECHO_MSG("Warning: Homing Bump Divisor < 1");
  956. }
  957. return homing_feedrate(axis) / float(hbd);
  958. }
  959. #if ENABLED(SENSORLESS_HOMING)
  960. /**
  961. * Set sensorless homing if the axis has it, accounting for Core Kinematics.
  962. */
  963. sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis) {
  964. sensorless_t stealth_states { false };
  965. switch (axis) {
  966. default: break;
  967. #if X_SENSORLESS
  968. case X_AXIS:
  969. stealth_states.x = tmc_enable_stallguard(stepperX);
  970. #if AXIS_HAS_STALLGUARD(X2)
  971. stealth_states.x2 = tmc_enable_stallguard(stepperX2);
  972. #endif
  973. #if CORE_IS_XY && Y_SENSORLESS
  974. stealth_states.y = tmc_enable_stallguard(stepperY);
  975. #elif CORE_IS_XZ && Z_SENSORLESS
  976. stealth_states.z = tmc_enable_stallguard(stepperZ);
  977. #endif
  978. break;
  979. #endif
  980. #if Y_SENSORLESS
  981. case Y_AXIS:
  982. stealth_states.y = tmc_enable_stallguard(stepperY);
  983. #if AXIS_HAS_STALLGUARD(Y2)
  984. stealth_states.y2 = tmc_enable_stallguard(stepperY2);
  985. #endif
  986. #if CORE_IS_XY && X_SENSORLESS
  987. stealth_states.x = tmc_enable_stallguard(stepperX);
  988. #elif CORE_IS_YZ && Z_SENSORLESS
  989. stealth_states.z = tmc_enable_stallguard(stepperZ);
  990. #endif
  991. break;
  992. #endif
  993. #if Z_SENSORLESS
  994. case Z_AXIS:
  995. stealth_states.z = tmc_enable_stallguard(stepperZ);
  996. #if AXIS_HAS_STALLGUARD(Z2)
  997. stealth_states.z2 = tmc_enable_stallguard(stepperZ2);
  998. #endif
  999. #if AXIS_HAS_STALLGUARD(Z3)
  1000. stealth_states.z3 = tmc_enable_stallguard(stepperZ3);
  1001. #endif
  1002. #if AXIS_HAS_STALLGUARD(Z4)
  1003. stealth_states.z4 = tmc_enable_stallguard(stepperZ4);
  1004. #endif
  1005. #if CORE_IS_XZ && X_SENSORLESS
  1006. stealth_states.x = tmc_enable_stallguard(stepperX);
  1007. #elif CORE_IS_YZ && Y_SENSORLESS
  1008. stealth_states.y = tmc_enable_stallguard(stepperY);
  1009. #endif
  1010. break;
  1011. #endif
  1012. }
  1013. #if ENABLED(SPI_ENDSTOPS)
  1014. switch (axis) {
  1015. case X_AXIS: if (ENABLED(X_SPI_SENSORLESS)) endstops.tmc_spi_homing.x = true; break;
  1016. case Y_AXIS: if (ENABLED(Y_SPI_SENSORLESS)) endstops.tmc_spi_homing.y = true; break;
  1017. case Z_AXIS: if (ENABLED(Z_SPI_SENSORLESS)) endstops.tmc_spi_homing.z = true; break;
  1018. default: break;
  1019. }
  1020. #endif
  1021. TERN_(IMPROVE_HOMING_RELIABILITY, sg_guard_period = millis() + default_sg_guard_duration);
  1022. return stealth_states;
  1023. }
  1024. void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) {
  1025. switch (axis) {
  1026. default: break;
  1027. #if X_SENSORLESS
  1028. case X_AXIS:
  1029. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1030. #if AXIS_HAS_STALLGUARD(X2)
  1031. tmc_disable_stallguard(stepperX2, enable_stealth.x2);
  1032. #endif
  1033. #if CORE_IS_XY && Y_SENSORLESS
  1034. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1035. #elif CORE_IS_XZ && Z_SENSORLESS
  1036. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1037. #endif
  1038. break;
  1039. #endif
  1040. #if Y_SENSORLESS
  1041. case Y_AXIS:
  1042. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1043. #if AXIS_HAS_STALLGUARD(Y2)
  1044. tmc_disable_stallguard(stepperY2, enable_stealth.y2);
  1045. #endif
  1046. #if CORE_IS_XY && X_SENSORLESS
  1047. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1048. #elif CORE_IS_YZ && Z_SENSORLESS
  1049. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1050. #endif
  1051. break;
  1052. #endif
  1053. #if Z_SENSORLESS
  1054. case Z_AXIS:
  1055. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1056. #if AXIS_HAS_STALLGUARD(Z2)
  1057. tmc_disable_stallguard(stepperZ2, enable_stealth.z2);
  1058. #endif
  1059. #if AXIS_HAS_STALLGUARD(Z3)
  1060. tmc_disable_stallguard(stepperZ3, enable_stealth.z3);
  1061. #endif
  1062. #if AXIS_HAS_STALLGUARD(Z4)
  1063. tmc_disable_stallguard(stepperZ4, enable_stealth.z4);
  1064. #endif
  1065. #if CORE_IS_XZ && X_SENSORLESS
  1066. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1067. #elif CORE_IS_YZ && Y_SENSORLESS
  1068. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1069. #endif
  1070. break;
  1071. #endif
  1072. }
  1073. #if ENABLED(SPI_ENDSTOPS)
  1074. switch (axis) {
  1075. case X_AXIS: if (ENABLED(X_SPI_SENSORLESS)) endstops.tmc_spi_homing.x = false; break;
  1076. case Y_AXIS: if (ENABLED(Y_SPI_SENSORLESS)) endstops.tmc_spi_homing.y = false; break;
  1077. case Z_AXIS: if (ENABLED(Z_SPI_SENSORLESS)) endstops.tmc_spi_homing.z = false; break;
  1078. default: break;
  1079. }
  1080. #endif
  1081. }
  1082. #endif // SENSORLESS_HOMING
  1083. /**
  1084. * Home an individual linear axis
  1085. */
  1086. void do_homing_move(const AxisEnum axis, const float distance, const feedRate_t fr_mm_s=0.0) {
  1087. const feedRate_t real_fr_mm_s = fr_mm_s ?: homing_feedrate(axis);
  1088. if (DEBUGGING(LEVELING)) {
  1089. DEBUG_ECHOPAIR(">>> do_homing_move(", axis_codes[axis], ", ", distance, ", ");
  1090. if (fr_mm_s)
  1091. DEBUG_ECHO(fr_mm_s);
  1092. else
  1093. DEBUG_ECHOPAIR("[", real_fr_mm_s, "]");
  1094. DEBUG_ECHOLNPGM(")");
  1095. }
  1096. #if ALL(HOMING_Z_WITH_PROBE, HAS_HEATED_BED, WAIT_FOR_BED_HEATER)
  1097. // Wait for bed to heat back up between probing points
  1098. if (axis == Z_AXIS && distance < 0)
  1099. thermalManager.wait_for_bed_heating();
  1100. #endif
  1101. // Only do some things when moving towards an endstop
  1102. const int8_t axis_home_dir = TERN0(DUAL_X_CARRIAGE, axis == X_AXIS)
  1103. ? x_home_dir(active_extruder) : home_dir(axis);
  1104. const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
  1105. #if ENABLED(SENSORLESS_HOMING)
  1106. sensorless_t stealth_states;
  1107. #endif
  1108. if (is_home_dir) {
  1109. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1110. if (axis == Z_AXIS) probe.set_probing_paused(true);
  1111. #endif
  1112. // Disable stealthChop if used. Enable diag1 pin on driver.
  1113. TERN_(SENSORLESS_HOMING, stealth_states = start_sensorless_homing_per_axis(axis));
  1114. }
  1115. #if IS_SCARA
  1116. // Tell the planner the axis is at 0
  1117. current_position[axis] = 0;
  1118. sync_plan_position();
  1119. current_position[axis] = distance;
  1120. line_to_current_position(real_fr_mm_s);
  1121. #else
  1122. // Get the ABC or XYZ positions in mm
  1123. abce_pos_t target = planner.get_axis_positions_mm();
  1124. target[axis] = 0; // Set the single homing axis to 0
  1125. planner.set_machine_position_mm(target); // Update the machine position
  1126. #if HAS_DIST_MM_ARG
  1127. const xyze_float_t cart_dist_mm{0};
  1128. #endif
  1129. // Set delta/cartesian axes directly
  1130. target[axis] = distance; // The move will be towards the endstop
  1131. planner.buffer_segment(target
  1132. #if HAS_DIST_MM_ARG
  1133. , cart_dist_mm
  1134. #endif
  1135. , real_fr_mm_s, active_extruder
  1136. );
  1137. #endif
  1138. planner.synchronize();
  1139. if (is_home_dir) {
  1140. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1141. if (axis == Z_AXIS) probe.set_probing_paused(false);
  1142. #endif
  1143. endstops.validate_homing_move();
  1144. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  1145. TERN_(SENSORLESS_HOMING, end_sensorless_homing_per_axis(axis, stealth_states));
  1146. }
  1147. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< do_homing_move(", axis_codes[axis], ")");
  1148. }
  1149. /**
  1150. * Set an axis' current position to its home position (after homing).
  1151. *
  1152. * For Core and Cartesian robots this applies one-to-one when an
  1153. * individual axis has been homed.
  1154. *
  1155. * DELTA should wait until all homing is done before setting the XYZ
  1156. * current_position to home, because homing is a single operation.
  1157. * In the case where the axis positions are already known and previously
  1158. * homed, DELTA could home to X or Y individually by moving either one
  1159. * to the center. However, homing Z always homes XY and Z.
  1160. *
  1161. * SCARA should wait until all XY homing is done before setting the XY
  1162. * current_position to home, because neither X nor Y is at home until
  1163. * both are at home. Z can however be homed individually.
  1164. *
  1165. * Callers must sync the planner position after calling this!
  1166. */
  1167. void set_axis_is_at_home(const AxisEnum axis) {
  1168. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_at_home(", axis_codes[axis], ")");
  1169. SBI(axis_known_position, axis);
  1170. SBI(axis_homed, axis);
  1171. #if ENABLED(DUAL_X_CARRIAGE)
  1172. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  1173. current_position.x = x_home_pos(active_extruder);
  1174. return;
  1175. }
  1176. #endif
  1177. #if ENABLED(MORGAN_SCARA)
  1178. scara_set_axis_is_at_home(axis);
  1179. #elif ENABLED(DELTA)
  1180. current_position[axis] = (axis == Z_AXIS) ? delta_height - TERN0(HAS_BED_PROBE, probe.offset.z) : base_home_pos(axis);
  1181. #else
  1182. current_position[axis] = base_home_pos(axis);
  1183. #endif
  1184. /**
  1185. * Z Probe Z Homing? Account for the probe's Z offset.
  1186. */
  1187. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  1188. if (axis == Z_AXIS) {
  1189. #if HOMING_Z_WITH_PROBE
  1190. current_position.z -= probe.offset.z;
  1191. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***\n> probe.offset.z = ", probe.offset.z);
  1192. #else
  1193. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("*** Z HOMED TO ENDSTOP ***");
  1194. #endif
  1195. }
  1196. #endif
  1197. TERN_(I2C_POSITION_ENCODERS, I2CPEM.homed(axis));
  1198. TERN_(BABYSTEP_DISPLAY_TOTAL, babystep.reset_total(axis));
  1199. #if HAS_POSITION_SHIFT
  1200. position_shift[axis] = 0;
  1201. update_workspace_offset(axis);
  1202. #endif
  1203. if (DEBUGGING(LEVELING)) {
  1204. #if HAS_HOME_OFFSET
  1205. DEBUG_ECHOLNPAIR("> home_offset[", axis_codes[axis], "] = ", home_offset[axis]);
  1206. #endif
  1207. DEBUG_POS("", current_position);
  1208. DEBUG_ECHOLNPAIR("<<< set_axis_is_at_home(", axis_codes[axis], ")");
  1209. }
  1210. }
  1211. /**
  1212. * Set an axis' to be unhomed.
  1213. */
  1214. void set_axis_not_trusted(const AxisEnum axis) {
  1215. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_not_trusted(", axis_codes[axis], ")");
  1216. CBI(axis_known_position, axis);
  1217. CBI(axis_homed, axis);
  1218. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< set_axis_not_trusted(", axis_codes[axis], ")");
  1219. TERN_(I2C_POSITION_ENCODERS, I2CPEM.unhomed(axis));
  1220. }
  1221. #ifdef TMC_HOME_PHASE
  1222. /**
  1223. * Move the axis back to its home_phase if set and driver is capable (TMC)
  1224. *
  1225. * Improves homing repeatability by homing to stepper coil's nearest absolute
  1226. * phase position. Trinamic drivers use a stepper phase table with 1024 values
  1227. * spanning 4 full steps with 256 positions each (ergo, 1024 positions).
  1228. */
  1229. void backout_to_tmc_homing_phase(const AxisEnum axis) {
  1230. const xyz_long_t home_phase = TMC_HOME_PHASE;
  1231. // check if home phase is disabled for this axis.
  1232. if (home_phase[axis] < 0) return;
  1233. int16_t phasePerUStep, // TMC µsteps(phase) per Marlin µsteps
  1234. phaseCurrent, // The TMC µsteps(phase) count of the current position
  1235. effectorBackoutDir, // Direction in which the effector mm coordinates move away from endstop.
  1236. stepperBackoutDir; // Direction in which the TMC µstep count(phase) move away from endstop.
  1237. switch (axis) {
  1238. #ifdef X_MICROSTEPS
  1239. case X_AXIS:
  1240. phasePerUStep = 256 / (X_MICROSTEPS);
  1241. phaseCurrent = stepperX.get_microstep_counter();
  1242. effectorBackoutDir = -X_HOME_DIR;
  1243. stepperBackoutDir = INVERT_X_DIR ? effectorBackoutDir : -effectorBackoutDir;
  1244. break;
  1245. #endif
  1246. #ifdef Y_MICROSTEPS
  1247. case Y_AXIS:
  1248. phasePerUStep = 256 / (Y_MICROSTEPS);
  1249. phaseCurrent = stepperY.get_microstep_counter();
  1250. effectorBackoutDir = -Y_HOME_DIR;
  1251. stepperBackoutDir = INVERT_Y_DIR ? effectorBackoutDir : -effectorBackoutDir;
  1252. break;
  1253. #endif
  1254. #ifdef Z_MICROSTEPS
  1255. case Z_AXIS:
  1256. phasePerUStep = 256 / (Z_MICROSTEPS);
  1257. phaseCurrent = stepperZ.get_microstep_counter();
  1258. effectorBackoutDir = -Z_HOME_DIR;
  1259. stepperBackoutDir = INVERT_Z_DIR ? effectorBackoutDir : -effectorBackoutDir;
  1260. break;
  1261. #endif
  1262. default: return;
  1263. }
  1264. // Phase distance to nearest home phase position when moving in the backout direction from endstop(may be negative).
  1265. int16_t phaseDelta = (home_phase[axis] - phaseCurrent) * stepperBackoutDir;
  1266. // Check if home distance within endstop assumed repeatability noise of .05mm and warn.
  1267. if (ABS(phaseDelta) * planner.steps_to_mm[axis] / phasePerUStep < 0.05f)
  1268. SERIAL_ECHOLNPAIR("Selected home phase ", home_phase[axis],
  1269. " too close to endstop trigger phase ", phaseCurrent,
  1270. ". Pick a different phase for ", axis_codes[axis]);
  1271. // Skip to next if target position is behind current. So it only moves away from endstop.
  1272. if (phaseDelta < 0) phaseDelta += 1024;
  1273. // Convert TMC µsteps(phase) to whole Marlin µsteps to effector backout direction to mm
  1274. const float mmDelta = int16_t(phaseDelta / phasePerUStep) * effectorBackoutDir * planner.steps_to_mm[axis];
  1275. // Optional debug messages
  1276. if (DEBUGGING(LEVELING)) {
  1277. DEBUG_ECHOLNPAIR(
  1278. "Endstop ", axis_codes[axis], " hit at Phase:", phaseCurrent,
  1279. " Delta:", phaseDelta, " Distance:", mmDelta
  1280. );
  1281. }
  1282. if (mmDelta != 0) {
  1283. // Retrace by the amount computed in mmDelta.
  1284. do_homing_move(axis, mmDelta, get_homing_bump_feedrate(axis));
  1285. }
  1286. }
  1287. #endif
  1288. /**
  1289. * Home an individual "raw axis" to its endstop.
  1290. * This applies to XYZ on Cartesian and Core robots, and
  1291. * to the individual ABC steppers on DELTA and SCARA.
  1292. *
  1293. * At the end of the procedure the axis is marked as
  1294. * homed and the current position of that axis is updated.
  1295. * Kinematic robots should wait till all axes are homed
  1296. * before updating the current position.
  1297. */
  1298. void homeaxis(const AxisEnum axis) {
  1299. #if IS_SCARA
  1300. // Only Z homing (with probe) is permitted
  1301. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  1302. #else
  1303. #define _CAN_HOME(A) (axis == _AXIS(A) && ( \
  1304. ENABLED(A##_SPI_SENSORLESS) \
  1305. || (_AXIS(A) == Z_AXIS && ENABLED(HOMING_Z_WITH_PROBE)) \
  1306. || (A##_MIN_PIN > -1 && A##_HOME_DIR < 0) \
  1307. || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0) \
  1308. ))
  1309. if (!_CAN_HOME(X) && !_CAN_HOME(Y) && !_CAN_HOME(Z)) return;
  1310. #endif
  1311. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")");
  1312. const int axis_home_dir = TERN0(DUAL_X_CARRIAGE, axis == X_AXIS)
  1313. ? x_home_dir(active_extruder) : home_dir(axis);
  1314. // Homing Z towards the bed? Deploy the Z probe or endstop.
  1315. if (TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS && probe.deploy()))
  1316. return;
  1317. // Set flags for X, Y, Z motor locking
  1318. #if HAS_EXTRA_ENDSTOPS
  1319. switch (axis) {
  1320. TERN_(X_DUAL_ENDSTOPS, case X_AXIS:)
  1321. TERN_(Y_DUAL_ENDSTOPS, case Y_AXIS:)
  1322. TERN_(Z_MULTI_ENDSTOPS, case Z_AXIS:)
  1323. stepper.set_separate_multi_axis(true);
  1324. default: break;
  1325. }
  1326. #endif
  1327. // Fast move towards endstop until triggered
  1328. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 1 Fast:");
  1329. #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH)
  1330. if (axis == Z_AXIS && bltouch.deploy()) return; // The initial DEPLOY
  1331. #endif
  1332. #if DISABLED(DELTA) && defined(SENSORLESS_BACKOFF_MM)
  1333. const xy_float_t backoff = SENSORLESS_BACKOFF_MM;
  1334. if (((ENABLED(X_SENSORLESS) && axis == X_AXIS) || (ENABLED(Y_SENSORLESS) && axis == Y_AXIS)) && backoff[axis])
  1335. do_homing_move(axis, -ABS(backoff[axis]) * axis_home_dir, homing_feedrate(axis));
  1336. #endif
  1337. do_homing_move(axis, 1.5f * max_length(TERN(DELTA, Z_AXIS, axis)) * axis_home_dir);
  1338. #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1339. if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE)
  1340. #endif
  1341. // When homing Z with probe respect probe clearance
  1342. const bool use_probe_bump = TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS && home_bump_mm(Z_AXIS));
  1343. const float bump = axis_home_dir * (
  1344. use_probe_bump ? _MAX(Z_CLEARANCE_BETWEEN_PROBES, home_bump_mm(Z_AXIS)) : home_bump_mm(axis)
  1345. );
  1346. // If a second homing move is configured...
  1347. if (bump) {
  1348. // Move away from the endstop by the axis HOMING_BUMP_MM
  1349. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Move Away:");
  1350. do_homing_move(axis, -bump
  1351. #if HOMING_Z_WITH_PROBE
  1352. , MMM_TO_MMS(axis == Z_AXIS ? Z_PROBE_SPEED_FAST : 0)
  1353. #endif
  1354. );
  1355. // Slow move towards endstop until triggered
  1356. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 2 Slow:");
  1357. #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1358. if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE)
  1359. #endif
  1360. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1361. #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH)
  1362. if (axis == Z_AXIS) bltouch.stow(); // The final STOW
  1363. #endif
  1364. }
  1365. #if HAS_EXTRA_ENDSTOPS
  1366. const bool pos_dir = axis_home_dir > 0;
  1367. #if ENABLED(X_DUAL_ENDSTOPS)
  1368. if (axis == X_AXIS) {
  1369. const float adj = ABS(endstops.x2_endstop_adj);
  1370. if (adj) {
  1371. if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1372. do_homing_move(axis, pos_dir ? -adj : adj);
  1373. stepper.set_x_lock(false);
  1374. stepper.set_x2_lock(false);
  1375. }
  1376. }
  1377. #endif
  1378. #if ENABLED(Y_DUAL_ENDSTOPS)
  1379. if (axis == Y_AXIS) {
  1380. const float adj = ABS(endstops.y2_endstop_adj);
  1381. if (adj) {
  1382. if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1383. do_homing_move(axis, pos_dir ? -adj : adj);
  1384. stepper.set_y_lock(false);
  1385. stepper.set_y2_lock(false);
  1386. }
  1387. }
  1388. #endif
  1389. #if ENABLED(Z_MULTI_ENDSTOPS)
  1390. if (axis == Z_AXIS) {
  1391. #if NUM_Z_STEPPER_DRIVERS == 2
  1392. const float adj = ABS(endstops.z2_endstop_adj);
  1393. if (adj) {
  1394. if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z1_lock(true); else stepper.set_z2_lock(true);
  1395. do_homing_move(axis, pos_dir ? -adj : adj);
  1396. stepper.set_z1_lock(false);
  1397. stepper.set_z2_lock(false);
  1398. }
  1399. #else
  1400. // Handy arrays of stepper lock function pointers
  1401. typedef void (*adjustFunc_t)(const bool);
  1402. adjustFunc_t lock[] = {
  1403. stepper.set_z1_lock, stepper.set_z2_lock, stepper.set_z3_lock
  1404. #if NUM_Z_STEPPER_DRIVERS >= 4
  1405. , stepper.set_z4_lock
  1406. #endif
  1407. };
  1408. float adj[] = {
  1409. 0, endstops.z2_endstop_adj, endstops.z3_endstop_adj
  1410. #if NUM_Z_STEPPER_DRIVERS >= 4
  1411. , endstops.z4_endstop_adj
  1412. #endif
  1413. };
  1414. adjustFunc_t tempLock;
  1415. float tempAdj;
  1416. // Manual bubble sort by adjust value
  1417. if (adj[1] < adj[0]) {
  1418. tempLock = lock[0], tempAdj = adj[0];
  1419. lock[0] = lock[1], adj[0] = adj[1];
  1420. lock[1] = tempLock, adj[1] = 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. #if NUM_Z_STEPPER_DRIVERS >= 4
  1428. if (adj[3] < adj[2]) {
  1429. tempLock = lock[2], tempAdj = adj[2];
  1430. lock[2] = lock[3], adj[2] = adj[3];
  1431. lock[3] = tempLock, adj[3] = tempAdj;
  1432. }
  1433. if (adj[2] < adj[1]) {
  1434. tempLock = lock[1], tempAdj = adj[1];
  1435. lock[1] = lock[2], adj[1] = adj[2];
  1436. lock[2] = tempLock, adj[2] = tempAdj;
  1437. }
  1438. #endif
  1439. if (adj[1] < adj[0]) {
  1440. tempLock = lock[0], tempAdj = adj[0];
  1441. lock[0] = lock[1], adj[0] = adj[1];
  1442. lock[1] = tempLock, adj[1] = tempAdj;
  1443. }
  1444. if (pos_dir) {
  1445. // normalize adj to smallest value and do the first move
  1446. (*lock[0])(true);
  1447. do_homing_move(axis, adj[1] - adj[0]);
  1448. // lock the second stepper for the final correction
  1449. (*lock[1])(true);
  1450. do_homing_move(axis, adj[2] - adj[1]);
  1451. #if NUM_Z_STEPPER_DRIVERS >= 4
  1452. // lock the third stepper for the final correction
  1453. (*lock[2])(true);
  1454. do_homing_move(axis, adj[3] - adj[2]);
  1455. #endif
  1456. }
  1457. else {
  1458. #if NUM_Z_STEPPER_DRIVERS >= 4
  1459. (*lock[3])(true);
  1460. do_homing_move(axis, adj[2] - adj[3]);
  1461. #endif
  1462. (*lock[2])(true);
  1463. do_homing_move(axis, adj[1] - adj[2]);
  1464. (*lock[1])(true);
  1465. do_homing_move(axis, adj[0] - adj[1]);
  1466. }
  1467. stepper.set_z1_lock(false);
  1468. stepper.set_z2_lock(false);
  1469. stepper.set_z3_lock(false);
  1470. #if NUM_Z_STEPPER_DRIVERS >= 4
  1471. stepper.set_z4_lock(false);
  1472. #endif
  1473. #endif
  1474. }
  1475. #endif
  1476. // Reset flags for X, Y, Z motor locking
  1477. switch (axis) {
  1478. default: break;
  1479. TERN_(X_DUAL_ENDSTOPS, case X_AXIS:)
  1480. TERN_(Y_DUAL_ENDSTOPS, case Y_AXIS:)
  1481. TERN_(Z_MULTI_ENDSTOPS, case Z_AXIS:)
  1482. stepper.set_separate_multi_axis(false);
  1483. }
  1484. #endif
  1485. #ifdef TMC_HOME_PHASE
  1486. // move back to homing phase if configured and capable
  1487. backout_to_tmc_homing_phase(axis);
  1488. #endif
  1489. #if IS_SCARA
  1490. set_axis_is_at_home(axis);
  1491. sync_plan_position();
  1492. #elif ENABLED(DELTA)
  1493. // Delta has already moved all three towers up in G28
  1494. // so here it re-homes each tower in turn.
  1495. // Delta homing treats the axes as normal linear axes.
  1496. const float adjDistance = delta_endstop_adj[axis],
  1497. minDistance = (MIN_STEPS_PER_SEGMENT) * planner.steps_to_mm[axis];
  1498. // Retrace by the amount specified in delta_endstop_adj if more than min steps.
  1499. if (adjDistance * (Z_HOME_DIR) < 0 && ABS(adjDistance) > minDistance) { // away from endstop, more than min distance
  1500. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("adjDistance:", adjDistance);
  1501. do_homing_move(axis, adjDistance, get_homing_bump_feedrate(axis));
  1502. }
  1503. #else // CARTESIAN / CORE
  1504. set_axis_is_at_home(axis);
  1505. sync_plan_position();
  1506. destination[axis] = current_position[axis];
  1507. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1508. #endif
  1509. // Put away the Z probe
  1510. #if HOMING_Z_WITH_PROBE
  1511. if (axis == Z_AXIS && probe.stow()) return;
  1512. #endif
  1513. #if DISABLED(DELTA) && defined(HOMING_BACKOFF_POST_MM)
  1514. const xyz_float_t endstop_backoff = HOMING_BACKOFF_POST_MM;
  1515. if (endstop_backoff[axis]) {
  1516. current_position[axis] -= ABS(endstop_backoff[axis]) * axis_home_dir;
  1517. line_to_current_position(
  1518. #if HOMING_Z_WITH_PROBE
  1519. (axis == Z_AXIS) ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) :
  1520. #endif
  1521. homing_feedrate(axis)
  1522. );
  1523. #if ENABLED(SENSORLESS_HOMING)
  1524. planner.synchronize();
  1525. if (TERN0(IS_CORE, axis != NORMAL_AXIS))
  1526. safe_delay(200); // Short delay to allow belts to spring back
  1527. #endif
  1528. }
  1529. #endif
  1530. // Clear retracted status if homing the Z axis
  1531. #if ENABLED(FWRETRACT)
  1532. if (axis == Z_AXIS) fwretract.current_hop = 0.0;
  1533. #endif
  1534. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< homeaxis(", axis_codes[axis], ")");
  1535. } // homeaxis()
  1536. #if HAS_WORKSPACE_OFFSET
  1537. void update_workspace_offset(const AxisEnum axis) {
  1538. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1539. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Axis ", XYZ_CHAR(axis), " home_offset = ", home_offset[axis], " position_shift = ", position_shift[axis]);
  1540. }
  1541. #endif
  1542. #if HAS_M206_COMMAND
  1543. /**
  1544. * Change the home offset for an axis.
  1545. * Also refreshes the workspace offset.
  1546. */
  1547. void set_home_offset(const AxisEnum axis, const float v) {
  1548. home_offset[axis] = v;
  1549. update_workspace_offset(axis);
  1550. }
  1551. #endif // HAS_M206_COMMAND