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

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