My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

motion.cpp 82KB

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