My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

motion.cpp 54KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * motion.cpp
  24. */
  25. #include "motion.h"
  26. #include "endstops.h"
  27. #include "stepper.h"
  28. #include "planner.h"
  29. #include "temperature.h"
  30. #include "../gcode/gcode.h"
  31. #include "../inc/MarlinConfig.h"
  32. #if IS_SCARA
  33. #include "../libs/buzzer.h"
  34. #include "../lcd/ultralcd.h"
  35. #endif
  36. #if HAS_BED_PROBE
  37. #include "probe.h"
  38. #endif
  39. #if HAS_LEVELING
  40. #include "../feature/bedlevel/bedlevel.h"
  41. #endif
  42. #if ENABLED(BLTOUCH)
  43. #include "../feature/bltouch.h"
  44. #endif
  45. #if HAS_DISPLAY
  46. #include "../lcd/ultralcd.h"
  47. #endif
  48. #if ENABLED(SENSORLESS_HOMING)
  49. #include "../feature/tmc_util.h"
  50. #endif
  51. #if ENABLED(FWRETRACT)
  52. #include "../feature/fwretract.h"
  53. #endif
  54. #if ENABLED(BABYSTEP_DISPLAY_TOTAL)
  55. #include "../feature/babystep.h"
  56. #endif
  57. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  58. #include "../core/debug_out.h"
  59. #define XYZ_CONSTS(T, NAME, OPT) const PROGMEM XYZval<T> NAME##_P = { X_##OPT, Y_##OPT, Z_##OPT }
  60. XYZ_CONSTS(float, base_min_pos, MIN_POS);
  61. XYZ_CONSTS(float, base_max_pos, MAX_POS);
  62. XYZ_CONSTS(float, base_home_pos, HOME_POS);
  63. XYZ_CONSTS(float, max_length, MAX_LENGTH);
  64. XYZ_CONSTS(float, home_bump_mm, HOME_BUMP_MM);
  65. XYZ_CONSTS(signed char, home_dir, HOME_DIR);
  66. /**
  67. * axis_homed
  68. * Flags that each linear axis was homed.
  69. * XYZ on cartesian, ABC on delta, ABZ on SCARA.
  70. *
  71. * axis_known_position
  72. * Flags that the position is known in each linear axis. Set when homed.
  73. * Cleared whenever a stepper powers off, potentially losing its position.
  74. */
  75. uint8_t axis_homed, axis_known_position; // = 0
  76. // Relative Mode. Enable with G91, disable with G90.
  77. bool relative_mode; // = false;
  78. /**
  79. * Cartesian Current Position
  80. * Used to track the native machine position as moves are queued.
  81. * Used by 'line_to_current_position' to do a move after changing it.
  82. * Used by 'sync_plan_position' to update 'planner.position'.
  83. */
  84. xyze_pos_t current_position = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
  85. /**
  86. * Cartesian Destination
  87. * The destination for a move, filled in by G-code movement commands,
  88. * and expected by functions like 'prepare_move_to_destination'.
  89. * G-codes can set destination using 'get_destination_from_command'
  90. */
  91. xyze_pos_t destination; // {0}
  92. // The active extruder (tool). Set with T<extruder> command.
  93. #if EXTRUDERS > 1
  94. uint8_t active_extruder; // = 0
  95. #endif
  96. // Extruder offsets
  97. #if HAS_HOTEND_OFFSET
  98. xyz_pos_t hotend_offset[HOTENDS]; // Initialized by settings.load()
  99. void reset_hotend_offsets() {
  100. constexpr float tmp[XYZ][HOTENDS] = { HOTEND_OFFSET_X, HOTEND_OFFSET_Y, HOTEND_OFFSET_Z };
  101. static_assert(
  102. !tmp[X_AXIS][0] && !tmp[Y_AXIS][0] && !tmp[Z_AXIS][0],
  103. "Offsets for the first hotend must be 0.0."
  104. );
  105. // Transpose from [XYZ][HOTENDS] to [HOTENDS][XYZ]
  106. HOTEND_LOOP() LOOP_XYZ(a) hotend_offset[e][a] = tmp[a][e];
  107. #if ENABLED(DUAL_X_CARRIAGE)
  108. hotend_offset[1].x = _MAX(X2_HOME_POS, X2_MAX_POS);
  109. #endif
  110. }
  111. #endif
  112. // The feedrate for the current move, often used as the default if
  113. // no other feedrate is specified. Overridden for special moves.
  114. // Set by the last G0 through G5 command's "F" parameter.
  115. // Functions that override this for custom moves *must always* restore it!
  116. feedRate_t feedrate_mm_s = MMM_TO_MMS(1500);
  117. int16_t feedrate_percentage = 100;
  118. // Homing feedrate is const progmem - compare to constexpr in the header
  119. const feedRate_t homing_feedrate_mm_s[XYZ] PROGMEM = {
  120. #if ENABLED(DELTA)
  121. MMM_TO_MMS(HOMING_FEEDRATE_Z), MMM_TO_MMS(HOMING_FEEDRATE_Z),
  122. #else
  123. MMM_TO_MMS(HOMING_FEEDRATE_XY), MMM_TO_MMS(HOMING_FEEDRATE_XY),
  124. #endif
  125. MMM_TO_MMS(HOMING_FEEDRATE_Z)
  126. };
  127. // Cartesian conversion result goes here:
  128. xyz_pos_t cartes;
  129. #if IS_KINEMATIC
  130. abc_pos_t delta;
  131. #if HAS_SCARA_OFFSET
  132. abc_pos_t scara_home_offset;
  133. #endif
  134. #if HAS_SOFTWARE_ENDSTOPS
  135. float delta_max_radius, delta_max_radius_2;
  136. #elif IS_SCARA
  137. constexpr float delta_max_radius = SCARA_PRINTABLE_RADIUS,
  138. delta_max_radius_2 = sq(SCARA_PRINTABLE_RADIUS);
  139. #else // DELTA
  140. constexpr float delta_max_radius = DELTA_PRINTABLE_RADIUS,
  141. delta_max_radius_2 = sq(DELTA_PRINTABLE_RADIUS);
  142. #endif
  143. #endif
  144. /**
  145. * The workspace can be offset by some commands, or
  146. * these offsets may be omitted to save on computation.
  147. */
  148. #if HAS_POSITION_SHIFT
  149. // The distance that XYZ has been offset by G92. Reset by G28.
  150. xyz_pos_t position_shift{0};
  151. #endif
  152. #if HAS_HOME_OFFSET
  153. // This offset is added to the configured home position.
  154. // Set by M206, M428, or menu item. Saved to EEPROM.
  155. xyz_pos_t home_offset{0};
  156. #endif
  157. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  158. // The above two are combined to save on computes
  159. xyz_pos_t workspace_offset{0};
  160. #endif
  161. #if HAS_ABL_NOT_UBL
  162. float xy_probe_feedrate_mm_s = MMM_TO_MMS(XY_PROBE_SPEED);
  163. #endif
  164. /**
  165. * Output the current position to serial
  166. */
  167. void report_current_position() {
  168. const xyz_pos_t lpos = current_position.asLogical();
  169. SERIAL_ECHOPAIR("X:", lpos.x, " Y:", lpos.y, " Z:", lpos.z, " E:", current_position.e);
  170. stepper.report_positions();
  171. #if IS_SCARA
  172. scara_report_positions();
  173. #endif
  174. }
  175. /**
  176. * sync_plan_position
  177. *
  178. * Set the planner/stepper positions directly from current_position with
  179. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  180. */
  181. void sync_plan_position() {
  182. if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position", current_position);
  183. planner.set_position_mm(current_position);
  184. }
  185. void sync_plan_position_e() { planner.set_e_position_mm(current_position.e); }
  186. /**
  187. * Get the stepper positions in the cartes[] array.
  188. * Forward kinematics are applied for DELTA and SCARA.
  189. *
  190. * The result is in the current coordinate space with
  191. * leveling applied. The coordinates need to be run through
  192. * unapply_leveling to obtain the "ideal" coordinates
  193. * suitable for current_position, etc.
  194. */
  195. void get_cartesian_from_steppers() {
  196. #if ENABLED(DELTA)
  197. forward_kinematics_DELTA(
  198. planner.get_axis_position_mm(A_AXIS),
  199. planner.get_axis_position_mm(B_AXIS),
  200. planner.get_axis_position_mm(C_AXIS)
  201. );
  202. #else
  203. #if IS_SCARA
  204. forward_kinematics_SCARA(
  205. planner.get_axis_position_degrees(A_AXIS),
  206. planner.get_axis_position_degrees(B_AXIS)
  207. );
  208. #else
  209. cartes.set(planner.get_axis_position_mm(X_AXIS), planner.get_axis_position_mm(Y_AXIS));
  210. #endif
  211. cartes.z = planner.get_axis_position_mm(Z_AXIS);
  212. #endif
  213. }
  214. /**
  215. * Set the current_position for an axis based on
  216. * the stepper positions, removing any leveling that
  217. * may have been applied.
  218. *
  219. * To prevent small shifts in axis position always call
  220. * sync_plan_position after updating axes with this.
  221. *
  222. * To keep hosts in sync, always call report_current_position
  223. * after updating the current_position.
  224. */
  225. void set_current_from_steppers_for_axis(const AxisEnum axis) {
  226. get_cartesian_from_steppers();
  227. #if HAS_POSITION_MODIFIERS
  228. xyze_pos_t pos = { cartes.x, cartes.y, cartes.z, current_position.e };
  229. planner.unapply_modifiers(pos
  230. #if HAS_LEVELING
  231. , true
  232. #endif
  233. );
  234. xyze_pos_t &cartes = pos;
  235. #endif
  236. if (axis == ALL_AXES)
  237. current_position = cartes;
  238. else
  239. current_position[axis] = cartes[axis];
  240. }
  241. /**
  242. * Move the planner to the current position from wherever it last moved
  243. * (or from wherever it has been told it is located).
  244. */
  245. void line_to_current_position(const feedRate_t &fr_mm_s/*=feedrate_mm_s*/) {
  246. planner.buffer_line(current_position, fr_mm_s, active_extruder);
  247. }
  248. #if IS_KINEMATIC
  249. /**
  250. * Buffer a fast move without interpolation. Set current_position to destination
  251. */
  252. void prepare_fast_move_to_destination(const feedRate_t &scaled_fr_mm_s/*=MMS_SCALED(feedrate_mm_s)*/) {
  253. if (DEBUGGING(LEVELING)) DEBUG_POS("prepare_fast_move_to_destination", destination);
  254. #if UBL_SEGMENTED
  255. // UBL segmented line will do Z-only moves in single segment
  256. ubl.line_to_destination_segmented(scaled_fr_mm_s);
  257. #else
  258. if (current_position == destination) return;
  259. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  260. #endif
  261. current_position = destination;
  262. }
  263. #endif // IS_KINEMATIC
  264. void _internal_move_to_destination(const feedRate_t &fr_mm_s/*=0.0f*/
  265. #if IS_KINEMATIC
  266. , const bool is_fast/*=false*/
  267. #endif
  268. ) {
  269. const feedRate_t old_feedrate = feedrate_mm_s;
  270. if (fr_mm_s) feedrate_mm_s = fr_mm_s;
  271. const uint16_t old_pct = feedrate_percentage;
  272. feedrate_percentage = 100;
  273. #if EXTRUDERS
  274. const float old_fac = planner.e_factor[active_extruder];
  275. planner.e_factor[active_extruder] = 1.0f;
  276. #endif
  277. #if IS_KINEMATIC
  278. if (is_fast)
  279. prepare_fast_move_to_destination();
  280. else
  281. #endif
  282. prepare_move_to_destination();
  283. feedrate_mm_s = old_feedrate;
  284. feedrate_percentage = old_pct;
  285. #if EXTRUDERS
  286. planner.e_factor[active_extruder] = old_fac;
  287. #endif
  288. }
  289. /**
  290. * Plan a move to (X, Y, Z) and set the current_position
  291. */
  292. void do_blocking_move_to(const float rx, const float ry, const float rz, const feedRate_t &fr_mm_s/*=0.0*/) {
  293. if (DEBUGGING(LEVELING)) DEBUG_XYZ(">>> do_blocking_move_to", rx, ry, rz);
  294. const feedRate_t z_feedrate = fr_mm_s ?: homing_feedrate(Z_AXIS),
  295. xy_feedrate = fr_mm_s ?: feedRate_t(XY_PROBE_FEEDRATE_MM_S);
  296. #if ENABLED(DELTA)
  297. if (!position_is_reachable(rx, ry)) return;
  298. REMEMBER(fr, feedrate_mm_s, xy_feedrate);
  299. destination = current_position; // sync destination at the start
  300. if (DEBUGGING(LEVELING)) DEBUG_POS("destination = current_position", destination);
  301. // when in the danger zone
  302. if (current_position.z > delta_clip_start_height) {
  303. if (rz > delta_clip_start_height) { // staying in the danger zone
  304. destination.set(rx, ry, rz); // move directly (uninterpolated)
  305. prepare_internal_fast_move_to_destination(); // set current_position from destination
  306. if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position);
  307. return;
  308. }
  309. destination.z = delta_clip_start_height;
  310. prepare_internal_fast_move_to_destination(); // set current_position from destination
  311. if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position);
  312. }
  313. if (rz > current_position.z) { // raising?
  314. destination.z = rz;
  315. prepare_internal_fast_move_to_destination(z_feedrate); // set current_position from destination
  316. if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position);
  317. }
  318. destination.set(rx, ry);
  319. prepare_internal_move_to_destination(); // set current_position from destination
  320. if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position);
  321. if (rz < current_position.z) { // lowering?
  322. destination.z = rz;
  323. prepare_internal_fast_move_to_destination(z_feedrate); // set current_position from destination
  324. if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position);
  325. }
  326. #elif IS_SCARA
  327. if (!position_is_reachable(rx, ry)) return;
  328. destination = current_position;
  329. // If Z needs to raise, do it before moving XY
  330. if (destination.z < rz) {
  331. destination.z = rz;
  332. prepare_internal_fast_move_to_destination(z_feedrate);
  333. }
  334. destination.set(rx, ry);
  335. prepare_internal_fast_move_to_destination(xy_feedrate);
  336. // If Z needs to lower, do it after moving XY
  337. if (destination.z > rz) {
  338. destination.z = rz;
  339. prepare_internal_fast_move_to_destination(z_feedrate);
  340. }
  341. #else
  342. // If Z needs to raise, do it before moving XY
  343. if (current_position.z < rz) {
  344. current_position.z = rz;
  345. line_to_current_position(z_feedrate);
  346. }
  347. current_position.set(rx, ry);
  348. line_to_current_position(xy_feedrate);
  349. // If Z needs to lower, do it after moving XY
  350. if (current_position.z > rz) {
  351. current_position.z = rz;
  352. line_to_current_position(z_feedrate);
  353. }
  354. #endif
  355. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< do_blocking_move_to");
  356. planner.synchronize();
  357. }
  358. void do_blocking_move_to_x(const float &rx, const feedRate_t &fr_mm_s/*=0.0*/) {
  359. do_blocking_move_to(rx, current_position.y, current_position.z, fr_mm_s);
  360. }
  361. void do_blocking_move_to_y(const float &ry, const feedRate_t &fr_mm_s/*=0.0*/) {
  362. do_blocking_move_to(current_position.x, ry, current_position.z, fr_mm_s);
  363. }
  364. void do_blocking_move_to_z(const float &rz, const feedRate_t &fr_mm_s/*=0.0*/) {
  365. do_blocking_move_to(current_position.x, current_position.y, rz, fr_mm_s);
  366. }
  367. void do_blocking_move_to_xy(const float &rx, const float &ry, const feedRate_t &fr_mm_s/*=0.0*/) {
  368. do_blocking_move_to(rx, ry, current_position.z, fr_mm_s);
  369. }
  370. //
  371. // Prepare to do endstop or probe moves with custom feedrates.
  372. // - Save / restore current feedrate and multiplier
  373. //
  374. static float saved_feedrate_mm_s;
  375. static int16_t saved_feedrate_percentage;
  376. void remember_feedrate_and_scaling() {
  377. saved_feedrate_mm_s = feedrate_mm_s;
  378. saved_feedrate_percentage = feedrate_percentage;
  379. }
  380. void remember_feedrate_scaling_off() {
  381. remember_feedrate_and_scaling();
  382. feedrate_percentage = 100;
  383. }
  384. void restore_feedrate_and_scaling() {
  385. feedrate_mm_s = saved_feedrate_mm_s;
  386. feedrate_percentage = saved_feedrate_percentage;
  387. }
  388. #if HAS_SOFTWARE_ENDSTOPS
  389. bool soft_endstops_enabled = true;
  390. // Software Endstops are based on the configured limits.
  391. axis_limits_t soft_endstop = {
  392. { X_MIN_POS, Y_MIN_POS, Z_MIN_POS },
  393. { X_MAX_POS, Y_MAX_POS, Z_MAX_POS }
  394. };
  395. /**
  396. * Software endstops can be used to monitor the open end of
  397. * an axis that has a hardware endstop on the other end. Or
  398. * they can prevent axes from moving past endstops and grinding.
  399. *
  400. * To keep doing their job as the coordinate system changes,
  401. * the software endstop positions must be refreshed to remain
  402. * at the same positions relative to the machine.
  403. */
  404. void update_software_endstops(const AxisEnum axis
  405. #if HAS_HOTEND_OFFSET
  406. , const uint8_t old_tool_index/*=0*/, const uint8_t new_tool_index/*=0*/
  407. #endif
  408. ) {
  409. #if ENABLED(DUAL_X_CARRIAGE)
  410. if (axis == X_AXIS) {
  411. // In Dual X mode hotend_offset[X] is T1's home position
  412. const float dual_max_x = _MAX(hotend_offset[1].x, X2_MAX_POS);
  413. if (new_tool_index != 0) {
  414. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  415. soft_endstop.min.x = X2_MIN_POS;
  416. soft_endstop.max.x = dual_max_x;
  417. }
  418. else if (dxc_is_duplicating()) {
  419. // In Duplication Mode, T0 can move as far left as X1_MIN_POS
  420. // but not so far to the right that T1 would move past the end
  421. soft_endstop.min.x = X1_MIN_POS;
  422. soft_endstop.max.x = _MIN(X1_MAX_POS, dual_max_x - duplicate_extruder_x_offset);
  423. }
  424. else {
  425. // In other modes, T0 can move from X1_MIN_POS to X1_MAX_POS
  426. soft_endstop.min.x = X1_MIN_POS;
  427. soft_endstop.max.x = X1_MAX_POS;
  428. }
  429. }
  430. #elif ENABLED(DELTA)
  431. soft_endstop.min[axis] = base_min_pos(axis);
  432. soft_endstop.max[axis] = (axis == Z_AXIS ? delta_height
  433. #if HAS_BED_PROBE
  434. - probe_offset.z
  435. #endif
  436. : base_max_pos(axis));
  437. switch (axis) {
  438. case X_AXIS:
  439. case Y_AXIS:
  440. // Get a minimum radius for clamping
  441. delta_max_radius = _MIN(ABS(_MAX(soft_endstop.min.x, soft_endstop.min.y)), soft_endstop.max.x, soft_endstop.max.y);
  442. delta_max_radius_2 = sq(delta_max_radius);
  443. break;
  444. case Z_AXIS:
  445. delta_clip_start_height = soft_endstop.max[axis] - delta_safe_distance_from_top();
  446. default: break;
  447. }
  448. #elif HAS_HOTEND_OFFSET
  449. // Software endstops are relative to the tool 0 workspace, so
  450. // the movement limits must be shifted by the tool offset to
  451. // retain the same physical limit when other tools are selected.
  452. if (old_tool_index != new_tool_index) {
  453. const float offs = hotend_offset[new_tool_index][axis] - hotend_offset[old_tool_index][axis];
  454. soft_endstop.min[axis] += offs;
  455. soft_endstop.max[axis] += offs;
  456. }
  457. else {
  458. const float offs = hotend_offset[active_extruder][axis];
  459. soft_endstop.min[axis] = base_min_pos(axis) + offs;
  460. soft_endstop.max[axis] = base_max_pos(axis) + offs;
  461. }
  462. #else
  463. soft_endstop.min[axis] = base_min_pos(axis);
  464. soft_endstop.max[axis] = base_max_pos(axis);
  465. #endif
  466. if (DEBUGGING(LEVELING))
  467. SERIAL_ECHOLNPAIR("Axis ", axis_codes[axis], " min:", soft_endstop.min[axis], " max:", soft_endstop.max[axis]);
  468. }
  469. /**
  470. * Constrain the given coordinates to the software endstops.
  471. *
  472. * For DELTA/SCARA the XY constraint is based on the smallest
  473. * radius within the set software endstops.
  474. */
  475. void apply_motion_limits(xyz_pos_t &target) {
  476. if (!soft_endstops_enabled || !all_axes_homed()) return;
  477. #if IS_KINEMATIC
  478. #if HAS_HOTEND_OFFSET && ENABLED(DELTA)
  479. // The effector center position will be the target minus the hotend offset.
  480. const xy_pos_t offs = hotend_offset[active_extruder];
  481. #else
  482. // SCARA needs to consider the angle of the arm through the entire move, so for now use no tool offset.
  483. constexpr xy_pos_t offs{0};
  484. #endif
  485. const float dist_2 = HYPOT2(target.x - offs.x, target.y - offs.y);
  486. if (dist_2 > delta_max_radius_2)
  487. target *= delta_max_radius / SQRT(dist_2); // 200 / 300 = 0.66
  488. #else
  489. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  490. NOLESS(target.x, soft_endstop.min.x);
  491. #endif
  492. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  493. NOMORE(target.x, soft_endstop.max.x);
  494. #endif
  495. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  496. NOLESS(target.y, soft_endstop.min.y);
  497. #endif
  498. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  499. NOMORE(target.y, soft_endstop.max.y);
  500. #endif
  501. #endif
  502. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  503. NOLESS(target.z, soft_endstop.min.z);
  504. #endif
  505. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  506. NOMORE(target.z, soft_endstop.max.z);
  507. #endif
  508. }
  509. #endif // HAS_SOFTWARE_ENDSTOPS
  510. #if !UBL_SEGMENTED
  511. #if IS_KINEMATIC
  512. #if IS_SCARA
  513. /**
  514. * Before raising this value, use M665 S[seg_per_sec] to decrease
  515. * the number of segments-per-second. Default is 200. Some deltas
  516. * do better with 160 or lower. It would be good to know how many
  517. * segments-per-second are actually possible for SCARA on AVR.
  518. *
  519. * Longer segments result in less kinematic overhead
  520. * but may produce jagged lines. Try 0.5mm, 1.0mm, and 2.0mm
  521. * and compare the difference.
  522. */
  523. #define SCARA_MIN_SEGMENT_LENGTH 0.5f
  524. #endif
  525. /**
  526. * Prepare a linear move in a DELTA or SCARA setup.
  527. *
  528. * Called from prepare_move_to_destination as the
  529. * default Delta/SCARA segmenter.
  530. *
  531. * This calls planner.buffer_line several times, adding
  532. * small incremental moves for DELTA or SCARA.
  533. *
  534. * For Unified Bed Leveling (Delta or Segmented Cartesian)
  535. * the ubl.line_to_destination_segmented method replaces this.
  536. *
  537. * For Auto Bed Leveling (Bilinear) with SEGMENT_LEVELED_MOVES
  538. * this is replaced by segmented_line_to_destination below.
  539. */
  540. inline bool line_to_destination_kinematic() {
  541. // Get the top feedrate of the move in the XY plane
  542. const float scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
  543. const xyze_float_t diff = destination - current_position;
  544. // If the move is only in Z/E don't split up the move
  545. if (!diff.x && !diff.y) {
  546. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  547. return false; // caller will update current_position
  548. }
  549. // Fail if attempting move outside printable radius
  550. if (!position_is_reachable(destination)) return true;
  551. // Get the linear distance in XYZ
  552. float cartesian_mm = diff.magnitude();
  553. // If the move is very short, check the E move distance
  554. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(diff.e);
  555. // No E move either? Game over.
  556. if (UNEAR_ZERO(cartesian_mm)) return true;
  557. // Minimum number of seconds to move the given distance
  558. const float seconds = cartesian_mm / scaled_fr_mm_s;
  559. // The number of segments-per-second times the duration
  560. // gives the number of segments
  561. uint16_t segments = delta_segments_per_second * seconds;
  562. // For SCARA enforce a minimum segment size
  563. #if IS_SCARA
  564. NOMORE(segments, cartesian_mm * RECIPROCAL(SCARA_MIN_SEGMENT_LENGTH));
  565. #endif
  566. // At least one segment is required
  567. NOLESS(segments, 1U);
  568. // The approximate length of each segment
  569. const float inv_segments = 1.0f / float(segments),
  570. cartesian_segment_mm = cartesian_mm * inv_segments;
  571. const xyze_float_t segment_distance = diff * inv_segments;
  572. #if ENABLED(SCARA_FEEDRATE_SCALING)
  573. const float inv_duration = scaled_fr_mm_s / cartesian_segment_mm;
  574. #endif
  575. /*
  576. SERIAL_ECHOPAIR("mm=", cartesian_mm);
  577. SERIAL_ECHOPAIR(" seconds=", seconds);
  578. SERIAL_ECHOPAIR(" segments=", segments);
  579. SERIAL_ECHOPAIR(" segment_mm=", cartesian_segment_mm);
  580. SERIAL_EOL();
  581. //*/
  582. // Get the current position as starting point
  583. xyze_pos_t raw = current_position;
  584. // Calculate and execute the segments
  585. while (--segments) {
  586. static millis_t next_idle_ms = millis() + 200UL;
  587. thermalManager.manage_heater(); // This returns immediately if not really needed.
  588. if (ELAPSED(millis(), next_idle_ms)) {
  589. next_idle_ms = millis() + 200UL;
  590. idle();
  591. }
  592. raw += segment_distance;
  593. if (!planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, cartesian_segment_mm
  594. #if ENABLED(SCARA_FEEDRATE_SCALING)
  595. , inv_duration
  596. #endif
  597. ))
  598. break;
  599. }
  600. // Ensure last segment arrives at target location.
  601. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder, cartesian_segment_mm
  602. #if ENABLED(SCARA_FEEDRATE_SCALING)
  603. , inv_duration
  604. #endif
  605. );
  606. return false; // caller will update current_position
  607. }
  608. #else // !IS_KINEMATIC
  609. #if ENABLED(SEGMENT_LEVELED_MOVES)
  610. /**
  611. * Prepare a segmented move on a CARTESIAN setup.
  612. *
  613. * This calls planner.buffer_line several times, adding
  614. * small incremental moves. This allows the planner to
  615. * apply more detailed bed leveling to the full move.
  616. */
  617. inline void segmented_line_to_destination(const feedRate_t &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
  618. const xyze_float_t diff = destination - current_position;
  619. // If the move is only in Z/E don't split up the move
  620. if (!diff.x && !diff.y) {
  621. planner.buffer_line(destination, fr_mm_s, active_extruder);
  622. return;
  623. }
  624. // Get the linear distance in XYZ
  625. // If the move is very short, check the E move distance
  626. // No E move either? Game over.
  627. float cartesian_mm = diff.magnitude();
  628. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(diff.e);
  629. if (UNEAR_ZERO(cartesian_mm)) return;
  630. // The length divided by the segment size
  631. // At least one segment is required
  632. uint16_t segments = cartesian_mm / segment_size;
  633. NOLESS(segments, 1U);
  634. // The approximate length of each segment
  635. const float inv_segments = 1.0f / float(segments),
  636. cartesian_segment_mm = cartesian_mm * inv_segments;
  637. const xyze_float_t segment_distance = diff * inv_segments;
  638. #if ENABLED(SCARA_FEEDRATE_SCALING)
  639. const float inv_duration = scaled_fr_mm_s / cartesian_segment_mm;
  640. #endif
  641. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  642. // SERIAL_ECHOLNPAIR(" segments=", segments);
  643. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  644. // Get the raw current position as starting point
  645. xyze_pos_t raw = current_position;
  646. // Calculate and execute the segments
  647. while (--segments) {
  648. static millis_t next_idle_ms = millis() + 200UL;
  649. thermalManager.manage_heater(); // This returns immediately if not really needed.
  650. if (ELAPSED(millis(), next_idle_ms)) {
  651. next_idle_ms = millis() + 200UL;
  652. idle();
  653. }
  654. raw += segment_distance;
  655. if (!planner.buffer_line(raw, fr_mm_s, active_extruder, cartesian_segment_mm
  656. #if ENABLED(SCARA_FEEDRATE_SCALING)
  657. , inv_duration
  658. #endif
  659. ))
  660. break;
  661. }
  662. // Since segment_distance is only approximate,
  663. // the final move must be to the exact destination.
  664. planner.buffer_line(destination, fr_mm_s, active_extruder, cartesian_segment_mm
  665. #if ENABLED(SCARA_FEEDRATE_SCALING)
  666. , inv_duration
  667. #endif
  668. );
  669. }
  670. #endif // SEGMENT_LEVELED_MOVES
  671. /**
  672. * Prepare a linear move in a Cartesian setup.
  673. *
  674. * When a mesh-based leveling system is active, moves are segmented
  675. * according to the configuration of the leveling system.
  676. *
  677. * Return true if 'current_position' was set to 'destination'
  678. */
  679. inline bool prepare_move_to_destination_cartesian() {
  680. const float scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
  681. #if HAS_MESH
  682. if (planner.leveling_active && planner.leveling_active_at_z(destination.z)) {
  683. #if ENABLED(AUTO_BED_LEVELING_UBL)
  684. ubl.line_to_destination_cartesian(scaled_fr_mm_s, active_extruder); // UBL's motion routine needs to know about
  685. return true; // all moves, including Z-only moves.
  686. #elif ENABLED(SEGMENT_LEVELED_MOVES)
  687. segmented_line_to_destination(scaled_fr_mm_s);
  688. return false; // caller will update current_position
  689. #else
  690. /**
  691. * For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
  692. * Otherwise fall through to do a direct single move.
  693. */
  694. if (xy_pos_t(current_position) != xy_pos_t(destination)) {
  695. #if ENABLED(MESH_BED_LEVELING)
  696. mbl.line_to_destination(scaled_fr_mm_s);
  697. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  698. bilinear_line_to_destination(scaled_fr_mm_s);
  699. #endif
  700. return true;
  701. }
  702. #endif
  703. }
  704. #endif // HAS_MESH
  705. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  706. return false; // caller will update current_position
  707. }
  708. #endif // !IS_KINEMATIC
  709. #endif // !UBL_SEGMENTED
  710. #if HAS_DUPLICATION_MODE
  711. bool extruder_duplication_enabled,
  712. mirrored_duplication_mode;
  713. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  714. uint8_t duplication_e_mask; // = 0
  715. #endif
  716. #endif
  717. #if ENABLED(DUAL_X_CARRIAGE)
  718. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  719. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  720. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  721. xyz_pos_t raised_parked_position; // used in mode 1
  722. bool active_extruder_parked = false; // used in mode 1 & 2
  723. millis_t delayed_move_time = 0; // used in mode 1
  724. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  725. float x_home_pos(const int extruder) {
  726. if (extruder == 0)
  727. return base_home_pos(X_AXIS);
  728. else
  729. /**
  730. * In dual carriage mode the extruder offset provides an override of the
  731. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  732. * This allows soft recalibration of the second extruder home position
  733. * without firmware reflash (through the M218 command).
  734. */
  735. return hotend_offset[1].x > 0 ? hotend_offset[1].x : X2_HOME_POS;
  736. }
  737. /**
  738. * Prepare a linear move in a dual X axis setup
  739. *
  740. * Return true if current_position[] was set to destination[]
  741. */
  742. inline bool dual_x_carriage_unpark() {
  743. if (active_extruder_parked) {
  744. switch (dual_x_carriage_mode) {
  745. case DXC_FULL_CONTROL_MODE:
  746. break;
  747. case DXC_AUTO_PARK_MODE:
  748. if (current_position.e == destination.e) {
  749. // This is a travel move (with no extrusion)
  750. // Skip it, but keep track of the current position
  751. // (so it can be used as the start of the next non-travel move)
  752. if (delayed_move_time != 0xFFFFFFFFUL) {
  753. current_position = destination;
  754. NOLESS(raised_parked_position.z, destination.z);
  755. delayed_move_time = millis();
  756. return true;
  757. }
  758. }
  759. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  760. #define CUR_X current_position.x
  761. #define CUR_Y current_position.y
  762. #define CUR_Z current_position.z
  763. #define CUR_E current_position.e
  764. #define RAISED_X raised_parked_position.x
  765. #define RAISED_Y raised_parked_position.y
  766. #define RAISED_Z raised_parked_position.z
  767. if ( planner.buffer_line(RAISED_X, RAISED_Y, RAISED_Z, CUR_E, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder))
  768. if (planner.buffer_line( CUR_X, CUR_Y, RAISED_Z, CUR_E, PLANNER_XY_FEEDRATE(), active_extruder))
  769. line_to_current_position(planner.settings.max_feedrate_mm_s[Z_AXIS]);
  770. delayed_move_time = 0;
  771. active_extruder_parked = false;
  772. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Clear active_extruder_parked");
  773. break;
  774. case DXC_MIRRORED_MODE:
  775. case DXC_DUPLICATION_MODE:
  776. if (active_extruder == 0) {
  777. xyze_pos_t new_pos = current_position;
  778. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  779. new_pos.x += duplicate_extruder_x_offset;
  780. else
  781. new_pos.x = inactive_extruder_x_pos;
  782. // move duplicate extruder into correct duplication position.
  783. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Set planner X", inactive_extruder_x_pos, " ... Line to X", new_pos.x);
  784. planner.set_position_mm(inactive_extruder_x_pos, current_position.y, current_position.z, current_position.e);
  785. if (!planner.buffer_line(new_pos, planner.settings.max_feedrate_mm_s[X_AXIS], 1)) break;
  786. planner.synchronize();
  787. sync_plan_position();
  788. extruder_duplication_enabled = true;
  789. active_extruder_parked = false;
  790. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  791. }
  792. else if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Active extruder not 0");
  793. break;
  794. }
  795. }
  796. stepper.set_directions();
  797. return false;
  798. }
  799. #endif // DUAL_X_CARRIAGE
  800. /**
  801. * Prepare a single move and get ready for the next one
  802. *
  803. * This may result in several calls to planner.buffer_line to
  804. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  805. *
  806. * Make sure current_position.e and destination.e are good
  807. * before calling or cold/lengthy extrusion may get missed.
  808. *
  809. * Before exit, current_position is set to destination.
  810. */
  811. void prepare_move_to_destination() {
  812. apply_motion_limits(destination);
  813. #if EITHER(PREVENT_COLD_EXTRUSION, PREVENT_LENGTHY_EXTRUDE)
  814. if (!DEBUGGING(DRYRUN)) {
  815. if (destination.e != current_position.e) {
  816. #if ENABLED(PREVENT_COLD_EXTRUSION)
  817. if (thermalManager.tooColdToExtrude(active_extruder)) {
  818. current_position.e = destination.e; // Behave as if the move really took place, but ignore E part
  819. SERIAL_ECHO_MSG(MSG_ERR_COLD_EXTRUDE_STOP);
  820. }
  821. #endif // PREVENT_COLD_EXTRUSION
  822. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  823. const float e_delta = ABS(destination.e - current_position.e) * planner.e_factor[active_extruder];
  824. if (e_delta > (EXTRUDE_MAXLENGTH)) {
  825. #if ENABLED(MIXING_EXTRUDER)
  826. bool ignore_e = false;
  827. float collector[MIXING_STEPPERS];
  828. mixer.refresh_collector(1.0, mixer.get_current_vtool(), collector);
  829. MIXER_STEPPER_LOOP(e)
  830. if (e_delta * collector[e] > (EXTRUDE_MAXLENGTH)) { ignore_e = true; break; }
  831. #else
  832. constexpr bool ignore_e = true;
  833. #endif
  834. if (ignore_e) {
  835. current_position.e = destination.e; // Behave as if the move really took place, but ignore E part
  836. SERIAL_ECHO_MSG(MSG_ERR_LONG_EXTRUDE_STOP);
  837. }
  838. }
  839. #endif // PREVENT_LENGTHY_EXTRUDE
  840. }
  841. }
  842. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  843. #if ENABLED(DUAL_X_CARRIAGE)
  844. if (dual_x_carriage_unpark()) return;
  845. #endif
  846. if (
  847. #if UBL_SEGMENTED
  848. #if IS_KINEMATIC // UBL using Kinematic / Cartesian cases as a workaround for now.
  849. ubl.line_to_destination_segmented(MMS_SCALED(feedrate_mm_s))
  850. #else
  851. prepare_move_to_destination_cartesian()
  852. #endif
  853. #elif IS_KINEMATIC
  854. line_to_destination_kinematic()
  855. #else
  856. prepare_move_to_destination_cartesian()
  857. #endif
  858. ) return;
  859. current_position = destination;
  860. }
  861. uint8_t axes_need_homing(uint8_t axis_bits/*=0x07*/) {
  862. #if ENABLED(HOME_AFTER_DEACTIVATE)
  863. #define HOMED_FLAGS axis_known_position
  864. #else
  865. #define HOMED_FLAGS axis_homed
  866. #endif
  867. // Clear test bits that are homed
  868. if (TEST(axis_bits, X_AXIS) && TEST(HOMED_FLAGS, X_AXIS)) CBI(axis_bits, X_AXIS);
  869. if (TEST(axis_bits, Y_AXIS) && TEST(HOMED_FLAGS, Y_AXIS)) CBI(axis_bits, Y_AXIS);
  870. if (TEST(axis_bits, Z_AXIS) && TEST(HOMED_FLAGS, Z_AXIS)) CBI(axis_bits, Z_AXIS);
  871. return axis_bits;
  872. }
  873. bool axis_unhomed_error(uint8_t axis_bits/*=0x07*/) {
  874. if ((axis_bits = axes_need_homing(axis_bits))) {
  875. static const char home_first[] PROGMEM = MSG_HOME_FIRST;
  876. char msg[sizeof(home_first)];
  877. sprintf_P(msg, home_first,
  878. TEST(axis_bits, X_AXIS) ? "X" : "",
  879. TEST(axis_bits, Y_AXIS) ? "Y" : "",
  880. TEST(axis_bits, Z_AXIS) ? "Z" : ""
  881. );
  882. SERIAL_ECHO_START();
  883. SERIAL_ECHOLN(msg);
  884. #if HAS_DISPLAY
  885. ui.set_status(msg);
  886. #endif
  887. return true;
  888. }
  889. return false;
  890. }
  891. /**
  892. * Homing bump feedrate (mm/s)
  893. */
  894. feedRate_t get_homing_bump_feedrate(const AxisEnum axis) {
  895. #if HOMING_Z_WITH_PROBE
  896. if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW);
  897. #endif
  898. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  899. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  900. if (hbd < 1) {
  901. hbd = 10;
  902. SERIAL_ECHO_MSG("Warning: Homing Bump Divisor < 1");
  903. }
  904. return homing_feedrate(axis) / float(hbd);
  905. }
  906. #if ENABLED(SENSORLESS_HOMING)
  907. /**
  908. * Set sensorless homing if the axis has it, accounting for Core Kinematics.
  909. */
  910. sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis) {
  911. sensorless_t stealth_states { false };
  912. switch (axis) {
  913. default: break;
  914. #if X_SENSORLESS
  915. case X_AXIS:
  916. stealth_states.x = tmc_enable_stallguard(stepperX);
  917. #if AXIS_HAS_STALLGUARD(X2)
  918. stealth_states.x2 = tmc_enable_stallguard(stepperX2);
  919. #endif
  920. #if CORE_IS_XY && Y_SENSORLESS
  921. stealth_states.y = tmc_enable_stallguard(stepperY);
  922. #elif CORE_IS_XZ && Z_SENSORLESS
  923. stealth_states.z = tmc_enable_stallguard(stepperZ);
  924. #endif
  925. break;
  926. #endif
  927. #if Y_SENSORLESS
  928. case Y_AXIS:
  929. stealth_states.y = tmc_enable_stallguard(stepperY);
  930. #if AXIS_HAS_STALLGUARD(Y2)
  931. stealth_states.y2 = tmc_enable_stallguard(stepperY2);
  932. #endif
  933. #if CORE_IS_XY && X_SENSORLESS
  934. stealth_states.x = tmc_enable_stallguard(stepperX);
  935. #elif CORE_IS_YZ && Z_SENSORLESS
  936. stealth_states.z = tmc_enable_stallguard(stepperZ);
  937. #endif
  938. break;
  939. #endif
  940. #if Z_SENSORLESS
  941. case Z_AXIS:
  942. stealth_states.z = tmc_enable_stallguard(stepperZ);
  943. #if AXIS_HAS_STALLGUARD(Z2)
  944. stealth_states.z2 = tmc_enable_stallguard(stepperZ2);
  945. #endif
  946. #if AXIS_HAS_STALLGUARD(Z3)
  947. stealth_states.z3 = tmc_enable_stallguard(stepperZ3);
  948. #endif
  949. #if CORE_IS_XZ && X_SENSORLESS
  950. stealth_states.x = tmc_enable_stallguard(stepperX);
  951. #elif CORE_IS_YZ && Y_SENSORLESS
  952. stealth_states.y = tmc_enable_stallguard(stepperY);
  953. #endif
  954. break;
  955. #endif
  956. }
  957. #if ENABLED(SPI_ENDSTOPS)
  958. switch (axis) {
  959. #if X_SPI_SENSORLESS
  960. case X_AXIS: endstops.tmc_spi_homing.x = true; break;
  961. #endif
  962. #if Y_SPI_SENSORLESS
  963. case Y_AXIS: endstops.tmc_spi_homing.y = true; break;
  964. #endif
  965. #if Z_SPI_SENSORLESS
  966. case Z_AXIS: endstops.tmc_spi_homing.z = true; break;
  967. #endif
  968. default: break;
  969. }
  970. #endif
  971. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  972. sg_guard_period = millis() + default_sg_guard_duration;
  973. #endif
  974. return stealth_states;
  975. }
  976. void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) {
  977. switch (axis) {
  978. default: break;
  979. #if X_SENSORLESS
  980. case X_AXIS:
  981. tmc_disable_stallguard(stepperX, enable_stealth.x);
  982. #if AXIS_HAS_STALLGUARD(X2)
  983. tmc_disable_stallguard(stepperX2, enable_stealth.x2);
  984. #endif
  985. #if CORE_IS_XY && Y_SENSORLESS
  986. tmc_disable_stallguard(stepperY, enable_stealth.y);
  987. #elif CORE_IS_XZ && Z_SENSORLESS
  988. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  989. #endif
  990. break;
  991. #endif
  992. #if Y_SENSORLESS
  993. case Y_AXIS:
  994. tmc_disable_stallguard(stepperY, enable_stealth.y);
  995. #if AXIS_HAS_STALLGUARD(Y2)
  996. tmc_disable_stallguard(stepperY2, enable_stealth.y2);
  997. #endif
  998. #if CORE_IS_XY && X_SENSORLESS
  999. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1000. #elif CORE_IS_YZ && Z_SENSORLESS
  1001. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1002. #endif
  1003. break;
  1004. #endif
  1005. #if Z_SENSORLESS
  1006. case Z_AXIS:
  1007. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1008. #if AXIS_HAS_STALLGUARD(Z2)
  1009. tmc_disable_stallguard(stepperZ2, enable_stealth.z2);
  1010. #endif
  1011. #if AXIS_HAS_STALLGUARD(Z3)
  1012. tmc_disable_stallguard(stepperZ3, enable_stealth.z3);
  1013. #endif
  1014. #if CORE_IS_XZ && X_SENSORLESS
  1015. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1016. #elif CORE_IS_YZ && Y_SENSORLESS
  1017. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1018. #endif
  1019. break;
  1020. #endif
  1021. }
  1022. #if ENABLED(SPI_ENDSTOPS)
  1023. switch (axis) {
  1024. #if X_SPI_SENSORLESS
  1025. case X_AXIS: endstops.tmc_spi_homing.x = false; break;
  1026. #endif
  1027. #if Y_SPI_SENSORLESS
  1028. case Y_AXIS: endstops.tmc_spi_homing.y = false; break;
  1029. #endif
  1030. #if Z_SPI_SENSORLESS
  1031. case Z_AXIS: endstops.tmc_spi_homing.z = false; break;
  1032. #endif
  1033. default: break;
  1034. }
  1035. #endif
  1036. }
  1037. #endif // SENSORLESS_HOMING
  1038. /**
  1039. * Home an individual linear axis
  1040. */
  1041. void do_homing_move(const AxisEnum axis, const float distance, const feedRate_t fr_mm_s=0.0) {
  1042. if (DEBUGGING(LEVELING)) {
  1043. DEBUG_ECHOPAIR(">>> do_homing_move(", axis_codes[axis], ", ", distance, ", ");
  1044. if (fr_mm_s)
  1045. DEBUG_ECHO(fr_mm_s);
  1046. else
  1047. DEBUG_ECHOPAIR("[", homing_feedrate(axis), "]");
  1048. DEBUG_ECHOLNPGM(")");
  1049. }
  1050. #if HOMING_Z_WITH_PROBE && HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  1051. // Wait for bed to heat back up between probing points
  1052. if (axis == Z_AXIS && distance < 0 && thermalManager.isHeatingBed()) {
  1053. serialprintPGM(msg_wait_for_bed_heating);
  1054. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1055. thermalManager.wait_for_bed();
  1056. ui.reset_status();
  1057. }
  1058. #endif
  1059. // Only do some things when moving towards an endstop
  1060. const int8_t axis_home_dir =
  1061. #if ENABLED(DUAL_X_CARRIAGE)
  1062. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  1063. #endif
  1064. home_dir(axis);
  1065. const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
  1066. #if ENABLED(SENSORLESS_HOMING)
  1067. sensorless_t stealth_states;
  1068. #endif
  1069. if (is_home_dir) {
  1070. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1071. if (axis == Z_AXIS) probing_pause(true);
  1072. #endif
  1073. // Disable stealthChop if used. Enable diag1 pin on driver.
  1074. #if ENABLED(SENSORLESS_HOMING)
  1075. stealth_states = start_sensorless_homing_per_axis(axis);
  1076. #endif
  1077. }
  1078. const feedRate_t real_fr_mm_s = fr_mm_s ?: homing_feedrate(axis);
  1079. #if IS_SCARA
  1080. // Tell the planner the axis is at 0
  1081. current_position[axis] = 0;
  1082. sync_plan_position();
  1083. current_position[axis] = distance;
  1084. line_to_current_position(real_fr_mm_s);
  1085. #else
  1086. abce_pos_t target = { planner.get_axis_position_mm(A_AXIS), planner.get_axis_position_mm(B_AXIS), planner.get_axis_position_mm(C_AXIS), planner.get_axis_position_mm(E_AXIS) };
  1087. target[axis] = 0;
  1088. planner.set_machine_position_mm(target);
  1089. target[axis] = distance;
  1090. #if IS_KINEMATIC && ENABLED(JUNCTION_DEVIATION)
  1091. const xyze_float_t delta_mm_cart{0};
  1092. #endif
  1093. // Set delta/cartesian axes directly
  1094. planner.buffer_segment(target
  1095. #if IS_KINEMATIC && ENABLED(JUNCTION_DEVIATION)
  1096. , delta_mm_cart
  1097. #endif
  1098. , real_fr_mm_s, active_extruder
  1099. );
  1100. #endif
  1101. planner.synchronize();
  1102. if (is_home_dir) {
  1103. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1104. if (axis == Z_AXIS) probing_pause(false);
  1105. #endif
  1106. endstops.validate_homing_move();
  1107. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  1108. #if ENABLED(SENSORLESS_HOMING)
  1109. end_sensorless_homing_per_axis(axis, stealth_states);
  1110. #endif
  1111. }
  1112. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< do_homing_move(", axis_codes[axis], ")");
  1113. }
  1114. /**
  1115. * Set an axis' current position to its home position (after homing).
  1116. *
  1117. * For Core and Cartesian robots this applies one-to-one when an
  1118. * individual axis has been homed.
  1119. *
  1120. * DELTA should wait until all homing is done before setting the XYZ
  1121. * current_position to home, because homing is a single operation.
  1122. * In the case where the axis positions are already known and previously
  1123. * homed, DELTA could home to X or Y individually by moving either one
  1124. * to the center. However, homing Z always homes XY and Z.
  1125. *
  1126. * SCARA should wait until all XY homing is done before setting the XY
  1127. * current_position to home, because neither X nor Y is at home until
  1128. * both are at home. Z can however be homed individually.
  1129. *
  1130. * Callers must sync the planner position after calling this!
  1131. */
  1132. void set_axis_is_at_home(const AxisEnum axis) {
  1133. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_at_home(", axis_codes[axis], ")");
  1134. SBI(axis_known_position, axis);
  1135. SBI(axis_homed, axis);
  1136. #if ENABLED(DUAL_X_CARRIAGE)
  1137. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  1138. current_position.x = x_home_pos(active_extruder);
  1139. return;
  1140. }
  1141. #endif
  1142. #if ENABLED(MORGAN_SCARA)
  1143. scara_set_axis_is_at_home(axis);
  1144. #elif ENABLED(DELTA)
  1145. current_position[axis] = (axis == Z_AXIS ? delta_height
  1146. #if HAS_BED_PROBE
  1147. - probe_offset.z
  1148. #endif
  1149. : base_home_pos(axis));
  1150. #else
  1151. current_position[axis] = base_home_pos(axis);
  1152. #endif
  1153. /**
  1154. * Z Probe Z Homing? Account for the probe's Z offset.
  1155. */
  1156. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  1157. if (axis == Z_AXIS) {
  1158. #if HOMING_Z_WITH_PROBE
  1159. current_position.z -= probe_offset.z;
  1160. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***\n> probe_offset.z = ", probe_offset.z);
  1161. #else
  1162. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("*** Z HOMED TO ENDSTOP ***");
  1163. #endif
  1164. }
  1165. #endif
  1166. #if ENABLED(I2C_POSITION_ENCODERS)
  1167. I2CPEM.homed(axis);
  1168. #endif
  1169. #if ENABLED(BABYSTEP_DISPLAY_TOTAL)
  1170. babystep.reset_total(axis);
  1171. #endif
  1172. if (DEBUGGING(LEVELING)) {
  1173. #if HAS_HOME_OFFSET
  1174. DEBUG_ECHOLNPAIR("> home_offset[", axis_codes[axis], "] = ", home_offset[axis]);
  1175. #endif
  1176. DEBUG_POS("", current_position);
  1177. DEBUG_ECHOLNPAIR("<<< set_axis_is_at_home(", axis_codes[axis], ")");
  1178. }
  1179. }
  1180. /**
  1181. * Set an axis' to be unhomed.
  1182. */
  1183. void set_axis_is_not_at_home(const AxisEnum axis) {
  1184. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_not_at_home(", axis_codes[axis], ")");
  1185. CBI(axis_known_position, axis);
  1186. CBI(axis_homed, axis);
  1187. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< set_axis_is_not_at_home(", axis_codes[axis], ")");
  1188. #if ENABLED(I2C_POSITION_ENCODERS)
  1189. I2CPEM.unhomed(axis);
  1190. #endif
  1191. }
  1192. /**
  1193. * Home an individual "raw axis" to its endstop.
  1194. * This applies to XYZ on Cartesian and Core robots, and
  1195. * to the individual ABC steppers on DELTA and SCARA.
  1196. *
  1197. * At the end of the procedure the axis is marked as
  1198. * homed and the current position of that axis is updated.
  1199. * Kinematic robots should wait till all axes are homed
  1200. * before updating the current position.
  1201. */
  1202. void homeaxis(const AxisEnum axis) {
  1203. #if IS_SCARA
  1204. // Only Z homing (with probe) is permitted
  1205. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  1206. #else
  1207. #define _CAN_HOME(A) \
  1208. (axis == _AXIS(A) && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  1209. #if X_SPI_SENSORLESS
  1210. #define CAN_HOME_X true
  1211. #else
  1212. #define CAN_HOME_X _CAN_HOME(X)
  1213. #endif
  1214. #if Y_SPI_SENSORLESS
  1215. #define CAN_HOME_Y true
  1216. #else
  1217. #define CAN_HOME_Y _CAN_HOME(Y)
  1218. #endif
  1219. #if Z_SPI_SENSORLESS
  1220. #define CAN_HOME_Z true
  1221. #else
  1222. #define CAN_HOME_Z _CAN_HOME(Z)
  1223. #endif
  1224. if (!CAN_HOME_X && !CAN_HOME_Y && !CAN_HOME_Z) return;
  1225. #endif
  1226. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")");
  1227. const int axis_home_dir = (
  1228. #if ENABLED(DUAL_X_CARRIAGE)
  1229. axis == X_AXIS ? x_home_dir(active_extruder) :
  1230. #endif
  1231. home_dir(axis)
  1232. );
  1233. // Homing Z towards the bed? Deploy the Z probe or endstop.
  1234. #if HOMING_Z_WITH_PROBE
  1235. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  1236. #endif
  1237. // Set flags for X, Y, Z motor locking
  1238. #if HAS_EXTRA_ENDSTOPS
  1239. switch (axis) {
  1240. #if ENABLED(X_DUAL_ENDSTOPS)
  1241. case X_AXIS:
  1242. #endif
  1243. #if ENABLED(Y_DUAL_ENDSTOPS)
  1244. case Y_AXIS:
  1245. #endif
  1246. #if Z_MULTI_ENDSTOPS
  1247. case Z_AXIS:
  1248. #endif
  1249. stepper.set_separate_multi_axis(true);
  1250. default: break;
  1251. }
  1252. #endif
  1253. // Fast move towards endstop until triggered
  1254. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 1 Fast:");
  1255. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1256. if (axis == Z_AXIS && bltouch.deploy()) return; // The initial DEPLOY
  1257. #endif
  1258. do_homing_move(axis, 1.5f * max_length(
  1259. #if ENABLED(DELTA)
  1260. Z_AXIS
  1261. #else
  1262. axis
  1263. #endif
  1264. ) * axis_home_dir
  1265. );
  1266. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1267. if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE)
  1268. #endif
  1269. // When homing Z with probe respect probe clearance
  1270. const float bump = axis_home_dir * (
  1271. #if HOMING_Z_WITH_PROBE
  1272. (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? _MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) :
  1273. #endif
  1274. home_bump_mm(axis)
  1275. );
  1276. // If a second homing move is configured...
  1277. if (bump) {
  1278. // Move away from the endstop by the axis HOME_BUMP_MM
  1279. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Move Away:");
  1280. do_homing_move(axis, -bump
  1281. #if HOMING_Z_WITH_PROBE
  1282. , MMM_TO_MMS(axis == Z_AXIS ? Z_PROBE_SPEED_FAST : 0)
  1283. #endif
  1284. );
  1285. // Slow move towards endstop until triggered
  1286. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 2 Slow:");
  1287. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1288. if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE)
  1289. #endif
  1290. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1291. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1292. if (axis == Z_AXIS) bltouch.stow(); // The final STOW
  1293. #endif
  1294. }
  1295. #if HAS_EXTRA_ENDSTOPS
  1296. const bool pos_dir = axis_home_dir > 0;
  1297. #if ENABLED(X_DUAL_ENDSTOPS)
  1298. if (axis == X_AXIS) {
  1299. const float adj = ABS(endstops.x2_endstop_adj);
  1300. if (adj) {
  1301. if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1302. do_homing_move(axis, pos_dir ? -adj : adj);
  1303. stepper.set_x_lock(false);
  1304. stepper.set_x2_lock(false);
  1305. }
  1306. }
  1307. #endif
  1308. #if ENABLED(Y_DUAL_ENDSTOPS)
  1309. if (axis == Y_AXIS) {
  1310. const float adj = ABS(endstops.y2_endstop_adj);
  1311. if (adj) {
  1312. if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1313. do_homing_move(axis, pos_dir ? -adj : adj);
  1314. stepper.set_y_lock(false);
  1315. stepper.set_y2_lock(false);
  1316. }
  1317. }
  1318. #endif
  1319. #if ENABLED(Z_DUAL_ENDSTOPS)
  1320. if (axis == Z_AXIS) {
  1321. const float adj = ABS(endstops.z2_endstop_adj);
  1322. if (adj) {
  1323. if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  1324. do_homing_move(axis, pos_dir ? -adj : adj);
  1325. stepper.set_z_lock(false);
  1326. stepper.set_z2_lock(false);
  1327. }
  1328. }
  1329. #endif
  1330. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  1331. if (axis == Z_AXIS) {
  1332. // we push the function pointers for the stepper lock function into an array
  1333. void (*lock[3]) (bool)= {&stepper.set_z_lock, &stepper.set_z2_lock, &stepper.set_z3_lock};
  1334. float adj[3] = {0, endstops.z2_endstop_adj, endstops.z3_endstop_adj};
  1335. void (*tempLock) (bool);
  1336. float tempAdj;
  1337. // manual bubble sort by adjust value
  1338. if (adj[1] < adj[0]) {
  1339. tempLock = lock[0], tempAdj = adj[0];
  1340. lock[0] = lock[1], adj[0] = adj[1];
  1341. lock[1] = tempLock, adj[1] = tempAdj;
  1342. }
  1343. if (adj[2] < adj[1]) {
  1344. tempLock = lock[1], tempAdj = adj[1];
  1345. lock[1] = lock[2], adj[1] = adj[2];
  1346. lock[2] = tempLock, adj[2] = tempAdj;
  1347. }
  1348. if (adj[1] < adj[0]) {
  1349. tempLock = lock[0], tempAdj = adj[0];
  1350. lock[0] = lock[1], adj[0] = adj[1];
  1351. lock[1] = tempLock, adj[1] = tempAdj;
  1352. }
  1353. if (pos_dir) {
  1354. // normalize adj to smallest value and do the first move
  1355. (*lock[0])(true);
  1356. do_homing_move(axis, adj[1] - adj[0]);
  1357. // lock the second stepper for the final correction
  1358. (*lock[1])(true);
  1359. do_homing_move(axis, adj[2] - adj[1]);
  1360. }
  1361. else {
  1362. (*lock[2])(true);
  1363. do_homing_move(axis, adj[1] - adj[2]);
  1364. (*lock[1])(true);
  1365. do_homing_move(axis, adj[0] - adj[1]);
  1366. }
  1367. stepper.set_z_lock(false);
  1368. stepper.set_z2_lock(false);
  1369. stepper.set_z3_lock(false);
  1370. }
  1371. #endif
  1372. // Reset flags for X, Y, Z motor locking
  1373. switch (axis) {
  1374. #if ENABLED(X_DUAL_ENDSTOPS)
  1375. case X_AXIS:
  1376. #endif
  1377. #if ENABLED(Y_DUAL_ENDSTOPS)
  1378. case Y_AXIS:
  1379. #endif
  1380. #if Z_MULTI_ENDSTOPS
  1381. case Z_AXIS:
  1382. #endif
  1383. stepper.set_separate_multi_axis(false);
  1384. default: break;
  1385. }
  1386. #endif
  1387. #if IS_SCARA
  1388. set_axis_is_at_home(axis);
  1389. sync_plan_position();
  1390. #elif ENABLED(DELTA)
  1391. // Delta has already moved all three towers up in G28
  1392. // so here it re-homes each tower in turn.
  1393. // Delta homing treats the axes as normal linear axes.
  1394. // retrace by the amount specified in delta_endstop_adj + additional dist in order to have minimum steps
  1395. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  1396. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("delta_endstop_adj:");
  1397. do_homing_move(axis, delta_endstop_adj[axis] - (MIN_STEPS_PER_SEGMENT + 1) * planner.steps_to_mm[axis] * Z_HOME_DIR);
  1398. }
  1399. #else // CARTESIAN / CORE
  1400. set_axis_is_at_home(axis);
  1401. sync_plan_position();
  1402. destination[axis] = current_position[axis];
  1403. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1404. #endif
  1405. // Put away the Z probe
  1406. #if HOMING_Z_WITH_PROBE
  1407. if (axis == Z_AXIS && STOW_PROBE()) return;
  1408. #endif
  1409. #ifdef HOMING_BACKOFF_MM
  1410. constexpr xyz_float_t endstop_backoff = HOMING_BACKOFF_MM;
  1411. const float backoff_mm = endstop_backoff[
  1412. #if ENABLED(DELTA)
  1413. Z_AXIS
  1414. #else
  1415. axis
  1416. #endif
  1417. ];
  1418. if (backoff_mm) {
  1419. current_position[axis] -= ABS(backoff_mm) * axis_home_dir;
  1420. line_to_current_position(
  1421. #if HOMING_Z_WITH_PROBE
  1422. (axis == Z_AXIS) ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) :
  1423. #endif
  1424. homing_feedrate(axis)
  1425. );
  1426. }
  1427. #endif
  1428. // Clear retracted status if homing the Z axis
  1429. #if ENABLED(FWRETRACT)
  1430. if (axis == Z_AXIS) fwretract.current_hop = 0.0;
  1431. #endif
  1432. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< homeaxis(", axis_codes[axis], ")");
  1433. } // homeaxis()
  1434. #if HAS_WORKSPACE_OFFSET
  1435. void update_workspace_offset(const AxisEnum axis) {
  1436. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1437. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Axis ", axis_codes[axis], " home_offset = ", home_offset[axis], " position_shift = ", position_shift[axis]);
  1438. }
  1439. #endif
  1440. #if HAS_M206_COMMAND
  1441. /**
  1442. * Change the home offset for an axis.
  1443. * Also refreshes the workspace offset.
  1444. */
  1445. void set_home_offset(const AxisEnum axis, const float v) {
  1446. home_offset[axis] = v;
  1447. update_workspace_offset(axis);
  1448. }
  1449. #endif // HAS_M206_COMMAND