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.

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