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

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