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

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