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.

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