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

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